Merge pull request #137 from vanitasvitae/jingleTests

Add tests for jingle classes
This commit is contained in:
Florian Schmaus 2017-06-05 19:06:29 +02:00 committed by GitHub
commit acc98b4b2f
6 changed files with 379 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;
/**
* Test the JingleAction class.
*/
public class JingleActionTest extends SmackTestSuite {
@Test
public void enumTest() {
assertEquals("content-accept", JingleAction.content_accept.toString());
assertEquals(JingleAction.content_accept, JingleAction.fromString("content-accept"));
for (JingleAction a : JingleAction.values()) {
assertEquals(a, JingleAction.fromString(a.toString()));
}
try {
JingleAction inexistentAction = JingleAction.fromString("inexistent-action");
fail();
} catch (IllegalArgumentException e) {
// Expected
}
}
}

View File

@ -0,0 +1,79 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNotSame;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.fail;
/**
* Test the JingleContent class.
*/
public class JingleContentTest extends SmackTestSuite {
@Test
public void parserTest() {
JingleContent.Builder builder = JingleContent.getBuilder();
try {
builder.build();
fail();
} catch (NullPointerException e) {
// Expected
}
builder.setCreator(JingleContent.Creator.initiator);
try {
builder.build();
fail();
} catch (IllegalArgumentException e) {
// Expected
}
builder.setName("A name");
JingleContent content = builder.build();
assertNotNull(content);
assertNull(content.getDescription());
assertEquals(JingleContent.Creator.initiator, content.getCreator());
assertEquals("A name", content.getName());
builder.setSenders(JingleContent.Senders.both);
content = builder.build();
assertEquals(JingleContent.Senders.both, content.getSenders());
builder.setDisposition("session");
JingleContent content1 = builder.build();
assertEquals("session", content1.getDisposition());
assertNotSame(content.toXML().toString(), content1.toXML().toString());
assertEquals(content1.toXML().toString(), builder.build().toXML().toString());
assertEquals(0, content.getJingleTransportsCount());
assertNotNull(content.getJingleTransports());
String xml =
"<content creator='initiator' disposition='session' name='A name' senders='both'>" +
"</content>";
assertEquals(xml, content1.toXML().toString());
}
}

View File

@ -0,0 +1,50 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.JingleError;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;
/**
* Test the JingleError class. TODO: Uncomment missing tests once implemented.
*/
public class JingleErrorTest extends SmackTestSuite {
@Test
public void parserTest() {
assertEquals("<out-of-order xmlns='urn:xmpp:jingle:errors:1'/>",
JingleError.fromString("out-of-order").toXML().toString());
//assertEquals("<tie-break xmlns='urn:xmpp:jingle:errors:1'/>",
// JingleError.fromString("tie-break").toXML().toString());
assertEquals("<unknown-session xmlns='urn:xmpp:jingle:errors:1'/>",
JingleError.fromString("unknown-session").toXML().toString());
//assertEquals("<unsupported-info xmlns='urn:xmpp:jingle:errors:1'/>",
// JingleError.fromString("unsupported-info").toXML().toString());
assertEquals("unknown-session", JingleError.fromString("unknown-session").getMessage());
try {
JingleError.fromString("inexistent-error");
fail();
} catch (IllegalArgumentException e) {
// Expected
}
}
}

View File

@ -0,0 +1,73 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smackx.jingle.element.JingleReason;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;
/**
* Test JingleReason functionality.
*/
public class JingleReasonTest extends SmackTestSuite {
@Test
public void parserTest() {
assertEquals("<reason><success/></reason>",
new JingleReason(JingleReason.Reason.success).toXML().toString());
assertEquals("<reason><busy/></reason>",
new JingleReason(JingleReason.Reason.busy).toXML().toString());
assertEquals("<reason><cancel/></reason>",
new JingleReason(JingleReason.Reason.cancel).toXML().toString());
assertEquals("<reason><connectivity-error/></reason>",
new JingleReason(JingleReason.Reason.connectivity_error).toXML().toString());
assertEquals("<reason><decline/></reason>",
new JingleReason(JingleReason.Reason.decline).toXML().toString());
assertEquals("<reason><expired/></reason>",
new JingleReason(JingleReason.Reason.expired).toXML().toString());
assertEquals("<reason><unsupported-transports/></reason>",
new JingleReason(JingleReason.Reason.unsupported_transports).toXML().toString());
assertEquals("<reason><failed-transport/></reason>",
new JingleReason(JingleReason.Reason.failed_transport).toXML().toString());
assertEquals("<reason><general-error/></reason>",
new JingleReason(JingleReason.Reason.general_error).toXML().toString());
assertEquals("<reason><gone/></reason>",
new JingleReason(JingleReason.Reason.gone).toXML().toString());
assertEquals("<reason><media-error/></reason>",
new JingleReason(JingleReason.Reason.media_error).toXML().toString());
assertEquals("<reason><security-error/></reason>",
new JingleReason(JingleReason.Reason.security_error).toXML().toString());
assertEquals("<reason><unsupported-applications/></reason>",
new JingleReason(JingleReason.Reason.unsupported_applications).toXML().toString());
assertEquals("<reason><timeout/></reason>",
new JingleReason(JingleReason.Reason.timeout).toXML().toString());
assertEquals("<reason><failed-application/></reason>",
new JingleReason(JingleReason.Reason.failed_application).toXML().toString());
assertEquals("<reason><incompatible-parameters/></reason>",
new JingleReason(JingleReason.Reason.incompatible_parameters).toXML().toString());
assertEquals(JingleReason.Reason.alternative_session, JingleReason.Reason.fromString("alternative-session"));
try {
JingleReason.Reason nonExistent = JingleReason.Reason.fromString("illegal-reason");
fail();
} catch (IllegalArgumentException e) {
// Expected
}
}
}

View File

@ -0,0 +1,51 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;
import org.junit.Test;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotSame;
/**
* Test JingleSession class.
*/
public class JingleSessionTest extends SmackTestSuite {
@Test
public void sessionTest() throws XmppStringprepException {
Jid romeo = JidCreate.from("romeo@montague.lit");
Jid juliet = JidCreate.from("juliet@capulet.lit");
String sid = StringUtils.randomString(24);
JingleSession s1 = new JingleSession(romeo, juliet, sid);
JingleSession s2 = new JingleSession(juliet, romeo, sid);
JingleSession s3 = new JingleSession(romeo, juliet, StringUtils.randomString(23));
JingleSession s4 = new JingleSession(juliet, romeo, sid);
assertNotSame(s1, s2);
assertNotSame(s1, s3);
assertNotSame(s2, s3);
assertEquals(s2, s4);
assertEquals(s2.hashCode(), s4.hashCode());
}
}

View File

@ -0,0 +1,79 @@
/**
*
* Copyright 2017 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.jingle;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.junit.Test;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;
/**
* Test the Jingle class.
*/
public class JingleTest extends SmackTestSuite {
@Test
public void parserTest() throws XmppStringprepException {
String sessionId = StringUtils.randomString(24);
Jingle.Builder builder = Jingle.getBuilder();
try {
builder.build();
fail();
} catch (IllegalArgumentException e) {
// Expected
}
builder.setSessionId(sessionId);
try {
builder.build();
fail();
} catch (NullPointerException e) {
// Expected
}
builder.setAction(JingleAction.session_initiate);
FullJid romeo = JidCreate.fullFrom("romeo@montague.lit/orchard");
FullJid juliet = JidCreate.fullFrom("juliet@capulet.lit/balcony");
builder.setInitiator(romeo);
builder.setResponder(juliet);
Jingle jingle = builder.build();
assertNotNull(jingle);
assertEquals(romeo, jingle.getInitiator());
assertEquals(juliet, jingle.getResponder());
assertEquals(jingle.getAction(), JingleAction.session_initiate);
assertEquals(sessionId, jingle.getSid());
String xml = "<jingle xmlns='urn:xmpp:jingle:1' " +
"initiator='romeo@montague.lit/orchard' " +
"responder='juliet@capulet.lit/balcony' " +
"action='session-initiate' " +
"sid='" + sessionId + "'>" +
"</jingle>";
assertTrue(jingle.toXML().toString().contains(xml));
}
}