1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

[mam] Fix MamPrfsIQProviderTest

Parsers handed over to IQ providers should be positioned at the IQ
child element when being invoked. Therefore we remove the wrapping
<iq> in some test XML.

Also make checkMamPrefsIQProvider() a paramterized test.
This commit is contained in:
Florian Schmaus 2021-08-25 22:52:26 +02:00
parent 613f1afcab
commit c13f4ccac3
2 changed files with 24 additions and 14 deletions

View file

@ -41,6 +41,8 @@ import org.jxmpp.jid.impl.JidCreate;
*/ */
public class MamPrefsIQProvider extends IQProvider<MamPrefsIQ> { public class MamPrefsIQProvider extends IQProvider<MamPrefsIQ> {
public static final MamPrefsIQProvider INSTANCE = new MamPrefsIQProvider();
@Override @Override
public MamPrefsIQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { public MamPrefsIQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
String defaultBehaviorString = parser.getAttributeValue("", "default"); String defaultBehaviorString = parser.getAttributeValue("", "default");

View file

@ -19,54 +19,62 @@ package org.jivesoftware.smackx.mam;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.SmackTestUtil;
import org.jivesoftware.smack.test.util.SmackTestUtil.XmlPullParserKind;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.mam.element.MamPrefsIQ; import org.jivesoftware.smackx.mam.element.MamPrefsIQ;
import org.jivesoftware.smackx.mam.provider.MamPrefsIQProvider; import org.jivesoftware.smackx.mam.provider.MamPrefsIQProvider;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
public class MamPrefIQProviderTest extends MamTest { public class MamPrefIQProviderTest extends MamTest {
private static final String exampleMamPrefsIQ1 = "<iq type='set' id='juliet3'>" + "<prefs xmlns='urn:xmpp:mam:2' default='roster'>" private static final String exampleMamPrefsIQ1 = "<prefs xmlns='urn:xmpp:mam:2' default='roster'>"
+ "<always>" + "<jid>romeo@montague.lit</jid>" + "</always>" + "<never>" + "<always>" + "<jid>romeo@montague.lit</jid>" + "</always>" + "<never>"
+ "<jid>montague@montague.lit</jid>" + "</never>" + "</prefs>" + "</iq>"; + "<jid>montague@montague.lit</jid>" + "</never>" + "</prefs>";
private static final String exampleMamPrefsIQ2 = "<iq type='set' id='juliet3'>" + "<prefs xmlns='urn:xmpp:mam:2' default='roster'>" private static final String exampleMamPrefsIQ2 = "<prefs xmlns='urn:xmpp:mam:2' default='roster'>"
+ "<always>" + "<jid>romeo@montague.lit</jid>" + "<jid>montague@montague.lit</jid>" + "</always>" + "<always>" + "<jid>romeo@montague.lit</jid>" + "<jid>montague@montague.lit</jid>" + "</always>"
+ "<never>" + "</never>" + "</prefs>" + "</iq>"; + "<never>" + "</never>" + "</prefs>";
private static final String exampleMamPrefsIQ3 = "<iq type='get' id='juliet3'>" + "<prefs xmlns='urn:xmpp:mam:2'>" + "</prefs>" private static final String exampleMamPrefsIQ3 = "<prefs xmlns='urn:xmpp:mam:2'>" + "</prefs>";
+ "</iq>";
private static final String exampleMamPrefsResultIQ = "<iq type='result' id='juliet3'>" private static final String exampleMamPrefsResultIQ = "<iq type='result' id='juliet3'>"
+ "<prefs xmlns='urn:xmpp:mam:2' default='roster'>" + "<always>" + "<jid>romeo@montague.lit</jid>" + "<prefs xmlns='urn:xmpp:mam:2' default='roster'>" + "<always>" + "<jid>romeo@montague.lit</jid>"
+ "</always>" + "<never>" + "<jid>sarasa@montague.lit</jid>" + "<jid>montague@montague.lit</jid>" + "</always>" + "<never>" + "<jid>sarasa@montague.lit</jid>" + "<jid>montague@montague.lit</jid>"
+ "</never>" + "</prefs>" + "</iq>"; + "</never>" + "</prefs>" + "</iq>";
@Test @ParameterizedTest
public void checkMamPrefsIQProvider() throws Exception { @EnumSource(value = SmackTestUtil.XmlPullParserKind.class)
XmlPullParser parser1 = PacketParserUtils.getParserFor(exampleMamPrefsIQ1); public void checkMamPrefsIQProvider(XmlPullParserKind parserKind)
MamPrefsIQ mamPrefIQ1 = new MamPrefsIQProvider().parse(parser1); throws XmlPullParserException, IOException, SmackParsingException {
XmlPullParser parser1 = SmackTestUtil.getParserFor(exampleMamPrefsIQ1, parserKind);
MamPrefsIQ mamPrefIQ1 = MamPrefsIQProvider.INSTANCE.parse(parser1);
assertEquals(IQ.Type.set, mamPrefIQ1.getType()); assertEquals(IQ.Type.set, mamPrefIQ1.getType());
assertEquals(mamPrefIQ1.getAlwaysJids().get(0).toString(), "romeo@montague.lit"); assertEquals(mamPrefIQ1.getAlwaysJids().get(0).toString(), "romeo@montague.lit");
assertEquals(mamPrefIQ1.getNeverJids().get(0).toString(), "montague@montague.lit"); assertEquals(mamPrefIQ1.getNeverJids().get(0).toString(), "montague@montague.lit");
XmlPullParser parser2 = PacketParserUtils.getParserFor(exampleMamPrefsIQ2); XmlPullParser parser2 = SmackTestUtil.getParserFor(exampleMamPrefsIQ2, parserKind);
MamPrefsIQ mamPrefIQ2 = new MamPrefsIQProvider().parse(parser2); MamPrefsIQ mamPrefIQ2 = MamPrefsIQProvider.INSTANCE.parse(parser2);
assertEquals(IQ.Type.set, mamPrefIQ2.getType()); assertEquals(IQ.Type.set, mamPrefIQ2.getType());
assertEquals(mamPrefIQ2.getAlwaysJids().get(0).toString(), "romeo@montague.lit"); assertEquals(mamPrefIQ2.getAlwaysJids().get(0).toString(), "romeo@montague.lit");
assertEquals(mamPrefIQ2.getAlwaysJids().get(1).toString(), "montague@montague.lit"); assertEquals(mamPrefIQ2.getAlwaysJids().get(1).toString(), "montague@montague.lit");
assertTrue(mamPrefIQ2.getNeverJids().isEmpty()); assertTrue(mamPrefIQ2.getNeverJids().isEmpty());
XmlPullParser parser3 = PacketParserUtils.getParserFor(exampleMamPrefsIQ3); XmlPullParser parser3 = SmackTestUtil.getParserFor(exampleMamPrefsIQ3, parserKind);
MamPrefsIQ mamPrefIQ3 = new MamPrefsIQProvider().parse(parser3); MamPrefsIQ mamPrefIQ3 = MamPrefsIQProvider.INSTANCE.parse(parser3);
assertEquals(IQ.Type.set, mamPrefIQ3.getType()); assertEquals(IQ.Type.set, mamPrefIQ3.getType());
} }