From 93281829129cef3179bd9a2afdf1bcc85b34ef27 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 3 Jan 2017 10:59:56 +0100 Subject: [PATCH] Rework Bits of Binary implementation --- .../smack/util/PacketParserUtils.java | 5 +- .../org/jivesoftware/smackx/bob/BoBData.java | 65 ++++++++++---- .../org/jivesoftware/smackx/bob/BoBHash.java | 31 +++++-- .../{BoBSaverManager.java => BoBInfo.java} | 27 +++--- .../jivesoftware/smackx/bob/BoBManager.java | 90 +++++++++++-------- .../smackx/bob/DefaultBoBSaverManager.java | 46 ---------- .../smackx/bob/element/BoBIQ.java | 6 +- .../bob/provider/BoBExtensionProvider.java | 65 -------------- .../smackx/bob/provider/BoBIQProvider.java | 41 +++------ .../extensions.providers | 5 -- .../smackx/bob/BoBExtensionTest.java | 67 -------------- .../jivesoftware/smackx/bob/BoBIQTest.java | 18 +--- 12 files changed, 166 insertions(+), 300 deletions(-) rename smack-extensions/src/main/java/org/jivesoftware/smackx/bob/{BoBSaverManager.java => BoBInfo.java} (61%) delete mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/bob/DefaultBoBSaverManager.java delete mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBExtensionProvider.java delete mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBExtensionTest.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java index a2e69cd79..f702052fd 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java @@ -128,8 +128,9 @@ public class PacketParserUtils { return parser; } - public static Stanza parseStanza(String stanza) throws Exception { - return parseStanza(getParserFor(stanza)); + @SuppressWarnings("unchecked") + public static S parseStanza(String stanza) throws Exception { + return (S) parseStanza(getParserFor(stanza)); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java index 2a8bf9a81..544a2d7a6 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,34 +16,50 @@ */ package org.jivesoftware.smackx.bob; -import java.io.UnsupportedEncodingException; - +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.stringencoder.Base64; /** * Bits of Binary data class. * * @author Fernando Ramirez + * @author Florian Schmaus * @see XEP-0231: Bits of * Binary */ public class BoBData { - private final long maxAge; + private final int maxAge; private final String type; - private final byte[] content; + + private byte[] contentBinary; + private String contentString; + + public BoBData(String type, byte[] content) { + this(type, content, -1); + } /** * BoB data constructor. * - * @param maxAge * @param type * @param content + * @param maxAge */ - public BoBData(long maxAge, String type, byte[] content) { - this.maxAge = maxAge; + public BoBData(String type, byte[] content, int maxAge) { this.type = type; - this.content = content; + this.contentBinary = content; + this.maxAge = maxAge; + } + + public BoBData(String type, String content) { + this(type, content, -1); + } + + public BoBData(String type, String content, int maxAge) { + this.type = type; + this.contentString = content; + this.maxAge = maxAge; } /** @@ -51,7 +67,7 @@ public class BoBData { * * @return the max age */ - public long getMaxAge() { + public int getMaxAge() { return maxAge; } @@ -64,13 +80,21 @@ public class BoBData { return type; } + private void setContentBinaryIfRequired() { + if (contentBinary == null) { + assert(StringUtils.isNotEmpty(contentString)); + contentBinary = Base64.decode(contentString); + } + } + /** * Get the content. * * @return the content */ public byte[] getContent() { - return content; + setContentBinaryIfRequired(); + return contentBinary.clone(); } /** @@ -78,13 +102,20 @@ public class BoBData { * * @return the content in a Base64 encoded String */ - public String getBase64Encoded() { - byte[] bytes = Base64.encode(content); - try { - return new String(bytes, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("UTF-8 is not supported."); + public String getContentBase64Encoded() { + if (contentString == null) { + contentString = Base64.encodeToString(getContent()); } + return contentString; } + /** + * Check if the data is of reasonable size. XEP-0231 suggest that the size should not be more than 8 KiB. + * + * @return true if the data if of reasonable size. + */ + public boolean isOfReasonableSize() { + setContentBinaryIfRequired(); + return contentBinary.length <= 8 * 1024; + } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java index 3e9b77fda..47bcf766b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,13 @@ */ package org.jivesoftware.smackx.bob; +import org.jivesoftware.smack.util.StringUtils; + /** * Bits of Binary hash class. * * @author Fernando Ramirez + * @author Florian Schmaus * @see XEP-0231: Bits of * Binary */ @@ -27,6 +30,7 @@ public class BoBHash { private final String hash; private final String hashType; + private final String cid; /** * BoB hash constructor. @@ -35,8 +39,9 @@ public class BoBHash { * @param hashType */ public BoBHash(String hash, String hashType) { - this.hash = hash; - this.hashType = hashType; + this.hash = StringUtils.requireNotNullOrEmpty(hash, "hash must not be null or empty"); + this.hashType = StringUtils.requireNotNullOrEmpty(hashType, "hashType must not be null or empty"); + this.cid = this.hashType + '+' + this.hash + "@bob.xmpp.org"; } /** @@ -63,7 +68,7 @@ public class BoBHash { * @return src attribute string */ public String toSrc() { - return "cid:" + toCid(); + return "cid:" + getCid(); } /** @@ -71,8 +76,22 @@ public class BoBHash { * * @return cid attribute string */ - public String toCid() { - return this.hashType + "+" + this.hash + "@bob.xmpp.org"; + public String getCid() { + return cid; + } + + @Override + public boolean equals(Object other) { + if (other instanceof BoBHash) { + BoBHash otherBob = (BoBHash) other; + return cid.equals(otherBob.cid); + } + return false; + } + + @Override + public int hashCode() { + return cid.hashCode(); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBSaverManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java similarity index 61% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBSaverManager.java rename to smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java index a40b3dd34..a4db4aeae 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBSaverManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2017 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,23 @@ */ package org.jivesoftware.smackx.bob; -/** - * Bits of Binary Saver Manager interface. - * - * @author Fernando Ramirez - * - */ -public interface BoBSaverManager { +import java.util.Set; - public void addBoB(BoBHash bobHash, BoBData bobData); +public class BoBInfo { - public void removeBoB(BoBHash bobHash); + private final Set hashes; + private final BoBData data; - public BoBData getBoB(BoBHash bobHash); + BoBInfo(Set hashes, BoBData data) { + this.hashes = hashes; + this.data = data; + } + public Set getHashes() { + return hashes; + } + + public BoBData getData() { + return data; + } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java index 767cee02c..365d2c22a 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,11 @@ */ package org.jivesoftware.smackx.bob; +import java.util.Collections; import java.util.Map; +import java.util.Set; import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.Manager; @@ -31,27 +34,29 @@ import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler; import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smack.util.SHA1; import org.jivesoftware.smackx.bob.element.BoBIQ; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jxmpp.jid.Jid; +import org.jxmpp.util.cache.LruCache; /** * Bits of Binary manager class. * * @author Fernando Ramirez + * @author Florian Schmaus * @see XEP-0231: Bits of * Binary */ public final class BoBManager extends Manager { public static final String NAMESPACE = "urn:xmpp:bob"; - public static BoBSaverManager bobSaverManager; static { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { @Override public void connectionCreated(XMPPConnection connection) { - getInstanceFor(connection, bobSaverManager); + getInstanceFor(connection); } }); } @@ -62,16 +67,9 @@ public final class BoBManager extends Manager { * Get the singleton instance of BoBManager. * * @param connection - * @param saverManager * @return the instance of BoBManager */ - public static synchronized BoBManager getInstanceFor(XMPPConnection connection, BoBSaverManager saverManager) { - if (saverManager == null) { - bobSaverManager = new DefaultBoBSaverManager(); - } else { - bobSaverManager = saverManager; - } - + public static synchronized BoBManager getInstanceFor(XMPPConnection connection) { BoBManager bobManager = INSTANCES.get(connection); if (bobManager == null) { bobManager = new BoBManager(connection); @@ -81,37 +79,34 @@ public final class BoBManager extends Manager { return bobManager; } + private static final LruCache BOB_CACHE = new LruCache<>(128); + + private final Map bobs = new ConcurrentHashMap<>(); + private BoBManager(XMPPConnection connection) { super(connection); ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); serviceDiscoveryManager.addFeature(NAMESPACE); connection.registerIQRequestHandler( - new AbstractIqRequestHandler(BoBIQ.ELEMENT, BoBIQ.NAMESPACE, Type.get, Mode.sync) { + new AbstractIqRequestHandler(BoBIQ.ELEMENT, BoBIQ.NAMESPACE, Type.get, Mode.async) { @Override public IQ handleIQRequest(IQ iqRequest) { - BoBIQ getBoBIQ = (BoBIQ) iqRequest; + BoBIQ bobIQRequest = (BoBIQ) iqRequest; - BoBData bobData = bobSaverManager.getBoB(getBoBIQ.getBoBHash()); - BoBIQ responseBoBIQ = null; - try { - responseBoBIQ = responseBoB(getBoBIQ, bobData); - } catch (NotConnectedException | NotLoggedInException | InterruptedException e) { + BoBInfo bobInfo = bobs.get(bobIQRequest.getBoBHash()); + if (bobInfo == null) { + // TODO return item-not-found + return null; } + BoBData bobData = bobInfo.getData(); + BoBIQ responseBoBIQ = new BoBIQ(bobIQRequest.getBoBHash(), bobData); + responseBoBIQ.setType(Type.result); + responseBoBIQ.setTo(bobIQRequest.getFrom()); return responseBoBIQ; } }); - - connection.registerIQRequestHandler( - new AbstractIqRequestHandler(BoBIQ.ELEMENT, BoBIQ.NAMESPACE, Type.result, Mode.sync) { - @Override - public IQ handleIQRequest(IQ iqRequest) { - BoBIQ resultBoBIQ = (BoBIQ) iqRequest; - bobSaverManager.addBoB(resultBoBIQ.getBoBHash(), resultBoBIQ.getBoBData()); - return null; - } - }); } /** @@ -142,21 +137,46 @@ public final class BoBManager extends Manager { */ public BoBData requestBoB(Jid to, BoBHash bobHash) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + BoBData bobData = BOB_CACHE.lookup(bobHash); + if (bobData != null) { + return bobData; + } + BoBIQ requestBoBIQ = new BoBIQ(bobHash); requestBoBIQ.setType(Type.get); requestBoBIQ.setTo(to); XMPPConnection connection = getAuthenticatedConnectionOrThrow(); BoBIQ responseBoBIQ = connection.createPacketCollectorAndSend(requestBoBIQ).nextResultOrThrow(); - return responseBoBIQ.getBoBData(); + + bobData = responseBoBIQ.getBoBData(); + BOB_CACHE.put(bobHash, bobData); + + return bobData; } - private BoBIQ responseBoB(BoBIQ requestBoBIQ, BoBData bobData) - throws NotConnectedException, InterruptedException, NotLoggedInException { - BoBIQ responseBoBIQ = new BoBIQ(requestBoBIQ.getBoBHash(), bobData); - responseBoBIQ.setType(Type.result); - responseBoBIQ.setTo(requestBoBIQ.getFrom()); - return responseBoBIQ; + public BoBInfo addBoB(BoBData bobData) { + // We only support SHA-1 for now. + BoBHash bobHash = new BoBHash("sha1", SHA1.hex(bobData.getContent())); + + Set bobHashes = Collections.singleton(bobHash); + bobHashes = Collections.unmodifiableSet(bobHashes); + + BoBInfo bobInfo = new BoBInfo(bobHashes, bobData); + + bobs.put(bobHash, bobInfo); + + return bobInfo; } + public BoBInfo removeBoB(BoBHash bobHash) { + BoBInfo bobInfo = bobs.remove(bobHash); + if (bobInfo == null) { + return null; + } + for (BoBHash otherBobHash : bobInfo.getHashes()) { + bobs.remove(otherBobHash); + } + return bobInfo; + } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/DefaultBoBSaverManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/DefaultBoBSaverManager.java deleted file mode 100644 index 9dc2eb9b8..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/DefaultBoBSaverManager.java +++ /dev/null @@ -1,46 +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. - */ -package org.jivesoftware.smackx.bob; - -import java.util.HashMap; - -/** - * Default Bits of Binary Saver Manager class. - * - * @author Fernando Ramirez - * - */ -public class DefaultBoBSaverManager implements BoBSaverManager { - - HashMap bobs = new HashMap<>(); - - @Override - public void addBoB(BoBHash bobHash, BoBData bobData) { - bobs.put(bobHash, bobData); - } - - @Override - public void removeBoB(BoBHash bobHash) { - bobs.remove(bobHash); - } - - @Override - public BoBData getBoB(BoBHash bobHash) { - return bobs.get(bobHash); - } - -} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java index c741a01b9..c93e91b60 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java @@ -84,13 +84,13 @@ public class BoBIQ extends IQ { @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { - xml.attribute("cid", bobHash.toCid()); + xml.attribute("cid", bobHash.getCid()); if (bobData != null) { - xml.attribute("max-age", String.valueOf(bobData.getMaxAge())); + xml.optIntAttribute("max_age", bobData.getMaxAge()); xml.attribute("type", bobData.getType()); xml.rightAngleBracket(); - xml.escape(bobData.getBase64Encoded()); + xml.escape(bobData.getContentBase64Encoded()); } else { xml.setEmptyElement(); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBExtensionProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBExtensionProvider.java deleted file mode 100644 index a8d83b598..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBExtensionProvider.java +++ /dev/null @@ -1,65 +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. - */ -package org.jivesoftware.smackx.bob.provider; - -import org.jivesoftware.smack.provider.ExtensionElementProvider; -import org.jivesoftware.smackx.bob.BoBHash; -import org.jivesoftware.smackx.bob.element.BoBExtension; -import org.jivesoftware.smackx.xhtmlim.XHTMLText; -import org.xmlpull.v1.XmlPullParser; - -/** - * Bits of Binary extension provider class. - * - * @author Fernando Ramirez - * @see XEP-0231: Bits of - * Binary - */ -public class BoBExtensionProvider extends ExtensionElementProvider { - - @Override - public BoBExtension parse(XmlPullParser parser, int initialDepth) throws Exception { - BoBHash bobHash = null; - String alt = null; - - outerloop: while (true) { - int eventType = parser.next(); - - switch (eventType) { - - case XmlPullParser.START_TAG: - if (parser.getName().equals(XHTMLText.IMG)) { - alt = parser.getAttributeValue("", "alt"); - - String src = parser.getAttributeValue("", "src"); - bobHash = BoBHash.fromSrc(src); - } - break; - - case XmlPullParser.END_TAG: - if (parser.getDepth() == initialDepth) { - break outerloop; - } - break; - - } - } - - return new BoBExtension(bobHash, alt, null); - } - -} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java index 099defe7a..699403c62 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2017 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.jivesoftware.smackx.bob.provider; import org.jivesoftware.smack.provider.IQProvider; -import org.jivesoftware.smack.util.stringencoder.Base64; +import org.jivesoftware.smack.util.ParserUtils; import org.jivesoftware.smackx.bob.BoBData; import org.jivesoftware.smackx.bob.BoBHash; import org.jivesoftware.smackx.bob.element.BoBIQ; @@ -26,7 +26,7 @@ import org.xmlpull.v1.XmlPullParser; /** * Bits of Binary IQ provider class. * - * @author Fernando Ramirez + * @author Florian Schmaus * @see XEP-0231: Bits of * Binary */ @@ -34,34 +34,19 @@ public class BoBIQProvider extends IQProvider { @Override public BoBIQ parse(XmlPullParser parser, int initialDepth) throws Exception { - BoBHash bobHash = null; - BoBData bobData = null; + String cid = parser.getAttributeValue("", "cid"); + BoBHash bobHash = BoBHash.fromCid(cid); - parser.next(); + String dataType = parser.getAttributeValue("", "type"); + int maxAge = ParserUtils.getIntegerAttribute(parser, "max-age", -1); - if (parser.getName().equals(BoBIQ.ELEMENT)) { - String cid = parser.getAttributeValue("", "cid"); - bobHash = BoBHash.fromCid(cid); - - String dataType = parser.getAttributeValue("", "type"); - String maxAgeString = parser.getAttributeValue("", "max-age"); - - long maxAge = 0; - if (maxAgeString != null) { - maxAge = Long.parseLong(maxAgeString); - } - - String base64EncodedData = null; - try { - base64EncodedData = parser.nextText(); - } catch (Exception e) { - } - - if (base64EncodedData != null && dataType != null) { - byte[] base64EncodedDataBytes = base64EncodedData.getBytes(); - bobData = new BoBData(maxAge, dataType, Base64.decode(base64EncodedDataBytes)); - } + String base64EncodedData = parser.nextText(); + BoBData bobData; + if (dataType != null) { + bobData = new BoBData(dataType, base64EncodedData, maxAge); + } else { + bobData = null; } return new BoBIQ(bobHash, bobData); diff --git a/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers b/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers index 7fde63c7b..15247d647 100644 --- a/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers +++ b/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers @@ -538,11 +538,6 @@ - - html - http://jabber.org/protocol/xhtml-im - org.jivesoftware.smackx.bob.provider.BoBExtensionProvider - data urn:xmpp:bob diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBExtensionTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBExtensionTest.java deleted file mode 100644 index eedfb9703..000000000 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBExtensionTest.java +++ /dev/null @@ -1,67 +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. - */ -package org.jivesoftware.smackx.bob; - -import org.jivesoftware.smack.packet.Message; -import org.jivesoftware.smack.packet.Message.Type; -import org.jivesoftware.smack.util.PacketParserUtils; -import org.jivesoftware.smackx.bob.element.BoBExtension; -import org.jivesoftware.smackx.bob.provider.BoBExtensionProvider; -import org.junit.Assert; -import org.junit.Test; -import org.jxmpp.jid.impl.JidCreate; -import org.xmlpull.v1.XmlPullParser; - -public class BoBExtensionTest { - - String sampleMessageWithBoBExtension = "" - + "Yet here's a spot." + "" - + "" - + "A spot" + "" - + "" + ""; - - String sampleBoBExtension = "" - + "" - + "A spot" + "" - + ""; - - @Test - public void checkBoBMessageExtension() throws Exception { - Message message = (Message) PacketParserUtils.parseStanza(sampleMessageWithBoBExtension); - - BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); - - Message createdMessage = new Message(JidCreate.from("macbeth@chat.shakespeare.lit")); - createdMessage.setStanzaId("sarasa"); - createdMessage.setType(Type.groupchat); - createdMessage.setBody("Yet here's a spot."); - createdMessage.addExtension(new BoBExtension(bobHash, "A spot", null)); - - Assert.assertEquals(message.toXML().toString(), createdMessage.toXML().toString()); - } - - @Test - public void checkBoBExtensionProvider() throws Exception { - XmlPullParser parser = PacketParserUtils.getParserFor(sampleBoBExtension); - BoBExtension bobExtension = new BoBExtensionProvider().parse(parser); - - Assert.assertEquals("A spot", bobExtension.getAlt()); - Assert.assertEquals("sha1", bobExtension.getBoBHash().getHashType()); - Assert.assertEquals("8f35fef110ffc5df08d579a50083ff9308fb6242", bobExtension.getBoBHash().getHash()); - } - -} diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java index 337018e03..dab9c9e81 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java @@ -20,11 +20,9 @@ import org.jivesoftware.smack.packet.IQ.Type; import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.bob.element.BoBIQ; -import org.jivesoftware.smackx.bob.provider.BoBIQProvider; import org.junit.Assert; import org.junit.Test; import org.jxmpp.jid.impl.JidCreate; -import org.xmlpull.v1.XmlPullParser; public class BoBIQTest extends SmackTestSuite { @@ -37,12 +35,6 @@ public class BoBIQTest extends SmackTestSuite { @Test public void checkBoBIQRequest() throws Exception { - XmlPullParser parser = PacketParserUtils.getParserFor(sampleBoBIQRequest); - BoBIQ bobIQ = new BoBIQProvider().parse(parser); - bobIQ.setStanzaId("sarasa"); - bobIQ.setTo(JidCreate.from("ladymacbeth@shakespeare.lit/castle")); - bobIQ.setType(Type.get); - BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); BoBIQ createdBoBIQ = new BoBIQ(bobHash); @@ -55,14 +47,10 @@ public class BoBIQTest extends SmackTestSuite { @Test public void checkBoBIQResponse() throws Exception { - XmlPullParser parser = PacketParserUtils.getParserFor(sampleBoBIQResponse); - BoBIQ bobIQ = new BoBIQProvider().parse(parser); - bobIQ.setStanzaId("sarasa"); - bobIQ.setTo(JidCreate.from("doctor@shakespeare.lit/pda")); - bobIQ.setType(Type.result); + BoBIQ bobIQ = PacketParserUtils.parseStanza(sampleBoBIQResponse); BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); - BoBData bobData = new BoBData(86400, "image/png", "sarasade2354j2".getBytes()); + BoBData bobData = new BoBData("image/png", "sarasade2354j2".getBytes(), 86400); BoBIQ createdBoBIQ = new BoBIQ(bobHash, bobData); createdBoBIQ.setStanzaId("sarasa"); @@ -73,7 +61,7 @@ public class BoBIQTest extends SmackTestSuite { Assert.assertEquals(bobIQ.getBoBHash().getHashType(), createdBoBIQ.getBoBHash().getHashType()); Assert.assertEquals(bobIQ.getBoBData().getMaxAge(), createdBoBIQ.getBoBData().getMaxAge()); Assert.assertEquals(bobIQ.getBoBData().getType(), createdBoBIQ.getBoBData().getType()); - Assert.assertEquals(bobIQ.getBoBData().getBase64Encoded(), createdBoBIQ.getBoBData().getBase64Encoded()); + Assert.assertEquals(bobIQ.getBoBData().getContentBase64Encoded(), createdBoBIQ.getBoBData().getContentBase64Encoded()); Assert.assertEquals(bobIQ.toXML().toString(), createdBoBIQ.toXML().toString()); }