1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 09:39:39 +02:00

Merge branch '4.0'

Conflicts:
	build.gradle
	documentation/connections.html
	documentation/gettingstarted.html
	smack-core/src/main/java/org/jivesoftware/smack/SmackException.java
	smack-core/src/test/java/org/jivesoftware/smack/parsing/ParsingExceptionTest.java
	smack-core/src/test/java/org/jivesoftware/smack/test/util/TestUtils.java
	smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java
	smack-extensions/src/main/java/org/jivesoftware/smackx/caps/cache/SimpleDirectoryPersistentCache.java
	smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java
	smack-tcp/src/main/java/org/jivesoftware/smack/tcp/PacketReader.java
This commit is contained in:
Florian Schmaus 2014-07-05 14:37:22 +02:00
commit 94a16ba41e
61 changed files with 806 additions and 300 deletions

View file

@ -19,7 +19,6 @@ package org.jivesoftware.smack;
import static org.junit.Assert.*;
import java.io.StringReader;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList;
@ -31,11 +30,11 @@ import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
/**
@ -315,8 +314,6 @@ public class RosterTest {
final String contactJID = "nurse@example.com";
final Roster roster = connection.getRoster();
assertNotNull("Can't get the roster from the provided connection!", roster);
final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
final StringBuilder sb = new StringBuilder();
sb.append("<iq id=\"rostertest1\" type=\"set\" ")
.append("to=\"").append(connection.getUser()).append("\">")
@ -324,8 +321,7 @@ public class RosterTest {
.append("<item jid=\"").append(contactJID).append("\"/>")
.append("</query>")
.append("</iq>");
parser.setInput(new StringReader(sb.toString()));
parser.next();
final XmlPullParser parser = TestUtils.getIQParser(sb.toString());
final IQ rosterPush = PacketParserUtils.parseIQ(parser, connection);
initRoster(connection, roster);
rosterListener.reset();
@ -449,8 +445,6 @@ public class RosterTest {
final String contactJID = "nurse@example.com";
final Roster roster = connection.getRoster();
assertNotNull("Can't get the roster from the provided connection!", roster);
final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
final StringBuilder sb = new StringBuilder();
sb.append("<iq id=\"rostertest2\" type=\"set\" ")
.append("to=\"").append(connection.getUser()).append("\">")
@ -460,8 +454,7 @@ public class RosterTest {
.append("</item>")
.append("</query>")
.append("</iq>");
parser.setInput(new StringReader(sb.toString()));
parser.next();
final XmlPullParser parser = TestUtils.getIQParser(sb.toString());
final IQ rosterPush = PacketParserUtils.parseIQ(parser, connection);
initRoster(connection, roster);
rosterListener.reset();

View file

@ -23,6 +23,7 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.After;
import org.junit.Before;
@ -34,7 +35,7 @@ public class ParsingExceptionTest {
private final static String EXTENSION2 =
"<extension2 xmlns='namespace'>" +
"<bar node='testNode'>" +
"<i id='testid1' >" +
"<i id='testid1'>" +
"</i>" +
"</bar>" +
"</extension2>";
@ -51,12 +52,14 @@ public class ParsingExceptionTest {
@Test
public void consumeUnparsedInput() throws Exception {
XmlPullParser parser = PacketParserUtils.getParserFor(
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'>" +
"<" + ThrowException.ELEMENT + " xmlns='" + ThrowException.NAMESPACE + "'>" +
"<nothingInHere>" +
"</nothingInHere>" +
"</" + ThrowException.ELEMENT + ">" +
MESSAGE_EXCEPTION_ELEMENT +
EXTENSION2 +
"</message>");
int parserDepth = parser.getDepth();
@ -67,8 +70,7 @@ public class ParsingExceptionTest {
content = PacketParserUtils.parseContentDepth(parser, parserDepth);
}
assertNotNull(content);
assertEquals(content, "<nothingInHere></nothingInHere>" + "</" + ThrowException.ELEMENT + ">" + EXTENSION2);
assertEquals(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>", content);
}
static class ThrowException implements PacketExtensionProvider {

View file

@ -0,0 +1,72 @@
/**
*
* Copyright 2013 Robin Collier
*
* 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.smack.test.util;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
final public class TestUtils {
private TestUtils() {
}
public static XmlPullParser getIQParser(String stanza) {
return getParser(stanza, "iq");
}
public static XmlPullParser getMessageParser(String stanza) {
return getParser(stanza, "message");
}
public static XmlPullParser getPresenceParser(String stanza) {
return getParser(stanza, "presence");
}
public static XmlPullParser getParser(String string, String startTag) {
return getParser(new StringReader(string), startTag);
}
public static XmlPullParser getParser(Reader reader, String startTag) {
XmlPullParser parser;
try {
parser = PacketParserUtils.newXmppParser();
parser.setInput(reader);
if (startTag == null) {
return parser;
}
boolean found = false;
while (!found) {
if ((parser.next() == XmlPullParser.START_TAG) && parser.getName().equals(startTag))
found = true;
}
if (!found)
throw new IllegalArgumentException("Can not find start tag '" + startTag + "'");
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return parser;
}
}

View file

@ -27,12 +27,12 @@ import static org.junit.Assert.fail;
import java.util.Locale;
import java.util.Properties;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.test.util.TestUtils;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -653,7 +653,13 @@ public class PacketParserUtilsTest {
}
@Test
/**
* 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")
.a("from", "romeo@montague.lit/orchard")
@ -667,23 +673,10 @@ public class PacketParserUtilsTest {
.a("style", "font-weight: bold;")
.t("Bad Message Body")
.asString(outputProperties);
try {
Message message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
String body = "<span style=\"font-weight: bold;\">"
+ "Bad Message Body</span>";
assertEquals(body, message.getBody());
assertXMLNotEqual(control, message.toXML().toString());
DetailedDiff diffs = new DetailedDiff(new Diff(control, message.toXML().toString()));
// body has no namespace URI, span is escaped
assertEquals(6, diffs.getAllDifferences().size());
} catch(XmlPullParserException e) {
fail("No parser exception should be thrown" + e.getMessage());
}
Message message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
fail("Should throw exception. Instead got message: " + message.toXML().toString());
}
@Test
@ -792,8 +785,8 @@ public class PacketParserUtilsTest {
@Test
public void parseContentDepthTest() throws Exception {
final String stanza = "<iq type='result' to='foo@bar.com' from='baz.com' id='42'/>";
XmlPullParser parser = PacketParserUtils.getParserFor(stanza, "iq");
String content = PacketParserUtils.parseContentDepth(parser, 1);
XmlPullParser parser = TestUtils.getParser(stanza, "iq");
String content = PacketParserUtils.parseContent(parser);
assertEquals("", content);
}