Skip to content
Merged
Show file tree
Hide file tree
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 @@ -69,13 +69,35 @@ GenericContainer<?> lookupOrCreate(final Class<GenericContainer<?>> type, final
*/
GenericContainer<?> lookup(final String name) {
for (TestcontainerDescription containerDesc : this.containers) {
if (name.equals(containerDesc.name)) {
if (containerDesc.name.equals(name)) {
return containerDesc.instance;
}
}
return null;
}

/**
* Typed lookup by name. Throws {@link IllegalArgumentException} if a container with the given name is registered
* but its runtime type is not assignable to {@code type}.
*
* @param name the container name
* @param type the expected container type
*
* @return the container cast to {@code type}, or {@code null} if no container with the given name is registered
*/
<T extends GenericContainer<?>> T lookup(final String name, final Class<T> type) {
final GenericContainer<?> container = lookup(name);
if (container == null) {
return null;
}
if (!type.isInstance(container)) {
throw new IllegalArgumentException(
String.format("Container with name '%s' is of type %s, which is not assignable to %s",
name, container.getClass().getName(), type.getName()));
}
return type.cast(container);
}

/**
* Lookup the container in the test container instances. If more than one container is found for the type or
* qualifier, an {@link IllegalArgumentException} is thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static JavaArchive createDeployment() {
@Testcontainer(name = "first")
private SimpleTestContainer first;

@Testcontainer(name = "first")
private SimpleTestContainer firstAgain;

@Testcontainer(name = "second")
private SimpleTestContainer second;

Expand All @@ -59,4 +62,9 @@ public void checkAllDifferentInstances() {
Assertions.assertNotSame(first, unnamed, "Expected named and unnamed containers to be different instances.");
Assertions.assertNotSame(second, unnamed, "Expected named and unnamed containers to be different instances.");
}

@Test
public void sameNameYieldsSameInstance() {
Assertions.assertSame(first, firstAgain, "Fields sharing a name must resolve to the same instance.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.arquillian.testcontainers;

import java.lang.reflect.Field;
import java.util.List;

import org.arquillian.testcontainers.api.Testcontainer;
import org.arquillian.testcontainers.common.SimpleTestContainer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;

/**
* Plain-JUnit unit tests for {@link TestcontainerRegistry} that exercise lookup semantics without
* starting any Docker containers. Annotation instances are obtained by reading the annotations off
* fixture fields on this class so the registry can be driven through {@code lookupOrCreate}
* outside an Arquillian context.
*/
public class TestcontainerRegistryTest {

@Testcontainer(name = "alpha")
private SimpleTestContainer alphaFixture;

@Test
public void sameNameViaLookupOrCreateReturnsSameInstance() throws Exception {
TestcontainerRegistry registry = new TestcontainerRegistry();
GenericContainer<?> first = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
GenericContainer<?> second = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());

Assertions.assertSame(first, second);
}

@Test
public void lookupReturnsNullForUnknownName() throws Exception {
TestcontainerRegistry registry = new TestcontainerRegistry();
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());

Assertions.assertNull(registry.lookup("missing"));
}

@Test
public void emptyNameLookupDoesNotMatchNamedContainers() throws Exception {
TestcontainerRegistry registry = new TestcontainerRegistry();
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());

Assertions.assertNull(registry.lookup(""),
"Empty-string lookup should not return any registered container");
}

@Test
public void lookupTreatsNullNameAsAbsent() throws Exception {
TestcontainerRegistry registry = new TestcontainerRegistry();
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());

Assertions.assertNull(registry.lookup((String) null));
}

@Test
public void typedLookupReturnsTypedContainer() throws Exception {
TestcontainerRegistry registry = new TestcontainerRegistry();
GenericContainer<?> alpha = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());

SimpleTestContainer typed = registry.lookup("alpha", SimpleTestContainer.class);
Assertions.assertSame(alpha, typed);
Assertions.assertNull(registry.lookup("missing", SimpleTestContainer.class));
}

@Test
public void typedLookupThrowsOnTypeMismatch() throws Exception {
TestcontainerRegistry registry = new TestcontainerRegistry();
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());

Assertions.assertThrows(IllegalArgumentException.class,
() -> registry.lookup("alpha", IncompatibleContainer.class));
}

/** Distinct {@link GenericContainer} subtype used to exercise the type-mismatch path on typed lookup. */
private static class IncompatibleContainer extends GenericContainer<IncompatibleContainer> {
}

@SuppressWarnings("unchecked")
private static Class<GenericContainer<?>> simpleType() {
return (Class<GenericContainer<?>>) (Class<?>) SimpleTestContainer.class;
}

private Testcontainer annotation(final String fieldName) throws NoSuchFieldException {
final Field field = TestcontainerRegistryTest.class.getDeclaredField(fieldName);
return field.getAnnotation(Testcontainer.class);
}
}