1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 15:32:06 +01:00

Add CapsExtensionProviderTest

This commit is contained in:
Florian Schmaus 2015-03-04 13:08:35 +01:00
parent 85a9b07230
commit 18d9be0099
2 changed files with 67 additions and 0 deletions

View file

@ -20,7 +20,10 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.ParserUtils;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -40,6 +43,10 @@ final public class TestUtils {
return getParser(stanza, "presence"); return getParser(stanza, "presence");
} }
public static XmlPullParser getParser(String string) {
return getParser(string, null);
}
public static XmlPullParser getParser(String string, String startTag) { public static XmlPullParser getParser(String string, String startTag) {
return getParser(new StringReader(string), startTag); return getParser(new StringReader(string), startTag);
} }
@ -49,6 +56,9 @@ final public class TestUtils {
try { try {
parser = PacketParserUtils.newXmppParser(reader); parser = PacketParserUtils.newXmppParser(reader);
if (startTag == null) { if (startTag == null) {
while (parser.getEventType() != XmlPullParser.START_TAG) {
parser.next();
}
return parser; return parser;
} }
boolean found = false; boolean found = false;
@ -68,4 +78,17 @@ final public class TestUtils {
return parser; return parser;
} }
public static <EE extends ExtensionElement> EE parseExtensionElement(String elementString)
throws XmlPullParserException, IOException, SmackException {
return parseExtensionElement(getParser(elementString));
}
@SuppressWarnings("unchecked")
public static <EE extends ExtensionElement> EE parseExtensionElement(XmlPullParser parser)
throws XmlPullParserException, IOException, SmackException {
ParserUtils.assertAtStartTag(parser);
final String elementName = parser.getName();
final String namespace = parser.getNamespace();
return (EE) PacketParserUtils.parseExtensionElement(elementName, namespace, parser);
}
} }

View file

@ -0,0 +1,44 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.caps.provider;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.caps.packet.CapsExtension;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParserException;
public class CapsExtensionProviderTest extends InitExtensions {
@Test
public void parseTest() throws XmlPullParserException, IOException, SmackException {
// @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);
}
}