diff --git a/smack-core/src/main/java/org/jivesoftware/smack/parsing/UnparsablePacket.java b/smack-core/src/main/java/org/jivesoftware/smack/parsing/UnparsablePacket.java index 216c2cd01..229409816 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/parsing/UnparsablePacket.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/parsing/UnparsablePacket.java @@ -25,10 +25,10 @@ package org.jivesoftware.smack.parsing; * */ public class UnparsablePacket { - private final String content; + private final CharSequence content; private final Exception e; - public UnparsablePacket(final String content, final Exception e) { + public UnparsablePacket(final CharSequence content, final Exception e) { this.content = content; this.e = e; } @@ -46,7 +46,7 @@ public class UnparsablePacket { * * @return the raw stanza data */ - public String getContent() { + public CharSequence getContent() { return content; } 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 e1ccba93e..2c8cd7744 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 @@ -288,13 +288,13 @@ public class PacketParserUtils { * @throws XmlPullParserException * @throws IOException */ - public static String parseElement(XmlPullParser parser) throws XmlPullParserException, IOException { + public static CharSequence parseElement(XmlPullParser parser) throws XmlPullParserException, IOException { assert(parser.getEventType() == XmlPullParser.START_TAG); return parseContentDepth(parser, parser.getDepth()); } /** - * Returns the content of a element as string. + * Returns the content of a element. *

* The parser must be positioned on the START_TAG of the element which content is going to get * returned. If the current element is the empty element, then the empty string is returned. If @@ -305,11 +305,11 @@ public class PacketParserUtils { * Note that only the outermost namespace attributes ("xmlns") will be returned, not nested ones. * * @param parser the XML pull parser - * @return the content of a tag as string + * @return the content of a tag * @throws XmlPullParserException if parser encounters invalid XML * @throws IOException if an IO error occurs */ - public static String parseContent(XmlPullParser parser) + public static CharSequence parseContent(XmlPullParser parser) throws XmlPullParserException, IOException { assert(parser.getEventType() == XmlPullParser.START_TAG); if (parser.isEmptyElementTag()) { @@ -338,7 +338,7 @@ public class PacketParserUtils { * @throws XmlPullParserException * @throws IOException */ - public static String parseContentDepth(XmlPullParser parser, int depth) throws XmlPullParserException, IOException { + public static CharSequence parseContentDepth(XmlPullParser parser, int depth) throws XmlPullParserException, IOException { XmlStringBuilder xml = new XmlStringBuilder(); int event = parser.getEventType(); boolean isEmptyElement = false; @@ -389,7 +389,7 @@ public class PacketParserUtils { } event = parser.next(); } - return xml.toString(); + return xml; } /** @@ -995,15 +995,15 @@ public class PacketParserUtils { * */ public static class UnparsedResultIQ extends IQ { - public UnparsedResultIQ(String content) { - this.str = content; + public UnparsedResultIQ(CharSequence content) { + this.content = content; } - private final String str; + private final CharSequence content; @Override - public String getChildElementXML() { - return this.str; + public CharSequence getChildElementXML() { + return this.content; } } } diff --git a/smack-core/src/test/java/org/jivesoftware/smack/parsing/ParsingExceptionTest.java b/smack-core/src/test/java/org/jivesoftware/smack/parsing/ParsingExceptionTest.java index ebf801c23..791fd5b2c 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/parsing/ParsingExceptionTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/parsing/ParsingExceptionTest.java @@ -16,8 +16,8 @@ */ package org.jivesoftware.smack.parsing; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.jivesoftware.smack.test.util.CharsequenceEquals.equalsCharSequence; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.packet.PacketExtension; @@ -63,14 +63,13 @@ public class ParsingExceptionTest { EXTENSION2 + ""); int parserDepth = parser.getDepth(); - String content = null; + CharSequence content = null; try { PacketParserUtils.parseMessage(parser); } catch (Exception e) { content = PacketParserUtils.parseContentDepth(parser, parserDepth); } - assertNotNull(content); - assertEquals(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "", content); + assertThat(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "", equalsCharSequence(content)); } static class ThrowException implements PacketExtensionProvider { diff --git a/smack-core/src/test/java/org/jivesoftware/smack/test/util/CharsequenceEquals.java b/smack-core/src/test/java/org/jivesoftware/smack/test/util/CharsequenceEquals.java new file mode 100644 index 000000000..c9a6233ef --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/test/util/CharsequenceEquals.java @@ -0,0 +1,47 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * 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.smack.test.util; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +public class CharsequenceEquals extends TypeSafeMatcher { + + private final String charSequenceString; + + public CharsequenceEquals(CharSequence charSequence) { + charSequenceString = charSequence.toString(); + } + + @Override + public void describeTo(Description description) { + description.appendText("Does not match CharSequence ").appendValue(charSequenceString); + } + + @Override + protected boolean matchesSafely(CharSequence item) { + String itemString = item.toString(); + return charSequenceString.equals(itemString); + } + + @Factory + public static Matcher equalsCharSequence(CharSequence charSequence) { + return new CharsequenceEquals(charSequence); + } +} diff --git a/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java b/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java index 0161b32f9..547ad4bdf 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java @@ -786,7 +786,7 @@ public class PacketParserUtilsTest { public void parseContentDepthTest() throws Exception { final String stanza = ""; XmlPullParser parser = TestUtils.getParser(stanza, "iq"); - String content = PacketParserUtils.parseContent(parser); + CharSequence content = PacketParserUtils.parseContent(parser); assertEquals("", content); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SimplePayload.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SimplePayload.java index e37657745..689b5e9cb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SimplePayload.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SimplePayload.java @@ -26,9 +26,9 @@ import org.jivesoftware.smack.packet.PacketExtension; */ public class SimplePayload implements PacketExtension { - private String elemName; - private String ns; - private String payload; + private final String elemName; + private final String ns; + private final CharSequence payload; /** * Construct a SimplePayload object with the specified element name, @@ -38,7 +38,7 @@ public class SimplePayload implements PacketExtension * @param namespace The namespace of the payload, null if there is none * @param xmlPayload The payload data */ - public SimplePayload(String elementName, String namespace, String xmlPayload) + public SimplePayload(String elementName, String namespace, CharSequence xmlPayload) { elemName = elementName; payload = xmlPayload; @@ -55,7 +55,8 @@ public class SimplePayload implements PacketExtension return ns; } - public String toXML() + @Override + public CharSequence toXML() { return payload; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java index 3dc3b1cb7..7bd68393d 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java @@ -54,7 +54,7 @@ public class ItemProvider implements PacketExtensionProvider if (ProviderManager.getExtensionProvider(payloadElemName, payloadNS) == null) { - String payloadText = PacketParserUtils.parseElement(parser); + CharSequence payloadText = PacketParserUtils.parseElement(parser); return new PayloadItem(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText)); } else diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ItemValidationTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ItemValidationTest.java index 3e7184bf9..72c08130d 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ItemValidationTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ItemValidationTest.java @@ -148,7 +148,7 @@ public class ItemValidationTest extends InitExtensions { SimplePayload payload = (SimplePayload) item.getPayload(); assertEquals("foo", payload.getElementName()); assertEquals("smack:test", payload.getNamespace()); - assertXMLEqual(itemContent, payload.toXML()); + assertXMLEqual(itemContent, payload.toXML().toString()); } @Test @@ -195,7 +195,7 @@ public class ItemValidationTest extends InitExtensions { SimplePayload payload = (SimplePayload) item.getPayload(); assertEquals("entry", payload.getElementName()); assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace()); - assertXMLEqual(itemContent, payload.toXML()); + assertXMLEqual(itemContent, payload.toXML().toString()); } @Test @@ -231,6 +231,6 @@ public class ItemValidationTest extends InitExtensions { assertEquals("testid1", item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); - assertXMLEqual(itemContent, ((SimplePayload)item.getPayload()).toXML()); + assertXMLEqual(itemContent, ((SimplePayload)item.getPayload()).toXML().toString()); } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/xhtmlim/provider/XHTMLExtensionProviderTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/xhtmlim/provider/XHTMLExtensionProviderTest.java index bcb741697..3f13e251c 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/xhtmlim/provider/XHTMLExtensionProviderTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/xhtmlim/provider/XHTMLExtensionProviderTest.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smackx.xhtmlim.provider; +import static org.jivesoftware.smack.test.util.CharsequenceEquals.equalsCharSequence; import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension; @@ -26,8 +27,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -43,10 +42,10 @@ public class XHTMLExtensionProviderTest { XHTMLExtensionProvider provider = new XHTMLExtensionProvider(); PacketExtension extension = provider.parseExtension(parser); - assertThat(extension, is(instanceOf(XHTMLExtension.class))); + assertThat(extension, instanceOf(XHTMLExtension.class)); XHTMLExtension attachmentsInfo = (XHTMLExtension) extension; - assertEquals(sampleXhtml(), attachmentsInfo.getBodies().get(0)); + assertThat(sampleXhtml(), equalsCharSequence(attachmentsInfo.getBodies().get(0))); } private String sampleXhtml() { diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 381eb47d6..baf2e2181 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -982,7 +982,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { try { packet = PacketParserUtils.parseStanza(parser, XMPPTCPConnection.this); } catch (Exception e) { - String content = PacketParserUtils.parseContentDepth(parser, parserDepth); + CharSequence content = PacketParserUtils.parseContentDepth(parser, parserDepth); UnparsablePacket message = new UnparsablePacket(content, e); if (callback != null) { callback.handleUnparsablePacket(message);