Search in sources :

Example 1 with ConfigurationPropertySource

use of cn.taketoday.context.properties.source.ConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class InactiveConfigDataAccessExceptionTests method throwIfPropertyFoundWhenPropertyNotFoundDoesNothing.

@Test
void throwIfPropertyFoundWhenPropertyNotFoundDoesNothing() {
    ConfigDataEnvironmentContributor contributor = mock(ConfigDataEnvironmentContributor.class);
    ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(this.propertySource);
    given(contributor.getConfigurationPropertySource()).willReturn(configurationPropertySource);
    InactiveConfigDataAccessException.throwIfPropertyFound(contributor, ConfigurationPropertyName.of("spring"));
}
Also used : ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationPropertySource

use of cn.taketoday.context.properties.source.ConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ProfilesTests method getDefaultWhenDefaultEnvironmentProfileAndBinderProperty.

@Test
void getDefaultWhenDefaultEnvironmentProfileAndBinderProperty() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("context.profiles.default", "default");
    List<ConfigurationPropertySource> sources = new ArrayList<>();
    ConfigurationPropertySources.get(environment).forEach(sources::add);
    sources.add(new MapConfigurationPropertySource(Collections.singletonMap("context.profiles.default", "a,b,c")));
    Binder binder = new Binder(sources);
    Profiles profiles = new Profiles(environment, binder, null);
    assertThat(profiles.getDefault()).containsExactly("default");
}
Also used : Binder(cn.taketoday.context.properties.bind.Binder) MockEnvironment(cn.taketoday.mock.env.MockEnvironment) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) MapConfigurationPropertySource(cn.taketoday.context.properties.source.MapConfigurationPropertySource) ArrayList(java.util.ArrayList) MapConfigurationPropertySource(cn.taketoday.context.properties.source.MapConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurationPropertySource

use of cn.taketoday.context.properties.source.ConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class MapBinder method bindAggregate.

@Override
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target, AggregateElementBinder elementBinder) {
    Map<Object, Object> map = CollectionUtils.createMap((target.getValue() != null) ? Map.class : target.getType().resolve(Object.class), 0);
    Bindable<?> resolvedTarget = resolveTarget(target);
    boolean hasDescendants = hasDescendants(name);
    for (ConfigurationPropertySource source : getContext().getSources()) {
        if (!ConfigurationPropertyName.EMPTY.equals(name)) {
            ConfigurationProperty property = source.getConfigurationProperty(name);
            if (property != null && !hasDescendants) {
                return getContext().getConverter().convert(property.getValue(), target);
            }
            source = source.filter(name::isAncestorOf);
        }
        new EntryBinder(name, resolvedTarget, elementBinder).bindEntries(source, map);
    }
    return map.isEmpty() ? null : map;
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) IterableConfigurationPropertySource(cn.taketoday.context.properties.source.IterableConfigurationPropertySource) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) Map(java.util.Map)

Example 4 with ConfigurationPropertySource

use of cn.taketoday.context.properties.source.ConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributor method ofUnboundImport.

/**
 * Factory method to create an {@link Kind#UNBOUND_IMPORT unbound import} contributor.
 * This contributor has been actively imported from another contributor and may itself
 * import further contributors later.
 *
 * @param location the location of this contributor
 * @param resource the config data resource
 * @param profileSpecific if the contributor is from a profile specific import
 * @param configData the config data
 * @param propertySourceIndex the index of the property source that should be used
 * @return a new {@link ConfigDataEnvironmentContributor} instance
 */
static ConfigDataEnvironmentContributor ofUnboundImport(@Nullable ConfigDataLocation location, @Nullable ConfigDataResource resource, boolean profileSpecific, ConfigData configData, int propertySourceIndex) {
    PropertySource<?> propertySource = configData.getPropertySources().get(propertySourceIndex);
    Options options = configData.getOptions(propertySource);
    ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
    return new ConfigDataEnvironmentContributor(Kind.UNBOUND_IMPORT, location, resource, profileSpecific, propertySource, configurationPropertySource, null, options, null);
}
Also used : Options(cn.taketoday.framework.context.config.ConfigData.Options) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource)

Example 5 with ConfigurationPropertySource

use of cn.taketoday.context.properties.source.ConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributors method getBinder.

private Binder getBinder(@Nullable ConfigDataActivationContext activationContext, Predicate<ConfigDataEnvironmentContributor> filter, Set<BinderOption> options) {
    boolean failOnInactiveSource = options.contains(BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
    Iterable<ConfigurationPropertySource> sources = () -> getBinderSources(filter.and((contributor) -> failOnInactiveSource || contributor.isActive(activationContext)));
    var placeholdersResolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(this.root, activationContext, null, failOnInactiveSource);
    BindHandler bindHandler = !failOnInactiveSource ? null : new InactiveSourceChecker(activationContext);
    return new Binder(sources, placeholdersResolver, null, null, bindHandler);
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) Bindable(cn.taketoday.context.properties.bind.Bindable) Predicate(java.util.function.Predicate) Set(java.util.Set) BindHandler(cn.taketoday.context.properties.bind.BindHandler) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) LoggerFactory(cn.taketoday.logging.LoggerFactory) List(java.util.List) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) ObjectUtils(cn.taketoday.util.ObjectUtils) ConfigurationPropertyName(cn.taketoday.context.properties.source.ConfigurationPropertyName) Nullable(cn.taketoday.lang.Nullable) Map(java.util.Map) Logger(cn.taketoday.logging.Logger) Kind(cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.Kind) Binder(cn.taketoday.context.properties.bind.Binder) ConfigurableBootstrapContext(cn.taketoday.framework.ConfigurableBootstrapContext) Collections(java.util.Collections) ImportPhase(cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase) EnumSet(java.util.EnumSet) BindContext(cn.taketoday.context.properties.bind.BindContext) Binder(cn.taketoday.context.properties.bind.Binder) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) BindHandler(cn.taketoday.context.properties.bind.BindHandler)

Aggregations

ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)26 Binder (cn.taketoday.context.properties.bind.Binder)14 Test (org.junit.jupiter.api.Test)12 MapConfigurationPropertySource (cn.taketoday.context.properties.source.MapConfigurationPropertySource)8 ArrayList (java.util.ArrayList)8 BindHandler (cn.taketoday.context.properties.bind.BindHandler)4 Bindable (cn.taketoday.context.properties.bind.Bindable)4 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)4 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)4 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)4 MockEnvironment (cn.taketoday.mock.env.MockEnvironment)4 List (java.util.List)4 Map (java.util.Map)4 BindContext (cn.taketoday.context.properties.bind.BindContext)2 BindException (cn.taketoday.context.properties.bind.BindException)2 PlaceholdersResolver (cn.taketoday.context.properties.bind.PlaceholdersResolver)2 IterableConfigurationPropertySource (cn.taketoday.context.properties.source.IterableConfigurationPropertySource)2 MapPropertySource (cn.taketoday.core.env.MapPropertySource)2 ConfigurableBootstrapContext (cn.taketoday.framework.ConfigurableBootstrapContext)2 Options (cn.taketoday.framework.context.config.ConfigData.Options)2