Search in sources :

Example 16 with Environment

use of cn.taketoday.core.env.Environment 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 17 with Environment

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

the class ConfigurationPropertySourcesTests method fromPropertySourceShouldFlattenPropertySources.

@Test
void fromPropertySourceShouldFlattenPropertySources() {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", "bar")));
    environment.getPropertySources().addFirst(new MapPropertySource("far", Collections.singletonMap("far", "far")));
    PropertySources sources = new PropertySources();
    sources.addFirst(new PropertySource<Environment>("env", environment) {

        @Override
        public String getProperty(String key) {
            return this.source.getProperty(key);
        }
    });
    sources.addLast(new MapPropertySource("baz", Collections.singletonMap("baz", "barf")));
    Iterable<ConfigurationPropertySource> configurationSources = ConfigurationPropertySources.from(sources);
    assertThat(configurationSources.iterator()).toIterable().hasSize(5);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Environment(cn.taketoday.core.env.Environment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 18 with Environment

use of cn.taketoday.core.env.Environment in project today-framework by TAKETODAY.

the class AbstractWebServer method prepareInitialize.

protected void prepareInitialize() {
    log.info("Prepare initialize web server");
    WebServerApplicationContext context = obtainApplicationContext();
    Environment environment = context.getEnvironment();
    TodayStrategies.setProperty(WebApplicationLoader.ENABLE_WEB_STARTED_LOG, Boolean.FALSE.toString());
    String webMvcConfigLocation = environment.getProperty(WebApplicationLoader.WEB_MVC_CONFIG_LOCATION);
    if (StringUtils.isNotEmpty(webMvcConfigLocation)) {
        TodayStrategies.setProperty(WebApplicationLoader.ENABLE_WEB_MVC_XML, Boolean.TRUE.toString());
    }
}
Also used : WebServerApplicationContext(cn.taketoday.web.framework.WebServerApplicationContext) Environment(cn.taketoday.core.env.Environment)

Example 19 with Environment

use of cn.taketoday.core.env.Environment in project today-framework by TAKETODAY.

the class ParserStrategyUtils method instantiateClass.

/**
 * Instantiate a class using an appropriate constructor and return the new
 * instance as the specified assignable type. The returned instance will
 * have {@link BeanClassLoaderAware}, {@link BeanFactoryAware},
 * {@link EnvironmentAware}, and {@link ResourceLoaderAware} contracts
 * invoked if they are implemented by the given object.
 */
@SuppressWarnings("unchecked")
static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo, BootstrapContext loadingContext) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.isAssignable(assignableTo, clazz);
    if (clazz.isInterface()) {
        throw new BeanInstantiationException(clazz, "Specified class is an interface");
    }
    BeanDefinitionRegistry registry = loadingContext.getRegistry();
    PatternResourceLoader resourceLoader = loadingContext.getResourceLoader();
    Environment environment = loadingContext.getEnvironment();
    ClassLoader classLoader = registry instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader();
    T instance = (T) createInstance(clazz, environment, resourceLoader, registry, classLoader);
    ParserStrategyUtils.invokeAwareMethods(instance, environment, resourceLoader, registry, classLoader);
    return instance;
}
Also used : ConfigurableBeanFactory(cn.taketoday.beans.factory.config.ConfigurableBeanFactory) BeanInstantiationException(cn.taketoday.beans.BeanInstantiationException) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) Environment(cn.taketoday.core.env.Environment) PatternResourceLoader(cn.taketoday.core.io.PatternResourceLoader)

Example 20 with Environment

use of cn.taketoday.core.env.Environment in project today-framework by TAKETODAY.

the class ConfigurationPropertySourcesTests method fromPropertySourceShouldFlattenPropertySources.

@Test
void fromPropertySourceShouldFlattenPropertySources() {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", "bar")));
    environment.getPropertySources().addFirst(new MapPropertySource("far", Collections.singletonMap("far", "far")));
    PropertySources sources = new PropertySources();
    sources.addFirst(new PropertySource<Environment>("env", environment) {

        @Override
        public String getProperty(String key) {
            return this.source.getProperty(key);
        }
    });
    sources.addLast(new MapPropertySource("baz", Collections.singletonMap("baz", "barf")));
    Iterable<ConfigurationPropertySource> configurationSources = ConfigurationPropertySources.from(sources);
    assertThat(configurationSources.iterator()).toIterable().hasSize(5);
}
Also used : PropertySources(cn.taketoday.core.env.PropertySources) MapPropertySource(cn.taketoday.core.env.MapPropertySource) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Environment(cn.taketoday.core.env.Environment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) StandardEnvironment(cn.taketoday.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

Environment (cn.taketoday.core.env.Environment)66 Test (org.junit.jupiter.api.Test)58 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)50 MockEnvironment (cn.taketoday.mock.env.MockEnvironment)48 HashMap (java.util.HashMap)16 Binder (cn.taketoday.context.properties.bind.Binder)14 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)10 MapConfigurationPropertySource (cn.taketoday.context.properties.source.MapConfigurationPropertySource)6 MapPropertySource (cn.taketoday.core.env.MapPropertySource)6 PropertySources (cn.taketoday.core.env.PropertySources)6 ApplicationPropertySourcesProcessor (cn.taketoday.context.support.ApplicationPropertySourcesProcessor)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)4 BeanInstantiationException (cn.taketoday.beans.BeanInstantiationException)3 ConfigurableBeanFactory (cn.taketoday.beans.factory.config.ConfigurableBeanFactory)3 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)3 PatternResourceLoader (cn.taketoday.core.io.PatternResourceLoader)3 BeanWrapper (cn.taketoday.beans.BeanWrapper)2 BeansException (cn.taketoday.beans.BeansException)2 PropertyValues (cn.taketoday.beans.PropertyValues)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2