Search in sources :

Example 6 with StandardEnvironment

use of cn.taketoday.core.env.StandardEnvironment in project today-infrastructure by TAKETODAY.

the class ApplicationTests method disableCommandLinePropertySource.

@Test
void disableCommandLinePropertySource() {
    Application application = new Application(ExampleConfig.class);
    application.setApplicationType(ApplicationType.NONE_WEB);
    application.setAddCommandLineProperties(false);
    ConfigurableEnvironment environment = new StandardEnvironment();
    application.setEnvironment(environment);
    this.context = application.run("--foo=bar");
    assertThat(environment).doesNotHave(matchingPropertySource(PropertySource.class, "commandLineArgs"));
}
Also used : ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) PropertySource(cn.taketoday.core.env.PropertySource) CommandLinePropertySource(cn.taketoday.core.env.CommandLinePropertySource) MapPropertySource(cn.taketoday.core.env.MapPropertySource) CompositePropertySource(cn.taketoday.core.env.CompositePropertySource) Test(org.junit.jupiter.api.Test)

Example 7 with StandardEnvironment

use of cn.taketoday.core.env.StandardEnvironment in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertySourcesTests method descendantOfPropertyAccessWhenMutableWithCacheShouldBePerformant.

@Test
// gh-21416
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 : MapPropertySource(cn.taketoday.core.env.MapPropertySource) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Environment(cn.taketoday.core.env.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PropertySourcesPropertyResolver(cn.taketoday.core.env.PropertySourcesPropertyResolver) PropertySources(cn.taketoday.core.env.PropertySources) Disabled(org.junit.jupiter.api.Disabled) OriginLookup(cn.taketoday.origin.OriginLookup) Function(java.util.function.Function) PropertySource(cn.taketoday.core.env.PropertySource) PropertyResolver(cn.taketoday.core.env.PropertyResolver) Test(org.junit.jupiter.api.Test) LinkedHashMap(java.util.LinkedHashMap) TimeUnit(java.util.concurrent.TimeUnit) Origin(cn.taketoday.origin.Origin) Map(java.util.Map) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) Collections(java.util.Collections) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 8 with StandardEnvironment

use of cn.taketoday.core.env.StandardEnvironment in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertySourcesTests method getWhenAttachedShouldReturnAttached.

@Test
void getWhenAttachedShouldReturnAttached() {
    ConfigurableEnvironment environment = new StandardEnvironment();
    PropertySources sources = environment.getPropertySources();
    sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
    int expectedSize = sources.size();
    ConfigurationPropertySources.attach(environment);
    assertThat(ConfigurationPropertySources.get(environment)).hasSize(expectedSize);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) MapPropertySource(cn.taketoday.core.env.MapPropertySource) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 9 with StandardEnvironment

use of cn.taketoday.core.env.StandardEnvironment in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertySourcesTests method attachShouldAddAdapterAtBeginning.

@Test
void attachShouldAddAdapterAtBeginning() {
    ConfigurableEnvironment environment = new StandardEnvironment();
    PropertySources sources = environment.getPropertySources();
    sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
    sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
    int size = sources.size();
    ConfigurationPropertySources.attach(environment);
    assertThat(sources.size()).isEqualTo(size + 1);
    PropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
    assertThat(resolver.getProperty("server.port")).isEqualTo("1234");
}
Also used : PropertySourcesPropertyResolver(cn.taketoday.core.env.PropertySourcesPropertyResolver) SystemEnvironmentPropertySource(cn.taketoday.core.env.SystemEnvironmentPropertySource) PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) MapPropertySource(cn.taketoday.core.env.MapPropertySource) PropertySourcesPropertyResolver(cn.taketoday.core.env.PropertySourcesPropertyResolver) PropertyResolver(cn.taketoday.core.env.PropertyResolver) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 10 with StandardEnvironment

use of cn.taketoday.core.env.StandardEnvironment in project today-infrastructure by TAKETODAY.

the class ConfigurationPropertySourcesTests method createPerformanceTestEnvironment.

private StandardEnvironment createPerformanceTestEnvironment(boolean immutable) {
    StandardEnvironment environment = new StandardEnvironment();
    PropertySources propertySources = environment.getPropertySources();
    for (int i = 0; i < 100; i++) {
        propertySources.addLast(new TestPropertySource(i, immutable));
    }
    ConfigurationPropertySources.attach(environment);
    return environment;
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment)

Aggregations

StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)96 Test (org.junit.jupiter.api.Test)88 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)46 MapPropertySource (cn.taketoday.core.env.MapPropertySource)24 PropertySources (cn.taketoday.core.env.PropertySources)18 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)8 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)8 Environment (cn.taketoday.core.env.Environment)6 SystemEnvironmentPropertySource (cn.taketoday.core.env.SystemEnvironmentPropertySource)6 StandardServletEnvironment (cn.taketoday.web.context.support.StandardServletEnvironment)6 LinkedHashMap (java.util.LinkedHashMap)6 AnnotatedBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)4 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 ClassPathScanningCandidateComponentProvider (cn.taketoday.context.loader.ClassPathScanningCandidateComponentProvider)4 MockEnvironment (cn.taketoday.context.support.MockEnvironment)4 AbstractEnvironment (cn.taketoday.core.env.AbstractEnvironment)4 CommandLinePropertySource (cn.taketoday.core.env.CommandLinePropertySource)4 CompositePropertySource (cn.taketoday.core.env.CompositePropertySource)4 MockPropertySource (cn.taketoday.core.env.MockPropertySource)4 PropertyResolver (cn.taketoday.core.env.PropertyResolver)4