@Test public void parseEmptyTag() throws Exception { String itemContent = "<foo xmlns='smack:test'><bar/></foo>"; XmlPullParser parser = PacketParserUtils.getParserFor( "<message from='pubsub.myserver.com' to='*****@*****.**' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement 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()); ExtensionElement 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().toString()); }
@Test public void parseComplexItem() throws Exception { String itemContent = "<entry xmlns='http://www.w3.org/2005/Atom'>" + "<title>Soliloquy</title>" + "<summary>" + "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?" + "</summary>" + "<link rel='alternate' type='text/html' href='http://denmark.lit/2003/12/13/atom03'/>" + "<id>tag:denmark.lit,2003:entry-32397</id>" + "<published>2003-12-13T18:30:02Z</published>" + "<updated>2003-12-13T18:30:02Z</updated>" + "</entry>"; XmlPullParser parser = PacketParserUtils.getParserFor( "<message from='pubsub.myserver.com' to='*****@*****.**' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns()); EventElement event = (EventElement) eventExt; ExtensionElement 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().toString()); }
@SuppressWarnings("unchecked") @Override public void handlePublishedItems(ItemPublishEvent<Item> items) { System.out.println("PUBSUB event received!"); for (Item item : items.getItems()) { if (item instanceof PayloadItem<?>) { PayloadItem<SimplePayload> payloadItem = (PayloadItem<SimplePayload>) item; SimplePayload payload = payloadItem.getPayload(); System.out.println("as string :" + payload.toString()); /* * TODO: payload.toString() is a quick hack to allowing to not * implement our own payload class */ processPayload(payload.toString()); } } }
@Test public void verifyPayloadItem() throws Exception { SimplePayload payload = new SimplePayload(null, null, "<data>This is the payload</data>"); PayloadItem<SimplePayload> simpleItem = new PayloadItem<SimplePayload>(payload); String simpleCtrl = "<item>" + payload.toXML() + "</item>"; assertXMLEqual(simpleCtrl, simpleItem.toXML()); PayloadItem<SimplePayload> idItem = new PayloadItem<SimplePayload>("uniqueid", payload); String idCtrl = "<item id='uniqueid'>" + payload.toXML() + "</item>"; assertXMLEqual(idCtrl, idItem.toXML()); PayloadItem<SimplePayload> itemWithNodeId = new PayloadItem<SimplePayload>("testId", "testNode", payload); String nodeIdCtrl = "<item id='testId' node='testNode'>" + payload.toXML() + "</item>"; assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML()); }