Search in sources :

Example 21 with Environment

use of cn.taketoday.core.env.Environment in project today-framework 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 22 with Environment

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

the class ProfilesTests method getActiveWhenNoEnvironmentProfilesAndNoPropertyReturnsEmptyArray.

@Test
void getActiveWhenNoEnvironmentProfilesAndNoPropertyReturnsEmptyArray() {
    Environment environment = new MockEnvironment();
    Binder binder = Binder.get(environment);
    Profiles profiles = new Profiles(environment, binder, null);
    assertThat(profiles.getActive()).isEmpty();
}
Also used : Binder(cn.taketoday.context.properties.bind.Binder) MockEnvironment(cn.taketoday.mock.env.MockEnvironment) MockEnvironment(cn.taketoday.mock.env.MockEnvironment) Environment(cn.taketoday.core.env.Environment) Test(org.junit.jupiter.api.Test)

Example 23 with Environment

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

the class ParserStrategyUtils method newInstance.

/**
 * 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 newInstance(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 24 with Environment

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

the class PropertySourcesDeducerTests method getPropertySourcesWhenUnavailableThrowsException.

@Test
void getPropertySourcesWhenUnavailableThrowsException() {
    ApplicationContext applicationContext = mock(ApplicationContext.class);
    Environment environment = mock(Environment.class);
    given(applicationContext.getEnvironment()).willReturn(environment);
    PropertySourcesDeducer deducer = new PropertySourcesDeducer(applicationContext);
    assertThatIllegalStateException().isThrownBy(() -> deducer.getPropertySources()).withMessage("Unable to obtain PropertySources from PropertySourcesPlaceholderConfigurer or Environment");
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) Environment(cn.taketoday.core.env.Environment) Test(org.junit.jupiter.api.Test)

Example 25 with Environment

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

the class CloudPlatformTests method getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes.

@Test
void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
    Environment environment = getEnvironmentWithEnvVariables(Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---"));
    CloudPlatform platform = CloudPlatform.getActive(environment);
    assertThat(platform).isNull();
}
Also used : MockEnvironment(cn.taketoday.mock.env.MockEnvironment) Environment(cn.taketoday.core.env.Environment) 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