1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-27 05:44:50 +02:00
Smack/smack-experimental/src/test/java/org/jivesoftware/smackx/reference/ReferenceTest.java
Florian Schmaus 4133eb175c Replace XPP3 by XmlPullParser interface wrapping StAX and XPP3
Introducing Smack's own XmlPullParser interface which tries to stay as
compatible as possible to XPP3. The interface is used to either wrap
StAX's XMLStreamReader if Smack is used on Java SE, and XPP3's
XmlPullParser if Smack is used on on Android.

Fixes SMACK-591.

Also introduce JUnit 5 and non-strict javadoc projects.
2019-05-06 22:10:50 +02:00

113 lines
4.5 KiB
Java

/**
*
* Copyright 2018 Paul Schaub
*
* 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.reference;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.net.URI;
import java.net.URISyntaxException;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.reference.element.ReferenceElement;
import org.jivesoftware.smackx.reference.provider.ReferenceProvider;
import org.junit.jupiter.api.Test;
public class ReferenceTest extends SmackTestSuite {
@Test
public void providerMentionTest() throws Exception {
String xml = "<reference xmlns='urn:xmpp:reference:0' " +
"begin='72' " +
"end='78' " +
"type='mention' " +
"uri='xmpp:juliet@capulet.lit' />";
URI uri = new URI("xmpp:juliet@capulet.lit");
ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null, uri);
assertXMLEqual(xml, element.toXML().toString());
assertEquals(72, (int) element.getBegin());
assertEquals(78, (int) element.getEnd());
assertEquals(ReferenceElement.Type.mention, element.getType());
assertNull(element.getAnchor());
assertEquals(uri, element.getUri());
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
assertXMLEqual(xml, parsed.toXML().toString());
}
/**
* TODO: The uri might not be following the XMPP schema.
* That shouldn't matter though.
* @throws Exception
*/
@Test
public void providerDataTest() throws Exception {
String xml = "<reference xmlns='urn:xmpp:reference:0' " +
"type='data' " +
"uri='xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be' />";
URI uri = new URI("xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be");
ReferenceElement element = new ReferenceElement(null, null, ReferenceElement.Type.data, null, uri);
assertXMLEqual(xml, element.toXML().toString());
assertNull(element.getBegin());
assertNull(element.getEnd());
assertNull(element.getAnchor());
assertEquals(ReferenceElement.Type.data, element.getType());
assertEquals(uri, element.getUri());
ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml));
assertXMLEqual(xml, parsed.toXML().toString());
}
@Test
public void beginGreaterEndIllegalTest() throws URISyntaxException {
assertThrows(IllegalArgumentException.class, () ->
new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test")));
}
@Test
public void beginSmallerZeroTest() throws URISyntaxException {
assertThrows(IllegalArgumentException.class, () ->
new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, new URI("xmpp:test@test.test")));
}
@Test
public void endSmallerZeroTest() throws URISyntaxException {
assertThrows(IllegalArgumentException.class, () ->
new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test")));
}
@Test
public void typeArgumentNullTest() throws URISyntaxException {
assertThrows(NullPointerException.class, () ->
new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test")));
}
/*
* TODO: Later maybe remove this test in case the uri attribute becomes optional.
@Test(expected = NullPointerException.class)
public void uriArgumentNullTest() {
new ReferenceElement(1, 2, ReferenceElement.Type.mention, null, null);
}
*/
}