2014-02-17 18:57:38 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-06-22 19:01:40 +02:00
|
|
|
package org.jivesoftware.smack.parsing;
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
|
2017-06-14 17:12:43 +02:00
|
|
|
import static org.junit.Assert.assertThat;
|
2013-06-22 19:01:40 +02:00
|
|
|
|
2019-02-05 10:41:50 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2019-02-15 23:21:30 +01:00
|
|
|
import org.jivesoftware.smack.packet.XmlEnvironment;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
2013-06-22 19:01:40 +02:00
|
|
|
import org.jivesoftware.smack.provider.ProviderManager;
|
2014-01-31 04:41:25 +01:00
|
|
|
import org.jivesoftware.smack.test.util.TestUtils;
|
2013-06-22 19:01:40 +02:00
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2013-06-22 19:01:40 +02:00
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
|
|
public class ParsingExceptionTest {
|
|
|
|
|
2018-03-29 12:35:11 +02:00
|
|
|
private static final String EXTENSION2 =
|
2013-06-22 19:01:40 +02:00
|
|
|
"<extension2 xmlns='namespace'>" +
|
|
|
|
"<bar node='testNode'>" +
|
2014-06-18 09:06:48 +02:00
|
|
|
"<i id='testid1'>" +
|
2013-06-22 19:01:40 +02:00
|
|
|
"</i>" +
|
|
|
|
"</bar>" +
|
|
|
|
"</extension2>";
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void init() {
|
2014-05-11 09:27:41 +02:00
|
|
|
ProviderManager.addExtensionProvider(ThrowException.ELEMENT, ThrowException.NAMESPACE, new ThrowException());
|
2013-06-22 19:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tini() {
|
2014-05-11 09:27:41 +02:00
|
|
|
ProviderManager.removeExtensionProvider(ThrowException.ELEMENT, ThrowException.NAMESPACE);
|
2013-06-22 19:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void consumeUnparsedInput() throws Exception {
|
2018-05-09 23:06:12 +02:00
|
|
|
final String MESSAGE_EXCEPTION_ELEMENT =
|
2014-06-18 09:06:48 +02:00
|
|
|
"<" + ThrowException.ELEMENT + " xmlns='" + ThrowException.NAMESPACE + "'>" +
|
|
|
|
"<nothingInHere>" +
|
|
|
|
"</nothingInHere>" +
|
|
|
|
"</" + ThrowException.ELEMENT + ">";
|
2013-06-22 19:01:40 +02:00
|
|
|
XmlPullParser parser = TestUtils.getMessageParser(
|
|
|
|
"<message from='user@server.example' to='francisco@denmark.lit' id='foo'>" +
|
2014-06-18 09:06:48 +02:00
|
|
|
MESSAGE_EXCEPTION_ELEMENT +
|
2013-06-22 19:01:40 +02:00
|
|
|
EXTENSION2 +
|
|
|
|
"</message>");
|
|
|
|
int parserDepth = parser.getDepth();
|
2014-07-06 21:10:40 +02:00
|
|
|
CharSequence content = null;
|
2013-06-22 19:01:40 +02:00
|
|
|
try {
|
|
|
|
PacketParserUtils.parseMessage(parser);
|
|
|
|
} catch (Exception e) {
|
2014-08-29 14:59:54 +02:00
|
|
|
content = PacketParserUtils.parseContentDepth(parser, parserDepth, false);
|
2013-06-22 19:01:40 +02:00
|
|
|
}
|
2014-07-06 21:10:40 +02:00
|
|
|
assertThat(MESSAGE_EXCEPTION_ELEMENT + EXTENSION2 + "</message>", equalsCharSequence(content));
|
2013-06-22 19:01:40 +02:00
|
|
|
}
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
static class ThrowException extends ExtensionElementProvider<ExtensionElement> {
|
2013-06-22 19:01:40 +02:00
|
|
|
public static final String ELEMENT = "exception";
|
|
|
|
public static final String NAMESPACE = "http://smack.jivesoftware.org/exception";
|
|
|
|
|
|
|
|
@Override
|
2019-02-15 23:21:30 +01:00
|
|
|
public ExtensionElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException {
|
2019-02-05 10:41:50 +01:00
|
|
|
throw new IOException("Test Exception");
|
2013-06-22 19:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|