Skip to content

Commit ffaedfb

Browse files
authored
Merge pull request #1 from topicusonderwijs/issue-117-part1-a
Additional tests for named container lookup
2 parents a515cc6 + 32cba95 commit ffaedfb

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

src/main/java/org/arquillian/testcontainers/TestcontainerRegistry.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,35 @@ GenericContainer<?> lookupOrCreate(final Class<GenericContainer<?>> type, final
6969
*/
7070
GenericContainer<?> lookup(final String name) {
7171
for (TestcontainerDescription containerDesc : this.containers) {
72-
if (name.equals(containerDesc.name)) {
72+
if (containerDesc.name.equals(name)) {
7373
return containerDesc.instance;
7474
}
7575
}
7676
return null;
7777
}
7878

79+
/**
80+
* Typed lookup by name. Throws {@link IllegalArgumentException} if a container with the given name is registered
81+
* but its runtime type is not assignable to {@code type}.
82+
*
83+
* @param name the container name
84+
* @param type the expected container type
85+
*
86+
* @return the container cast to {@code type}, or {@code null} if no container with the given name is registered
87+
*/
88+
<T extends GenericContainer<?>> T lookup(final String name, final Class<T> type) {
89+
final GenericContainer<?> container = lookup(name);
90+
if (container == null) {
91+
return null;
92+
}
93+
if (!type.isInstance(container)) {
94+
throw new IllegalArgumentException(
95+
String.format("Container with name '%s' is of type %s, which is not assignable to %s",
96+
name, container.getClass().getName(), type.getName()));
97+
}
98+
return type.cast(container);
99+
}
100+
79101
/**
80102
* Lookup the container in the test container instances. If more than one container is found for the type or
81103
* qualifier, an {@link IllegalArgumentException} is thrown.

src/test/java/org/arquillian/testcontainers/NamedContainerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public static JavaArchive createDeployment() {
3333
@Testcontainer(name = "first")
3434
private SimpleTestContainer first;
3535

36+
@Testcontainer(name = "first")
37+
private SimpleTestContainer firstAgain;
38+
3639
@Testcontainer(name = "second")
3740
private SimpleTestContainer second;
3841

@@ -59,4 +62,9 @@ public void checkAllDifferentInstances() {
5962
Assertions.assertNotSame(first, unnamed, "Expected named and unnamed containers to be different instances.");
6063
Assertions.assertNotSame(second, unnamed, "Expected named and unnamed containers to be different instances.");
6164
}
65+
66+
@Test
67+
public void sameNameYieldsSameInstance() {
68+
Assertions.assertSame(first, firstAgain, "Fields sharing a name must resolve to the same instance.");
69+
}
6270
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright The Arquillian Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.arquillian.testcontainers;
7+
8+
import java.lang.reflect.Field;
9+
import java.util.List;
10+
11+
import org.arquillian.testcontainers.api.Testcontainer;
12+
import org.arquillian.testcontainers.common.SimpleTestContainer;
13+
import org.junit.jupiter.api.Assertions;
14+
import org.junit.jupiter.api.Test;
15+
import org.testcontainers.containers.GenericContainer;
16+
17+
/**
18+
* Plain-JUnit unit tests for {@link TestcontainerRegistry} that exercise lookup semantics without
19+
* starting any Docker containers. Annotation instances are obtained by reading the annotations off
20+
* fixture fields on this class so the registry can be driven through {@code lookupOrCreate}
21+
* outside an Arquillian context.
22+
*/
23+
public class TestcontainerRegistryTest {
24+
25+
@Testcontainer(name = "alpha")
26+
private SimpleTestContainer alphaFixture;
27+
28+
@Test
29+
public void sameNameViaLookupOrCreateReturnsSameInstance() throws Exception {
30+
TestcontainerRegistry registry = new TestcontainerRegistry();
31+
GenericContainer<?> first = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
32+
GenericContainer<?> second = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
33+
34+
Assertions.assertSame(first, second);
35+
}
36+
37+
@Test
38+
public void lookupReturnsNullForUnknownName() throws Exception {
39+
TestcontainerRegistry registry = new TestcontainerRegistry();
40+
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
41+
42+
Assertions.assertNull(registry.lookup("missing"));
43+
}
44+
45+
@Test
46+
public void emptyNameLookupDoesNotMatchNamedContainers() throws Exception {
47+
TestcontainerRegistry registry = new TestcontainerRegistry();
48+
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
49+
50+
Assertions.assertNull(registry.lookup(""),
51+
"Empty-string lookup should not return any registered container");
52+
}
53+
54+
@Test
55+
public void lookupTreatsNullNameAsAbsent() throws Exception {
56+
TestcontainerRegistry registry = new TestcontainerRegistry();
57+
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
58+
59+
Assertions.assertNull(registry.lookup((String) null));
60+
}
61+
62+
@Test
63+
public void typedLookupReturnsTypedContainer() throws Exception {
64+
TestcontainerRegistry registry = new TestcontainerRegistry();
65+
GenericContainer<?> alpha = registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
66+
67+
SimpleTestContainer typed = registry.lookup("alpha", SimpleTestContainer.class);
68+
Assertions.assertSame(alpha, typed);
69+
Assertions.assertNull(registry.lookup("missing", SimpleTestContainer.class));
70+
}
71+
72+
@Test
73+
public void typedLookupThrowsOnTypeMismatch() throws Exception {
74+
TestcontainerRegistry registry = new TestcontainerRegistry();
75+
registry.lookupOrCreate(simpleType(), annotation("alphaFixture"), List.of());
76+
77+
Assertions.assertThrows(IllegalArgumentException.class,
78+
() -> registry.lookup("alpha", IncompatibleContainer.class));
79+
}
80+
81+
/** Distinct {@link GenericContainer} subtype used to exercise the type-mismatch path on typed lookup. */
82+
private static class IncompatibleContainer extends GenericContainer<IncompatibleContainer> {
83+
}
84+
85+
@SuppressWarnings("unchecked")
86+
private static Class<GenericContainer<?>> simpleType() {
87+
return (Class<GenericContainer<?>>) (Class<?>) SimpleTestContainer.class;
88+
}
89+
90+
private Testcontainer annotation(final String fieldName) throws NoSuchFieldException {
91+
final Field field = TestcontainerRegistryTest.class.getDeclaredField(fieldName);
92+
return field.getAnnotation(Testcontainer.class);
93+
}
94+
}

0 commit comments

Comments
 (0)