Best Testcontainers-java code snippet using org.testcontainers.utility.DockerImageName.DockerImageName
Source:DockerImageName.java  
1package org.testcontainers.utility; // (rank 384) copied from https://github.com/testcontainers/testcontainers-java/blob/0e263f296556c2357f0d4ea99d120a3a2880ceb3/core/src/main/java/org/testcontainers/utility/DockerImageName.java2import com.google.common.net.HostAndPort;3import lombok.AccessLevel;4import lombok.AllArgsConstructor;5import lombok.EqualsAndHashCode;6import lombok.Getter;7import lombok.With;8import org.jetbrains.annotations.NotNull;9import org.jetbrains.annotations.Nullable;10import org.testcontainers.utility.Versioning.Sha256Versioning;11import org.testcontainers.utility.Versioning.TagVersioning;12import java.util.regex.Pattern;13@EqualsAndHashCode(exclude = { "rawName", "compatibleSubstituteFor" })14@AllArgsConstructor(access = AccessLevel.PRIVATE)15public final class DockerImageName {16    /* Regex patterns used for validation */17    private static final String ALPHA_NUMERIC = "[a-z0-9]+";18    private static final String SEPARATOR = "([.]|_{1,2}|-+)";19    private static final String REPO_NAME_PART = ALPHA_NUMERIC + "(" + SEPARATOR + ALPHA_NUMERIC + ")*";20    private static final Pattern REPO_NAME = Pattern.compile(REPO_NAME_PART + "(/" + REPO_NAME_PART + ")*");21    private final String rawName;22    @With @Getter private final String registry;23    @With @Getter private final String repository;24    @NotNull @With(AccessLevel.PRIVATE)25    private final Versioning versioning;26    @Nullable @With(AccessLevel.PRIVATE)27    private final DockerImageName compatibleSubstituteFor;28    /**29     * Parses a docker image name from a provided string.30     *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:MySqlDatabaseContainerSupportProvider.java  
...4import com.link_intersystems.dbunit.testcontainers.DefaultDatabaseContainerSupport;5import org.dbunit.ext.mysql.MySqlDataTypeFactory;6import org.dbunit.ext.mysql.MySqlMetadataHandler;7import org.testcontainers.containers.MySQLContainer;8import org.testcontainers.utility.DockerImageName;9import static org.dbunit.database.DatabaseConfig.PROPERTY_DATATYPE_FACTORY;10import static org.dbunit.database.DatabaseConfig.PROPERTY_METADATA_HANDLER;11/**12 * @author René Link {@literal <rene.link@link-intersystems.com>}13 */14public class MySqlDatabaseContainerSupportProvider implements DatabaseContainerSupportProvider {15    @Override16    public boolean canProvideSupport(DockerImageName dockerImageName) {17        return dockerImageName.getUnversionedPart().contains("mysql");18    }19    @Override20    public DatabaseContainerSupport createDatabaseContainerSupport(DockerImageName dockerImageName) {21        DefaultDatabaseContainerSupport containerSupport = new DefaultDatabaseContainerSupport(() -> new MySQLContainer<>(dockerImageName));22        containerSupport.getDatabaseConfig().setProperty(PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());23        containerSupport.getDatabaseConfig().setProperty(PROPERTY_METADATA_HANDLER, new MySqlMetadataHandler());24        return containerSupport;25    }26}...Source:PostgresDatabaseContainerSupportProvider.java  
...3import com.link_intersystems.dbunit.testcontainers.DatabaseContainerSupportProvider;4import com.link_intersystems.dbunit.testcontainers.DefaultDatabaseContainerSupport;5import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;6import org.testcontainers.containers.PostgreSQLContainer;7import org.testcontainers.utility.DockerImageName;8import static org.dbunit.database.DatabaseConfig.PROPERTY_DATATYPE_FACTORY;9/**10 * @author René Link {@literal <rene.link@link-intersystems.com>}11 */12public class PostgresDatabaseContainerSupportProvider implements DatabaseContainerSupportProvider {13    @Override14    public boolean canProvideSupport(DockerImageName dockerImageName) {15        return dockerImageName.getUnversionedPart().contains("postgres");16    }17    @Override18    public DatabaseContainerSupport createDatabaseContainerSupport(DockerImageName dockerImageName) {19        DefaultDatabaseContainerSupport containerSupport = new DefaultDatabaseContainerSupport(() -> new PostgreSQLContainer<>(dockerImageName));20        containerSupport.getDatabaseConfig().setProperty(PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());21        return containerSupport;22    }23}...DockerImageName
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.utility.DockerImageName;3public class 1 {4    public static void main(String[] args) {5        try (GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.11.5"))) {6            container.start();7        }8    }9}10	at org.testcontainers.utility.DockerImageName.validate(DockerImageName.java:202)11	at org.testcontainers.utility.DockerImageName.parse(DockerImageName.java:172)12	at 1.main(1.java:7)13Recommended Posts: Java | DockerImageName.parse() method14Java | DockerImageName.get() method15Java | DockerImageName.getUnversionedPart() method16Java | DockerImageName.getVersionedPart() method17Java | DockerImageName.getRegistry() method18Java | DockerImageName.getRepository() method19Java | DockerImageName.getTag() method20Java | DockerImageName.getPart() method21Java | DockerImageName.getBaseName() method22Java | DockerImageName.getBaseNameWithoutTag() method23Java | DockerImageName.getBaseNameWithoutTagOrDigest() method24Java | DockerImageName.getBaseNameWithoutDigest() method25Java | DockerImageName.getDigest() method26Java | DockerImageName.getUnversionedPartWithoutRegistry() method27Java | DockerImageName.getUnversionedPartWithoutRegistryAndRepository() method28Java | DockerImageName.getUnversionedPartWithoutRepository() method29Java | DockerImageName.getVersionedPartWithoutRegistry() method30Java | DockerImageName.getVersionedPartWithoutRegistryAndRepository() method31Java | DockerImageName.getVersionedPartWithoutRepository() method32Java | DockerImageName.getVersionedPartWithoutTag() method33Java | DockerImageName.getVersionedPartWithoutTagOrDigest() method34Java | DockerImageName.getVersionedPartWithoutDigest() method35Java | DockerImageName.getRegistryAndRepository() method36Java | DockerImageName.getRegistryAndRepositoryWithoutTag() method37Java | DockerImageName.getRegistryAndRepositoryWithoutTagOrDigest() methodDockerImageName
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.utility.DockerImageName;3public class TestDockerImageName {4    public static void main(String[] args) {5        GenericContainer container = new GenericContainer(DockerImageName.parse("alpine:3.9"));6        container.setCommand("echo hello world");7        container.start();8        System.out.println(container.getLogs());9        container.stop();10    }11}DockerImageName
Using AI Code Generation
1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.wait.strategy.Wait;3import org.testcontainers.utility.DockerImageName;4public class TestContainer {5    public static void main(String[] args) {6        GenericContainer container = new GenericContainer(DockerImageName.parse("postgres:latest"))7                .withExposedPorts(5432)8                .waitingFor(Wait.forListeningDockerImageName
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2import org.testcontainers.utility.DockerImageName;3public class DockerImageNameTest {4    public static void main(String[] args) {5        DockerImageName dockerImageName = DockerImageName.parse("alpine:3.9");6        System.out.println(dockerImageName.getUnversionedPart());7    }8}9import org.testcontainers.utility.DockerImageName;10import org.testcontainers.utility.DockerImageName;11public class DockerImageNameTest {12    public static void main(String[] args) {13        DockerImageName dockerImageName = DockerImageName.parse("alpine:3.9");14        System.out.println(dockerImageName.getVersionPart());15    }16}17import org.testcontainers.utility.DockerImageName;18import org.testcontainers.utility.DockerImageName;19public class DockerImageNameTest {20    public static void main(String[] args) {21        DockerImageName dockerImageName = DockerImageName.parse("alpine:3.9");22        System.out.println(dockerImageName.getRegistry());23    }24}25import org.testcontainers.utility.DockerImageName;26import org.testcontainers.utility.DockerImageName;27public class DockerImageNameTest {28    public static void main(String[] args) {29        DockerImageName dockerImageName = DockerImageName.parse("alpine:3.9");30        System.out.println(dockerImageName.getRepository());31    }32}33import org.testcontainers.utility.DockerImageName;34import org.testcontainers.utility.DockerImageName;35public class DockerImageNameTest {36    public static void main(String[] args) {37        DockerImageName dockerImageName = DockerImageName.parse("alpine:3.9");38        System.out.println(dockerImageName.getFullName());39    }40}DockerImageName
Using AI Code Generation
1import org.testcontainers.utility.DockerImageName;2public class DockerImageNameDemo {3    public static void main(String[] args) {4        DockerImageName dockerImage = DockerImageName.parse("busybox:latest");5        System.out.println("DockerImageName object: " + dockerImage);6        DockerImageName dockerImage1 = DockerImageName.parse("busybox");7        System.out.println("DockerImageName object: " + dockerImage1);8        DockerImageName dockerImage2 = DockerImageName.parse("busybox:latest");9        System.out.println("DockerImageName object: " + dockerImage2);10        DockerImageName dockerImage3 = DockerImageName.parse("my-registry.example.com:5000/my-org/my-image:latest");11        System.out.println("DockerImageName object: " + dockerImage3);12        DockerImageName dockerImage4 = DockerImageName.parse("my-registry.example.com:5000/my-org/my-image");13        System.out.println("DockerImageName object: " + dockerImage4);14        DockerImageName dockerImage5 = DockerImageName.parse("my-registry.example.com:5000/my-org/my-image:latest");15        System.out.println("DockerImageName object: " + dockerImage5);16        DockerImageName dockerImage6 = DockerImageName.parse("my-registry.example.com:5000/my-org/my-image");17        System.out.println("DockerImageName object: " + dockerImage6);18        DockerImageName dockerImage7 = DockerImageName.parse("my-registry.example.com:5000/my-org/my-image:latest");19        System.out.println("DockerImageName object: " + dockerDockerImageName
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.utility.DockerImageName;4public class DockerImageNameTest {5    public void testDockerImageName() {6        DockerImageName dockerImageName = DockerImageName.parse("testcontainers/ryuk:0.3.0");7        System.out.println(dockerImageName.getVersionPart());8        System.out.println(dockerImageName.getUnversionedPart());9    }10}11package org.testcontainers.containers;12import org.junit.Test;13import org.testcontainers.utility.DockerImageName;14public class DockerImageNameTest {15    public void testDockerImageName() {16        DockerImageName dockerImageName = DockerImageName.parse("testcontainers/ryuk:0.3.0");17        System.out.println(dockerImageName.getVersionPart());18        System.out.println(dockerImageName.getUnversionedPart());19    }20}21package org.testcontainers.containers;22import org.junit.Test;23import org.testcontainers.utility.DockerImageName;24public class DockerImageNameTest {25    public void testDockerImageName() {26        DockerImageName dockerImageName = DockerImageName.parse("testcontainers/ryuk:0.3.0");27        System.out.println(dockerImageName.getVersionPart());28        System.out.println(dockerImageName.getUnversionedPart());29    }30}31package org.testcontainers.containers;32import org.junit.Test;33import org.testcontainers.utility.DockerImageName;34public class DockerImageNameTest {35    public void testDockerImageName() {36        DockerImageName dockerImageName = DockerImageName.parse("testcontainers/ryukLearn 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!!
