From f0d729ef52b6d0fd9a378f031fb0a3f749d6b9d9 Mon Sep 17 00:00:00 2001 From: rcollier Date: Sat, 27 Apr 2013 15:37:21 +0000 Subject: [PATCH] SMACK-415 Added fix for handling the empty tag case. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13624 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/pubsub/provider/ItemProvider.java | 14 +- .../smackx/pubsub/ItemValidationTest.java | 208 ++++++++++++++---- 2 files changed, 168 insertions(+), 54 deletions(-) diff --git a/source/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java b/source/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java index 833392a43..05c2513f3 100644 --- a/source/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java +++ b/source/org/jivesoftware/smackx/pubsub/provider/ItemProvider.java @@ -36,14 +36,13 @@ public class ItemProvider implements PacketExtensionProvider public PacketExtension parseExtension(XmlPullParser parser) throws Exception { String id = parser.getAttributeValue(null, "id"); - String node = parser.getAttributeValue(null, "node"); String elem = parser.getName(); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { - return new Item(id, node); + return new Item(id); } else { @@ -53,6 +52,7 @@ public class ItemProvider implements PacketExtensionProvider if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null) { boolean done = false; + boolean isEmptyElement = false; StringBuilder payloadText = new StringBuilder(); while (!done) @@ -76,7 +76,7 @@ public class ItemProvider implements PacketExtensionProvider if (parser.isEmptyElementTag()) { payloadText.append("/>"); - done = true; + isEmptyElement = true; } else { @@ -85,8 +85,8 @@ public class ItemProvider implements PacketExtensionProvider } else if (parser.getEventType() == XmlPullParser.END_TAG) { - if (done) - done = false; + if (isEmptyElement) + isEmptyElement = false; else payloadText.append(""); } @@ -96,10 +96,10 @@ public class ItemProvider implements PacketExtensionProvider } tag = parser.next(); } - return new PayloadItem(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); + return new PayloadItem(id, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); } else { - return new PayloadItem(id, node, PacketParserUtils.parsePacketExtension( + return new PayloadItem(id, PacketParserUtils.parsePacketExtension( payloadElemName, payloadNS, parser)); } } diff --git a/test-unit/org/jivesoftware/smackx/pubsub/ItemValidationTest.java b/test-unit/org/jivesoftware/smackx/pubsub/ItemValidationTest.java index aee47a946..eb967e5f7 100644 --- a/test-unit/org/jivesoftware/smackx/pubsub/ItemValidationTest.java +++ b/test-unit/org/jivesoftware/smackx/pubsub/ItemValidationTest.java @@ -19,20 +19,25 @@ */ package org.jivesoftware.smackx.pubsub; -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -import java.io.Reader; -import java.io.StringReader; - -import org.jivesoftware.smack.ThreadedDummyConnection; -import org.jivesoftware.smackx.pubsub.provider.ItemsProvider; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.xmlpull.mxp1.MXParser; -import org.xmlpull.v1.XmlPullParser; +import static org.custommonkey.xmlunit.XMLAssert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.Reader; +import java.io.StringReader; + +import org.jivesoftware.smack.TestUtils; +import org.jivesoftware.smack.ThreadedDummyConnection; +import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace; +import org.jivesoftware.smackx.pubsub.provider.ItemsProvider; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.xmlpull.mxp1.MXParser; +import org.xmlpull.v1.XmlPullParser; /** * @@ -95,37 +100,146 @@ public class ItemValidationTest assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML()); } -// @Test -// public void parseBasicItemWithoutNode() throws Exception -// { -// XmlPullParser parser = new MXParser(); -// Reader reader = new StringReader( -// "" + -// "" + -// "" + -// ""); -// parser.setInput(reader); -// ItemsProvider itemsProvider = new ItemsProvider(); -// ItemsExtension ext = (ItemsExtension) itemsProvider.parseExtension(parser); -// Item basicItem = (Item) ext.getItems().get(0); -// -// assertEquals("testid1", basicItem.getId()); -// assertNull(basicItem.getNode()); -// } - -// @Test -// public void parseBasicItemNode() throws Exception -// { -// BlockingQueue itemQ = new ArrayBlockingQueue(1); -// -// setupListener(itemQ); -// Message itemMsg = getMessage(""); -// connection.addMessage(itemMsg); -// -// Item basicItem = itemQ.poll(2, TimeUnit.SECONDS); -// -// assertNotNull(basicItem); -// assertEquals("testid1", basicItem.getId()); -// assertEquals("testNode", basicItem.getNode()); -// } + @Test + public void parseBasicItem() throws Exception + { + XmlPullParser parser = TestUtils.getMessageParser( + "" + + "" + + "" + + "" + + "" + + "" + + ""); + + Packet message = PacketParserUtils.parseMessage(parser); + PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); + + assertTrue(eventExt instanceof EventElement); + EventElement event = (EventElement) eventExt; + assertEquals(EventElementType.items, event.getEventType()); + assertEquals(1, event.getExtensions().size()); + assertTrue(event.getExtensions().get(0) instanceof ItemsExtension); + assertEquals(1, ((ItemsExtension)event.getExtensions().get(0)).items.size()); + + PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0); + assertTrue(itemExt instanceof Item); + assertEquals("testid1", ((Item)itemExt).getId()); + } + + @Test + public void parseSimplePayloadItem() throws Exception + { + String itemContent = "Some text"; + + XmlPullParser parser = TestUtils.getMessageParser( + "" + + "" + + "" + + "" + + itemContent + + "" + + "" + + "" + + ""); + + Packet message = PacketParserUtils.parseMessage(parser); + PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); + EventElement event = (EventElement) eventExt; + PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0); + + assertTrue(itemExt instanceof PayloadItem); + PayloadItem item = (PayloadItem)itemExt; + + assertEquals("testid1", item.getId()); + assertTrue(item.getPayload() instanceof SimplePayload); + + SimplePayload payload = (SimplePayload) item.getPayload(); + assertEquals("foo", payload.getElementName()); + assertEquals("smack:test", payload.getNamespace()); + assertXMLEqual(itemContent, payload.toXML()); + } + + @Test + public void parseComplexItem() throws Exception + { + String itemContent = + "" + + "Soliloquy" + + "" + + "To be, or not to be: that is the question:" + + "Whether 'tis nobler in the mind to suffer" + + "The slings and arrows of outrageous fortune," + + "Or to take arms against a sea of troubles," + + "And by opposing end them?" + + "" + + "" + + "tag:denmark.lit,2003:entry-32397" + + "2003-12-13T18:30:02Z" + + "2003-12-13T18:30:02Z" + + ""; + + XmlPullParser parser = TestUtils.getMessageParser( + "" + + "" + + "" + + "" + + itemContent + + "" + + "" + + "" + + ""); + + Packet message = PacketParserUtils.parseMessage(parser); + PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); + EventElement event = (EventElement) eventExt; + PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0); + + assertTrue(itemExt instanceof PayloadItem); + PayloadItem item = (PayloadItem)itemExt; + + assertEquals("testid1", item.getId()); + assertTrue(item.getPayload() instanceof SimplePayload); + + SimplePayload payload = (SimplePayload) item.getPayload(); + assertEquals("entry", payload.getElementName()); + assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace()); + assertXMLEqual(itemContent, payload.toXML()); + } + + @Test + public void parseEmptyTag() throws Exception + { + String itemContent = ""; + + XmlPullParser parser = TestUtils.getMessageParser( + "" + + "" + + "" + + "" + + itemContent + + "" + + "" + + "" + + ""); + + Packet message = PacketParserUtils.parseMessage(parser); + PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); + + assertTrue(eventExt instanceof EventElement); + EventElement event = (EventElement) eventExt; + assertEquals(EventElementType.items, event.getEventType()); + assertEquals(1, event.getExtensions().size()); + assertTrue(event.getExtensions().get(0) instanceof ItemsExtension); + assertEquals(1, ((ItemsExtension)event.getExtensions().get(0)).items.size()); + + PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0); + assertTrue(itemExt instanceof PayloadItem); + PayloadItem item = (PayloadItem)itemExt; + + assertEquals("testid1", item.getId()); + assertTrue(item.getPayload() instanceof SimplePayload); + + assertXMLEqual(itemContent, ((SimplePayload)item.getPayload()).toXML()); + } }