Best Testcontainers-java code snippet using org.testcontainers.utility.DockerImageName.parse
Source:DockerImageName.java
...31 * @param fullImageName in standard Docker format, e.g. <code>name:tag</code>,32 * <code>some.registry/path/name:tag</code>,33 * <code>some.registry/path/name@sha256:abcdef...</code>, etc.34 */35 public static DockerImageName parse(String fullImageName) {36 return new DockerImageName(fullImageName);37 }38 /**39 * Parses a docker image name from a provided string.40 *41 * @param fullImageName in standard Docker format, e.g. <code>name:tag</code>,42 * <code>some.registry/path/name:tag</code>,43 * <code>some.registry/path/name@sha256:abcdef...</code>, etc.44 * @deprecated use {@link DockerImageName#parse(String)} instead45 */46 @Deprecated47 public DockerImageName(String fullImageName) {48 this.rawName = fullImageName;49 final int slashIndex = fullImageName.indexOf('/');50 String remoteName;51 if (slashIndex == -1 ||52 (!fullImageName.substring(0, slashIndex).contains(".") &&53 !fullImageName.substring(0, slashIndex).contains(":") &&54 !fullImageName.substring(0, slashIndex).equals("localhost"))) {55 registry = "";56 remoteName = fullImageName;57 } else {58 registry = fullImageName.substring(0, slashIndex);59 remoteName = fullImageName.substring(slashIndex + 1);60 }61 if (remoteName.contains("@sha256:")) {62 repository = remoteName.split("@sha256:")[0];63 versioning = new Sha256Versioning(remoteName.split("@sha256:")[1]);64 } else if (remoteName.contains(":")) {65 repository = remoteName.split(":")[0];66 versioning = new TagVersioning(remoteName.split(":")[1]);67 } else {68 repository = remoteName;69 versioning = Versioning.ANY;70 }71 compatibleSubstituteFor = null;72 }73 /**74 * Parses a docker image name from a provided string, and uses a separate provided version.75 *76 * @param nameWithoutTag in standard Docker format, e.g. <code>name</code>,77 * <code>some.registry/path/name</code>,78 * <code>some.registry/path/name</code>, etc.79 * @param version a docker image version identifier, either as a tag or sha256 checksum, e.g.80 * <code>tag</code>,81 * <code>sha256:abcdef...</code>.82 * @deprecated use {@link DockerImageName#parse(String)}.{@link DockerImageName#withTag(String)} instead83 */84 @Deprecated85 public DockerImageName(String nameWithoutTag, @NotNull String version) {86 this.rawName = nameWithoutTag;87 final int slashIndex = nameWithoutTag.indexOf('/');88 String remoteName;89 if (slashIndex == -1 ||90 (!nameWithoutTag.substring(0, slashIndex).contains(".") &&91 !nameWithoutTag.substring(0, slashIndex).contains(":") &&92 !nameWithoutTag.substring(0, slashIndex).equals("localhost"))) {93 registry = "";94 remoteName = nameWithoutTag;95 } else {96 registry = nameWithoutTag.substring(0, slashIndex);97 remoteName = nameWithoutTag.substring(slashIndex + 1);98 }99 if (version.startsWith("sha256:")) {100 repository = remoteName;101 versioning = new Sha256Versioning(version.replace("sha256:", ""));102 } else {103 repository = remoteName;104 versioning = new TagVersioning(version);105 }106 compatibleSubstituteFor = null;107 }108 /**109 * @return the unversioned (non 'tag') part of this name110 */111 public String getUnversionedPart() {112 if (!"".equals(registry)) {113 return registry + "/" + repository;114 } else {115 return repository;116 }117 }118 /**119 * @return the versioned part of this name (tag or sha256)120 */121 public String getVersionPart() {122 return versioning.toString();123 }124 /**125 * @return canonical name for the image126 */127 public String asCanonicalNameString() {128 return getUnversionedPart() + versioning.getSeparator() + getVersionPart();129 }130 @Override131 public String toString() {132 return asCanonicalNameString();133 }134 /**135 * Is the image name valid?136 *137 * @throws IllegalArgumentException if not valid138 */139 public void assertValid() {140 //noinspection UnstableApiUsage141 HostAndPort.fromString(registry); // return value ignored - this throws if registry is not a valid host:port string142 if (!REPO_NAME.matcher(repository).matches()) {143 throw new IllegalArgumentException(repository + " is not a valid Docker image name (in " + rawName + ")");144 }145 if (!versioning.isValid()) {146 throw new IllegalArgumentException(versioning + " is not a valid image versioning identifier (in " + rawName + ")");147 }148 }149 /**150 * @param newTag version tag for the copy to use151 * @return an immutable copy of this {@link DockerImageName} with the new version tag152 */153 public DockerImageName withTag(final String newTag) {154 return withVersioning(new TagVersioning(newTag));155 }156 /**157 * Declare that this {@link DockerImageName} is a compatible substitute for another image - i.e. that this image158 * behaves as the other does, and is compatible with Testcontainers' assumptions about the other image.159 *160 * @param otherImageName the image name of the other image161 * @return an immutable copy of this {@link DockerImageName} with the compatibility declaration attached.162 */163 public DockerImageName asCompatibleSubstituteFor(String otherImageName) {164 return withCompatibleSubstituteFor(DockerImageName.parse(otherImageName));165 }166 /**167 * Declare that this {@link DockerImageName} is a compatible substitute for another image - i.e. that this image168 * behaves as the other does, and is compatible with Testcontainers' assumptions about the other image.169 *170 * @param otherImageName the image name of the other image171 * @return an immutable copy of this {@link DockerImageName} with the compatibility declaration attached.172 */173 public DockerImageName asCompatibleSubstituteFor(DockerImageName otherImageName) {174 return withCompatibleSubstituteFor(otherImageName);175 }176 /**177 * Test whether this {@link DockerImageName} has declared compatibility with another image (set using178 * {@link DockerImageName#asCompatibleSubstituteFor(String)} or179 * {@link DockerImageName#asCompatibleSubstituteFor(DockerImageName)}.180 * <p>181 * If a version tag part is present in the <code>other</code> image name, the tags must exactly match, unless it182 * is 'latest'. If a version part is not present in the <code>other</code> image name, the tag contents are ignored.183 *184 * @param other the other image that we are trying to test compatibility with185 * @return whether this image has declared compatibility.186 */187 public boolean isCompatibleWith(DockerImageName other) {188 // is this image already the same or equivalent?189 if (other.equals(this)) {190 return true;191 }192 if (this.compatibleSubstituteFor == null) {193 return false;194 }195 return this.compatibleSubstituteFor.isCompatibleWith(other);196 }197 /**198 * Behaves as {@link DockerImageName#isCompatibleWith(DockerImageName)} but throws an exception199 * rather than returning false if a mismatch is detected.200 *201 * @param anyOthers the other image(s) that we are trying to check compatibility with. If more202 * than one is provided, this method will check compatibility with at least one203 * of them.204 * @throws IllegalStateException if {@link DockerImageName#isCompatibleWith(DockerImageName)}205 * returns false206 */207 public void assertCompatibleWith(DockerImageName... anyOthers) {208 if (anyOthers.length == 0) {209 throw new IllegalArgumentException("anyOthers parameter must be non-empty");210 }211 for (DockerImageName anyOther : anyOthers) {212 if (this.isCompatibleWith(anyOther)) {213 return;214 }215 }216 final DockerImageName exampleOther = anyOthers[0];217 throw new IllegalStateException(218 String.format(219 "Failed to verify that image '%s' is a compatible substitute for '%s'. This generally means that "220 +221 "you are trying to use an image that Testcontainers has not been designed to use. If this is "222 +223 "deliberate, and if you are confident that the image is compatible, you should declare "224 +225 "compatibility in code using the `asCompatibleSubstituteFor` method. For example:\n"226 +227 " DockerImageName myImage = DockerImageName.parse(\"%s\").asCompatibleSubstituteFor(\"%s\");\n"228 +229 "and then use `myImage` instead.",230 this.rawName, exampleOther.rawName, this.rawName, exampleOther.rawName231 )232 );233 }234}...
Source:RemoteDockerImage.java
...32 this.imageNameFuture = CompletableFuture.completedFuture(dockerImageName);33 }34 @Deprecated35 public RemoteDockerImage(String dockerImageName) {36 this(DockerImageName.parse(dockerImageName));37 }38 @Deprecated39 public RemoteDockerImage(@NonNull String repository, @NonNull String tag) {40 this(DockerImageName.parse(repository).withTag(tag));41 }42 public RemoteDockerImage(@NonNull Future<String> imageFuture) {43 this.imageNameFuture = Futures.lazyTransform(imageFuture, DockerImageName::new);44 }45 @Override46 @SneakyThrows({InterruptedException.class, ExecutionException.class})47 protected final String resolve() {48 final DockerImageName imageName = getImageName();49 Logger logger = DockerLoggerFactory.getLogger(imageName.toString());50 try {51 if (!imagePullPolicy.shouldPull(imageName)) {52 return imageName.asCanonicalNameString();53 }54 // The image is not available locally - pull it...
Source:OpensearchContainer.java
...26 private static final int DEFAULT_TCP_PORT = 9600;27 private static final String DEFAULT_VERSION = "1.1.0";28 private static final long ONE_GIGABYTES = 1024 * 1024 * 1024;29 private static final DockerImageName DEFAULT_IMAGE_NAME =30 DockerImageName.parse("opensearchproject/opensearch").withTag(DEFAULT_VERSION);31 public OpensearchContainer() {32 this(DEFAULT_IMAGE_NAME);33 }34 public OpensearchContainer(final DockerImageName dockerImageName) {35 super(dockerImageName);36 logger().info("Starting an Opensearch container using [{}]", dockerImageName);37 withNetworkAliases(String.format("opensearch-%s", Base58.randomString(6)));38 withEnv("discovery.type", "single-node");39 withLogConsumer(this::opensearchLog);40 withSharedMemorySize(ONE_GIGABYTES);41 addExposedPorts(DEFAULT_HTTP_PORT, DEFAULT_TCP_PORT);42 setWaitStrategy(43 new LogMessageWaitStrategy()44 .withRegEx(".*(Node '\\w+' initialized).*")...
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class 1 {3 public static void main(String[] args) {4 DockerImageName dockerImageName = DockerImageName.parse("testcontainers/ryuk:0.2.3");5 System.out.println("Repository: " + dockerImageName.getUnversionedPart());6 System.out.println("Tag: " + dockerImageName.getTag());7 }8}
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class 1 {3 public static void main(String[] args) {4 DockerImageName dockerImageName = DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.13.2");5 System.out.println("Registry: " + dockerImageName.getRegistry());6 System.out.println("Repository: " + dockerImageName.getRepository());7 System.out.println("Tag: " + dockerImageName.getTag());8 System.out.println("Version: " + dockerImageName.getVersionPart());9 System.out.println("User: " + dockerImageName.getUser());10 }11}12import org.testcontainers.utility.DockerImageName;13public class 2 {14 public static void main(String[] args) {15 DockerImageName dockerImageName = DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.13.2");16 System.out.println("Registry: " + dockerImageName.getRegistry());17 System.out.println("Repository: " + dockerImageName.getRepository());18 System.out.println("Tag: " + dockerImageName.getTag());19 System.out.println("Version: " + dockerImageName.getVersionPart());20 System.out.println("User: " + dockerImageName.getUser());21 }22}23import org.testcontainers.utility.DockerImageName;24public class 3 {25 public static void main(String[] args) {26 DockerImageName dockerImageName = DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.13.2");27 System.out.println("Registry: " + dockerImageName.getRegistry());28 System.out.println("Repository: " + dockerImageName.getRepository());29 System.out.println("Tag: " + dockerImageName.getTag());30 System.out.println("Version: " + dockerImageName.getVersionPart());31 System.out.println("User
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class 1 {3 public static void main(String[] args) {4 DockerImageName dockerImageName = DockerImageName.parse("redis:latest");5 System.out.println(dockerImageName.getUnversionedPart());6 System.out.println(dockerImageName.getVersionPart());7 }8}9import org.testcontainers.utility.DockerImageName;10public class 2 {11 public static void main(String[] args) {12 DockerImageName dockerImageName = DockerImageName.parse("redis:latest");13 System.out.println(dockerImageName.getUnversionedPart());14 System.out.println(dockerImageName.getVersionPart());
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class 1 {3 public static void main(String[] args) {4 String image = "testcontainers/ryuk:0.2.3";5 DockerImageName dockerImageName = DockerImageName.parse(image);6 System.out.println(dockerImageName);7 }8}9This article has been derived from "DockerImageName.parse(String) method" written by amit on 30-06-2020
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class 1 {3 public static void main(String[] args) {4 DockerImageName dockerImageName = DockerImageName.parse("docker.io/library/redis:latest");5 System.out.println(dockerImageName);6 }7}8import org.testcontainers.utility.DockerImageName;9public class 2 {10 public static void main(String[] args) {11 DockerImageName dockerImageName = DockerImageName.parse("docker.io/library/redis:latest");12 System.out.println(dockerImageName);13 }14}15import org.testcontainers.utility.DockerImageName;16public class 3 {17 public static void main(String[] args) {18 DockerImageName dockerImageName = DockerImageName.parse("docker.io/library/redis:latest");19 System.out.println(dockerImageName);20 }21}22import org.testcontainers.utility.DockerImageName;23public class 4 {24 public static void main(String[] args) {25 DockerImageName dockerImageName = DockerImageName.parse("docker.io/library/redis:latest");26 System.out.println(dockerImageName);27 }28}29import org.testcontainers.utility.DockerImageName;30public class 5 {31 public static void main(String[] args) {32 DockerImageName dockerImageName = DockerImageName.parse("docker.io/library/redis:latest");33 System.out.println(dockerImageName);34 }35}36import org.testcontainers.utility.DockerImageName;37public class 6 {38 public static void main(String[] args) {39 DockerImageName dockerImageName = DockerImageName.parse("
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class Test {3 public static void main(String[] args) {4 DockerImageName name = DockerImageName.parse("mysql:8.0.11");5 System.out.println(name.getUnversionedPart());6 System.out.println(name.getVersionPart());7 }8}
parse
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class Test {3 public static void main(String[] args) {4 DockerImageName image = DockerImageName.parse("myregistry.com:5000/path/to/image:tag");5 System.out.println("image = " + image);6 }7}8import org.testcontainers.utility.DockerImageName;9public class Test {10 public static void main(String[] args) {11 DockerImageName image = DockerImageName.parse("myregistry.com:5000/path/to/image");12 System.out.println("image = " + image);13 }14}15import org.testcontainers.utility.DockerImageName;16public class Test {17 public static void main(String[] args) {18 DockerImageName image = DockerImageName.parse("myregistry.com:5000/path/to");19 System.out.println("image = " + image);20 }21}22import org.testcontainers.utility.DockerImageName;23public class Test {24 public static void main(String[] args) {25 DockerImageName image = DockerImageName.parse("myregistry.com:5000");26 System.out.println("image = " + image);27 }28}29import org.testcontainers.utility.DockerImageName;30public class Test {31 public static void main(String[] args) {32 DockerImageName image = DockerImageName.parse("myregistry.com");33 System.out.println("image = " + image);34 }35}36import org.testcontainers.utility.DockerImageName;37public class Test {38 public static void main(String[] args) {
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!!