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
|
public PacketExtension parseExtension(XmlPullParser parser) throws Exception
|
||||||
{
|
{
|
||||||
String id = parser.getAttributeValue(null, "id");
|
String id = parser.getAttributeValue(null, "id");
|
||||||
String node = parser.getAttributeValue(null, "node");
|
|
||||||
String elem = parser.getName();
|
String elem = parser.getName();
|
||||||
|
|
||||||
int tag = parser.next();
|
int tag = parser.next();
|
||||||
|
|
||||||
if (tag == XmlPullParser.END_TAG)
|
if (tag == XmlPullParser.END_TAG)
|
||||||
{
|
{
|
||||||
return new Item(id, node);
|
return new Item(id);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -53,6 +52,7 @@ public class ItemProvider implements PacketExtensionProvider
|
||||||
if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null)
|
if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null)
|
||||||
{
|
{
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
|
boolean isEmptyElement = false;
|
||||||
StringBuilder payloadText = new StringBuilder();
|
StringBuilder payloadText = new StringBuilder();
|
||||||
|
|
||||||
while (!done)
|
while (!done)
|
||||||
|
@ -76,7 +76,7 @@ public class ItemProvider implements PacketExtensionProvider
|
||||||
if (parser.isEmptyElementTag())
|
if (parser.isEmptyElementTag())
|
||||||
{
|
{
|
||||||
payloadText.append("/>");
|
payloadText.append("/>");
|
||||||
done = true;
|
isEmptyElement = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -85,8 +85,8 @@ public class ItemProvider implements PacketExtensionProvider
|
||||||
}
|
}
|
||||||
else if (parser.getEventType() == XmlPullParser.END_TAG)
|
else if (parser.getEventType() == XmlPullParser.END_TAG)
|
||||||
{
|
{
|
||||||
if (done)
|
if (isEmptyElement)
|
||||||
done = false;
|
isEmptyElement = false;
|
||||||
else
|
else
|
||||||
payloadText.append("</").append(parser.getName()).append(">");
|
payloadText.append("</").append(parser.getName()).append(">");
|
||||||
}
|
}
|
||||||
|
@ -96,10 +96,10 @@ public class ItemProvider implements PacketExtensionProvider
|
||||||
}
|
}
|
||||||
tag = parser.next();
|
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 {
|
else {
|
||||||
return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension(
|
return new PayloadItem<PacketExtension>(id, PacketParserUtils.parsePacketExtension(
|
||||||
payloadElemName, payloadNS, parser));
|
payloadElemName, payloadNS, parser));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,20 +19,25 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.pubsub;
|
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.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
|
||||||
import org.jivesoftware.smack.ThreadedDummyConnection;
|
import org.jivesoftware.smack.TestUtils;
|
||||||
import org.jivesoftware.smackx.pubsub.provider.ItemsProvider;
|
import org.jivesoftware.smack.ThreadedDummyConnection;
|
||||||
import org.junit.After;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.junit.Before;
|
import org.jivesoftware.smack.packet.PacketExtension;
|
||||||
import org.junit.Test;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
import org.xmlpull.mxp1.MXParser;
|
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
|
||||||
import org.xmlpull.v1.XmlPullParser;
|
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());
|
assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML());
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test
|
@Test
|
||||||
// public void parseBasicItemWithoutNode() throws Exception
|
public void parseBasicItem() throws Exception
|
||||||
// {
|
{
|
||||||
// XmlPullParser parser = new MXParser();
|
XmlPullParser parser = TestUtils.getMessageParser(
|
||||||
// Reader reader = new StringReader(
|
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
|
||||||
// "<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
|
||||||
// "<items node='testNode'>" +
|
"<items node='testNode'>" +
|
||||||
// "<item id='testid1' />" +
|
"<item id='testid1' />" +
|
||||||
// "</items></event>");
|
"</items>" +
|
||||||
// parser.setInput(reader);
|
"</event>" +
|
||||||
// ItemsProvider itemsProvider = new ItemsProvider();
|
"</message>");
|
||||||
// ItemsExtension ext = (ItemsExtension) itemsProvider.parseExtension(parser);
|
|
||||||
// Item basicItem = (Item) ext.getItems().get(0);
|
Packet message = PacketParserUtils.parseMessage(parser);
|
||||||
//
|
PacketExtension eventExt = message.getExtension(PubSubNamespace.EVENT.getXmlns());
|
||||||
// assertEquals("testid1", basicItem.getId());
|
|
||||||
// assertNull(basicItem.getNode());
|
assertTrue(eventExt instanceof EventElement);
|
||||||
// }
|
EventElement event = (EventElement) eventExt;
|
||||||
|
assertEquals(EventElementType.items, event.getEventType());
|
||||||
// @Test
|
assertEquals(1, event.getExtensions().size());
|
||||||
// public void parseBasicItemNode() throws Exception
|
assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
|
||||||
// {
|
assertEquals(1, ((ItemsExtension)event.getExtensions().get(0)).items.size());
|
||||||
// BlockingQueue<Item> itemQ = new ArrayBlockingQueue<Item>(1);
|
|
||||||
//
|
PacketExtension itemExt = ((ItemsExtension)event.getExtensions().get(0)).items.get(0);
|
||||||
// setupListener(itemQ);
|
assertTrue(itemExt instanceof Item);
|
||||||
// Message itemMsg = getMessage("<item id='testid1' node='testNode'>");
|
assertEquals("testid1", ((Item)itemExt).getId());
|
||||||
// connection.addMessage(itemMsg);
|
}
|
||||||
//
|
|
||||||
// Item basicItem = itemQ.poll(2, TimeUnit.SECONDS);
|
@Test
|
||||||
//
|
public void parseSimplePayloadItem() throws Exception
|
||||||
// assertNotNull(basicItem);
|
{
|
||||||
// assertEquals("testid1", basicItem.getId());
|
String itemContent = "<foo xmlns='smack:test'>Some text</foo>";
|
||||||
// assertEquals("testNode", basicItem.getNode());
|
|
||||||
// }
|
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