Skip to content

Commit

Permalink
Use TestInstances API from JUnit Jupiter (#2719)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard North <rich.north@gmail.com>
  • Loading branch information
tobiasstadler and rnorth authored May 24, 2022
1 parent b04540b commit f60051c
Showing 1 changed file with 6 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.junit.platform.commons.support.AnnotationSupport;
import org.junit.platform.commons.util.AnnotationUtils;
import org.junit.platform.commons.util.Preconditions;
Expand All @@ -23,7 +22,8 @@
import org.testcontainers.lifecycle.TestLifecycleAware;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
Expand All @@ -34,20 +34,13 @@
import static java.util.stream.Collectors.toList;
import static org.testcontainers.junit.jupiter.FilesystemFriendlyNameGenerator.filesystemFriendlyNameOf;

class TestcontainersExtension implements BeforeEachCallback, BeforeAllCallback, AfterEachCallback, AfterAllCallback, ExecutionCondition, TestInstancePostProcessor {
class TestcontainersExtension implements BeforeEachCallback, BeforeAllCallback, AfterEachCallback, AfterAllCallback, ExecutionCondition {

private static final Namespace NAMESPACE = Namespace.create(TestcontainersExtension.class);

private static final String TEST_INSTANCE = "testInstance";
private static final String SHARED_LIFECYCLE_AWARE_CONTAINERS = "sharedLifecycleAwareContainers";
private static final String LOCAL_LIFECYCLE_AWARE_CONTAINERS = "localLifecycleAwareContainers";

@Override
public void postProcessTestInstance(final Object testInstance, final ExtensionContext context) {
Store store = context.getStore(NAMESPACE);
store.put(TEST_INSTANCE, testInstance);
}

@Override
public void beforeAll(ExtensionContext context) {
Class<?> testClass = context.getTestClass()
Expand Down Expand Up @@ -156,17 +149,9 @@ boolean isDockerAvailable() {
}

private Set<Object> collectParentTestInstances(final ExtensionContext context) {
Set<Object> testInstances = new LinkedHashSet<>();
Optional<ExtensionContext> current = Optional.of(context);
while (current.isPresent()) {
ExtensionContext ctx = current.get();
Object testInstance = ctx.getStore(NAMESPACE).remove(TEST_INSTANCE);
if (testInstance != null) {
testInstances.add(testInstance);
}
current = ctx.getParent();
}
return testInstances;
List<Object> allInstances = new ArrayList<>(context.getRequiredTestInstances().getAllInstances());
Collections.reverse(allInstances);
return new LinkedHashSet<>(allInstances);
}

private List<StoreAdapter> findSharedContainers(Class<?> testClass) {
Expand Down

0 comments on commit f60051c

Please sign in to comment.