|
4 | 4 | import org.junit.Before;
|
5 | 5 | import org.junit.Rule;
|
6 | 6 | import org.junit.Test;
|
| 7 | +import org.junit.rules.TemporaryFolder; |
7 | 8 | import org.mockito.Mockito;
|
8 | 9 | import org.testcontainers.containers.GenericContainer;
|
9 | 10 |
|
| 11 | +import java.io.FileWriter; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.URL; |
| 14 | +import java.net.URLClassLoader; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | + |
10 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
11 | 20 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
12 | 21 | import static org.mockito.ArgumentMatchers.eq;
|
13 | 22 |
|
14 | 23 | public class ImageNameSubstitutorTest {
|
15 | 24 |
|
| 25 | + @Rule |
| 26 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 27 | + |
16 | 28 | @Rule
|
17 | 29 | public MockTestcontainersConfigurationRule config = new MockTestcontainersConfigurationRule();
|
18 | 30 |
|
@@ -81,4 +93,47 @@ public void testImageNameSubstitutorToString() {
|
81 | 93 | );
|
82 | 94 | }
|
83 | 95 | }
|
| 96 | + |
| 97 | + @Test |
| 98 | + public void testImageNameSubstitutorFromServiceLoader() throws IOException { |
| 99 | + Path tempDir = this.tempFolder.newFolder("image-name-substitutor-test").toPath(); |
| 100 | + Path metaInfDir = Paths.get(tempDir.toString(), "META-INF", "services"); |
| 101 | + Files.createDirectories(metaInfDir); |
| 102 | + |
| 103 | + createClassFile(tempDir, "org/testcontainers/utility/ImageNameSubstitutor.class", ImageNameSubstitutor.class); |
| 104 | + createClassFile(tempDir, "org/testcontainers/utility/FakeImageSubstitutor.class", FakeImageSubstitutor.class); |
| 105 | + |
| 106 | + // Create service provider configuration file |
| 107 | + createServiceProviderFile( |
| 108 | + metaInfDir, |
| 109 | + "org.testcontainers.utility.ImageNameSubstitutor", |
| 110 | + "org.testcontainers.utility.FakeImageSubstitutor" |
| 111 | + ); |
| 112 | + |
| 113 | + URL[] urls = { tempDir.toUri().toURL() }; |
| 114 | + URLClassLoader classLoader = new URLClassLoader(urls, ImageNameSubstitutorTest.class.getClassLoader()); |
| 115 | + |
| 116 | + final ImageNameSubstitutor imageNameSubstitutor = ImageNameSubstitutor.instance(classLoader); |
| 117 | + |
| 118 | + DockerImageName result = imageNameSubstitutor.apply(DockerImageName.parse("original")); |
| 119 | + assertThat(result.asCanonicalNameString()) |
| 120 | + .as("the image has been substituted by default then configured implementations") |
| 121 | + .isEqualTo("transformed-substituted-image:latest"); |
| 122 | + } |
| 123 | + |
| 124 | + private void createClassFile(Path tempDir, String classFilePath, Class<?> clazz) throws IOException { |
| 125 | + Path classFile = Paths.get(tempDir.toString(), classFilePath); |
| 126 | + Files.createDirectories(classFile.getParent()); |
| 127 | + Files.copy(clazz.getResourceAsStream("/" + classFilePath), classFile); |
| 128 | + } |
| 129 | + |
| 130 | + private void createServiceProviderFile(Path metaInfDir, String serviceInterface, String... implementations) |
| 131 | + throws IOException { |
| 132 | + Path serviceFile = Paths.get(metaInfDir.toString(), serviceInterface); |
| 133 | + try (FileWriter writer = new FileWriter(serviceFile.toFile())) { |
| 134 | + for (String impl : implementations) { |
| 135 | + writer.write(impl + "\n"); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
84 | 139 | }
|
0 commit comments