Search in sources :

Example 1 with Origin

use of org.springframework.boot.origin.Origin in project spring-boot by spring-projects.

the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenImportingIncludesParentOrigin.

@Test
void runWhenImportingIncludesParentOrigin() {
    ConfigurableApplicationContext context = this.application.run("--spring.config.location=classpath:application-import-with-placeholder.properties");
    Binder binder = Binder.get(context.getEnvironment());
    List<ConfigurationProperty> properties = new ArrayList<>();
    BindHandler bindHandler = new BindHandler() {

        @Override
        public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
            properties.add(context.getConfigurationProperty());
            return result;
        }
    };
    binder.bind("my.value", Bindable.of(String.class), bindHandler);
    assertThat(properties).hasSize(1);
    Origin origin = properties.get(0).getOrigin();
    assertThat(origin.toString()).contains("application-import-with-placeholder-imported");
    assertThat(origin.getParent().toString()).contains("application-import-with-placeholder");
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) BindContext(org.springframework.boot.context.properties.bind.BindContext) Origin(org.springframework.boot.origin.Origin) Binder(org.springframework.boot.context.properties.bind.Binder) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) ArrayList(java.util.ArrayList) Bindable(org.springframework.boot.context.properties.bind.Bindable) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) Test(org.junit.jupiter.api.Test)

Example 2 with Origin

use of org.springframework.boot.origin.Origin in project spring-boot by spring-projects.

the class ConfigDataLocationTests method getOriginWhenWithOriginReturnsOrigin.

@Test
void getOriginWhenWithOriginReturnsOrigin() {
    Origin origin = mock(Origin.class);
    ConfigDataLocation location = ConfigDataLocation.of("test").withOrigin(origin);
    assertThat(location.getOrigin()).isSameAs(origin);
}
Also used : Origin(org.springframework.boot.origin.Origin) Test(org.junit.jupiter.api.Test)

Example 3 with Origin

use of org.springframework.boot.origin.Origin in project spring-boot by spring-projects.

the class ConfigDataEnvironmentContributorPlaceholdersResolver method resolvePlaceholder.

private String resolvePlaceholder(String placeholder) {
    Object result = null;
    for (ConfigDataEnvironmentContributor contributor : this.contributors) {
        PropertySource<?> propertySource = contributor.getPropertySource();
        Object value = (propertySource != null) ? propertySource.getProperty(placeholder) : null;
        if (value != null && !isActive(contributor)) {
            if (this.failOnResolveFromInactiveContributor) {
                ConfigDataResource resource = contributor.getResource();
                Origin origin = OriginLookup.getOrigin(propertySource, placeholder);
                throw new InactiveConfigDataAccessException(propertySource, resource, placeholder, origin);
            }
            value = null;
        }
        result = (result != null) ? result : value;
    }
    return (result != null) ? String.valueOf(result) : null;
}
Also used : Origin(org.springframework.boot.origin.Origin)

Example 4 with Origin

use of org.springframework.boot.origin.Origin in project spring-boot by spring-projects.

the class ConfigDataNotFoundFailureAnalyzer method analyze.

@Override
protected FailureAnalysis analyze(Throwable rootFailure, ConfigDataNotFoundException cause) {
    ConfigDataLocation location = getLocation(cause);
    Origin origin = Origin.from(location);
    String message = String.format("Config data %s does not exist", cause.getReferenceDescription());
    StringBuilder action = new StringBuilder("Check that the value ");
    if (location != null) {
        action.append(String.format("'%s' ", location));
    }
    if (origin != null) {
        action.append(String.format("at %s ", origin));
    }
    action.append("is correct");
    if (location != null && !location.isOptional()) {
        action.append(String.format(", or prefix it with '%s'", ConfigDataLocation.OPTIONAL_PREFIX));
    }
    return new FailureAnalysis(message, action.toString(), cause);
}
Also used : Origin(org.springframework.boot.origin.Origin) FailureAnalysis(org.springframework.boot.diagnostics.FailureAnalysis)

Example 5 with Origin

use of org.springframework.boot.origin.Origin in project spring-boot by spring-projects.

the class BindValidationFailureAnalyzer method appendFieldError.

private void appendFieldError(StringBuilder description, FieldError error) {
    Origin origin = Origin.from(error);
    description.append(String.format("%n    Property: %s", error.getObjectName() + "." + error.getField()));
    description.append(String.format("%n    Value: %s", error.getRejectedValue()));
    if (origin != null) {
        description.append(String.format("%n    Origin: %s", origin));
    }
}
Also used : Origin(org.springframework.boot.origin.Origin)

Aggregations

Origin (org.springframework.boot.origin.Origin)14 Test (org.junit.jupiter.api.Test)7 ArrayList (java.util.ArrayList)2 ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)2 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)2 OriginProvider (org.springframework.boot.origin.OriginProvider)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 BindContext (org.springframework.boot.context.properties.bind.BindContext)1 BindHandler (org.springframework.boot.context.properties.bind.BindHandler)1 Bindable (org.springframework.boot.context.properties.bind.Bindable)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 FailureAnalysis (org.springframework.boot.diagnostics.FailureAnalysis)1 MockOrigin (org.springframework.boot.origin.MockOrigin)1 OriginLookup (org.springframework.boot.origin.OriginLookup)1 TextResourceOrigin (org.springframework.boot.origin.TextResourceOrigin)1 Location (org.springframework.boot.origin.TextResourceOrigin.Location)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)1 InputStreamSource (org.springframework.core.io.InputStreamSource)1