From 93ea913981de897d9365b92bd0a8e2430bf95f6a Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Thu, 20 Jun 2019 15:07:05 +0100 Subject: [PATCH] Remove TestNG from TestHelper Allows the class to be used from JUnit tests Without this TestNG and JUnit are both seen by maven when running tests --- .../opengamma/strata/collect/TestHelper.java | 51 +++++++++---- .../strata/collect/TestHelperTest.java | 74 +++++++++++++++++++ 2 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 modules/collect/src/test/java/com/opengamma/strata/collect/TestHelperTest.java diff --git a/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelper.java b/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelper.java index 6dceaed615..f6cd9f513c 100644 --- a/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelper.java +++ b/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelper.java @@ -5,11 +5,6 @@ */ package com.opengamma.strata.collect; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -37,6 +32,7 @@ import org.joda.beans.test.JodaBeanTests; import org.joda.convert.StringConvert; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; /** @@ -205,7 +201,7 @@ public static void assertEqualsBeanDetailed(Bean actual, Bean expected) { * @param base the object to be tested */ public static void assertSerialization(Object base) { - assertNotNull(base); + assertNotNull(base, "assertSerialization() called with null Object"); try { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { @@ -213,7 +209,7 @@ public static void assertSerialization(Object base) { oos.close(); try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { try (ObjectInputStream ois = new ObjectInputStream(bais)) { - assertEquals(ois.readObject(), base); + assertEquals(ois.readObject(), base, "Result from roundtrip not equal to base object"); } } } @@ -232,11 +228,12 @@ public static void assertSerialization(Object base) { * @param base the object to be tested */ public static void assertJodaConvert(Class cls, Object base) { - assertNotNull(base); + assertNotNull(base, "assertJodaConvert() called with null Class"); + assertNotNull(base, "assertJodaConvert() called with null Object"); StringConvert convert = StringConvert.create(); String str = convert.convertToString(base); T result = convert.convertFromString(cls, str); - assertEquals(result, base); + assertEquals(result, base, "Result from roundtrip not equal to base object"); } //------------------------------------------------------------------------- @@ -521,14 +518,14 @@ public void flush() { */ public static void assertUtilityClass(Class clazz) { assertNotNull(clazz, "assertUtilityClass() called with null class"); - assertTrue(Modifier.isFinal(clazz.getModifiers()), "Utility class must be final"); + assertEquals(Modifier.isFinal(clazz.getModifiers()), true, "Utility class must be final"); assertEquals(clazz.getDeclaredConstructors().length, 1, "Utility class must have one constructor"); Constructor con = clazz.getDeclaredConstructors()[0]; assertEquals(con.getParameterTypes().length, 0, "Utility class must have zero-arg constructor"); - assertTrue(Modifier.isPrivate(con.getModifiers()), "Utility class must have private constructor"); + assertEquals(Modifier.isPrivate(con.getModifiers()), true, "Utility class must have private constructor"); for (Method method : clazz.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers())) { - assertTrue(Modifier.isStatic(method.getModifiers()), "Utility class public methods must be static"); + assertEquals(Modifier.isStatic(method.getModifiers()), true, "Utility class public methods must be static"); } } // coverage @@ -553,7 +550,7 @@ public static void coverPrivateConstructor(Class clazz) { con.setAccessible(true); con.newInstance(); }); - assertTrue(isPrivate.get(), "No-arg constructor must be private"); + assertEquals(isPrivate.get(), true, "No-arg constructor must be private"); } //------------------------------------------------------------------------- @@ -621,4 +618,32 @@ public static void coverBeanEquals(Bean bean1, Bean bean2) { JodaBeanTests.coverBeanEquals(bean1, bean2); } + //------------------------------------------------------------------------- + // avoid runtime dependency on either JUnit or TestNG by inlining code and using AssertionError + @VisibleForTesting + static void assertEquals(Object actual, Object expected, String message) { + if (expected == actual || + (expected != null && actual != null && expected.equals(actual) && actual.equals(expected))) { + return; + } + fail((message != null ? message + " " : "") + "expected [" + expected + "] but found [" + actual + ']'); + } + + @VisibleForTesting + static void assertNotNull(Object actual, String message) { + if (actual == null) { + fail(message); + } + } + + private static void fail(String message) { + throw new AssertionError(message); + } + + private static void fail(String message, Throwable cause) { + AssertionError ex = new AssertionError(message); + ex.initCause(cause); + throw ex; + } + } diff --git a/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelperTest.java b/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelperTest.java new file mode 100644 index 0000000000..ad90727482 --- /dev/null +++ b/modules/collect/src/test/java/com/opengamma/strata/collect/TestHelperTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2019 - present by OpenGamma Inc. and the OpenGamma group of companies + * + * Please see distribution for license. + */ +package com.opengamma.strata.collect; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + +import org.testng.annotations.Test; + +/** + * Tes {@link TestHelper}. + */ +public class TestHelperTest { + + @Test + public void test_assertEquals_objectObject() { + TestHelper.assertEquals("abc", "abc", null); // same string + TestHelper.assertEquals("abc", "abcd".substring(0, 3), null); // equals string + } + + @Test + public void test_assertEquals_nullNull() { + TestHelper.assertEquals(null, null, null); + } + + @Test + public void test_assertEquals_notEqual() { + try { + TestHelper.assertEquals("abc", "def", "Oops"); + fail("Should fail"); + } catch (AssertionError ex) { + assertEquals(ex.getMessage(), "Oops expected [def] but found [abc]"); + } + } + + @Test + public void test_assertEquals_objectNull() { + try { + TestHelper.assertEquals("abc", null, null); + fail("Should fail"); + } catch (AssertionError ex) { + assertEquals(ex.getMessage(), "expected [null] but found [abc]"); + } + } + + @Test + public void test_assertEquals_nullObject() { + try { + TestHelper.assertEquals(null, "abc", null); + fail("Should fail"); + } catch (AssertionError ex) { + assertEquals(ex.getMessage(), "expected [abc] but found [null]"); + } + } + + @Test + public void test_assertNotNull_notNull() { + TestHelper.assertNotNull("abc", "Oops"); + } + + @Test + public void test_assertNotNull_null() { + try { + TestHelper.assertNotNull(null, "Oops"); + fail("Should fail"); + } catch (AssertionError ex) { + assertEquals(ex.getMessage(), "Oops"); + } + } + +}