Use new implementation in PropertyPlaceholderHelper

This commit removes the previous implementation in favor of the new
PlaceholderParser. The only noticeable side effect is that the exception
is no longer an IllegalArgumentException, but rather the dedicated
PlaceholderResolutionException.

See gh-9628
This commit is contained in:
Stéphane Nicoll
2023-12-29 17:41:26 +01:00
parent 00e05e603d
commit e3aa5b6b11
24 changed files with 156 additions and 188 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 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.
@@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.testfixture.env.MockPropertySource;
import org.springframework.util.PlaceholderResolutionException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -227,7 +228,7 @@ class PropertySourcesPropertyResolverTests {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"));
}
@@ -290,11 +291,11 @@ class PropertySourcesPropertyResolverTests {
assertThat(pr.getProperty("p2")).isEqualTo("v2");
assertThat(pr.getProperty("p3")).isEqualTo("v1:v2");
assertThat(pr.getProperty("p4")).isEqualTo("v1:v2");
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
pr.getProperty("p5"))
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
assertThat(pr.getProperty("p6")).isEqualTo("v1:v2:def");
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
pr.getProperty("pL"))
.withMessageContaining("Circular");
}
@@ -315,7 +316,7 @@ class PropertySourcesPropertyResolverTests {
// placeholders nested within the value of "p4" are unresolvable and cause an
// exception by default
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
pr.getProperty("p4"))
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
@@ -327,7 +328,7 @@ class PropertySourcesPropertyResolverTests {
// resolve[Nested]Placeholders methods behave as usual regardless the value of
// ignoreUnresolvableNestedPlaceholders
assertThat(pr.resolvePlaceholders("${p1}:${p2}:${bogus}")).isEqualTo("v1:v2:${bogus}");
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
pr.resolveRequiredPlaceholders("${p1}:${p2}:${bogus}"))
.withMessageContaining("Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\"");
}

View File

@@ -23,8 +23,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.SpringProperties;
import org.springframework.core.testfixture.env.MockPropertySource;
import org.springframework.util.PlaceholderResolutionException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.springframework.core.env.AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME;
import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;
@@ -207,9 +209,9 @@ class StandardEnvironmentTests {
void defaultProfileWithCircularPlaceholder() {
try {
System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "${spring.profiles.default}");
assertThatIllegalArgumentException()
assertThatExceptionOfType(PlaceholderResolutionException.class)
.isThrownBy(environment::getDefaultProfiles)
.withMessage("Circular placeholder reference 'spring.profiles.default' in property definitions");
.withMessageContaining("Circular placeholder reference 'spring.profiles.default'");
}
finally {
System.clearProperty(DEFAULT_PROFILES_PROPERTY_NAME);

View File

@@ -21,8 +21,10 @@ import java.beans.PropertyEditor;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.PlaceholderResolutionException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
@@ -96,7 +98,7 @@ class ResourceEditorTests {
PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), new StandardEnvironment(), false);
System.setProperty("test.prop", "foo");
try {
assertThatIllegalArgumentException().isThrownBy(() -> {
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() -> {
editor.setAsText("${test.prop}-${bar}");
editor.getValue();
});

View File

@@ -32,10 +32,12 @@ import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
import org.springframework.util.PlaceholderResolutionException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link PropertySourceProcessor}.
@@ -73,8 +75,8 @@ class PropertySourceProcessorTests {
class FailOnErrorTests {
@Test
void processorFailsOnIllegalArgumentException() {
assertProcessorFailsOnError(IllegalArgumentExceptionPropertySourceFactory.class, IllegalArgumentException.class);
void processorFailsOnPlaceholderResolutionException() {
assertProcessorFailsOnError(PlaceholderResolutionExceptionPropertySourceFactory.class, PlaceholderResolutionException.class);
}
@Test
@@ -98,7 +100,7 @@ class PropertySourceProcessorTests {
@Test
void processorIgnoresIllegalArgumentException() {
assertProcessorIgnoresFailure(IllegalArgumentExceptionPropertySourceFactory.class);
assertProcessorIgnoresFailure(PlaceholderResolutionExceptionPropertySourceFactory.class);
}
@Test
@@ -134,11 +136,11 @@ class PropertySourceProcessorTests {
}
private static class IllegalArgumentExceptionPropertySourceFactory implements PropertySourceFactory {
private static class PlaceholderResolutionExceptionPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
throw new IllegalArgumentException("bogus");
throw mock(PlaceholderResolutionException.class);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -24,9 +24,10 @@ import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;
import org.springframework.util.PlaceholderResolutionException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link ResourceArrayPropertyEditor}.
@@ -81,7 +82,7 @@ class ResourceArrayPropertyEditorTests {
false);
System.setProperty("test.prop", "foo");
try {
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
editor.setAsText("${test.prop}-${bar}"));
}
finally {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -19,7 +19,6 @@ package org.springframework.util;
import java.util.Properties;
import java.util.stream.Stream;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -29,11 +28,11 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Tests for {@link PropertyPlaceholderHelper}.
@@ -116,16 +115,15 @@ class PropertyPlaceholderHelperTests {
Properties props = new Properties();
props.setProperty("foo", "bar");
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false);
assertThatIllegalArgumentException().isThrownBy(() ->
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", null, false, null);
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
helper.replacePlaceholders(text, props));
}
@Nested
class DefaultValueTests {
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true);
private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true, null);
@ParameterizedTest(name = "{0} -> {1}")
@MethodSource("defaultValues")
@@ -137,12 +135,11 @@ class PropertyPlaceholderHelperTests {
}
@Test
@Disabled("gh-26268")
void defaultValueIsNotEvaluatedEarly() {
PlaceholderResolver resolver = mockPlaceholderResolver("one", "1");
assertThat(this.helper.replacePlaceholders("This is ${one:or${two}}",resolver)).isEqualTo("This is 1");
assertThat(this.helper.replacePlaceholders("This is ${one:or${two}}", resolver)).isEqualTo("This is 1");
verify(resolver).resolvePlaceholder("one");
verifyNoMoreInteractions(resolver);
verify(resolver, never()).resolvePlaceholder("two");
}
static Stream<Arguments> defaultValues() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -21,7 +21,7 @@ import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Rob Harrop
@@ -97,7 +97,7 @@ class SystemPropertyUtilsTests {
@Test
void replaceWithNoDefault() {
assertThatIllegalArgumentException().isThrownBy(() ->
assertThatExceptionOfType(PlaceholderResolutionException.class).isThrownBy(() ->
SystemPropertyUtils.resolvePlaceholders("${test.prop}"));
}