-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation updates and updates from review.
Signed-off-by: James R. Perkins <[email protected]>
- Loading branch information
Showing
4 changed files
with
55 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InaccessibleObjectException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
@@ -18,13 +19,14 @@ | |
import org.jboss.arquillian.test.spi.TestEnricher; | ||
import org.jboss.arquillian.testcontainers.api.DockerRequired; | ||
import org.jboss.arquillian.testcontainers.api.Testcontainer; | ||
import org.testcontainers.DockerClientFactory; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
/** | ||
* A test enricher for injecting a {@link GenericContainer} into fields annotated with {@link Testcontainer @Testcontainer}. | ||
* | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
@SuppressWarnings({ "unchecked", "resource" }) | ||
@SuppressWarnings({ "unchecked" }) | ||
public class ContainerInjectionTestEnricher implements TestEnricher { | ||
@Inject | ||
private Instance<TestcontainerRegistry> instances; | ||
|
@@ -34,9 +36,7 @@ public void enrich(final Object testCase) { | |
if (!isAnnotatedWith(testCase.getClass(), DockerRequired.class)) { | ||
return; | ||
} | ||
final boolean isDockerAvailable = isDockerAvailable(); | ||
for (Field field : getFieldsWithAnnotation(testCase.getClass())) { | ||
checkForDocker(isDockerAvailable); | ||
Object value; | ||
try { | ||
final List<Annotation> qualifiers = Stream.of(field.getAnnotations()) | ||
|
@@ -61,19 +61,14 @@ public void enrich(final Object testCase) { | |
} | ||
|
||
value = instances.get() | ||
.lookupOrCreate((Class<GenericContainer<?>>) field.getType(), field, testcontainer, | ||
qualifiers); | ||
.lookupOrCreate((Class<GenericContainer<?>>) field.getType(), testcontainer, qualifiers); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not lookup value for field " + field, e); | ||
} | ||
try { | ||
if (field.trySetAccessible()) { | ||
field.set(testCase, value); | ||
} else { | ||
throw new RuntimeException("Could not set value for field " + field); | ||
} | ||
} catch (RuntimeException e) { | ||
throw e; | ||
// Field marked as accessible during lookup to fail early if it cannot be made accessible. See the | ||
// getFieldsWithAnnotation() method. | ||
field.set(testCase, value); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not set value on field " + field + " using " + value, e); | ||
} | ||
|
@@ -85,31 +80,14 @@ public Object[] resolve(final Method method) { | |
return new Object[method.getParameterTypes().length]; | ||
} | ||
|
||
private static void checkForDocker(boolean isDockerAvailable) { | ||
final String detailMessage = "No Docker environment is available."; | ||
if (!isDockerAvailable) { | ||
throw new AssertionError(detailMessage); | ||
} | ||
} | ||
|
||
@SuppressWarnings({ "resource", "BooleanMethodIsAlwaysInverted" }) | ||
private static boolean isDockerAvailable() { | ||
try { | ||
DockerClientFactory.instance().client(); | ||
return true; | ||
} catch (Throwable ex) { | ||
return false; | ||
} | ||
} | ||
|
||
private static List<Field> getFieldsWithAnnotation(final Class<?> source) { | ||
final List<Field> foundFields = new ArrayList<>(); | ||
Class<?> nextSource = source; | ||
while (nextSource != Object.class) { | ||
for (Field field : nextSource.getDeclaredFields()) { | ||
if (field.isAnnotationPresent(Testcontainer.class)) { | ||
if (!field.trySetAccessible()) { | ||
throw new IllegalStateException(String.format("Could not make field %s accessible", field)); | ||
throw new InaccessibleObjectException(String.format("Could not make field %s accessible", field)); | ||
} | ||
foundFields.add(field); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,27 @@ | |
|
||
package org.jboss.arquillian.testcontainers; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
|
||
import org.jboss.arquillian.testcontainers.api.Testcontainer; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
/** | ||
* A holder for information about the Testcontainer being injected into a field. | ||
* | ||
* @author <a href="mailto:[email protected]">James R. Perkins</a> | ||
*/ | ||
class TestcontainerDescription { | ||
|
||
/** | ||
* The annotation that was on the field | ||
*/ | ||
final Testcontainer testcontainer; | ||
/** | ||
* The instance of the container created | ||
*/ | ||
final GenericContainer<?> instance; | ||
final AnnotatedElement element; | ||
|
||
TestcontainerDescription(final Testcontainer testcontainer, final AnnotatedElement element, | ||
final GenericContainer<?> instance) { | ||
TestcontainerDescription(final Testcontainer testcontainer, final GenericContainer<?> instance) { | ||
this.testcontainer = testcontainer; | ||
this.element = element; | ||
this.instance = instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters