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);
}
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();
}
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;
}
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");
}
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();
}
Aggregations