summaryrefslogtreecommitdiffstats
path: root/testutil
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@yahooinc.com>2022-07-28 13:50:03 +0200
committerBjørn Christian Seime <bjorncs@yahooinc.com>2022-07-28 14:51:33 +0200
commit6218325aebf0940c351f8d2a2f6b818311c60c3c (patch)
treeb375cb7e513bd11aed0cfebc811dd9d0cc185c94 /testutil
parentb95b38590737137a362aa29492a070ac7ba60a40 (diff)
Make testutil compatible with both Junit4 and Junit5
Diffstat (limited to 'testutil')
-rw-r--r--testutil/src/main/java/com/yahoo/test/JunitCompat.java65
-rw-r--r--testutil/src/main/java/com/yahoo/test/PartialOrderTester.java9
-rw-r--r--testutil/src/main/java/com/yahoo/test/TotalOrderTester.java9
-rw-r--r--testutil/src/main/java/com/yahoo/test/json/JsonTestHelper.java6
4 files changed, 74 insertions, 15 deletions
diff --git a/testutil/src/main/java/com/yahoo/test/JunitCompat.java b/testutil/src/main/java/com/yahoo/test/JunitCompat.java
new file mode 100644
index 00000000000..b771ffa0e22
--- /dev/null
+++ b/testutil/src/main/java/com/yahoo/test/JunitCompat.java
@@ -0,0 +1,65 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.List;
+
+/**
+ * Hack to support both junit4 and junit5
+ *
+ * @author bjorncs
+ */
+public class JunitCompat {
+ private JunitCompat() {}
+
+ public static <T> void assertEquals(T l, T r) {
+ List<Class<?>> argTypes = List.of(Object.class, Object.class);
+ List<Object> argValues = List.of(l, r);
+ invokeAssert("assertEquals", argTypes, argValues, argTypes, argValues);
+ }
+
+ public static void assertEquals(String msg, long l, long r) {
+ List<Class<?>> junit4ArgTypes = List.of(String.class, long.class, long.class);
+ List<Object> junit4ArgValues = List.of(msg, l, r);
+ List<Class<?>> junit5ArgTypes = List.of(long.class, long.class, String.class);
+ List<Object> junit5ArgValues = List.of(l, r, msg);
+ invokeAssert("assertEquals", junit4ArgTypes, junit4ArgValues, junit5ArgTypes, junit5ArgValues);
+ }
+
+ public static void assertTrue(String msg, boolean b) {
+ List<Class<?>> junit4ArgTypes = List.of(String.class, boolean.class);
+ List<Object> junit4ArgValues = List.of(msg, b);
+ List<Class<?>> junit5ArgTypes = List.of(boolean.class, String.class);
+ List<Object> junit5ArgValues = List.of(b, msg);
+ invokeAssert("assertTrue", junit4ArgTypes, junit4ArgValues, junit5ArgTypes, junit5ArgValues);
+ }
+
+ private static void invokeAssert(String method, List<Class<?>> junit4ArgTypes, List<Object> junit4ArgValues,
+ List<Class<?>> junit5ArgTypes, List<Object> junit5ArgValues) {
+ try {
+ invokeAssert("org.junit.jupiter.api.Assertions", method, junit5ArgTypes, junit5ArgValues);
+ } catch (ReflectiveOperationException e) {
+ try {
+ invokeAssert("org.junit.Assert", method, junit4ArgTypes, junit4ArgValues);
+ } catch (ReflectiveOperationException ex) {
+ throw new RuntimeException("Unable to find junit4 or junit5 on test classpath", ex);
+ }
+ }
+ }
+
+ private static void invokeAssert(String clazz, String method, List<Class<?>> argTypes, List<Object> argValues)
+ throws ReflectiveOperationException {
+ try {
+ Class<?> c = Class.forName(clazz);
+ Method m = c.getMethod(method, argTypes.toArray(new Class<?>[0]));
+ m.invoke(null, argValues.toArray());
+ } catch (InvocationTargetException e) {
+ if (e.getCause() instanceof AssertionError ae) {
+ throw ae;
+ } else {
+ throw new RuntimeException(e.getCause());
+ }
+ }
+ }
+}
diff --git a/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java b/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java
index ed92171542c..0917355c50c 100644
--- a/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java
+++ b/testutil/src/main/java/com/yahoo/test/PartialOrderTester.java
@@ -1,9 +1,6 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
/**
* PartialOrderTester implements a partial order test for OrderTester
*
@@ -14,14 +11,14 @@ import static org.junit.Assert.assertTrue;
public class PartialOrderTester<T extends Comparable<T>> extends OrderTester<T> {
protected void lessTest(T a, T b) throws AssertionError {
- assertTrue(a + " must be less than or equal to " + b, a.compareTo(b) <= 0);
+ JunitCompat.assertTrue(a + " must be less than or equal to " + b, a.compareTo(b) <= 0);
}
protected void greaterTest(T a, T b) throws AssertionError {
- assertTrue(a + " must be greater than or equal to " + b, a.compareTo(b) >= 0);
+ JunitCompat.assertTrue(a + " must be greater than or equal to " + b, a.compareTo(b) >= 0);
}
protected void equalTest(T a, T b) throws AssertionError {
- assertEquals(a + " must be compared equal to " + b, 0, a.compareTo(b));
+ JunitCompat.assertEquals(a + " must be compared equal to " + b, 0, a.compareTo(b));
}
}
diff --git a/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java b/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java
index e95bc056ba8..2b5f100ca50 100644
--- a/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java
+++ b/testutil/src/main/java/com/yahoo/test/TotalOrderTester.java
@@ -1,9 +1,6 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
/**
* TotalOrderTester implements a total order test for OrderTester
*
@@ -22,14 +19,14 @@ import static org.junit.Assert.assertTrue;
public class TotalOrderTester<T extends Comparable<? super T>> extends OrderTester<T> {
protected void lessTest(T a, T b) throws AssertionError {
- assertTrue(a + " must be less than " + b, a.compareTo(b) <= -1);
+ JunitCompat.assertTrue(a + " must be less than " + b, a.compareTo(b) <= -1);
}
protected void greaterTest(T a, T b) throws AssertionError {
- assertTrue(a + " must be greater than " + b, a.compareTo(b) >= 1);
+ JunitCompat.assertTrue(a + " must be greater than " + b, a.compareTo(b) >= 1);
}
protected void equalTest(T a, T b) throws AssertionError {
- assertEquals(a + " must be compared equal to " + b, 0, a.compareTo(b));
+ JunitCompat.assertEquals(a + " must be compared equal to " + b, 0, a.compareTo(b));
}
}
diff --git a/testutil/src/main/java/com/yahoo/test/json/JsonTestHelper.java b/testutil/src/main/java/com/yahoo/test/json/JsonTestHelper.java
index 05532d2a504..f7112ee9379 100644
--- a/testutil/src/main/java/com/yahoo/test/json/JsonTestHelper.java
+++ b/testutil/src/main/java/com/yahoo/test/json/JsonTestHelper.java
@@ -5,11 +5,10 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
+import com.yahoo.test.JunitCompat;
import java.io.UncheckedIOException;
-import static org.junit.Assert.assertEquals;
-
/**
* @author Vegard Sjonfjell
*/
@@ -31,7 +30,7 @@ public class JsonTestHelper {
try {
JsonNode expected = mapper.readTree(expectedJson);
JsonNode actual = mapper.readTree(inputJson);
- assertEquals(expected, actual);
+ JunitCompat.assertEquals(expected, actual);
} catch (JsonProcessingException e) {
throw new RuntimeException("Exception when comparing json strings." , e);
}
@@ -57,4 +56,5 @@ public class JsonTestHelper {
throw new UncheckedIOException(e);
}
}
+
}