aboutsummaryrefslogtreecommitdiffstats
path: root/testutil/src/main/java/com/yahoo/test/JunitCompat.java
blob: 0d92f65729e8016793463b6351f7ed3854d68022 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Vespa.ai. 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());
            }
        }
    }
}