1 package net.avcompris.commons3.testutil;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static com.google.common.collect.Lists.newArrayList;
5 import static org.apache.commons.lang3.CharEncoding.UTF_8;
6 import static org.apache.commons.lang3.StringUtils.join;
7 import static org.junit.jupiter.api.Assertions.assertEquals;
8 import static org.junit.jupiter.api.Assertions.assertTrue;
9
10 import java.io.File;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.List;
17
18 import org.apache.commons.io.FileUtils;
19 import org.apache.commons.io.IOUtils;
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.junit.jupiter.api.Disabled;
22 import org.junit.jupiter.api.DynamicContainer;
23 import org.junit.jupiter.api.DynamicTest;
24 import org.junit.jupiter.api.Test;
25
26 public abstract class TestUtils {
27
28 public static void assertFileContentEquals(final File refFile, final File testFile) throws IOException {
29
30 final List<String> refLines = FileUtils.readLines(refFile, UTF_8);
31 final List<String> testLines = FileUtils.readLines(testFile, UTF_8);
32
33 for (int i = 0; i < refLines.size() || i < testLines.size(); ++i) {
34
35 final String refLine = refLines.get(i);
36 final String testLine = testLines.get(i);
37
38 assertEquals(refLine, testLine,
39 "Line #" + (i + 1));
40 }
41 }
42
43 public static InputStream getResourceAsStream(final String resourcePath) throws IOException {
44
45 final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
46
47 if (is == null) {
48 throw new FileNotFoundException("resourcePath: " + resourcePath);
49 }
50
51 return is;
52 }
53
54 public static byte[] readResource(final String resourcePath) throws IOException {
55
56 checkNotNull(resourcePath, "resourcePath");
57
58 final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
59
60 if (is == null) {
61 throw new FileNotFoundException("resourcePath: " + resourcePath);
62 }
63
64 try {
65
66 return IOUtils.toByteArray(is);
67
68 } finally {
69 is.close();
70 }
71 }
72
73 public static void assertContains(final String ref, final String[] array) {
74
75 assertTrue(ArrayUtils.contains(array, ref),
76 "Array should countain: " + ref + ", but was: " + join(array, ", "));
77 }
78
79 public static void assertStartsWith(final String ref, final String s) {
80
81 assertTrue(s.startsWith(ref),
82 "String should start with: " + ref + ", but was: " + s);
83 }
84
85 public static DynamicContainer dynamicContainer(final Object instance) {
86
87 checkNotNull(instance, "instance");
88
89 final List<DynamicTest> tests = newArrayList();
90
91 final Class<? extends Object> clazz = instance.getClass();
92
93 for (final Method method : clazz.getMethods()) {
94
95 if (!method.isAnnotationPresent(Test.class)) {
96 continue;
97 }
98
99 if (method.isAnnotationPresent(Disabled.class)) {
100 continue;
101 }
102
103 method.setAccessible(true);
104
105 tests.add(DynamicTest.dynamicTest(method.getName(), () -> {
106
107 try {
108
109 method.invoke(instance);
110
111 } catch (final InvocationTargetException e) {
112
113 throw e.getTargetException();
114 }
115
116 }));
117 }
118
119 return DynamicContainer.dynamicContainer(instance.toString(), tests);
120 }
121 }