/** * @param output * @return * @throws XmlPullParserException * @throws IOException */ private static XmlPullParser getParser(String output) throws XmlPullParserException, IOException { return TestUtils.getParser(output, "validate"); } }
public static <EE extends ExtensionElement> EE parseExtensionElement(String elementString) throws Exception { return parseExtensionElement(getParser(elementString)); }
@Test public void parseTest() throws Exception { // @formatter:off final String capsExtensionString = "<c xmlns='http://jabber.org/protocol/caps'" + " hash='sha-1'" + " node='http://foo.example.org/bar'" + " ver='QgayPKawpkPSDYmwt/WM94uA1u0='/>"; // @formatter:on CapsExtension capsExtension = TestUtils.parseExtensionElement(capsExtensionString); assertNotNull(capsExtension); } }
.append("</query>") .append("</iq>"); final XmlPullParser parser = TestUtils.getIQParser(sb.toString()); final IQ rosterPush = PacketParserUtils.parseIQ(parser); initRoster();
@Test public void headersInMessageTest() throws Exception { // @formatter:off final String messageStanza = "<message xmlns='jabber:client' from='romeo@shakespeare.lit/orchard' to='juliet@capulet.com' type='chat'>" + "<body>Wherefore are thou?!?</body>" + "<headers xmlns='http://jabber.org/protocol/shim'>" + "<header name='Urgency'>high</header>" + "</headers>" + "</message>"; // @formatter:on XmlPullParser parser = TestUtils.getMessageParser(messageStanza); Message message = PacketParserUtils.parseMessage(parser); HeadersExtension headers = HeadersExtension.from(message); Header header = headers.getHeaders().get(0); assertEquals("Urgency", header.getName()); assertEquals("high", header.getValue()); } }
@Test public void testParseOxEmeElement() throws Exception { ExplicitMessageEncryptionElement eme = TestUtils.parseExtensionElement(OX_EME_ELEMENT); assertEquals(ExplicitMessageEncryptionProtocol.openpgpV0, eme.getProtocol()); }
.append("</query>") .append("</iq>"); final XmlPullParser parser = TestUtils.getIQParser(sb.toString()); final IQ rosterPush = PacketParserUtils.parseIQ(parser); initRoster();
@Test public void consumeUnparsedInput() throws Exception { final String MESSAGE_EXCEPTION_ELEMENT = "<" + ThrowException.ELEMENT + " xmlns='" + ThrowException.NAMESPACE + "'>" + "<nothingInHere>" + "</nothingInHere>" + "</" + ThrowException.ELEMENT + ">"; XmlPullParser parser = TestUtils.getMessageParser( "<message from='user@server.example' to='francisco@denmark.lit' id='foo'>" + MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>"); int parserDepth = parser.getDepth(); CharSequence content = null; try { PacketParserUtils.parseMessage(parser); } catch (Exception e) { content = PacketParserUtils.parseContentDepth(parser, parserDepth, false); } assertThat(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>", equalsCharSequence(content)); }
public static XmlPullParser getMessageParser(String stanza) { return getParser(stanza, "message"); }
@Test public void testParseUnknownEmeElement() throws Exception { ExplicitMessageEncryptionElement eme = TestUtils.parseExtensionElement(UNKNOWN_EME_ELEMENT); assertEquals(UNKNOWN_NAMESPACE, eme.getEncryptionNamespace()); assertEquals(UNKNOWN_NAME, eme.getName()); } }
@Test public void subscriptionsOwnerResultTest() throws Exception { // @formatter:off final String resultStanza = "<iq from='pubsub.example.org' to='julia@example.org/Smack' id='HaT4m-13' type='result'>" + "<pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>" + "<subscriptions node='test'>" + "<subscription jid='foo@example.org/Smack' subscription='subscribed' subid='58C1A6F99F2A7'/>" + "<subscription jid='julia@example.org/Smack' subscription='subscribed' subid='58C18F8917321'/>" + "</subscriptions>" + "</pubsub>" + "</iq>"; // @formatter:on XmlPullParser parser = TestUtils.getIQParser(resultStanza); PubSub pubsubResult = (PubSub) PacketParserUtils.parseIQ(parser); SubscriptionsExtension subElem = pubsubResult.getExtension(PubSubElementType.SUBSCRIPTIONS_OWNER); List<Subscription> subscriptions = subElem.getSubscriptions(); assertEquals(2, subscriptions.size()); Subscription sub1 = subscriptions.get(0); assertThat("foo@example.org/Smack", equalsCharSequence(sub1.getJid())); assertEquals(Subscription.State.subscribed, sub1.getState()); assertEquals("58C1A6F99F2A7", sub1.getId()); Subscription sub2 = subscriptions.get(1); assertThat("julia@example.org/Smack", equalsCharSequence(sub2.getJid())); assertEquals(Subscription.State.subscribed, sub2.getState()); assertEquals("58C18F8917321", sub2.getId()); } }
/** * RFC6121 5.2.3 explicitly disallows mixed content in <body/> elements. Make sure that we throw * an exception if we encounter such an element. * * @throws Exception */ @Test(expected = XmlPullParserException.class) public void invalidMessageBodyContainingTagTest() throws Exception { String control = XMLBuilder.create("message") .namespace(StreamOpen.CLIENT_NAMESPACE) .a("from", "romeo@montague.lit/orchard") .a("to", "juliet@capulet.lit/balcony") .a("id", "zid615d9") .a("type", "chat") .a("xml:lang", "en") .e("body") .a("xmlns", "http://www.w3.org/1999/xhtml") .e("span") .a("style", "font-weight: bold;") .t("Bad Message Body") .asString(outputProperties); Message message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control)); fail("Should throw exception. Instead got message: " + message.toXML(null).toString()); }
public static XmlPullParser getIQParser(String stanza) { return getParser(stanza, "iq"); }
"</x>"; DataForm dataForm = TestUtils.parseExtensionElement(formFieldUsingNamespacePrefix);
@Test public void modifySubscriptionsAsOwnerTest() throws InterruptedException, SmackException, IOException, XMPPException, Exception { ThreadedDummyConnection con = ThreadedDummyConnection.newInstance(); PubSubManager mgr = new PubSubManager(con, JidTestUtil.PUBSUB_EXAMPLE_ORG); Node testNode = new LeafNode(mgr, "princely_musings"); List<Subscription> ChangeSubs = Arrays.asList( new Subscription(JidCreate.from("romeo@montague.org"), Subscription.State.subscribed), new Subscription(JidCreate.from("juliet@capulet.org"), Subscription.State.none) ); testNode.modifySubscriptionsAsOwner(ChangeSubs); PubSub request = con.getSentPacket(); assertEquals("http://jabber.org/protocol/pubsub#owner", request.getChildElementNamespace()); assertEquals("pubsub", request.getChildElementName()); XmlPullParser parser = TestUtils.getIQParser(request.toXML(null).toString()); PubSub pubsubResult = (PubSub) PacketParserUtils.parseIQ(parser); SubscriptionsExtension subElem = pubsubResult.getExtension(PubSubElementType.SUBSCRIPTIONS_OWNER); List<Subscription> subscriptions = subElem.getSubscriptions(); assertEquals(2, subscriptions.size()); Subscription sub1 = subscriptions.get(0); assertEquals("romeo@montague.org", sub1.getJid().toString()); assertEquals(Subscription.State.subscribed, sub1.getState()); Subscription sub2 = subscriptions.get(1); assertEquals("juliet@capulet.org", sub2.getJid().toString()); assertEquals(Subscription.State.none, sub2.getState()); }
public static XmlPullParser getPresenceParser(String stanza) { return getParser(stanza, "presence"); }
public static XmlPullParser getParser(String string) { return getParser(string, null); }
public static XmlPullParser getParser(String string, String startTag) { return getParser(new StringReader(string), startTag); }
@Test(expected = XmlPullParserException.class) public void unknownMoodValueExceptionTest() throws Exception { String xml = "<mood xmlns='http://jabber.org/protocol/mood'>" + "<unknown/>" + "</mood>"; XmlPullParser parser = TestUtils.getParser(xml); MoodElement element = MoodProvider.INSTANCE.parse(parser); } }
@Test public void parseContentDepthTest() throws Exception { final String stanza = "<iq type='result' to='foo@bar.com' from='baz.com' id='42'/>"; XmlPullParser parser = TestUtils.getParser(stanza, "iq"); CharSequence content = PacketParserUtils.parseContent(parser); assertEquals("", content.toString()); }