Search in sources :

Example 31 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class ConfigurationPropertySourcesTests method descendantOfPropertyAccessWhenMutableWithCacheShouldBePerformant.

// gh-21416
@Test
void descendantOfPropertyAccessWhenMutableWithCacheShouldBePerformant() {
    Function<StandardEnvironment, Long> descendantOfPerformance = (environment) -> {
        Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
        ConfigurationPropertyName missing = ConfigurationPropertyName.of("missing");
        long start = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            for (ConfigurationPropertySource source : sources) {
                assertThat(source.containsDescendantOf(missing)).isEqualTo(ConfigurationPropertyState.ABSENT);
            }
        }
        return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
    };
    StandardEnvironment environment = createPerformanceTestEnvironment(false);
    long baseline = descendantOfPerformance.apply(environment);
    ConfigurationPropertyCaching.get(environment).enable();
    long cached = descendantOfPerformance.apply(environment);
    assertThat(cached).isLessThan(baseline / 2);
}
Also used : Origin(org.springframework.boot.origin.Origin) PropertySource(org.springframework.core.env.PropertySource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StandardEnvironment(org.springframework.core.env.StandardEnvironment) SystemEnvironmentPropertySource(org.springframework.core.env.SystemEnvironmentPropertySource) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) OriginLookup(org.springframework.boot.origin.OriginLookup) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) PropertyResolver(org.springframework.core.env.PropertyResolver) Environment(org.springframework.core.env.Environment) MapPropertySource(org.springframework.core.env.MapPropertySource) Map(java.util.Map) MutablePropertySources(org.springframework.core.env.MutablePropertySources) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) Collections(java.util.Collections) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 32 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class AbstractApplicationContextRunnerTests method runWithMultiplePropertyValuesShouldAllAllValues.

@Test
void runWithMultiplePropertyValuesShouldAllAllValues() {
    get().withPropertyValues("test.foo=1").withPropertyValues("test.bar=2").run((context) -> {
        Environment environment = context.getEnvironment();
        assertThat(environment.getProperty("test.foo")).isEqualTo("1");
        assertThat(environment.getProperty("test.bar")).isEqualTo("2");
    });
}
Also used : Environment(org.springframework.core.env.Environment) Test(org.junit.jupiter.api.Test)

Example 33 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class CloudPlatformTests method getActiveWhenHasMissingWebsiteResourceGroupShouldNotReturnAzureAppService.

@Test
void getActiveWhenHasMissingWebsiteResourceGroupShouldNotReturnAzureAppService() {
    Map<String, Object> envVars = new HashMap<>();
    envVars.put("WEBSITE_SITE_NAME", "---");
    envVars.put("WEBSITE_INSTANCE_ID", "1234");
    envVars.put("WEBSITE_SKU", "1234");
    Environment environment = getEnvironmentWithEnvVariables(envVars);
    CloudPlatform platform = CloudPlatform.getActive(environment);
    assertThat(platform).isNull();
}
Also used : HashMap(java.util.HashMap) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.core.env.Environment) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.jupiter.api.Test)

Example 34 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class CloudPlatformTests method getActiveWhenHasDynoShouldReturnHeroku.

@Test
void getActiveWhenHasDynoShouldReturnHeroku() {
    Environment environment = new MockEnvironment().withProperty("DYNO", "---");
    CloudPlatform platform = CloudPlatform.getActive(environment);
    assertThat(platform).isEqualTo(CloudPlatform.HEROKU);
    assertThat(platform.isActive(environment)).isTrue();
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.core.env.Environment) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.jupiter.api.Test)

Example 35 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class CloudPlatformTests method getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes.

@Test
void getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes() {
    Environment environment = getEnvironmentWithEnvVariables(Collections.singletonMap("KUBERNETES_SERVICE_HOST", "---"));
    CloudPlatform platform = CloudPlatform.getActive(environment);
    assertThat(platform).isNull();
}
Also used : StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.core.env.Environment) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

Environment (org.springframework.core.env.Environment)163 Test (org.junit.jupiter.api.Test)68 StandardEnvironment (org.springframework.core.env.StandardEnvironment)30 EnvironmentVariableUtility (com.synopsys.integration.alert.environment.EnvironmentVariableUtility)26 MockEnvironment (org.springframework.mock.env.MockEnvironment)24 SpringApplication (org.springframework.boot.SpringApplication)21 EnvironmentProcessingResult (com.synopsys.integration.alert.environment.EnvironmentProcessingResult)19 EnvironmentVariableHandler (com.synopsys.integration.alert.environment.EnvironmentVariableHandler)19 EnvironmentVariableHandlerFactory (com.synopsys.integration.alert.environment.EnvironmentVariableHandlerFactory)19 HashMap (java.util.HashMap)14 ResourceLoader (org.springframework.core.io.ResourceLoader)13 Collectors (java.util.stream.Collectors)12 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)11 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)9 MutablePropertySources (org.springframework.core.env.MutablePropertySources)9 Map (java.util.Map)8 AlertConstants (com.synopsys.integration.alert.api.common.model.AlertConstants)7 ChannelKeys (com.synopsys.integration.alert.descriptor.api.model.ChannelKeys)7 EnvironmentVariableMockingUtil (com.synopsys.integration.alert.test.common.EnvironmentVariableMockingUtil)7 Predicate (java.util.function.Predicate)7