mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-26 16:22:06 +01:00
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
This commit is contained in:
parent
3c975426f6
commit
f0d729ef52
2 changed files with 168 additions and 54 deletions
|
@ -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("</").append(parser.getName()).append(">");
|
||||
}
|
||||
|
@ -96,10 +96,10 @@ public class ItemProvider implements PacketExtensionProvider
|
|||
}
|
||||
tag = parser.next();
|
||||
}
|
||||
return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText.toString()));
|
||||
return new PayloadItem<SimplePayload>(id, new SimplePayload(payloadElemName, payloadNS, payloadText.toString()));
|
||||
}
|
||||
else {
|
||||
return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension(
|
||||
return new PayloadItem<PacketExtension>(id, PacketParserUtils.parsePacketExtension(
|
||||
payloadElemName, payloadNS, parser));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,14 +19,19 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.pubsub;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
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;
|
||||
|
@ -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(
|
||||
// "<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
||||
// "<items node='testNode'>" +
|
||||
// "<item id='testid1' />" +
|
||||
// "</items></event>");
|
||||
// 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 parseBasicItem() throws Exception
|
||||
{
|
||||
XmlPullParser parser = TestUtils.getMessageParser(
|
||||
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
|
||||
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
||||
"<items node='testNode'>" +
|
||||
"<item id='testid1' />" +
|
||||
"</items>" +
|
||||
"</event>" +
|
||||
"</message>");
|
||||
|
||||
// @Test
|
||||
// public void parseBasicItemNode() throws Exception
|
||||
// {
|
||||
// BlockingQueue<Item> itemQ = new ArrayBlockingQueue<Item>(1);
|
||||
//
|
||||
// setupListener(itemQ);
|
||||
// Message itemMsg = getMessage("<item id='testid1' node='testNode'>");
|
||||
// connection.addMessage(itemMsg);
|
||||
//
|
||||
// Item basicItem = itemQ.poll(2, TimeUnit.SECONDS);
|
||||
//
|
||||
// assertNotNull(basicItem);
|
||||
// assertEquals("testid1", basicItem.getId());
|
||||
// assertEquals("testNode", basicItem.getNode());
|
||||
// }
|
||||
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 = "<foo xmlns='smack:test'>Some text</foo>";
|
||||
|
||||
XmlPullParser parser = TestUtils.getMessageParser(
|
||||
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
|
||||
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
||||
"<items node='testNode'>" +
|
||||
"<item id='testid1' >" +
|
||||
itemContent +
|
||||
"</item>" +
|
||||
"</items>" +
|
||||
"</event>" +
|
||||
"</message>");
|
||||
|
||||
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 =
|
||||
"<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 = TestUtils.getMessageParser(
|
||||
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
|
||||
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
||||
"<items node='testNode'>" +
|
||||
"<item id='testid1' >" +
|
||||
itemContent +
|
||||
"</item>" +
|
||||
"</items>" +
|
||||
"</event>" +
|
||||
"</message>");
|
||||
|
||||
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 = "<foo xmlns='smack:test'><bar/></foo>";
|
||||
|
||||
XmlPullParser parser = TestUtils.getMessageParser(
|
||||
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
|
||||
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
||||
"<items node='testNode'>" +
|
||||
"<item id='testid1' >" +
|
||||
itemContent +
|
||||
"</item>" +
|
||||
"</items>" +
|
||||
"</event>" +
|
||||
"</message>");
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue