Search in sources :

Example 1 with ConfigurationProperty

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

the class ArrayBinderTests method bindToArrayWhenNonSequentialShouldThrowException.

@Test
void bindToArrayWhenNonSequentialShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0]", "2");
    source.put("foo[1]", "1");
    source.put("foo[3]", "3");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", INTEGER_ARRAY)).satisfies((ex) -> {
        Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex.getCause()).getUnboundProperties();
        assertThat(unbound.size()).isEqualTo(1);
        ConfigurationProperty property = unbound.iterator().next();
        assertThat(property.getName().toString()).isEqualTo("foo[3]");
        assertThat(property.getValue()).isEqualTo("3");
    });
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationProperty

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

the class InvalidConfigDataPropertyExceptionTests method createWhenNoOriginHasCorrectMessage.

@Test
void createWhenNoOriginHasCorrectMessage() {
    ConfigurationProperty property = new ConfigurationProperty(this.invalid, "bad", null);
    assertThat(new InvalidConfigDataPropertyException(property, false, this.replacement, this.resource)).hasMessage("Property 'invalid' imported from location 'test' is invalid and should be replaced with 'replacement'");
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurationProperty

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

the class Binder method bindObject.

@Nullable
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, boolean allowRecursiveBinding) {
    ConfigurationProperty property = findProperty(name, target, context);
    if (property == null && context.depth != 0 && containsNoDescendantOf(context.getSources(), name)) {
        return null;
    }
    AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
    if (aggregateBinder != null) {
        return bindAggregate(name, target, handler, context, aggregateBinder);
    }
    if (property != null) {
        try {
            return bindProperty(target, context, property);
        } catch (ConverterNotFoundException ex) {
            // We might still be able to bind it using the recursive binders
            Object instance = bindDataObject(name, target, handler, context, allowRecursiveBinding);
            if (instance != null) {
                return instance;
            }
            throw ex;
        }
    }
    return bindDataObject(name, target, handler, context, allowRecursiveBinding);
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) Nullable(cn.taketoday.lang.Nullable)

Example 4 with ConfigurationProperty

use of cn.taketoday.context.properties.source.ConfigurationProperty 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 5 with ConfigurationProperty

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

the class InactiveConfigDataAccessException method throwIfPropertyFound.

/**
 * Throw an {@link InactiveConfigDataAccessException} if the given
 * {@link ConfigDataEnvironmentContributor} contains the property.
 *
 * @param contributor the contributor to check
 * @param name the name to check
 */
static void throwIfPropertyFound(ConfigDataEnvironmentContributor contributor, ConfigurationPropertyName name) {
    ConfigurationPropertySource source = contributor.getConfigurationPropertySource();
    ConfigurationProperty property = (source != null) ? source.getConfigurationProperty(name) : null;
    if (property != null) {
        PropertySource<?> propertySource = contributor.getPropertySource();
        ConfigDataResource location = contributor.getResource();
        throw new InactiveConfigDataAccessException(propertySource, location, name.toString(), property.getOrigin());
    }
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource)

Aggregations

ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)30 Test (org.junit.jupiter.api.Test)18 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)8 ArrayList (java.util.ArrayList)8 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)6 ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)6 Origin (cn.taketoday.origin.Origin)6 BindContext (cn.taketoday.context.properties.bind.BindContext)4 Bindable (cn.taketoday.context.properties.bind.Bindable)4 Binder (cn.taketoday.context.properties.bind.Binder)4 ConverterNotFoundException (cn.taketoday.core.conversion.ConverterNotFoundException)4 FieldError (cn.taketoday.validation.FieldError)4 ObjectError (cn.taketoday.validation.ObjectError)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Map (java.util.Map)4 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)2 AbstractBindHandler (cn.taketoday.context.properties.bind.AbstractBindHandler)2 BindException (cn.taketoday.context.properties.bind.BindException)2 BindHandler (cn.taketoday.context.properties.bind.BindHandler)2