Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TestInstances API from JUnit Jupiter #2719

Merged
merged 7 commits into from
May 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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