diff options
Diffstat (limited to 'test/com/isode/stroke/parser')
19 files changed, 1490 insertions, 0 deletions
diff --git a/test/com/isode/stroke/parser/AttributeMapTest.java b/test/com/isode/stroke/parser/AttributeMapTest.java new file mode 100644 index 0000000..45f5a4c --- /dev/null +++ b/test/com/isode/stroke/parser/AttributeMapTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.AttributeMap; + +public class AttributeMapTest { + + public AttributeMapTest() { + + } + + @Test + public void testGetAttribute_Namespaced() { + AttributeMap testling = new AttributeMap(); + testling.addAttribute("lang", "", "nl"); + testling.addAttribute("lang", "http://www.w3.org/XML/1998/namespace", "en"); + testling.addAttribute("lang", "", "fr"); + + assertEquals("en", testling.getAttribute("lang", "http://www.w3.org/XML/1998/namespace")); + } + + @Test + public void testGetBoolAttribute_True() { + AttributeMap testling = new AttributeMap(); + testling.addAttribute("foo", "", "true"); + + assertTrue(testling.getBoolAttribute("foo")); + } + + @Test + public void testGetBoolAttribute_1() { + AttributeMap testling = new AttributeMap(); + testling.addAttribute("foo", "", "1"); + + assertTrue(testling.getBoolAttribute("foo")); + } + + @Test + public void testGetBoolAttribute_False() { + AttributeMap testling = new AttributeMap(); + testling.addAttribute("foo", "", "false"); + + assertFalse(testling.getBoolAttribute("foo", true)); + } + + @Test + public void testGetBoolAttribute_0() { + AttributeMap testling = new AttributeMap(); + testling.addAttribute("foo", "", "0"); + + assertFalse(testling.getBoolAttribute("foo", true)); + } + + @Test + public void testGetBoolAttribute_Invalid() { + AttributeMap testling = new AttributeMap(); + testling.addAttribute("foo", "", "bla"); + + assertFalse(testling.getBoolAttribute("foo", true)); + } + + @Test + public void testGetBoolAttribute_UnknownWithDefaultTrue() { + AttributeMap testling = new AttributeMap(); + + assertTrue(testling.getBoolAttribute("foo", true)); + } + + @Test + public void testGetBoolAttribute_UnknownWithDefaultFalse() { + AttributeMap testling = new AttributeMap(); + + assertFalse(testling.getBoolAttribute("foo", false)); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/ElementParserTester.java b/test/com/isode/stroke/parser/ElementParserTester.java new file mode 100644 index 0000000..d8a0252 --- /dev/null +++ b/test/com/isode/stroke/parser/ElementParserTester.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import com.isode.stroke.parser.XMLParser; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.parser.XMLParserClient; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.ElementParser; +import com.isode.stroke.parser.PlatformXMLParserFactory; + +public class ElementParserTester<ParserType extends ElementParser> implements XMLParserClient { + + private XMLParser xmlParser_; + private ParserType parser_; + + public ElementParserTester(ParserType parser) { + this.parser_ = parser; + xmlParser_ = PlatformXMLParserFactory.createXMLParser(this); + } + + public boolean parse(String data) { + return xmlParser_.parse(data); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + parser_.handleStartElement(element, ns, attributes); + } + + public void handleEndElement(String element, String ns) { + parser_.handleEndElement(element, ns); + } + + public void handleCharacterData(String data) { + parser_.handleCharacterData(data); + } +} + diff --git a/test/com/isode/stroke/parser/EnumParserTest.java b/test/com/isode/stroke/parser/EnumParserTest.java new file mode 100644 index 0000000..aa20873 --- /dev/null +++ b/test/com/isode/stroke/parser/EnumParserTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.EnumParser; + +public class EnumParserTest { + + public EnumParserTest() { + + } + + public enum MyEnum { + MyValue1, + MyValue2, + MyValue3 + }; + + @Test + public void testParse() { + EnumParser parser = new EnumParser<MyEnum>(); + parser.addValue(MyEnum.MyValue1, "my-value-1"); + parser.addValue(MyEnum.MyValue2, "my-value-2"); + parser.addValue(MyEnum.MyValue3, "my-value-3"); + assertEquals(MyEnum.MyValue2, parser.parse("my-value-2")); + } + + @Test + public void testParse_NoValue() { + EnumParser parser = new EnumParser<MyEnum>(); + parser.addValue(MyEnum.MyValue1, "my-value-1"); + parser.addValue(MyEnum.MyValue2, "my-value-2"); + parser.addValue(MyEnum.MyValue3, "my-value-3"); + assertNull(parser.parse("my-value-4")); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/GenericPayloadTreeParserTest.java b/test/com/isode/stroke/parser/GenericPayloadTreeParserTest.java new file mode 100644 index 0000000..1021eae --- /dev/null +++ b/test/com/isode/stroke/parser/GenericPayloadTreeParserTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2011 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.GenericPayloadTreeParser; +import com.isode.stroke.elements.RawXMLPayload; +import com.isode.stroke.parser.tree.ParserElement; +import com.isode.stroke.parser.tree.NullParserElement; +import com.isode.stroke.parser.payloadparsers.PayloadParserTester; + +public class GenericPayloadTreeParserTest { + + public GenericPayloadTreeParserTest() { + + } + + private class MyParser extends GenericPayloadTreeParser<RawXMLPayload> { + + public MyParser() { + super(new RawXMLPayload()); + } + + public void handleTree(ParserElement root) { + tree = root; + } + + public ParserElement tree; + } + + @Test + public void testTree() { + MyParser testling = new MyParser(); + + String data = "<topLevel xmlns='urn:test:top'><firstLevelInheritedEmpty/><firstLevelInherited><secondLevelMultiChildren num='1'/><secondLevelMultiChildren num='2'/></firstLevelInherited><firstLevelNS xmlns='urn:test:first'/></topLevel>"; + + PayloadParserTester tester = new PayloadParserTester(testling); + tester.parse(data); + + ParserElement tree = testling.tree; + + assertEquals("topLevel", tree.getName()); + assertEquals("urn:test:top", tree.getNamespace()); + assertNotNull(tree.getChild("firstLevelInheritedEmpty", "urn:test:top")); + assertTrue(tree.getChild("firstLevelInheritedEmpty", "") instanceof NullParserElement); + assertNotNull(tree.getChild("firstLevelInherited", "urn:test:top")); + assertEquals(2, tree.getChild("firstLevelInherited", "urn:test:top").getChildren("secondLevelMultiChildren", "urn:test:top").size()); + assertEquals("1", tree.getChild("firstLevelInherited", "urn:test:top").getChildren("secondLevelMultiChildren", "urn:test:top").get(0).getAttributes().getAttribute("num")); + assertEquals("2", tree.getChild("firstLevelInherited", "urn:test:top").getChildren("secondLevelMultiChildren", "urn:test:top").get(1).getAttributes().getAttribute("num")); + assertNotNull(tree.getChild("firstLevelNS", "urn:test:first")); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/IQParserTest.java b/test/com/isode/stroke/parser/IQParserTest.java new file mode 100644 index 0000000..603caec --- /dev/null +++ b/test/com/isode/stroke/parser/IQParserTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.StanzaParserTester; +import com.isode.stroke.parser.IQParser; +import com.isode.stroke.elements.IQ; + +public class IQParserTest { + + private PayloadParserFactoryCollection factoryCollection_; + + public IQParserTest() { + + } + + @Before + public void setUp() { + factoryCollection_ = new PayloadParserFactoryCollection(); + } + + @Test + public void testParse_Set() { + IQParser testling = new IQParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<iq type=\"set\"/>")); + + assertEquals(IQ.Type.Set, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Get() { + IQParser testling = new IQParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<iq type=\"get\"/>")); + + assertEquals(IQ.Type.Get, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Result() { + IQParser testling = new IQParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<iq type=\"result\"/>")); + + assertEquals(IQ.Type.Result, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Error() { + IQParser testling = new IQParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<iq type=\"error\"/>")); + + assertEquals(IQ.Type.Error, testling.getStanzaGeneric().getType()); + } + +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/MessageParserTest.java b/test/com/isode/stroke/parser/MessageParserTest.java new file mode 100644 index 0000000..5b5e08a --- /dev/null +++ b/test/com/isode/stroke/parser/MessageParserTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.StanzaParserTester; +import com.isode.stroke.parser.MessageParser; +import com.isode.stroke.elements.Message; + +public class MessageParserTest { + + private PayloadParserFactoryCollection factoryCollection_; + + public MessageParserTest() { + + } + + @Before + public void setUp() { + factoryCollection_ = new PayloadParserFactoryCollection(); + } + + @Test + public void testParse_Chat() { + MessageParser testling = new MessageParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<message type=\"chat\"/>")); + + assertEquals(Message.Type.Chat, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Groupchat() { + MessageParser testling = new MessageParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<message type=\"groupchat\"/>")); + + assertEquals(Message.Type.Groupchat, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Error() { + MessageParser testling = new MessageParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<message type=\"error\"/>")); + + assertEquals(Message.Type.Error, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Headline() { + MessageParser testling = new MessageParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<message type=\"headline\"/>")); + + assertEquals(Message.Type.Headline, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Normal() { + MessageParser testling = new MessageParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<message/>")); + + assertEquals(Message.Type.Normal, testling.getStanzaGeneric().getType()); + } + +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/ParserTester.java b/test/com/isode/stroke/parser/ParserTester.java new file mode 100644 index 0000000..90c9586 --- /dev/null +++ b/test/com/isode/stroke/parser/ParserTester.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import com.isode.stroke.parser.XMLParser; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.parser.XMLParserClient; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.ElementParser; +import com.isode.stroke.parser.PlatformXMLParserFactory; + +public class ParserTester<ParserType extends XMLParserClient> implements XMLParserClient { + + private XMLParser xmlParser_; + private ParserType parser_; + + public ParserTester(ParserType parser) { + this.parser_ = parser; + xmlParser_ = PlatformXMLParserFactory.createXMLParser(this); + } + + public boolean parse(String data) { + return xmlParser_.parse(data); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + parser_.handleStartElement(element, ns, attributes); + } + + public void handleEndElement(String element, String ns) { + parser_.handleEndElement(element, ns); + } + + public void handleCharacterData(String data) { + parser_.handleCharacterData(data); + } +} + diff --git a/test/com/isode/stroke/parser/PresenceParserTest.java b/test/com/isode/stroke/parser/PresenceParserTest.java new file mode 100644 index 0000000..bcd617a --- /dev/null +++ b/test/com/isode/stroke/parser/PresenceParserTest.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.StanzaParserTester; +import com.isode.stroke.parser.PresenceParser; +import com.isode.stroke.elements.Presence; + +public class PresenceParserTest { + + private PayloadParserFactoryCollection factoryCollection_; + + public PresenceParserTest() { + + } + + @Before + public void setUp() { + factoryCollection_ = new PayloadParserFactoryCollection(); + } + + @Test + public void testParse_Available() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence/>")); + + assertEquals(Presence.Type.Available, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Unavailable() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"unavailable\"/>")); + + assertEquals(Presence.Type.Unavailable, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Probe() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"probe\"/>")); + + assertEquals(Presence.Type.Probe, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Subscribe() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"subscribe\"/>")); + + assertEquals(Presence.Type.Subscribe, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Subscribed() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"subscribed\"/>")); + + assertEquals(Presence.Type.Subscribed, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Unsubscribe() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"unsubscribe\"/>")); + + assertEquals(Presence.Type.Unsubscribe, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Unsubscribed() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"unsubscribed\"/>")); + + assertEquals(Presence.Type.Unsubscribed, testling.getStanzaGeneric().getType()); + } + + @Test + public void testParse_Error() { + PresenceParser testling = new PresenceParser(factoryCollection_); + StanzaParserTester parser = new StanzaParserTester(testling); + + assertTrue(parser.parse("<presence type=\"error\"/>")); + + assertEquals(Presence.Type.Error, testling.getStanzaGeneric().getType()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/SerializingParserTest.java b/test/com/isode/stroke/parser/SerializingParserTest.java new file mode 100644 index 0000000..eff8d6a --- /dev/null +++ b/test/com/isode/stroke/parser/SerializingParserTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + /* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.SerializingParser; +import com.isode.stroke.parser.XMLParserClient; +import com.isode.stroke.parser.ParserTester; +import com.isode.stroke.parser.AttributeMap; + +public class SerializingParserTest { + + private class ToTestSerializingParser extends SerializingParser implements XMLParserClient { + + } + + public SerializingParserTest() { + + } + + @Test + public void testParse() { + ToTestSerializingParser testling = new ToTestSerializingParser(); + ParserTester parser = new ParserTester(testling); + + assertTrue(parser.parse( + "<message type=\"chat\" to=\"me@foo.com\">" + + "<body>Hello<&World</body>" + + "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + + "foo<b>bar</b>baz" + + "</html>" + + "</message>")); + + assertEquals( + "<message to=\"me@foo.com\" type=\"chat\">" + + "<body>Hello<&World</body>" + + "<html xmlns=\"http://www.w3.org/1999/xhtml\">foo<b xmlns=\"http://www.w3.org/1999/xhtml\">bar</b>baz</html>" + + "</message>", testling.getResult()); + } + + @Test + public void testParse_Empty() { + ToTestSerializingParser testling = new ToTestSerializingParser(); + + assertEquals("", testling.getResult()); + } + + @Test + public void testParse_ToplevelCharacterData() { + ToTestSerializingParser testling = new ToTestSerializingParser(); + + AttributeMap attributes = new AttributeMap(); + testling.handleCharacterData("foo"); + testling.handleStartElement("message", "", attributes); + testling.handleEndElement("message", ""); + testling.handleCharacterData("bar"); + + assertEquals("<message/>", testling.getResult()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/StanzaAckParserTest.java b/test/com/isode/stroke/parser/StanzaAckParserTest.java new file mode 100644 index 0000000..ad6b951 --- /dev/null +++ b/test/com/isode/stroke/parser/StanzaAckParserTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.StanzaAckParser; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.parser.ElementParserTester; + +public class StanzaAckParserTest { + + public StanzaAckParserTest() { + + } + + @Test + public void testParse() { + StanzaAckParser testling = new StanzaAckParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse("<a h=\"12\" xmlns=\"urn:xmpp:sm:2\"/>")); + + assertTrue(testling.getElementGeneric().isValid()); + assertEquals(12, testling.getElementGeneric().getHandledStanzasCount()); + } + + @Test + public void testParse_Invalid() { + StanzaAckParser testling = new StanzaAckParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse("<a h=\"invalid\" xmlns=\"urn:xmpp:sm:2\"/>")); + + assertFalse(testling.getElementGeneric().isValid()); + } + + @Test + public void testParse_Empty() { + StanzaAckParser testling = new StanzaAckParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse("<a xmlns=\"urn:xmpp:sm:2\"/>")); + + assertFalse(testling.getElementGeneric().isValid()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/StanzaParserTest.java b/test/com/isode/stroke/parser/StanzaParserTest.java new file mode 100644 index 0000000..de10d44 --- /dev/null +++ b/test/com/isode/stroke/parser/StanzaParserTest.java @@ -0,0 +1,227 @@ +/* + * Copyright (c) 2010-2014 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.parser.StanzaParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.elements.Stanza; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.Element; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.jid.JID; + +public class StanzaParserTest { + + public StanzaParserTest() { + + } + + private class MyPayload1 extends Payload { + + public MyPayload1() { + this.hasChild = false; + } + + public boolean hasChild; + } + + private class MyPayload1Parser extends GenericPayloadParser<MyPayload1> { + + public MyPayload1Parser() { + super(new MyPayload1()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + if (!element.equals("mypayload1")) { + getPayloadInternal().hasChild = true; + } + } + + public void handleEndElement(String element, String ns) {} + public void handleCharacterData(String data) {} + } + + private class MyPayload1ParserFactory implements PayloadParserFactory { + + public MyPayload1ParserFactory() { + + } + + public PayloadParser createPayloadParser() { + return new MyPayload1Parser(); + } + + public boolean canParse(String element, String ns, AttributeMap attributes) { + return element.equals("mypayload1"); + } + } + + private class MyPayload2 extends Payload { + + public MyPayload2() { + + } + } + + private class MyPayload2Parser extends GenericPayloadParser<MyPayload2> { + + public MyPayload2Parser() { + super(new MyPayload2()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) {} + public void handleEndElement(String element, String ns) {} + public void handleCharacterData(String data) {} + } + + private class MyPayload2ParserFactory implements PayloadParserFactory { + + public MyPayload2ParserFactory() { + + } + + public PayloadParser createPayloadParser() { + return new MyPayload2Parser(); + } + + public boolean canParse(String element, String ns, AttributeMap attributes) { + return element.equals("mypayload2"); + } + } + + + private class MyStanza extends Stanza { + + public MyStanza() {} + } + + private class MyStanzaParser extends StanzaParser { + + public MyStanzaParser(PayloadParserFactoryCollection collection) { + super(collection); + stanza_ = new MyStanza(); + } + + public Element getElement() { + return stanza_; + } + + private MyStanza stanza_; + } + + private MyPayload1ParserFactory factory1_ = new MyPayload1ParserFactory(); + private MyPayload2ParserFactory factory2_ = new MyPayload2ParserFactory(); + private PayloadParserFactoryCollection factoryCollection_; + + @Before + public void setUp() { + factoryCollection_ = new PayloadParserFactoryCollection(); + factoryCollection_.addFactory(factory1_); + factoryCollection_.addFactory(factory2_); + } + + @Test + public void testHandleEndElement_OnePayload() { + MyStanzaParser testling = new MyStanzaParser(factoryCollection_); + + AttributeMap attributes = new AttributeMap(); + attributes.addAttribute("foo", "", "fum"); + attributes.addAttribute("bar", "", "baz"); + testling.handleStartElement("mystanza", "", attributes); + testling.handleStartElement("mypayload1", "", attributes); + testling.handleStartElement("child", "", attributes); + testling.handleEndElement("child", ""); + testling.handleEndElement("mypayload1", ""); + testling.handleEndElement("mystanza", ""); + + assertNotNull(testling.getStanza().getPayload(new MyPayload1())); + assertTrue(testling.getStanza().getPayload(new MyPayload1()).hasChild); + } + + @Test + public void testHandleEndElement_MultiplePayloads() { + MyStanzaParser testling = new MyStanzaParser(factoryCollection_); + + AttributeMap attributes = new AttributeMap(); + testling.handleStartElement("mystanza", "", attributes); + testling.handleStartElement("mypayload1", "", attributes); + testling.handleEndElement("mypayload1", ""); + testling.handleStartElement("mypayload2", "", attributes); + testling.handleEndElement("mypayload2", ""); + testling.handleEndElement("mystanza", ""); + + assertNotNull(testling.getStanza().getPayload(new MyPayload1())); + assertNotNull(testling.getStanza().getPayload(new MyPayload2())); + } + + @Test + public void testHandleEndElement_StrayCharacterData() { + MyStanzaParser testling = new MyStanzaParser(factoryCollection_); + + AttributeMap attributes = new AttributeMap(); + testling.handleStartElement("mystanza", "", attributes); + testling.handleStartElement("mypayload1", "", attributes); + testling.handleEndElement("mypayload1", ""); + testling.handleCharacterData("bla"); + testling.handleStartElement("mypayload2", "", attributes); + testling.handleEndElement("mypayload2", ""); + testling.handleEndElement("mystanza", ""); + + assertNotNull(testling.getStanza().getPayload(new MyPayload1())); + assertNotNull(testling.getStanza().getPayload(new MyPayload2())); + } + + @Test + public void testHandleEndElement_UnknownPayload() { + MyStanzaParser testling = new MyStanzaParser(factoryCollection_); + + AttributeMap attributes = new AttributeMap(); + testling.handleStartElement("mystanza", "", attributes); + testling.handleStartElement("mypayload1", "", attributes); + testling.handleEndElement("mypayload1", ""); + testling.handleStartElement("unknown-payload", "", attributes); + testling.handleStartElement("unknown-payload-child", "", attributes); + testling.handleEndElement("unknown-payload-child", ""); + testling.handleEndElement("unknown-payload", ""); + testling.handleStartElement("mypayload2", "", attributes); + testling.handleEndElement("mypayload2", ""); + testling.handleEndElement("mystanza", ""); + + assertNotNull(testling.getStanza().getPayload(new MyPayload1())); + assertNotNull(testling.getStanza().getPayload(new MyPayload2())); + } + + @Test + public void testHandleParse_BasicAttributes() { + MyStanzaParser testling = new MyStanzaParser(factoryCollection_); + + AttributeMap attributes = new AttributeMap(); + attributes.addAttribute("to", "", "foo@example.com/blo"); + attributes.addAttribute("from", "", "bar@example.com/baz"); + attributes.addAttribute("id", "", "id-123"); + testling.handleStartElement("mystanza", "", attributes); + testling.handleEndElement("mypayload1", ""); + + assertEquals(new JID("foo@example.com/blo"), testling.getStanza().getTo()); + assertEquals(new JID("bar@example.com/baz"), testling.getStanza().getFrom()); + assertEquals("id-123", testling.getStanza().getID()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/StanzaParserTester.java b/test/com/isode/stroke/parser/StanzaParserTester.java new file mode 100644 index 0000000..09adcbb --- /dev/null +++ b/test/com/isode/stroke/parser/StanzaParserTester.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import com.isode.stroke.parser.XMLParser; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.parser.XMLParserClient; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.StanzaParser; +import com.isode.stroke.parser.PlatformXMLParserFactory; + +public class StanzaParserTester<ParserType extends StanzaParser> implements XMLParserClient { + + private XMLParser xmlParser_; + private ParserType parser_; + + public StanzaParserTester(ParserType parser) { + this.parser_ = parser; + xmlParser_ = PlatformXMLParserFactory.createXMLParser(this); + } + + public boolean parse(String data) { + return xmlParser_.parse(data); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + parser_.handleStartElement(element, ns, attributes); + } + + public void handleEndElement(String element, String ns) { + parser_.handleEndElement(element, ns); + } + + public void handleCharacterData(String data) { + parser_.handleCharacterData(data); + } +} + diff --git a/test/com/isode/stroke/parser/StreamFeaturesParserTest.java b/test/com/isode/stroke/parser/StreamFeaturesParserTest.java new file mode 100644 index 0000000..1a32a72 --- /dev/null +++ b/test/com/isode/stroke/parser/StreamFeaturesParserTest.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.StreamFeaturesParser; +import com.isode.stroke.elements.StreamFeatures; +import com.isode.stroke.parser.ElementParserTester; + +public class StreamFeaturesParserTest { + + public StreamFeaturesParserTest() { + + } + + @Test + public void testParse() { + StreamFeaturesParser testling = new StreamFeaturesParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse( + "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>" + + "<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>" + + "<compression xmlns=\"http://jabber.org/features/compress\">" + + "<method>zlib</method>" + + "<method>lzw</method>" + + "</compression>" + + "<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" + + "<mechanism>DIGEST-MD5</mechanism>" + + "<mechanism>PLAIN</mechanism>" + + "</mechanisms>" + + "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>" + + "<sm xmlns='urn:xmpp:sm:2'/>" + + "<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>" + + "<ver xmlns=\"urn:xmpp:features:rosterver\"/>" + + "</stream:features>")); + + StreamFeatures element = (StreamFeatures)(testling.getElement()); + assertTrue(element.hasStartTLS()); + assertTrue(element.hasSession()); + assertTrue(element.hasResourceBind()); + assertTrue(element.hasCompressionMethod("zlib")); + assertTrue(element.hasCompressionMethod("lzw")); + assertTrue(element.hasAuthenticationMechanisms()); + assertTrue(element.hasAuthenticationMechanism("DIGEST-MD5")); + assertTrue(element.hasAuthenticationMechanism("PLAIN")); + assertNull(element.getAuthenticationHostname()); + assertTrue(element.hasStreamManagement()); + assertTrue(element.hasRosterVersioning()); + } + + @Test + public void testParse_Empty() { + StreamFeaturesParser testling = new StreamFeaturesParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse("<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>")); + + StreamFeatures element = (StreamFeatures)(testling.getElement()); + assertFalse(element.hasStartTLS()); + assertFalse(element.hasSession()); + assertFalse(element.hasResourceBind()); + assertFalse(element.hasAuthenticationMechanisms()); + } + + + @Test + public void testParse_AuthenticationHostname() { + StreamFeaturesParser testling = new StreamFeaturesParser(); + ElementParserTester parser = new ElementParserTester(testling); + String hostname = "auth42.us.example.com"; + + assertTrue(parser.parse( + "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>" + + "<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" + + "<mechanism>GSSAPI</mechanism>" + + "<hostname xmlns=\"urn:xmpp:domain-based-name:1\">auth42.us.example.com</hostname>" + + "</mechanisms>" + + "</stream:features>")); + + StreamFeatures element = (StreamFeatures)(testling.getElement()); + assertTrue(element.hasAuthenticationMechanism("GSSAPI")); + assertEquals(element.getAuthenticationHostname(), hostname); + } + + + @Test + public void testParse_AuthenticationHostnameEmpty() { + StreamFeaturesParser testling = new StreamFeaturesParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse( + "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>" + + "<mechanisms xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" + + "<mechanism>GSSAPI</mechanism>" + + "<hostname xmlns=\"urn:xmpp:domain-based-name:1\"></hostname>" + + "</mechanisms>" + + "</stream:features>")); + + StreamFeatures element = (StreamFeatures)(testling.getElement()); + assertTrue(element.hasAuthenticationMechanism("GSSAPI")); + assertTrue(element.getAuthenticationHostname().isEmpty()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/StreamManagementEnabledParserTest.java b/test/com/isode/stroke/parser/StreamManagementEnabledParserTest.java new file mode 100644 index 0000000..1ec3d30 --- /dev/null +++ b/test/com/isode/stroke/parser/StreamManagementEnabledParserTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.parser.StreamManagementEnabledParser; +import com.isode.stroke.elements.StreamManagementEnabled; +import com.isode.stroke.parser.ElementParserTester; + +public class StreamManagementEnabledParserTest { + + public StreamManagementEnabledParserTest() { + + } + + @Test + public void testParse() { + StreamManagementEnabledParser testling = new StreamManagementEnabledParser(); + ElementParserTester parser = new ElementParserTester(testling); + + assertTrue(parser.parse( + "<enabled xmlns=\"urn:xmpp:sm:3\" id=\"some-long-sm-id\" resume=\"true\"/>")); + + StreamManagementEnabled element = (StreamManagementEnabled)(testling.getElement()); + assertTrue(element.getResumeSupported()); + assertEquals("some-long-sm-id", element.getResumeID()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/BodyParserTest.java b/test/com/isode/stroke/parser/payloadparsers/BodyParserTest.java new file mode 100644 index 0000000..12c67ac --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/BodyParserTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Test; +import com.isode.stroke.elements.Body; +import com.isode.stroke.parser.payloadparsers.BodyParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class BodyParserTest { + + public BodyParserTest() { + + } + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse("<body>foo<baz>bar</baz>fum</body>")); + + Body payload = (Body)(parser.getPayload()); + assertEquals("foobarfum", payload.getText()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/PayloadParserTester.java b/test/com/isode/stroke/parser/payloadparsers/PayloadParserTester.java new file mode 100644 index 0000000..d903b3b --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/PayloadParserTester.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.XMLParser; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.parser.XMLParserClient; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PlatformXMLParserFactory; + +public class PayloadParserTester<ParserType extends PayloadParser> implements XMLParserClient { + + private XMLParser xmlParser_; + private ParserType parser_; + + public PayloadParserTester(ParserType parser) { + this.parser_ = parser; + xmlParser_ = PlatformXMLParserFactory.createXMLParser(this); + } + + public boolean parse(String data) { + return xmlParser_.parse(data); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + parser_.handleStartElement(element, ns, attributes); + } + + public void handleEndElement(String element, String ns) { + parser_.handleEndElement(element, ns); + } + + public void handleCharacterData(String data) { + parser_.handleCharacterData(data); + } +} + diff --git a/test/com/isode/stroke/parser/payloadparsers/PrivateStorageParserTest.java b/test/com/isode/stroke/parser/payloadparsers/PrivateStorageParserTest.java new file mode 100644 index 0000000..004f68f --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/PrivateStorageParserTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Test; +import com.isode.stroke.elements.PrivateStorage; +import com.isode.stroke.elements.Storage; +import com.isode.stroke.parser.payloadparsers.PrivateStorageParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.parser.payloadparsers.PayloadParserTester; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.jid.JID; + +public class PrivateStorageParserTest { + + public PrivateStorageParserTest() { + + } + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse( + "<query xmlns='jabber:iq:private'>" + + "<storage xmlns='storage:bookmarks'>" + + "<conference name='Swift' jid='swift@rooms.swift.im'>" + + "<nick>Alice</nick>" + + "</conference>" + + "</storage>" + + "</query>")); + + PrivateStorage payload = (PrivateStorage)(parser.getPayload()); + assertNotNull(payload); + Storage storage = (Storage)(payload.getPayload()); + assertNotNull(storage); + assertEquals("Alice", storage.getRooms().get(0).nick); + assertEquals(new JID("swift@rooms.swift.im"), storage.getRooms().get(0).jid); + } + + @Test + public void testParse_NoPayload() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse("<query xmlns='jabber:iq:private'/>")); + + PrivateStorage payload = (PrivateStorage)(parser.getPayload()); + assertNotNull(payload); + assertNull(payload.getPayload()); + } + + @Test + public void testParse_MultiplePayloads() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse( + "<query xmlns='jabber:iq:private'>" + + "<storage xmlns='storage:bookmarks'>" + + "<conference name='Swift' jid='swift@rooms.swift.im'>" + + "<nick>Alice</nick>" + + "</conference>" + + "</storage>" + + "<storage xmlns='storage:bookmarks'>" + + "<conference name='Swift' jid='swift@rooms.swift.im'>" + + "<nick>Rabbit</nick>" + + "</conference>" + + "</storage>" + + "</query>")); + + PrivateStorage payload = (PrivateStorage)(parser.getPayload()); + assertNotNull(payload); + Storage storage = (Storage)(payload.getPayload()); + assertNotNull(storage); + assertEquals("Rabbit", storage.getRooms().get(0).nick); + } + + @Test + public void testParse_UnsupportedPayload() { + PayloadParserFactoryCollection factories = new PayloadParserFactoryCollection(); + PrivateStorageParser testling = new PrivateStorageParser(factories); + PayloadParserTester parser = new PayloadParserTester(testling); + + assertNotNull(parser.parse( + "<query xmlns='jabber:iq:private'>" + + "<foo>Bar</foo>" + + "</query>")); + + assertNull(((PrivateStorage)(testling.getPayload())).getPayload()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/RawXMLPayloadParserTest.java b/test/com/isode/stroke/parser/payloadparsers/RawXMLPayloadParserTest.java new file mode 100644 index 0000000..e416066 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/RawXMLPayloadParserTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.elements.RawXMLPayload; +import com.isode.stroke.parser.payloadparsers.RawXMLPayloadParser; +import com.isode.stroke.parser.payloadparsers.PayloadParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class RawXMLPayloadParserTest { + + public RawXMLPayloadParserTest() { + + } + + @Test + public void testParse() { + RawXMLPayloadParser testling = new RawXMLPayloadParser(); + PayloadParserTester parser = new PayloadParserTester(testling); + + String xml = + "<foo foo-attr=\"foo-val\" xmlns=\"ns:foo\">" + + "<bar bar-attr=\"bar-val\" xmlns=\"ns:bar\"/>" + + "<baz baz-attr=\"baz-val\" xmlns=\"ns:baz\"/>" + + "</foo>"; + assertTrue(parser.parse(xml)); + + RawXMLPayload payload = (RawXMLPayload)(testling.getPayload()); + assertNotNull(xml, payload.getRawXML()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/StorageParserTest.java b/test/com/isode/stroke/parser/payloadparsers/StorageParserTest.java new file mode 100644 index 0000000..d9e254c --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/StorageParserTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2010-2012 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.elements.Storage; +import com.isode.stroke.parser.payloadparsers.StorageParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.jid.JID; +import java.util.Vector; + +public class StorageParserTest { + + public StorageParserTest() { + + } + + @Test + public void testParse_Room() { + DummyEventLoop eventloop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventloop); + + assertNotNull(parser.parse( + "<storage xmlns='storage:bookmarks'>" + + "<conference " + + "name='Council of Oberon' " + + "autojoin='true' jid='council@conference.underhill.org'>" + + "<nick>Puck</nick>" + + "<password>MyPass</password>" + + "</conference>" + + "</storage>")); + + Storage payload = (Storage)(parser.getPayload()); + Vector<Storage.Room> rooms = payload.getRooms(); + assertEquals(1, rooms.size()); + assertEquals("Council of Oberon", rooms.get(0).name); + assertEquals(new JID("council@conference.underhill.org"), rooms.get(0).jid); + assertTrue(rooms.get(0).autoJoin); + assertEquals("Puck", rooms.get(0).nick); + assertEquals("MyPass", rooms.get(0).password); + } + + @Test + public void testParse_MultipleRooms() { + DummyEventLoop eventloop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventloop); + + assertNotNull(parser.parse( + "<storage xmlns='storage:bookmarks'>" + + "<conference " + + "name='Council of Oberon' " + + "jid='council@conference.underhill.org' />" + + "<conference " + + "name='Tea & jam party' " + + "jid='teaparty@wonderland.lit' />" + + "</storage>")); + + Storage payload = (Storage)(parser.getPayload()); + Vector<Storage.Room> rooms = payload.getRooms(); + assertEquals(2, rooms.size()); + assertEquals("Council of Oberon", rooms.get(0).name); + assertEquals(new JID("council@conference.underhill.org"), rooms.get(0).jid); + assertEquals("Tea & jam party", rooms.get(1).name); + assertEquals(new JID("teaparty@wonderland.lit"), rooms.get(1).jid); + } + + @Test + public void testParse_URL() { + DummyEventLoop eventloop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventloop); + + assertNotNull(parser.parse( + "<storage xmlns='storage:bookmarks'>" + + "<url name='Complete Works of Shakespeare' url='http://the-tech.mit.edu/Shakespeare/'/>" + + "</storage>")); + + Storage payload = (Storage)(parser.getPayload()); + Vector<Storage.URL> urls = payload.getURLs(); + assertEquals(1, urls.size()); + assertEquals("Complete Works of Shakespeare", urls.get(0).name); + assertEquals("http://the-tech.mit.edu/Shakespeare/", urls.get(0).url); + } +}
\ No newline at end of file |