From aeb385a022528b5cda3c3fe4136f0d0cd4c21179 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 23 Jul 2016 15:45:45 +0200 Subject: [PATCH] Apply refinements and fixes to the MAM code SMACK-625. --- .../org/jivesoftware/smack/packet/Stanza.java | 13 +++ .../jivesoftware/smackx/mam/MamManager.java | 86 +++++-------------- .../smackx/mam/element/MamElements.java | 2 +- .../smackx/mam/element/MamPrefsIQ.java | 79 ++++++++--------- .../smackx/mam/element/MamQueryIQ.java | 1 + .../smackx/mam/filter/MamResultFilter.java | 2 +- .../smackx/mam/provider/MamFinIQProvider.java | 14 +-- .../mam/provider/MamPrefsIQProvider.java | 48 +++++++---- .../mam/provider/MamQueryIQProvider.java | 16 ++-- .../mam/provider/MamResultProvider.java | 16 ++-- .../jivesoftware/smackx/mam/FiltersTest.java | 2 +- .../smackx/mam/MamFinProviderTest.java | 21 +++-- .../smackx/mam/MamPrefIQProviderTest.java | 6 +- .../jivesoftware/smackx/mam/PagingTest.java | 13 +-- .../smackx/mam/PreferencesTest.java | 18 ++-- .../smackx/mam/RetrieveFormFieldsTest.java | 9 +- .../forward/provider/ForwardedProvider.java | 2 + .../smackx/rsm/provider/RSMSetProvider.java | 4 +- .../xdata/provider/DataFormProvider.java | 2 + .../smackx/mam/MamIntegrationTest.java | 54 +++++------- .../jivesoftware/smackx/mam/package-info.java | 22 +---- 21 files changed, 194 insertions(+), 236 deletions(-) mode change 100644 => 120000 smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java index c483b4bf9..aed2905b1 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java @@ -148,6 +148,19 @@ public abstract class Stanza implements TopLevelStreamElement { return id != null; } + /** + * Set the stanza id if none is set. + * + * @return the stanza id. + * @since 4.2 + */ + public String setStanzaId() { + if (!hasStanzaIdSet()) { + setStanzaId(StanzaIdUtil.newStanzaId()); + } + return getStanzaId(); + } + /** * Returns who the stanza(/packet) is being sent "to", or null if * the value is not set. The XMPP protocol often makes the "to" diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java index 06e2d3dd4..2f70553ed 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java @@ -34,13 +34,14 @@ import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.filter.IQReplyFilter; import org.jivesoftware.smack.packet.IQ; -import org.jivesoftware.smack.packet.IQ.Type; import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.forward.packet.Forwarded; import org.jivesoftware.smackx.mam.element.MamElements; import org.jivesoftware.smackx.mam.element.MamFinIQ; import org.jivesoftware.smackx.mam.element.MamPrefsIQ; +import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior; import org.jivesoftware.smackx.mam.element.MamQueryIQ; import org.jivesoftware.smackx.mam.filter.MamResultFilter; import org.jivesoftware.smackx.rsm.packet.RSMSet; @@ -50,11 +51,12 @@ import org.jxmpp.jid.Jid; import org.jxmpp.util.XmppDateTime; /** - * Message Archive Management Manager class. + * A Manager for Message Archive Management (XEP-0313). * * @see XEP-0313: Message * Archive Management - * @author Fernando Ramirez and Florian Schmaus + * @author Florian Schmaus + * @author Fernando Ramirez * */ public final class MamManager extends Manager { @@ -223,7 +225,9 @@ public final class MamManager extends Manager { addAdditionalFields(additionalFields, dataForm); } - MamQueryIQ mamQueryIQ = prepareMamQueryIQSet(dataForm, queryId); + MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId, dataForm); + mamQueryIQ.setType(IQ.Type.set); + addResultsLimit(max, mamQueryIQ); return queryArchive(mamQueryIQ); } @@ -273,11 +277,6 @@ public final class MamManager extends Manager { dataForm.addField(formField); } - private void preparePageQuery(MamQueryIQ mamQueryIQ, RSMSet rsmSet) { - mamQueryIQ.setType(IQ.Type.set); - mamQueryIQ.addExtension(rsmSet); - } - /** * Returns a page of the archive. * @@ -293,7 +292,8 @@ public final class MamManager extends Manager { public MamQueryResult page(DataForm dataForm, RSMSet rsmSet) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException { MamQueryIQ mamQueryIQ = new MamQueryIQ(UUID.randomUUID().toString(), dataForm); - preparePageQuery(mamQueryIQ, rsmSet); + mamQueryIQ.setType(IQ.Type.set); + mamQueryIQ.addExtension(rsmSet); return queryArchive(mamQueryIQ); } @@ -373,20 +373,11 @@ public final class MamManager extends Manager { public List retrieveFormFields() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException { String queryId = UUID.randomUUID().toString(); - MamQueryIQ mamQueryIQ = prepareMamQueryIQGet(queryId); - return queryFormFields(mamQueryIQ); - } + MamQueryIQ mamQueryIq = new MamQueryIQ(queryId); - private MamQueryIQ prepareMamQueryIQSet(DataForm dataForm, String queryId) { - MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId, dataForm); - mamQueryIQ.setType(IQ.Type.set); - return mamQueryIQ; - } + MamQueryIQ mamResponseQueryIq = connection().createPacketCollectorAndSend(mamQueryIq).nextResultOrThrow(); - private MamQueryIQ prepareMamQueryIQGet(String queryId) { - MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId, null); - mamQueryIQ.setType(IQ.Type.get); - return mamQueryIQ; + return mamResponseQueryIq.getDataForm().getFields(); } private MamQueryResult queryArchive(MamQueryIQ mamQueryIq) throws NoResponseException, XMPPErrorException, @@ -435,23 +426,6 @@ public final class MamManager extends Manager { } } - private List queryFormFields(MamQueryIQ mamQueryIq) throws NoResponseException, XMPPErrorException, - NotConnectedException, InterruptedException, NotLoggedInException { - final XMPPConnection connection = connection(); - MamQueryIQ mamResponseQueryIQ = null; - PacketCollector mamResponseQueryIQCollector = connection - .createPacketCollector(new IQReplyFilter(mamQueryIq, connection)); - - try { - connection.sendStanza(mamQueryIq); - mamResponseQueryIQ = mamResponseQueryIQCollector.nextResultOrThrow(); - } finally { - mamResponseQueryIQCollector.cancel(); - } - - return mamResponseQueryIQ.getDataForm().getFields(); - } - /** * Returns true if Message Archive Management is supported by the server. * @@ -466,7 +440,7 @@ public final class MamManager extends Manager { return ServiceDiscoveryManager.getInstanceFor(connection()).serverSupportsFeature(MamElements.NAMESPACE); } - private DataForm getNewMamForm() { + private static DataForm getNewMamForm() { FormField field = new FormField(FormField.FORM_TYPE); field.setType(FormField.Type.hidden); field.addValue(MamElements.NAMESPACE); @@ -487,15 +461,10 @@ public final class MamManager extends Manager { */ public MamPrefsResult retrieveArchivingPreferences() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException { - MamPrefsIQ mamPrefIQ = prepareRetrievePreferencesStanza(); + MamPrefsIQ mamPrefIQ = new MamPrefsIQ(); return queryMamPrefs(mamPrefIQ); } - private MamPrefsIQ prepareRetrievePreferencesStanza() { - MamPrefsIQ mamPrefIQ = new MamPrefsIQ(Type.get, null, null, null); - return mamPrefIQ; - } - /** * Update the preferences in the server. * @@ -505,9 +474,8 @@ public final class MamManager extends Manager { * @param neverJids * is the list of JIDs that should never have messages to/from * archived in the user's store - * @param defaultField - * can be "roster", "always", "never" (look at the XEP-0313 - * documentation) + * @param defaultBehavior + * can be "roster", "always", "never" (see XEP-0313) * @return the MAM preferences result * @throws NoResponseException * @throws XMPPErrorException @@ -515,18 +483,14 @@ public final class MamManager extends Manager { * @throws InterruptedException * @throws NotLoggedInException */ - public MamPrefsResult updateArchivingPreferences(List alwaysJids, List neverJids, String defaultField) + public MamPrefsResult updateArchivingPreferences(List alwaysJids, List neverJids, DefaultBehavior defaultBehavior) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException { - MamPrefsIQ mamPrefIQ = prepareUpdatePreferencesStanza(alwaysJids, neverJids, defaultField); + Objects.requireNonNull(defaultBehavior, "Default behavior must be set"); + MamPrefsIQ mamPrefIQ = new MamPrefsIQ(alwaysJids, neverJids, defaultBehavior); return queryMamPrefs(mamPrefIQ); } - private MamPrefsIQ prepareUpdatePreferencesStanza(List alwaysJids, List neverJids, String defaultField) { - MamPrefsIQ mamPrefIQ = new MamPrefsIQ(Type.set, alwaysJids, neverJids, defaultField); - return mamPrefIQ; - } - /** * MAM preferences result class. * @@ -544,16 +508,8 @@ public final class MamManager extends Manager { private MamPrefsResult queryMamPrefs(MamPrefsIQ mamPrefsIQ) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException { final XMPPConnection connection = getAuthenticatedConnectionOrThrow(); - MamPrefsIQ mamPrefsResultIQ = null; - PacketCollector prefsResultIQCollector = connection - .createPacketCollector(new IQReplyFilter(mamPrefsIQ, connection)); - try { - connection.sendStanza(mamPrefsIQ); - mamPrefsResultIQ = prefsResultIQCollector.nextResultOrThrow(); - } finally { - prefsResultIQCollector.cancel(); - } + MamPrefsIQ mamPrefsResultIQ = connection.createPacketCollectorAndSend(mamPrefsIQ).nextResultOrThrow(); return new MamPrefsResult(mamPrefsResultIQ, DataForm.from(mamPrefsIQ)); } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java index 72e57e93d..8c8eecfe1 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java @@ -153,7 +153,7 @@ public class MamElements { /** * list of JIDs. */ - private List alwaysJids; + private final List alwaysJids; /** * Always JID list element constructor. diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamPrefsIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamPrefsIQ.java index 1c5bedf26..213737479 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamPrefsIQ.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamPrefsIQ.java @@ -33,6 +33,13 @@ import org.jxmpp.jid.Jid; */ public class MamPrefsIQ extends IQ { + public enum DefaultBehavior { + always, + never, + roster, + ; + } + /** * the preferences element. */ @@ -43,65 +50,44 @@ public class MamPrefsIQ extends IQ { */ public static final String NAMESPACE = MamElements.NAMESPACE; - /** - * true if it is a request for update preferences. - */ - private boolean isUpdate; - - /** - * true if it is a result preferences. - */ - private boolean isResult; - /** * list of always. */ - private List alwaysJids; + private final List alwaysJids; /** * list of never. */ - private List neverJids; + private final List neverJids; /** * default field. */ - private String defaultField; + private final DefaultBehavior defaultBehavior; + + /** + * Construct a new MAM {@code } IQ retrieval request (IQ type 'get'). + */ + public MamPrefsIQ() { + super(ELEMENT, NAMESPACE); + alwaysJids = null; + neverJids = null; + defaultBehavior = null; + } /** * MAM preferences IQ constructor. * - * @param type * @param alwaysJids * @param neverJids - * @param defaultField + * @param defaultBehavior */ - public MamPrefsIQ(Type type, List alwaysJids, List neverJids, String defaultField) { + public MamPrefsIQ(List alwaysJids, List neverJids, DefaultBehavior defaultBehavior) { super(ELEMENT, NAMESPACE); - this.setType(type); - this.isUpdate = this.getType().equals(Type.set); - this.isResult = this.getType().equals(Type.result); + setType(Type.set); this.alwaysJids = alwaysJids; this.neverJids = neverJids; - this.defaultField = defaultField; - } - - /** - * True if it is a request for update preferences. - * - * @return the update preferences boolean - */ - public boolean isUpdate() { - return isUpdate; - } - - /** - * True if it is a result. - * - * @return the result preferences boolean - */ - public boolean isResult() { - return isUpdate; + this.defaultBehavior = defaultBehavior; } /** @@ -123,19 +109,24 @@ public class MamPrefsIQ extends IQ { } /** - * Get the default field. + * Get the default behavior. * - * @return the default field + * @return the default behavior. */ - public String getDefault() { - return defaultField; + public DefaultBehavior getDefault() { + return defaultBehavior; } @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { - if (isUpdate || isResult) { - xml.attribute("default", defaultField); + if (getType().equals(IQ.Type.set) || getType().equals(IQ.Type.result)) { + xml.attribute("default", defaultBehavior); + } + + if (alwaysJids == null && neverJids == null) { + xml.setEmptyElement(); + return xml; } xml.rightAngleBracket(); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamQueryIQ.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamQueryIQ.java index 7a6f06678..9a0c19045 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamQueryIQ.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamQueryIQ.java @@ -51,6 +51,7 @@ public class MamQueryIQ extends IQ { */ public MamQueryIQ(String queryId) { this(queryId, null, null); + setType(IQ.Type.get); } /** diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/filter/MamResultFilter.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/filter/MamResultFilter.java index 19e2a1561..8dcd31897 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/filter/MamResultFilter.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/filter/MamResultFilter.java @@ -31,7 +31,7 @@ import org.jivesoftware.smackx.mam.element.MamQueryIQ; */ public class MamResultFilter extends FlexibleStanzaTypeFilter { - private String queryId; + private final String queryId; public MamResultFilter(MamQueryIQ mamQueryIQ) { super(Message.class); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamFinIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamFinIQProvider.java index aa1f12904..dbdd4942b 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamFinIQProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamFinIQProvider.java @@ -17,6 +17,7 @@ package org.jivesoftware.smackx.mam.provider; import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smack.util.ParserUtils; import org.jivesoftware.smackx.mam.element.MamFinIQ; import org.jivesoftware.smackx.rsm.packet.RSMSet; import org.jivesoftware.smackx.rsm.provider.RSMSetProvider; @@ -35,20 +36,23 @@ public class MamFinIQProvider extends IQProvider { @Override public MamFinIQ parse(XmlPullParser parser, int initialDepth) throws Exception { String queryId = parser.getAttributeValue("", "queryid"); - boolean complete = Boolean.parseBoolean(parser.getAttributeValue("", "complete")); - boolean stable = Boolean.parseBoolean(parser.getAttributeValue("", "stable")); + boolean complete = ParserUtils.getBooleanAttribute(parser, "complete", false); + boolean stable = ParserUtils.getBooleanAttribute(parser, "stable", true); RSMSet rsmSet = null; outerloop: while (true) { int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { + switch (eventType) { + case XmlPullParser.START_TAG: if (parser.getName().equals(RSMSet.ELEMENT)) { - rsmSet = new RSMSetProvider().parse(parser); + rsmSet = RSMSetProvider.INSTANCE.parse(parser); } - } else if (eventType == XmlPullParser.END_TAG) { + break; + case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } + break; } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamPrefsIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamPrefsIQProvider.java index 5d2e7586a..e65629780 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamPrefsIQProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamPrefsIQProvider.java @@ -16,15 +16,17 @@ */ package org.jivesoftware.smackx.mam.provider; +import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.jivesoftware.smack.packet.IQ.Type; import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smackx.mam.element.MamPrefsIQ; +import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior; import org.jxmpp.jid.Jid; import org.jxmpp.jid.impl.JidCreate; import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; /** * MAM Preferences IQ Provider class. @@ -37,9 +39,13 @@ import org.xmlpull.v1.XmlPullParser; public class MamPrefsIQProvider extends IQProvider { @Override - public MamPrefsIQ parse(XmlPullParser parser, int initialDepth) throws Exception { + public MamPrefsIQ parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { String iqType = parser.getAttributeValue("", "type"); - String defaultField = parser.getAttributeValue("", "default"); + String defaultBehaviorString = parser.getAttributeValue("", "default"); + DefaultBehavior defaultBehavior = null; + if (defaultBehaviorString != null) { + defaultBehavior = DefaultBehavior.valueOf(defaultBehaviorString); + } if (iqType == null) { iqType = "result"; @@ -49,40 +55,52 @@ public class MamPrefsIQProvider extends IQProvider { List neverJids = null; outerloop: while (true) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("always")) { + final int eventType = parser.next(); + final String name = parser.getName(); + switch (eventType) { + case XmlPullParser.START_TAG: + switch (name) { + case "always": alwaysJids = iterateJids(parser); - } - if (parser.getName().equals("never")) { + break; + case "never": neverJids = iterateJids(parser); + break; } - } else if (eventType == XmlPullParser.END_TAG) { + break; + case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } + break; } } - return new MamPrefsIQ(Type.fromString(iqType), alwaysJids, neverJids, defaultField); + return new MamPrefsIQ(alwaysJids, neverJids, defaultBehavior); } - private List iterateJids(XmlPullParser parser) throws Exception { + private static List iterateJids(XmlPullParser parser) throws XmlPullParserException, IOException { List jids = new ArrayList<>(); int initialDepth = parser.getDepth(); outerloop: while (true) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("jid")) { + final int eventType = parser.next(); + final String name = parser.getName(); + switch (eventType) { + case XmlPullParser.START_TAG: + switch (name) { + case "jid": parser.next(); jids.add(JidCreate.from(parser.getText())); + break; } - } else if (eventType == XmlPullParser.END_TAG) { + break; + case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } + break; } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamQueryIQProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamQueryIQProvider.java index 2d763073e..cdb859a04 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamQueryIQProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamQueryIQProvider.java @@ -39,16 +39,22 @@ public class MamQueryIQProvider extends IQProvider { String node = parser.getAttributeValue("", "node"); outerloop: while (true) { - int eventType = parser.next(); + final int eventType = parser.next(); + final String name = parser.getName(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals(DataForm.ELEMENT)) { - dataForm = new DataFormProvider().parse(parser); + switch (eventType) { + case XmlPullParser.START_TAG: + switch (name) { + case DataForm.ELEMENT: + dataForm = DataFormProvider.INSTANCE.parse(parser); + break; } - } else if (eventType == XmlPullParser.END_TAG) { + break; + case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } + break; } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java index 5ecc10f5a..c3b5be198 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java @@ -39,15 +39,21 @@ public class MamResultProvider extends ExtensionElementProvider fieldsNames, List fieldsValues) { + private static String getMamXMemberWith(List fieldsNames, List fieldsValues) { String xml = "" + "" + "" + MamElements.NAMESPACE + "" + ""; diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamFinProviderTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamFinProviderTest.java index 67654991e..a44be134e 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamFinProviderTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamFinProviderTest.java @@ -28,16 +28,10 @@ import org.xmlpull.v1.XmlPullParser; public class MamFinProviderTest extends MamTest { - String exmapleMamFinXml = "" + static final String exmapleMamFinXml = "" + "" + "10" + "09af3-cc343-b409f" + "" + ""; - private String getIQLimitedResultsExample() { - return "" + "" - + "" + "23452-4534-1" - + "390-2342-22" + "16" + "" + "" + ""; - } - @Test public void checkMamFinProvider() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor(exmapleMamFinXml); @@ -54,7 +48,18 @@ public class MamFinProviderTest extends MamTest { @Test public void checkQueryLimitedResults() throws Exception { - IQ iq = (IQ) PacketParserUtils.parseStanza(getIQLimitedResultsExample()); + // @formatter:off + final String IQ_LIMITED_RESULTS_EXAMPLE = "" + + "" + + "" + + "23452-4534-1" + + "390-2342-22" + "16" + + "" + + "" + + ""; + // @formatter:on + + IQ iq = (IQ) PacketParserUtils.parseStanza(IQ_LIMITED_RESULTS_EXAMPLE); MamFinIQ mamFinIQ = (MamFinIQ) iq; Assert.assertEquals(mamFinIQ.getType(), Type.result); diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamPrefIQProviderTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamPrefIQProviderTest.java index 6fb6ac212..30c270cd9 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamPrefIQProviderTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamPrefIQProviderTest.java @@ -50,20 +50,20 @@ public class MamPrefIQProviderTest extends MamTest { XmlPullParser parser1 = PacketParserUtils.getParserFor(exampleMamPrefsIQ1); MamPrefsIQ mamPrefIQ1 = new MamPrefsIQProvider().parse(parser1); - Assert.assertTrue(mamPrefIQ1.isUpdate()); + Assert.assertEquals(IQ.Type.set, mamPrefIQ1.getType()); Assert.assertEquals(mamPrefIQ1.getAlwaysJids().get(0), "romeo@montague.lit"); Assert.assertEquals(mamPrefIQ1.getNeverJids().get(0), "montague@montague.lit"); XmlPullParser parser2 = PacketParserUtils.getParserFor(exampleMamPrefsIQ2); MamPrefsIQ mamPrefIQ2 = new MamPrefsIQProvider().parse(parser2); - Assert.assertTrue(mamPrefIQ2.isUpdate()); + Assert.assertEquals(IQ.Type.set, mamPrefIQ2.getType()); Assert.assertEquals(mamPrefIQ2.getAlwaysJids().get(0), "romeo@montague.lit"); Assert.assertEquals(mamPrefIQ2.getAlwaysJids().get(1), "montague@montague.lit"); Assert.assertTrue(mamPrefIQ2.getNeverJids().isEmpty()); XmlPullParser parser3 = PacketParserUtils.getParserFor(exampleMamPrefsIQ3); MamPrefsIQ mamPrefIQ3 = new MamPrefsIQProvider().parse(parser3); - Assert.assertFalse(mamPrefIQ3.isUpdate()); + Assert.assertEquals(IQ.Type.set, mamPrefIQ3.getType()); } @Test diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PagingTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PagingTest.java index 58d44aa07..ad32e32d5 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PagingTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PagingTest.java @@ -16,8 +16,7 @@ */ package org.jivesoftware.smackx.mam; -import java.lang.reflect.Method; - +import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smackx.mam.element.MamQueryIQ; import org.jivesoftware.smackx.rsm.packet.RSMSet; import org.jivesoftware.smackx.xdata.packet.DataForm; @@ -27,25 +26,21 @@ import org.junit.Assert; public class PagingTest extends MamTest { - String pagingStanza = "" + "" + private static final String pagingStanza = "" + "" + "" + "" + "urn:xmpp:mam:1" + "" + "" + "" + "10" + "" + "" + ""; @Test public void checkPageQueryStanza() throws Exception { - Method methodPreparePageQuery = MamManager.class.getDeclaredMethod("preparePageQuery", MamQueryIQ.class, - RSMSet.class); - methodPreparePageQuery.setAccessible(true); - DataForm dataForm = getNewMamForm(); int max = 10; RSMSet rsmSet = new RSMSet(max); MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId, dataForm); mamQueryIQ.setStanzaId("sarasa"); - - methodPreparePageQuery.invoke(mamManager, mamQueryIQ, rsmSet); + mamQueryIQ.setType(IQ.Type.set); + mamQueryIQ.addExtension(rsmSet); Assert.assertEquals(mamQueryIQ.getDataForm(), dataForm); Assert.assertEquals(mamQueryIQ.getDataForm().getFields().get(0).getValues().get(0), "urn:xmpp:mam:1"); diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PreferencesTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PreferencesTest.java index 1c9681263..3ce06cc81 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PreferencesTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/PreferencesTest.java @@ -16,7 +16,6 @@ */ package org.jivesoftware.smackx.mam; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -25,12 +24,13 @@ import org.jxmpp.jid.Jid; import org.jxmpp.jid.impl.JidCreate; import org.jivesoftware.smackx.mam.element.MamElements; import org.jivesoftware.smackx.mam.element.MamPrefsIQ; +import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior; import org.junit.Assert; -public class PreferencesTest extends MamTest { +public class PreferencesTest { String retrievePrefsStanzaExample = "" + "" + ""; + + "'/>" + ""; String updatePrefsStanzaExample = "" + "" + "" + "romeo@montague.lit" + "other@montague.lit" @@ -38,21 +38,13 @@ public class PreferencesTest extends MamTest { @Test public void checkRetrievePrefsStanza() throws Exception { - Method prepareRetrievePreferencesStanza = MamManager.class - .getDeclaredMethod("prepareRetrievePreferencesStanza"); - prepareRetrievePreferencesStanza.setAccessible(true); - - MamPrefsIQ mamPrefIQ = (MamPrefsIQ) prepareRetrievePreferencesStanza.invoke(mamManager); + MamPrefsIQ mamPrefIQ = new MamPrefsIQ(); mamPrefIQ.setStanzaId("sarasa"); Assert.assertEquals(mamPrefIQ.toXML().toString(), retrievePrefsStanzaExample); } @Test public void checkUpdatePrefsStanza() throws Exception { - Method prepareUpdatePreferencesStanza = MamManager.class.getDeclaredMethod("prepareUpdatePreferencesStanza", - List.class, List.class, String.class); - prepareUpdatePreferencesStanza.setAccessible(true); - List alwaysJids = new ArrayList<>(); alwaysJids.add(JidCreate.from("romeo@montague.lit")); alwaysJids.add(JidCreate.from("other@montague.lit")); @@ -60,7 +52,7 @@ public class PreferencesTest extends MamTest { List neverJids = new ArrayList<>(); neverJids.add(JidCreate.from("montague@montague.lit")); - MamPrefsIQ mamPrefIQ = (MamPrefsIQ) prepareUpdatePreferencesStanza.invoke(mamManager, alwaysJids, neverJids, "roster"); + MamPrefsIQ mamPrefIQ = new MamPrefsIQ(alwaysJids, neverJids, DefaultBehavior.roster); mamPrefIQ.setStanzaId("sarasa"); Assert.assertEquals(mamPrefIQ.toXML().toString(), updatePrefsStanzaExample); } diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/RetrieveFormFieldsTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/RetrieveFormFieldsTest.java index df64ddc4a..46939c42d 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/RetrieveFormFieldsTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/RetrieveFormFieldsTest.java @@ -30,10 +30,10 @@ import org.junit.Assert; public class RetrieveFormFieldsTest extends MamTest { - String retrieveFormFieldStanza = "" + "" + "" + ""; - String additionalFieldsStanza = "" + "" + private static final String additionalFieldsStanza = "" + "" + "" + MamElements.NAMESPACE + "" + "" + "" + "Hi" + "" + "" + "Hi2" + "" @@ -41,10 +41,7 @@ public class RetrieveFormFieldsTest extends MamTest { @Test public void checkRetrieveFormFieldsStanza() throws Exception { - Method methodPrepareMamQueryIQGet = MamManager.class.getDeclaredMethod("prepareMamQueryIQGet", String.class); - methodPrepareMamQueryIQGet.setAccessible(true); - - MamQueryIQ mamQueryIQ = (MamQueryIQ) methodPrepareMamQueryIQGet.invoke(mamManager, queryId); + MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId); mamQueryIQ.setStanzaId("sarasa"); Assert.assertEquals(mamQueryIQ.toXML().toString(), retrieveFormFieldStanza); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java index f3564484e..03a59a93f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java @@ -36,6 +36,8 @@ import org.xmlpull.v1.XmlPullParser; */ public class ForwardedProvider extends ExtensionElementProvider { + public static final ForwardedProvider INSTANCE = new ForwardedProvider(); + private static final Logger LOGGER = Logger.getLogger(ForwardedProvider.class.getName()); @Override diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/rsm/provider/RSMSetProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/rsm/provider/RSMSetProvider.java index 8cc90bb24..c2bcbf7f1 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/rsm/provider/RSMSetProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/rsm/provider/RSMSetProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright © 2014 Florian Schmaus + * Copyright © 2014-2016 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,8 @@ import org.xmlpull.v1.XmlPullParserException; public class RSMSetProvider extends ExtensionElementProvider { + public static final RSMSetProvider INSTANCE = new RSMSetProvider(); + @Override public RSMSet parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java index cd59cc2e2..aaf04a9b0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java @@ -40,6 +40,8 @@ import java.util.List; */ public class DataFormProvider extends ExtensionElementProvider { + public static final DataFormProvider INSTANCE = new DataFormProvider(); + @Override public DataForm parse(XmlPullParser parser, int initialDepth) throws Exception { diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/MamIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/MamIntegrationTest.java index d61fa3ac7..c1ad1dce6 100644 --- a/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/MamIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/MamIntegrationTest.java @@ -16,8 +16,9 @@ */ package org.jivesoftware.smackx.mam; +import static org.junit.Assert.assertEquals; + import java.util.List; -import java.util.UUID; import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest; import org.igniterealtime.smack.inttest.SmackIntegrationTest; @@ -30,8 +31,7 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smackx.forward.packet.Forwarded; import org.jivesoftware.smackx.mam.MamManager.MamQueryResult; -import org.junit.Assert; -import org.jxmpp.jid.Jid; +import org.jxmpp.jid.EntityBareJid; public class MamIntegrationTest extends AbstractSmackIntegrationTest { @@ -49,8 +49,19 @@ public class MamIntegrationTest extends AbstractSmackIntegrationTest { } - private Message getConTwoLastMessageSentFrom(Jid userOne) throws NoResponseException, XMPPErrorException, - NotConnectedException, InterruptedException, NotLoggedInException { + @SmackIntegrationTest + public void mamTest() throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException, + NotLoggedInException { + EntityBareJid userOne = conOne.getUser().asEntityBareJid(); + EntityBareJid userTwo = conTwo.getUser().asEntityBareJid(); + + Message message = new Message(userTwo); + String messageId = message.setStanzaId(); + String messageBody = "test message"; + message.setBody(messageBody); + + conOne.sendStanza(message); + int pageSize = 20; MamQueryResult mamQueryResult = mamManagerConTwo.queryArchive(pageSize, null, null, userOne, null); @@ -59,35 +70,12 @@ public class MamIntegrationTest extends AbstractSmackIntegrationTest { } List forwardedMessages = mamQueryResult.forwardedMessages; - Message messageFromMAM = (Message) forwardedMessages.get(forwardedMessages.size() - 1).getForwardedStanza(); - return messageFromMAM; - } + Message mamMessage = (Message) forwardedMessages.get(forwardedMessages.size() - 1).getForwardedStanza(); - private Message prepareMessage(Jid to, String messageId, String body) { - Message message = new Message(); - message.setTo(to); - message.setStanzaId(messageId); - message.setBody(body); - return message; - } - - @SmackIntegrationTest - public void mamTest() throws Exception { - Jid userOne = conOne.getUser().asEntityBareJid(); - Jid userTwo = conTwo.getUser().asEntityBareJid(); - - String messageId = UUID.randomUUID().toString(); - String messageBody = "test message"; - - Message message = prepareMessage(userTwo, messageId, messageBody); - conOne.sendStanza(message); - - Message mamMessage = getConTwoLastMessageSentFrom(userOne); - - Assert.assertEquals(messageId, mamMessage.getStanzaId()); - Assert.assertEquals(messageBody, mamMessage.getBody()); - Assert.assertEquals(conOne.getUser(), mamMessage.getFrom()); - Assert.assertEquals(userTwo, mamMessage.getTo()); + assertEquals(messageId, mamMessage.getStanzaId()); + assertEquals(messageBody, mamMessage.getBody()); + assertEquals(conOne.getUser(), mamMessage.getFrom()); + assertEquals(userTwo, mamMessage.getTo()); } } diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java deleted file mode 100644 index 09b4b4074..000000000 --- a/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/** - * - * Copyright 2016 Fernando Ramirez - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Message Archive Management (XEP-0313) integration tests. - */ -package org.jivesoftware.smackx.mam; diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java new file mode 120000 index 000000000..54c99cb44 --- /dev/null +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/mam/package-info.java @@ -0,0 +1 @@ +../../../../../../../../smack-experimental/src/main/java/org/jivesoftware/smackx/mam/package-info.java \ No newline at end of file