|
1 | 1 | package org.testcontainers.junit.vintage; |
2 | 2 |
|
| 3 | +import lombok.SneakyThrows; |
3 | 4 | import org.junit.platform.commons.support.AnnotationSupport; |
4 | 5 | import org.junit.platform.commons.support.HierarchyTraversalMode; |
5 | 6 | import org.junit.platform.commons.support.ModifierSupport; |
6 | 7 | import org.junit.platform.commons.support.ReflectionSupport; |
7 | 8 | import org.junit.runner.Description; |
| 9 | +import org.junit.runners.model.FrameworkMethod; |
8 | 10 | import org.junit.runners.model.MultipleFailureException; |
9 | 11 | import org.testcontainers.lifecycle.Startable; |
10 | 12 | import org.testcontainers.lifecycle.TestDescription; |
11 | 13 | import org.testcontainers.lifecycle.TestLifecycleAware; |
12 | 14 |
|
| 15 | +import java.lang.reflect.AnnotatedElement; |
13 | 16 | import java.lang.reflect.Field; |
| 17 | +import java.lang.reflect.Member; |
| 18 | +import java.lang.reflect.Method; |
14 | 19 | import java.util.ArrayList; |
15 | 20 | import java.util.Collections; |
16 | 21 | import java.util.List; |
|
20 | 25 | import java.util.function.Consumer; |
21 | 26 | import java.util.function.Predicate; |
22 | 27 | import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
23 | 29 |
|
24 | 30 | /** |
25 | 31 | * Integrates Testcontainers with the JUnit4 lifecycle. |
26 | 32 | */ |
27 | 33 | public final class Testcontainers extends FailureDetectingExternalResource { |
28 | 34 |
|
| 35 | + private static HierarchyTraversalMode TRAVERSAL_MODE = HierarchyTraversalMode.TOP_DOWN; |
| 36 | + |
29 | 37 | private final Object testInstance; |
30 | 38 |
|
31 | 39 | private List<Startable> startedContainers = Collections.emptyList(); |
@@ -116,51 +124,76 @@ protected void finished(Description description) throws Exception { |
116 | 124 | } |
117 | 125 |
|
118 | 126 | private List<Startable> findContainers(Description description) { |
119 | | - if (description.getTestClass() == null) { |
| 127 | + Class<?> testClass = description.getTestClass(); |
| 128 | + if (testClass == null) { |
120 | 129 | return Collections.emptyList(); |
121 | 130 | } |
122 | | - Predicate<Field> isTargetedContainer = isContainer(); |
123 | | - if (testInstance == null) { |
124 | | - isTargetedContainer = isTargetedContainer.and(ModifierSupport::isStatic); |
125 | | - } else { |
126 | | - isTargetedContainer = isTargetedContainer.and(ModifierSupport::isNotStatic); |
127 | | - } |
128 | 131 |
|
129 | | - return ReflectionSupport |
130 | | - .findFields(description.getTestClass(), isTargetedContainer, HierarchyTraversalMode.TOP_DOWN) |
131 | | - .stream() |
132 | | - .map(this::getContainerInstance) |
| 132 | + Predicate<Member> hasExpectedModifier = testInstance == null |
| 133 | + ? ModifierSupport::isStatic |
| 134 | + : ModifierSupport::isNotStatic; |
| 135 | + |
| 136 | + return Stream |
| 137 | + .of( |
| 138 | + ReflectionSupport |
| 139 | + .findMethods(testClass, isContainerMethod().and(hasExpectedModifier), TRAVERSAL_MODE) |
| 140 | + .stream() |
| 141 | + .map(this::createContainerInstance), |
| 142 | + ReflectionSupport |
| 143 | + .findFields(testClass, isContainerField().and(hasExpectedModifier), TRAVERSAL_MODE) |
| 144 | + .stream() |
| 145 | + .map(this::getContainerInstance) |
| 146 | + ) |
| 147 | + .flatMap(s -> s) |
133 | 148 | .collect(Collectors.toList()); |
134 | 149 | } |
135 | 150 |
|
136 | | - private static Predicate<Field> isContainer() { |
137 | | - return field -> { |
138 | | - boolean isAnnotatedWithContainer = AnnotationSupport.isAnnotated(field, Container.class); |
139 | | - if (isAnnotatedWithContainer) { |
140 | | - boolean isStartable = Startable.class.isAssignableFrom(field.getType()); |
| 151 | + private static Predicate<Method> isContainerMethod() { |
| 152 | + return method -> isAnnotatedWithContainer(method); |
| 153 | + } |
141 | 154 |
|
142 | | - if (!isStartable) { |
143 | | - throw new RuntimeException( |
144 | | - String.format("The @Container field '%s' does not implement Startable", field.getName()) |
145 | | - ); |
146 | | - } |
147 | | - return true; |
148 | | - } |
149 | | - return false; |
150 | | - }; |
| 155 | + private static Predicate<Field> isContainerField() { |
| 156 | + return field -> isAnnotatedWithContainer(field); |
151 | 157 | } |
152 | 158 |
|
| 159 | + private static boolean isAnnotatedWithContainer(AnnotatedElement element) { |
| 160 | + return AnnotationSupport.isAnnotated(element, Container.class); |
| 161 | + } |
| 162 | + |
| 163 | + private Startable createContainerInstance(Method method) { |
| 164 | + if (!Startable.class.isAssignableFrom(method.getReturnType())) { |
| 165 | + throw new RuntimeException( |
| 166 | + String.format("The @Container method '%s()' does not return a Startable", method.getName()) |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + method.setAccessible(true); |
| 171 | + Object container = invokeExplosively(method, testInstance); |
| 172 | + if (container == null) { |
| 173 | + throw new RuntimeException(String.format("The @Container method '%s()' returned null", method.getName())); |
| 174 | + } |
| 175 | + return (Startable) container; |
| 176 | + } |
| 177 | + |
| 178 | + @SneakyThrows(IllegalAccessException.class) |
153 | 179 | private Startable getContainerInstance(Field field) { |
154 | | - try { |
155 | | - field.setAccessible(true); |
156 | | - Startable containerInstance = (Startable) field.get(testInstance); |
157 | | - if (containerInstance == null) { |
158 | | - throw new RuntimeException("Container " + field.getName() + " needs to be initialized"); |
159 | | - } |
160 | | - return containerInstance; |
161 | | - } catch (IllegalAccessException e) { |
162 | | - throw new RuntimeException("Cannot access container defined in field " + field.getName()); |
| 180 | + if (!Startable.class.isAssignableFrom(field.getType())) { |
| 181 | + throw new RuntimeException( |
| 182 | + String.format("The @Container field '%s' does not implement Startable", field.getName()) |
| 183 | + ); |
163 | 184 | } |
| 185 | + |
| 186 | + field.setAccessible(true); |
| 187 | + Startable container = (Startable) field.get(testInstance); |
| 188 | + if (container == null) { |
| 189 | + throw new RuntimeException("Container " + field.getName() + " needs to be initialized"); |
| 190 | + } |
| 191 | + return container; |
| 192 | + } |
| 193 | + |
| 194 | + @SneakyThrows(Throwable.class) |
| 195 | + private static Object invokeExplosively(Method method, Object target, final Object... params) { |
| 196 | + return new FrameworkMethod(method).invokeExplosively(target, params); |
164 | 197 | } |
165 | 198 |
|
166 | 199 | private static <T> void forEachReversed(List<T> list, Consumer<? super T> callback) { |
|
0 commit comments