Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -205,15 +201,15 @@ 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)) {
oos.writeObject(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");
}
}
}
Expand All @@ -232,11 +228,12 @@ public static void assertSerialization(Object base) {
* @param base the object to be tested
*/
public static <T> void assertJodaConvert(Class<T> 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");
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth defining assertTrue? It's such a standard method. assertEquals(..., true) seems a bit odd.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private methods, and since they are only used in a couple of places, it didn't seem worthwhile.

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
Expand All @@ -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");
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}

}