From 9a700505dfdc813d462f6d717f953a7773a4dc39 Mon Sep 17 00:00:00 2001 From: "James R. Perkins" Date: Wed, 3 Jul 2024 14:07:23 -0700 Subject: [PATCH] Revert back the type requirement of GenericContainer instead of Startable. Signed-off-by: James R. Perkins --- .../ContainerInjectionTestEnricher.java | 20 +++++++------ .../TestcontainerDescription.java | 7 +++-- .../testcontainers/TestcontainerRegistry.java | 29 ++++++++++++------- .../testcontainers/api/Testcontainer.java | 8 ++--- 4 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/jboss/arquillian/testcontainers/ContainerInjectionTestEnricher.java b/src/main/java/org/jboss/arquillian/testcontainers/ContainerInjectionTestEnricher.java index 6b8bb11..8d33b05 100644 --- a/src/main/java/org/jboss/arquillian/testcontainers/ContainerInjectionTestEnricher.java +++ b/src/main/java/org/jboss/arquillian/testcontainers/ContainerInjectionTestEnricher.java @@ -22,7 +22,7 @@ import org.jboss.arquillian.testcontainers.api.DockerRequired; import org.jboss.arquillian.testcontainers.api.Testcontainer; import org.testcontainers.DockerClientFactory; -import org.testcontainers.lifecycle.Startable; +import org.testcontainers.containers.GenericContainer; /** * @author James R. Perkins @@ -47,9 +47,9 @@ public void enrich(final Object testCase) { .collect(Collectors.toList()); final Testcontainer testcontainer = field.getAnnotation(Testcontainer.class); - // If the field is the default Startable, validate the field is a Startable - if (testcontainer.type() == Startable.class) { - if (!(Startable.class.isAssignableFrom(field.getType()))) { + // If the field is the default GenericContainer, validate the field is a GenericContainer + if (testcontainer.type() == GenericContainer.class) { + if (!(GenericContainer.class.isAssignableFrom(field.getType()))) { throw new IllegalArgumentException( String.format("Field %s is not assignable to %s", field, testcontainer.type().getName())); } @@ -62,7 +62,8 @@ public void enrich(final Object testCase) { } } - value = instances.get().lookupOrCreate((Class) field.getType(), field, testcontainer, qualifiers); + value = instances.get().lookupOrCreate((Class>) field.getType(), field, testcontainer, + qualifiers); } catch (Exception e) { throw new RuntimeException("Could not lookup value for field " + field, e); } @@ -95,9 +96,9 @@ public Object[] resolve(final Method method) { .filter(a -> !(a instanceof Testcontainer)) .collect(Collectors.toList()); - // If the field is the default Startable, validate the field is a Startable - if (testcontainer.type() == Startable.class) { - if (!(Startable.class.isAssignableFrom(parameter.getType()))) { + // If the field is the default GenericContainer, validate the field is a GenericContainer + if (testcontainer.type() == GenericContainer.class) { + if (!(GenericContainer.class.isAssignableFrom(parameter.getType()))) { throw new IllegalArgumentException( String.format("Parameter %s is not assignable to %s", parameter, testcontainer.type().getName())); @@ -110,7 +111,8 @@ public Object[] resolve(final Method method) { .getName())); } } - values[i] = instances.get().lookupOrCreate((Class) parameter.getType(), parameter, testcontainer, + values[i] = instances.get().lookupOrCreate((Class>) parameter.getType(), parameter, + testcontainer, qualifiers); } } diff --git a/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerDescription.java b/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerDescription.java index d5dbb40..bdb2eba 100644 --- a/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerDescription.java +++ b/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerDescription.java @@ -8,7 +8,7 @@ import java.lang.reflect.AnnotatedElement; import org.jboss.arquillian.testcontainers.api.Testcontainer; -import org.testcontainers.lifecycle.Startable; +import org.testcontainers.containers.GenericContainer; /** * @author James R. Perkins @@ -16,10 +16,11 @@ class TestcontainerDescription { final Testcontainer testcontainer; - final Startable instance; + final GenericContainer instance; final AnnotatedElement element; - TestcontainerDescription(final Testcontainer testcontainer, final AnnotatedElement element, final Startable instance) { + TestcontainerDescription(final Testcontainer testcontainer, final AnnotatedElement element, + final GenericContainer instance) { this.testcontainer = testcontainer; this.element = element; this.instance = instance; diff --git a/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerRegistry.java b/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerRegistry.java index 7204d4e..6f6eaa2 100644 --- a/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerRegistry.java +++ b/src/main/java/org/jboss/arquillian/testcontainers/TestcontainerRegistry.java @@ -14,7 +14,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import org.jboss.arquillian.testcontainers.api.Testcontainer; -import org.testcontainers.lifecycle.Startable; +import org.testcontainers.containers.GenericContainer; class TestcontainerRegistry implements Iterable { private final List containers; @@ -32,18 +32,13 @@ class TestcontainerRegistry implements Iterable { * * @return the generic type */ - Startable lookupOrCreate(final Class type, final AnnotatedElement element, final Testcontainer testcontainer, + GenericContainer lookupOrCreate(final Class> type, final AnnotatedElement element, + final Testcontainer testcontainer, final List qualifiers) { - Startable result = lookup(type, qualifiers); + GenericContainer result = lookup(type, qualifiers); if (result == null) { try { - Class constructType = (testcontainer.type() == Startable.class) ? type - : testcontainer.type(); - if (constructType.isInterface()) { - throw new IllegalArgumentException( - String.format("Type %s is an interface and cannot be created.", constructType)); - } - final Constructor constructor = constructType.getConstructor(); + final Constructor> constructor = getConstructor(type, testcontainer); result = constructor.newInstance(); this.containers.add(new TestcontainerDescription(testcontainer, element, result)); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { @@ -62,7 +57,7 @@ Startable lookupOrCreate(final Class type, final AnnotatedElement ele * * @return the generic type */ - Startable lookup(final Class type, final List qualifiers) { + GenericContainer lookup(final Class type, final List qualifiers) { final List foundContainers = new ArrayList<>(); if (qualifiers.isEmpty()) { for (TestcontainerDescription container : this.containers) { @@ -96,4 +91,16 @@ Startable lookup(final Class type, final List qualifiers) { public Iterator iterator() { return containers.iterator(); } + + private static Constructor> getConstructor(final Class> type, + final Testcontainer testcontainer) throws NoSuchMethodException { + @SuppressWarnings("unchecked") + Class> constructType = (testcontainer.type() == GenericContainer.class) ? type + : (Class>) testcontainer.type(); + if (constructType.isInterface()) { + throw new IllegalArgumentException( + String.format("Type %s is an interface and cannot be created.", constructType)); + } + return constructType.getConstructor(); + } } diff --git a/src/main/java/org/jboss/arquillian/testcontainers/api/Testcontainer.java b/src/main/java/org/jboss/arquillian/testcontainers/api/Testcontainer.java index 2b7fa6e..a17dfa1 100644 --- a/src/main/java/org/jboss/arquillian/testcontainers/api/Testcontainer.java +++ b/src/main/java/org/jboss/arquillian/testcontainers/api/Testcontainer.java @@ -13,11 +13,11 @@ import java.lang.reflect.Field; import java.lang.reflect.Parameter; -import org.testcontainers.lifecycle.Startable; +import org.testcontainers.containers.GenericContainer; /** * Used to annotate a field or parameter which must be an instance of a - * {@link Startable}. A {@link DockerRequired} annotation must be present on the + * {@link GenericContainer}. A {@link DockerRequired} annotation must be present on the * type to use Testcontainer injection. */ @Inherited @@ -37,11 +37,11 @@ /** * The type used to create the value for the field or parameter. The type must have a no-arg constructor. *

- * If left as the default value, {@link Startable}, the type to construct is derived from the + * If left as the default value, {@link GenericContainer}, the type to construct is derived from the * {@linkplain Field#getType() field} or {@linkplain Parameter#getType() parameter}. *

* * @return the type to construct */ - Class type() default Startable.class; + Class type() default GenericContainer.class; }