Best Testcontainers-java code snippet using org.testcontainers.utility.ImageNameSubstitutor.ChainedImageNameSubstitutor
Source:ImageNameSubstitutor.java
...29 } catch (Exception e) {30 throw new IllegalArgumentException("Configured Image Substitutor could not be loaded: " + configuredClassName, e);31 }32 log.info("Found configured ImageNameSubstitutor: {}", configuredInstance.getDescription());33 instance = new ChainedImageNameSubstitutor(34 wrapWithLogging(defaultImplementation),35 wrapWithLogging(configuredInstance)36 );37 } else {38 instance = wrapWithLogging(defaultImplementation);39 }40 log.info("Image name substitution will be performed by: {}", instance.getDescription());41 }42 return instance;43 }44 private static ImageNameSubstitutor wrapWithLogging(final ImageNameSubstitutor wrappedInstance) {45 return new LogWrappedImageNameSubstitutor(wrappedInstance);46 }47 /**48 * Substitute a {@link DockerImageName} for another, for example to replace a generic Docker Hub image name with a49 * private registry copy of the image.50 *51 * @param original original name to be replaced52 * @return a replacement name, or the original, as appropriate53 */54 public abstract DockerImageName apply(DockerImageName original);55 /**56 * @return a human-readable description of the substitutor57 */58 protected abstract String getDescription();59 /**60 * Wrapper substitutor which logs which substitutions have been performed.61 */62 static class LogWrappedImageNameSubstitutor extends ImageNameSubstitutor {63 @VisibleForTesting64 final ImageNameSubstitutor wrappedInstance;65 public LogWrappedImageNameSubstitutor(final ImageNameSubstitutor wrappedInstance) {66 this.wrappedInstance = wrappedInstance;67 }68 @Override69 public DockerImageName apply(final DockerImageName original) {70 final DockerImageName replacementImage = wrappedInstance.apply(original);71 if (!replacementImage.equals(original)) {72 log.info("Using {} as a substitute image for {} (using image substitutor: {})", replacementImage.asCanonicalNameString(), original.asCanonicalNameString(), wrappedInstance.getDescription());73 return replacementImage;74 } else {75 log.debug("Did not find a substitute image for {} (using image substitutor: {})", original.asCanonicalNameString(), wrappedInstance.getDescription());76 return original;77 }78 }79 @Override80 protected String getDescription() {81 return wrappedInstance.getDescription();82 }83 }84 /**85 * Wrapper substitutor that passes the original image name through a default substitutor and then the configured one86 */87 static class ChainedImageNameSubstitutor extends ImageNameSubstitutor {88 private final ImageNameSubstitutor defaultInstance;89 private final ImageNameSubstitutor configuredInstance;90 public ChainedImageNameSubstitutor(ImageNameSubstitutor defaultInstance, ImageNameSubstitutor configuredInstance) {91 this.defaultInstance = defaultInstance;92 this.configuredInstance = configuredInstance;93 }94 @Override95 public DockerImageName apply(DockerImageName original) {96 return defaultInstance.andThen(configuredInstance).apply(original);97 }98 @Override99 protected String getDescription() {100 return String.format(101 "Chained substitutor of '%s' and then '%s'",102 defaultInstance.getDescription(),103 configuredInstance.getDescription()104 );...
ChainedImageNameSubstitutor
Using AI Code Generation
1import java.util.Arrays;2import java.util.List;3import org.testcontainers.utility.Base58;4import org.testcontainers.utility.DockerImageName;5import org.testcontainers.utility.ImageNameSubstitutor;6public class ChainedImageNameSubstitutor implements ImageNameSubstitutor {7 private final List<ImageNameSubstitutor> imageNameSubstitutors;8 public ChainedImageNameSubstitutor(ImageNameSubstitutor... imageNameSubstitutors) {9 this.imageNameSubstitutors = Arrays.asList(imageNameSubstitutors);10 }11 public DockerImageName apply(DockerImageName original) {12 DockerImageName result = original;13 for (ImageNameSubstitutor imageNameSubstitutor : imageNameSubstitutors) {14 result = imageNameSubstitutor.apply(result);15 }16 return result;17 }18}19import java.util.Optional;20import org.testcontainers.utility.DockerImageName;21import org.testcontainers.utility.ImageNameSubstitutor;22public class CustomImageNameSubstitutor implements ImageNameSubstitutor {23 public DockerImageName apply(DockerImageName original) {24 return Optional.ofNullable(System.getenv("TESTCONTAINERS_IMAGE_NAME_SUBSTITUTE"))25 .map(substitute -> DockerImageName.parse(substitute))26 .orElse(original);27 }28}29import org.junit.jupiter.api.Test;30import org.testcontainers.containers.GenericContainer;31import org.testcontainers.utility.DockerImageName;32import org.testcontainers.utility.ImageNameSubstitutor;33public class CustomImageNameSubstitutorTest {34 public void testCustomImageNameSubstitutor() {35 ImageNameSubstitutor imageNameSubstitutor = new ChainedImageNameSubstitutor(new CustomImageNameSubstitutor());36 GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.13.4"))37 .withImageNameSubstitutor(imageNameSubstitutor);38 container.start();39 }40}
ChainedImageNameSubstitutor
Using AI Code Generation
1import org.testcontainers.utility.ImageNameSubstitutor;2public class ChainedImageNameSubstitutor {3 public static void main(String[] args) {4 .chained(5 ImageNameSubstitutor.withPrefix("my-custom-registry"),6 ImageNameSubstitutor.withPrefix("my-custom-registry-2"),7 ImageNameSubstitutor.withPrefix("my-custom-registry-3")8 );9 System.out.println(chainedImageNameSubstitutor.apply("alpine:3.9"));10 }11}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!