Ensure @ContextConfiguration & @TestPropertySource locations are cleaned

This commit ensures that locations to resources configured via
@ContextConfiguration & @TestPropertySource are consistently cleaned
using StringUtils.clean().

See gh-23544
This commit is contained in:
Sam Brannen
2019-08-30 17:55:13 +02:00
parent ff1f8aa5a8
commit 22494ba231
2 changed files with 61 additions and 8 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.test.context.BootstrapTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.web.WebDelegatingSmartContextLoader;
import org.springframework.test.context.web.WebMergedContextConfiguration;
@@ -194,6 +195,29 @@ class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigurationUt
AnnotationConfigContextLoader.class);
}
@Test
void buildMergedConfigAndVerifyLocationPathsAreCleanedEquivalently() {
assertMergedConfigForLocationPaths(AbsoluteFooXmlLocationWithoutClasspathPrefix.class);
assertMergedConfigForLocationPaths(AbsoluteFooXmlLocationWithInnerRelativePathWithoutClasspathPrefix.class);
assertMergedConfigForLocationPaths(AbsoluteFooXmlLocationWithClasspathPrefix.class);
assertMergedConfigForLocationPaths(RelativeFooXmlLocation.class);
}
private void assertMergedConfigForLocationPaths(Class<?> testClass) {
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertThat(mergedConfig).isNotNull();
assertThat(mergedConfig.getTestClass()).isEqualTo(testClass);
assertThat(mergedConfig.getContextLoader()).isInstanceOf(DelegatingSmartContextLoader.class);
assertThat(mergedConfig.getLocations()).containsExactly("classpath:/example/foo.xml");
assertThat(mergedConfig.getPropertySourceLocations()).containsExactly("classpath:/example/foo.properties");
assertThat(mergedConfig.getClasses()).isEmpty();
assertThat(mergedConfig.getActiveProfiles()).isEmpty();
assertThat(mergedConfig.getContextInitializerClasses()).isEmpty();
assertThat(mergedConfig.getPropertySourceProperties()).isEmpty();
}
@ContextConfiguration
@Retention(RetentionPolicy.RUNTIME)
@@ -217,4 +241,25 @@ class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigurationUt
static class MissingContextAttributesTestCase {
}
@ContextConfiguration(locations = "/example/foo.xml")
@TestPropertySource("/example/foo.properties")
static class AbsoluteFooXmlLocationWithoutClasspathPrefix {
}
@ContextConfiguration(locations = "/example/../org/../example/foo.xml")
@TestPropertySource("/example/../org/../example/foo.properties")
static class AbsoluteFooXmlLocationWithInnerRelativePathWithoutClasspathPrefix {
}
@ContextConfiguration(locations = "classpath:/example/foo.xml")
@TestPropertySource("classpath:/example/foo.properties")
static class AbsoluteFooXmlLocationWithClasspathPrefix {
}
// org.springframework.test.context.support --> 5 levels up to the root of the classpath
@ContextConfiguration(locations = "../../../../../example/foo.xml")
@TestPropertySource("../../../../../example/foo.properties")
static class RelativeFooXmlLocation {
}
}