Search in sources :

Example 6 with PropertySource

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

the class PropertySourcesPlaceholderConfigurerTests method withNonEnumerablePropertySource.

@Test
public void withNonEnumerablePropertySource() {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${foo}").getBeanDefinition());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    PropertySource<?> ps = new PropertySource<>("simplePropertySource", new Object()) {

        @Override
        public Object getProperty(String key) {
            return "bar";
        }
    };
    MockEnvironment env = new MockEnvironment();
    env.getPropertySources().addFirst(ps);
    ppc.setEnvironment(env);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("bar");
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) PropertySource(cn.taketoday.core.env.PropertySource) MockPropertySource(cn.taketoday.core.env.MockPropertySource) Test(org.junit.jupiter.api.Test)

Example 7 with PropertySource

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

the class ConfigDataEnvironmentTests method processAndApplyMovesDefaultPropertySourceToLast.

@Test
void processAndApplyMovesDefaultPropertySourceToLast(TestInfo info) {
    MockPropertySource defaultPropertySource = new MockPropertySource("defaultProperties");
    this.environment.getPropertySources().addFirst(defaultPropertySource);
    this.environment.setProperty("context.config.location", getConfigLocation(info));
    ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.bootstrapContext, this.environment, this.resourceLoader, this.additionalProfiles, null);
    configDataEnvironment.processAndApply();
    List<PropertySource<?>> sources = this.environment.getPropertySources().stream().collect(Collectors.toList());
    assertThat(sources.get(sources.size() - 1)).isSameAs(defaultPropertySource);
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) MapPropertySource(cn.taketoday.core.env.MapPropertySource) PropertySource(cn.taketoday.core.env.PropertySource) MockPropertySource(cn.taketoday.mock.env.MockPropertySource) AddedPropertySource(cn.taketoday.framework.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with PropertySource

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

the class StandardConfigDataLoader method load.

@Override
public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResource resource) throws IOException, ConfigDataNotFoundException {
    if (resource.isEmptyDirectory()) {
        return ConfigData.EMPTY;
    }
    ConfigDataResourceNotFoundException.throwIfDoesNotExist(resource, resource.getResource());
    StandardConfigDataReference reference = resource.getReference();
    Resource originTrackedResource = OriginTrackedResource.from(resource.getResource(), Origin.from(reference.getConfigDataLocation()));
    String name = String.format("Config resource '%s' via location '%s'", resource, reference.getConfigDataLocation());
    List<PropertySource<?>> propertySources = reference.getPropertySourceLoader().load(name, originTrackedResource);
    PropertySourceOptions options = (resource.getProfile() != null) ? PROFILE_SPECIFIC : NON_PROFILE_SPECIFIC;
    return new ConfigData(propertySources, options);
}
Also used : Resource(cn.taketoday.core.io.Resource) OriginTrackedResource(cn.taketoday.origin.OriginTrackedResource) PropertySourceOptions(cn.taketoday.framework.context.config.ConfigData.PropertySourceOptions) PropertySource(cn.taketoday.core.env.PropertySource)

Example 9 with PropertySource

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

the class StandardConfigDataLoader method load.

@Override
public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResource resource) throws IOException, ConfigDataNotFoundException {
    if (resource.isEmptyDirectory()) {
        return ConfigData.EMPTY;
    }
    ConfigDataResourceNotFoundException.throwIfDoesNotExist(resource, resource.getResource());
    StandardConfigDataReference reference = resource.getReference();
    Resource originTrackedResource = OriginTrackedResource.from(resource.getResource(), Origin.from(reference.getConfigDataLocation()));
    String name = String.format("Config resource '%s' via location '%s'", resource, reference.getConfigDataLocation());
    List<PropertySource<?>> propertySources = reference.getPropertySourceLoader().load(name, originTrackedResource);
    PropertySourceOptions options = (resource.getProfile() != null) ? PROFILE_SPECIFIC : NON_PROFILE_SPECIFIC;
    return new ConfigData(propertySources, options);
}
Also used : Resource(cn.taketoday.core.io.Resource) OriginTrackedResource(cn.taketoday.origin.OriginTrackedResource) PropertySourceOptions(cn.taketoday.framework.context.config.ConfigData.PropertySourceOptions) PropertySource(cn.taketoday.core.env.PropertySource)

Example 10 with PropertySource

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

the class ConfigDataEnvironmentTests method processAndApplyMovesDefaultPropertySourceToLast.

@Test
void processAndApplyMovesDefaultPropertySourceToLast(TestInfo info) {
    MockPropertySource defaultPropertySource = new MockPropertySource("defaultProperties");
    this.environment.getPropertySources().addFirst(defaultPropertySource);
    this.environment.setProperty("context.config.location", getConfigLocation(info));
    ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.bootstrapContext, this.environment, this.resourceLoader, this.additionalProfiles, null);
    configDataEnvironment.processAndApply();
    List<PropertySource<?>> sources = this.environment.getPropertySources().stream().collect(Collectors.toList());
    assertThat(sources.get(sources.size() - 1)).isSameAs(defaultPropertySource);
}
Also used : MockPropertySource(cn.taketoday.mock.env.MockPropertySource) MapPropertySource(cn.taketoday.core.env.MapPropertySource) PropertySource(cn.taketoday.core.env.PropertySource) MockPropertySource(cn.taketoday.mock.env.MockPropertySource) AddedPropertySource(cn.taketoday.framework.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

PropertySource (cn.taketoday.core.env.PropertySource)12 Test (org.junit.jupiter.api.Test)8 MapPropertySource (cn.taketoday.core.env.MapPropertySource)6 PropertySourceOptions (cn.taketoday.framework.context.config.ConfigData.PropertySourceOptions)4 MockPropertySource (cn.taketoday.mock.env.MockPropertySource)4 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)2 ValidationBindHandler (cn.taketoday.context.properties.bind.validation.ValidationBindHandler)2 ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)2 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)2 MockPropertySource (cn.taketoday.core.env.MockPropertySource)2 Resource (cn.taketoday.core.io.Resource)2 Option (cn.taketoday.framework.context.config.ConfigData.Option)2 Options (cn.taketoday.framework.context.config.ConfigData.Options)2 AddedPropertySource (cn.taketoday.framework.context.config.TestConfigDataEnvironmentUpdateListener.AddedPropertySource)2 OriginTrackedResource (cn.taketoday.origin.OriginTrackedResource)2 Validator (cn.taketoday.validation.Validator)2 ValidatorAdapter (cn.taketoday.validation.beanvalidation.ValidatorAdapter)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2