Best Testcontainers-java code snippet using org.testcontainers.utility.TestcontainersConfiguration.getEnvVarOrUserProperty
Source:TestcontainersConfiguration.java  
...125    public String getLocalStackImage() {126        return getImage(LOCALSTACK_IMAGE).asCanonicalNameString();127    }128    public boolean isDisableChecks() {129        return Boolean.parseBoolean(getEnvVarOrUserProperty("checks.disable", "false"));130    }131    @UnstableAPI132    public boolean environmentSupportsReuse() {133        // specifically not supported as an environment variable or classpath property134        return Boolean.parseBoolean(getEnvVarOrUserProperty("testcontainers.reuse.enable", "false"));135    }136    public String getDockerClientStrategyClassName() {137        // getConfigurable won't apply the TESTCONTAINERS_ prefix when looking for env vars if DOCKER_ appears at the beginning.138        // Because of this overlap, and the desire to not change this specific TESTCONTAINERS_DOCKER_CLIENT_STRATEGY setting,139        // we special-case the logic here so that docker.client.strategy is used when reading properties files and140        // TESTCONTAINERS_DOCKER_CLIENT_STRATEGY is used when searching environment variables.141        // looks for TESTCONTAINERS_ prefixed env var only142        String prefixedEnvVarStrategy = environment.get("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY");143        if (prefixedEnvVarStrategy != null) {144            return prefixedEnvVarStrategy;145        }146        // looks for unprefixed env var or unprefixed property, or null if the strategy is not set at all147        return getEnvVarOrUserProperty("docker.client.strategy", null);148    }149    public String getTransportType() {150        return getEnvVarOrProperty("transport.type", "httpclient5");151    }152    public Integer getImagePullPauseTimeout() {153        return Integer.parseInt(getEnvVarOrProperty("pull.pause.timeout", "30"));154    }155    public String getImageSubstitutorClassName() {156        return getEnvVarOrProperty("image.substitutor", null);157    }158    public Integer getClientPingTimeout() {159        return Integer.parseInt(getEnvVarOrProperty("client.ping.timeout", "5"));160    }161    @Nullable162    @Contract("_, !null, _ -> !null")163    private String getConfigurable(@NotNull final String propertyName, @Nullable final String defaultValue, Properties... propertiesSources) {164        String envVarName = propertyName.replaceAll("\\.", "_").toUpperCase();165        if (!envVarName.startsWith("TESTCONTAINERS_") && !envVarName.startsWith("DOCKER_")) {166            envVarName = "TESTCONTAINERS_" + envVarName;167        }168        if (environment.containsKey(envVarName)) {169            return environment.get(envVarName);170        }171        for (final Properties properties : propertiesSources) {172            if (properties.get(propertyName) != null) {173                return (String) properties.get(propertyName);174            }175        }176        return defaultValue;177    }178    /**179     * Gets a configured setting from an environment variable (if present) or a configuration file property otherwise.180     * The configuration file will be the <code>.testcontainers.properties</code> file in the user's home directory or181     * a <code>testcontainers.properties</code> found on the classpath.182     * <p>183     * Note that when searching environment variables, the prefix `TESTCONTAINERS_` will usually be applied to the184     * property name, which will be converted to upper-case with underscore separators. This prefix will not be added185     * if the property name begins `docker.`.186     *187     * @param propertyName name of configuration file property (dot-separated lower case)188     * @return the found value, or null if not set189     */190    @Contract("_, !null -> !null")191    public String getEnvVarOrProperty(@NotNull final String propertyName, @Nullable final String defaultValue) {192        return getConfigurable(propertyName, defaultValue, userProperties, classpathProperties);193    }194    /**195     * Gets a configured setting from an environment variable (if present) or a configuration file property otherwise.196     * The configuration file will be the <code>.testcontainers.properties</code> file in the user's home directory.197     * <p>198     * Note that when searching environment variables, the prefix `TESTCONTAINERS_` will usually be applied to the199     * property name, which will be converted to upper-case with underscore separators. This prefix will not be added200     * if the property name begins `docker.`.201     *202     * @param propertyName name of configuration file property (dot-separated lower case)203     * @return the found value, or null if not set204     */205    @Contract("_, !null -> !null")206    public String getEnvVarOrUserProperty(@NotNull final String propertyName, @Nullable final String defaultValue) {207        return getConfigurable(propertyName, defaultValue, userProperties);208    }209    /**210     * Gets a configured setting from an environment variable.211     * <p>212     * Note that when searching environment variables, the prefix `TESTCONTAINERS_` will usually be applied to the213     * property name, which will be converted to upper-case with underscore separators. This prefix will not be added214     * if the property name begins `docker.`.215     *216     * @param propertyName name of configuration file property (dot-separated lower case)217     * @return the found value, or null if not set218     */219    @Contract("_, !null -> !null")220    public String getUserProperty(@NotNull final String propertyName, @Nullable final String defaultValue) {221        return getConfigurable(propertyName, defaultValue);222    }223    /**224     * @return properties values available from user properties and classpath properties. Values set by environment225     * variable are NOT included.226     * @deprecated usages should be removed ASAP. See {@link TestcontainersConfiguration#getEnvVarOrProperty(String, String)},227     * {@link TestcontainersConfiguration#getEnvVarOrUserProperty(String, String)} or {@link TestcontainersConfiguration#getUserProperty(String, String)}228     * for suitable replacements.229     */230    @Deprecated231    public Properties getProperties() {232        return Stream.of(userProperties, classpathProperties)233            .reduce(new Properties(), (a, b) -> {234                a.putAll(b);235                return a;236            });237    }238    @Deprecated239    public boolean updateGlobalConfig(@NonNull String prop, @NonNull String value) {240        return updateUserConfig(prop, value);241    }...Source:EnvironmentAndSystemPropertyClientProviderStrategyTest.java  
...44    }45    @Test46    public void testWhenConfigAbsent() {47        Mockito.doReturn("auto").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());48        Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull());49        Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull());50        Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull());51        EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy();52        TransportConfig transportConfig = strategy.getTransportConfig();53        assertEquals(defaultDockerHost, transportConfig.getDockerHost());54        assertEquals(defaultSSLConfig, transportConfig.getSslConfig());55    }56    @Test57    public void testWhenDockerHostPresent() {58        Mockito.doReturn("auto").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());59        Mockito.doReturn("tcp://1.2.3.4:2375").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull());60        Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull());61        Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull());62        EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy();63        TransportConfig transportConfig = strategy.getTransportConfig();64        assertEquals("tcp://1.2.3.4:2375", transportConfig.getDockerHost().toString());65        assertEquals(defaultSSLConfig, transportConfig.getSslConfig());66    }67    @Test68    public void testWhenDockerHostAndSSLConfigPresent() throws IOException {69        Path tempDir = Files.createTempDirectory("testcontainers-test");70        String tempDirPath = tempDir.toAbsolutePath().toString();71        Mockito.doReturn("auto").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());72        Mockito.doReturn("tcp://1.2.3.4:2375").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull());73        Mockito.doReturn("1").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull());74        Mockito.doReturn(tempDirPath).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull());75        EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy();76        TransportConfig transportConfig = strategy.getTransportConfig();77        assertEquals("tcp://1.2.3.4:2375", transportConfig.getDockerHost().toString());78        SSLConfig sslConfig = transportConfig.getSslConfig();79        assertNotNull(sslConfig);80        assertTrue(sslConfig instanceof LocalDirectorySSLConfig);81        assertEquals(tempDirPath, ((LocalDirectorySSLConfig) sslConfig).getDockerCertPath());82    }83    @Test84    public void applicableWhenIgnoringUserPropertiesAndConfigured() {85        Mockito.doReturn("autoIgnoringUserProperties").when(TestcontainersConfiguration.getInstance()).getEnvVarOrProperty(eq("dockerconfig.source"), anyString());86        DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder()87            .withDockerHost("tcp://1.2.3.4:2375");88        EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy(configBuilder);...getEnvVarOrUserProperty
Using AI Code Generation
1import org.testcontainers.utility.TestcontainersConfiguration;2public class 1 {3   public static void main(String[] args) {4      TestcontainersConfiguration.getEnvVarOrUserProperty("TESTCONTAINERS_RYUK_DISABLED", "true");5   }6}getEnvVarOrUserProperty
Using AI Code Generation
1import org.testcontainers.utility.TestcontainersConfiguration;2public class 1 {3    public static void main(String[] args) {4        TestcontainersConfiguration.getInstance().getEnvVarOrUserProperty("TESTCONTAINERS_RYUK_DISABLED", "false");5    }6}getEnvVarOrUserProperty
Using AI Code Generation
1package org.testcontainers.utility;2import java.util.Map;3import org.testcontainers.utility.TestcontainersConfiguration;4public class TestcontainersConfigurationGetEnvVarOrUserProperty {5    public static void main(String[] args) {6        TestcontainersConfiguration config = TestcontainersConfiguration.getInstance();7        Map<String, String> envVars = System.getenv();8        String envVarName = "TESTCONTAINERS_RYUK_DISABLED";9        String envVarValue = envVars.get(envVarName);10        System.out.println(envVarName + "=" + envVarValue);11        String userPropertyName = "ryuk.container.image";12        String userPropertyValue = config.getEnvVarOrUserProperty(userPropertyName, "testcontainers/ryuk:0.3.0");13        System.out.println(userPropertyName + "=" + userPropertyValue);14    }15}16package org.testcontainers;17import org.testcontainers.DockerClientFactory;18public class DockerClientFactoryUseDockerSocket {19    public static void main(String[] args) {20        DockerClientFactory factory = DockerClientFactory.instance();21        boolean useDockerSocket = factory.useDockerSocket();22        System.out.println("useDockerSocket=" + useDockerSocket);23    }24}25package org.testcontainers;26import org.testcontainers.DockerClientFactory;27public class DockerClientFactoryGetDockerHostIpAddress {28    public static void main(String[] args) {29        DockerClientFactory factory = DockerClientFactory.instance();30        String dockerHostIpAddress = factory.getDockerHostIpAddress();31        System.out.println("dockerHostIpAddress=" + dockerHostIpAddress);32    }33}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!!
