Skip to content

Commit

Permalink
Ignore unresolvable placeholders during property binding
Browse files Browse the repository at this point in the history
Closes gh-13122
  • Loading branch information
wilkinsona committed Jun 4, 2018
1 parent 9c8d2c8 commit 0df37b9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,7 +51,7 @@ public PropertySourcesPlaceholdersResolver(Iterable<PropertySource<?>> sources,
this.helper = (helper != null ? helper
: new PropertyPlaceholderHelper(SystemPropertyUtils.PLACEHOLDER_PREFIX,
SystemPropertyUtils.PLACEHOLDER_SUFFIX,
SystemPropertyUtils.VALUE_SEPARATOR, false));
SystemPropertyUtils.VALUE_SEPARATOR, true));
}

@Override
Expand Down
Expand Up @@ -30,7 +30,6 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.matchers.ThrowableMessageMatcher;
import org.junit.rules.ExpectedException;
import org.mockito.Answers;
import org.mockito.InOrder;
Expand All @@ -53,7 +52,6 @@
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -153,15 +151,13 @@ public void bindToValueWithPlaceholdersShouldResolve() {
}

@Test
public void bindToValueWithMissingPlaceholdersShouldThrowException() {
public void bindToValueWithMissingPlaceholderShouldResolveToValueWithPlaceholder() {
StandardEnvironment environment = new StandardEnvironment();
this.sources.add(new MockConfigurationPropertySource("foo", "${bar}"));
this.binder = new Binder(this.sources,
new PropertySourcesPlaceholdersResolver(environment));
this.thrown.expect(BindException.class);
this.thrown.expectCause(ThrowableMessageMatcher.hasMessage(containsString(
"Could not resolve placeholder 'bar' in value \"${bar}\"")));
this.binder.bind("foo", Bindable.of(Integer.class));
BindResult<String> result = this.binder.bind("foo", Bindable.of(String.class));
assertThat(result.get()).isEqualTo("${bar}");
}

@Test
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -67,12 +67,10 @@ public void resolveIfPlaceholderAbsentUsesDefault() {
}

@Test
public void resolveIfPlaceholderAbsentAndNoDefaultShouldThrowException() {
public void resolveIfPlaceholderAbsentAndNoDefaultUsesPlaceholder() {
this.resolver = new PropertySourcesPlaceholdersResolver((PropertySources) null);
this.thrown.expect(IllegalArgumentException.class);
this.thrown
.expectMessage("Could not resolve placeholder 'FOO' in value \"${FOO}\"");
this.resolver.resolvePlaceholders("${FOO}");
Object resolved = this.resolver.resolvePlaceholders("${FOO}");
assertThat(resolved).isEqualTo("${FOO}");
}

@Test
Expand Down

0 comments on commit 0df37b9

Please sign in to comment.