|
| 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