From 08a4ee4eb2f0706252ad97478833b83347ece5dc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 17 Jun 2017 16:08:32 +0200 Subject: [PATCH] Reworked some Jingle unit tests to use Junit's "expected=" Also don't use randomString() because then the unit tests also depend on the correct behavior of it. --- .../smackx/jingle/JingleActionTest.java | 11 ++---- .../smackx/jingle/JingleContentTest.java | 27 +++++++------- .../smackx/jingle/JingleErrorTest.java | 11 ++---- .../smackx/jingle/JingleReasonTest.java | 37 ++++++++----------- .../smackx/jingle/JingleTest.java | 34 ++++++++--------- 5 files changed, 54 insertions(+), 66 deletions(-) diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleActionTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleActionTest.java index ec85aa7b1..b8b077c82 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleActionTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleActionTest.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.jingle; import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.fail; import org.jivesoftware.smack.test.util.SmackTestSuite; @@ -38,12 +37,10 @@ public class JingleActionTest extends SmackTestSuite { for (JingleAction a : JingleAction.values()) { assertEquals(a, JingleAction.fromString(a.toString())); } + } - try { - JingleAction inexistentAction = JingleAction.fromString("inexistent-action"); - fail(); - } catch (IllegalArgumentException e) { - // Expected - } + @Test(expected = IllegalArgumentException.class) + public void nonExistendEnumTest() { + JingleAction.fromString("inexistent-action"); } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleContentTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleContentTest.java index 3a3e69502..a3dd03156 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleContentTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleContentTest.java @@ -20,7 +20,6 @@ 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; import org.jivesoftware.smack.test.util.SmackTestSuite; @@ -33,25 +32,25 @@ import org.junit.Test; */ public class JingleContentTest extends SmackTestSuite { + @Test(expected = NullPointerException.class) + public void emptyBuilderThrowsTest() { + JingleContent.Builder builder = JingleContent.getBuilder(); + builder.build(); + } + + @Test(expected = IllegalArgumentException.class) + public void onlyCreatorBuilderThrowsTest() { + JingleContent.Builder builder = JingleContent.getBuilder(); + builder.setCreator(JingleContent.Creator.initiator); + builder.build(); + } + @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(); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleErrorTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleErrorTest.java index ba8f92ac1..d21ecf73d 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleErrorTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleErrorTest.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.jingle; import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.fail; import org.jivesoftware.smack.test.util.SmackTestSuite; @@ -41,12 +40,10 @@ public class JingleErrorTest extends SmackTestSuite { assertEquals("", JingleError.fromString("unsupported-info").toXML().toString()); assertEquals("unknown-session", JingleError.fromString("unknown-session").getMessage()); + } - try { - JingleError.fromString("inexistent-error"); - fail(); - } catch (IllegalArgumentException e) { - // Expected - } + @Test(expected = IllegalArgumentException.class) + public void illegalArgumentTest() { + JingleError.fromString("inexistent-error"); } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java index 6fa7caa2c..d62de70b3 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.jingle; import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.fail; import org.jivesoftware.smack.test.util.SmackTestSuite; @@ -66,26 +65,22 @@ public class JingleReasonTest extends SmackTestSuite { JingleReason.IncompatibleParameters.toXML().toString()); assertEquals("1234", JingleReason.AlternativeSession("1234").toXML().toString()); - // Alternative sessionID must not be empty - try { - JingleReason.AlternativeSession(""); - fail(); - } catch (NullPointerException e) { - // Expected - } - // Alternative sessionID must not be null - try { - JingleReason.AlternativeSession(null); - fail(); - } catch (NullPointerException e) { - // Expected - } + } - try { - JingleReason.Reason nonExistent = JingleReason.Reason.fromString("illegal-reason"); - fail(); - } catch (IllegalArgumentException e) { - // Expected - } + @Test(expected = NullPointerException.class) + public void alternativeSessionEmptyStringTest() { + // Alternative sessionID must not be empty + JingleReason.AlternativeSession(""); + } + + @Test(expected = NullPointerException.class) + public void alternativeSessionNullStringTest() { + // Alternative sessionID must not be null + JingleReason.AlternativeSession(null); + } + + @Test(expected = IllegalArgumentException.class) + public void illegalArgumentTest() { + JingleReason.Reason.fromString("illegal-reason"); } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleTest.java index 2ae9fb7de..c7827e475 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleTest.java @@ -19,10 +19,8 @@ package org.jivesoftware.smackx.jingle; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertNotNull; import static junit.framework.TestCase.assertTrue; -import static junit.framework.TestCase.fail; 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; @@ -37,25 +35,27 @@ import org.jxmpp.stringprep.XmppStringprepException; */ public class JingleTest extends SmackTestSuite { - @Test - public void parserTest() throws XmppStringprepException { - String sessionId = StringUtils.randomString(24); + @Test(expected = IllegalArgumentException.class) + public void emptyBuilderTest() { + Jingle.Builder builder = Jingle.getBuilder(); + builder.build(); + } + + @Test(expected = NullPointerException.class) + public void onlySessionIdBuilderTest() { + String sessionId = "testSessionId"; Jingle.Builder builder = Jingle.getBuilder(); - try { - builder.build(); - fail(); - } catch (IllegalArgumentException e) { - // Expected - } builder.setSessionId(sessionId); + builder.build(); + } - try { - builder.build(); - fail(); - } catch (NullPointerException e) { - // Expected - } + @Test + public void parserTest() throws XmppStringprepException { + String sessionId = "testSessionId"; + + Jingle.Builder builder = Jingle.getBuilder(); + builder.setSessionId(sessionId); builder.setAction(JingleAction.session_initiate); FullJid romeo = JidCreate.fullFrom("romeo@montague.lit/orchard");