Test status quo for relative paths & placeholders in @TestPropertySource

This commit introduces a collection of @Nested integration tests that
verify proper support for @TestPropertySource with explicit properties
files declared with absolute paths, relative paths, and placeholders in
the classpath and in the file system.

See gh-23544
This commit is contained in:
Sam Brannen 2019-08-30 15:51:05 +02:00
parent 7738e778fa
commit 1ea6ce72bb
6 changed files with 200 additions and 30 deletions

View File

@ -0,0 +1,54 @@
/*
* Copyright 2002-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.env;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sam Brannen
* @since 5.2
*/
@SpringJUnitConfig
abstract class AbstractExplicitPropertiesFileTests {
@Autowired
Environment env;
@Test
@DisplayName("verify properties are available in the Environment")
void verifyPropertiesAreAvailableInEnvironment() {
String userHomeKey = "user.home";
assertThat(env.getProperty(userHomeKey)).isEqualTo(System.getProperty(userHomeKey));
assertThat(env.getProperty("explicit")).isEqualTo("enigma");
}
@Configuration
static class Config {
/* no user beans required for these tests */
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright 2002-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.env;
import org.springframework.test.context.TestPropertySource;
/**
* Integration tests for {@link TestPropertySource @TestPropertySource}
* support with an explicitly named properties file in the classpath.
*
* @author Sam Brannen
* @since 4.1
*/
@TestPropertySource("explicit.properties")
public class ExplicitPropertiesFileInClasspathTestPropertySourceTests extends AbstractExplicitPropertiesFileTests {
}

View File

@ -16,45 +16,131 @@
package org.springframework.test.context.env;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import org.springframework.util.ClassUtils;
/**
* Integration tests for {@link TestPropertySource @TestPropertySource}
* support with an explicitly named properties file.
* Collection of integration tests that verify proper support for
* {@link TestPropertySource @TestPropertySource} with explicit properties
* files declared with absolute paths, relative paths, and placeholders in the
* classpath and in the file system.
*
* @author Sam Brannen
* @since 4.1
* @since 5.2
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@TestPropertySource("explicit.properties")
public class ExplicitPropertiesFileTestPropertySourceTests {
@DisplayName("Explicit properties file in @TestPropertySource")
class ExplicitPropertiesFileTestPropertySourceTests {
@Autowired
protected Environment env;
static final String CURRENT_TEST_PACKAGE = "current.test.package";
@Test
void verifyPropertiesAreAvailableInEnvironment() {
String userHomeKey = "user.home";
assertThat(env.getProperty(userHomeKey)).isEqualTo(System.getProperty(userHomeKey));
assertThat(env.getProperty("explicit")).isEqualTo("enigma");
@BeforeAll
static void setSystemProperty() {
String path = ClassUtils.classPackageAsResourcePath(ExplicitPropertiesFileTestPropertySourceTests.class);
System.setProperty(CURRENT_TEST_PACKAGE, path);
}
@AfterAll
static void clearSystemProperty() {
System.clearProperty(CURRENT_TEST_PACKAGE);
}
@Configuration
static class Config {
/* no user beans required for these tests */
@Nested
@DisplayName("in classpath")
class ClasspathTests {
@Nested
@DisplayName("with absolute path")
@TestPropertySource("/org/springframework/test/context/env/explicit.properties")
class AbsolutePathPathTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with absolute path with internal relative paths")
@TestPropertySource("/org/../org/springframework/test/../test/context/env/explicit.properties")
class AbsolutePathWithInternalRelativePathTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with relative path")
@TestPropertySource("explicit.properties")
class RelativePathTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with relative paths")
@TestPropertySource("../env/../env/../../context/env/explicit.properties")
class RelativePathsTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with placeholder")
@TestPropertySource("/${current.test.package}/explicit.properties")
class WithPlaceholderTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with placeholder and classpath: prefix")
@TestPropertySource("classpath:${current.test.package}/explicit.properties")
class WithPlaceholderAndClasspathPrefixTests extends AbstractExplicitPropertiesFileTests {
}
}
@Nested
@DisplayName("in file system")
class FileSystemTests {
@Nested
@DisplayName("with full local path")
@TestPropertySource("file:src/test/resources/org/springframework/test/context/env/explicit.properties")
class FullLocalPathTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with dot-path reference")
@TestPropertySource("file:./src/test/resources/org/springframework/test/context/env/explicit.properties")
class WithDotPathTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with relative path")
@TestPropertySource("file:../spring-test/src/test/resources/org/springframework/test/context/env/explicit.properties")
class WithRelativePathTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with ${current.test.package} placeholder")
@TestPropertySource("file:src/test/resources/${current.test.package}/explicit.properties")
class WithCustomPlaceholderTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with ${user.dir} placeholder")
@TestPropertySource("file:${user.dir}/src/test/resources/org/springframework/test/context/env/explicit.properties")
class WithUserDirPlaceholderTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with ${user.dir} and ${current.test.package} placeholders")
@TestPropertySource("file:${user.dir}/src/test/resources/${current.test.package}/explicit.properties")
class WithUserDirAndCustomPlaceholdersTests extends AbstractExplicitPropertiesFileTests {
}
@Nested
@DisplayName("with placeholder followed immediately by relative path")
@TestPropertySource("file:${user.dir}/../spring-test/src/test/resources/org/springframework/test/context/env/explicit.properties")
@Disabled("Currently not supported (see https://github.com/spring-projects/spring-framework/issues/23544)")
class WithPlaceholderFollowedByRelativePathTests extends AbstractExplicitPropertiesFileTests {
}
}
}

View File

@ -27,7 +27,7 @@ import org.springframework.test.context.TestPropertySource;
* @since 4.1
*/
class InheritedRelativePathPropertiesFileTestPropertySourceTests extends
ExplicitPropertiesFileTestPropertySourceTests {
ExplicitPropertiesFileInClasspathTestPropertySourceTests {
/* all tests are in superclass */

View File

@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@TestPropertySource("extended.properties")
class MergedPropertiesFilesTestPropertySourceTests extends
ExplicitPropertiesFileTestPropertySourceTests {
ExplicitPropertiesFileInClasspathTestPropertySourceTests {
@Test
void verifyExtendedPropertiesAreAvailableInEnvironment() {

View File

@ -17,7 +17,7 @@
package org.springframework.test.context.env.subpackage;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.env.ExplicitPropertiesFileTestPropertySourceTests;
import org.springframework.test.context.env.ExplicitPropertiesFileInClasspathTestPropertySourceTests;
/**
* Integration tests for {@link TestPropertySource @TestPropertySource}
@ -28,7 +28,7 @@ import org.springframework.test.context.env.ExplicitPropertiesFileTestPropertySo
* @since 4.1
*/
class SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests extends
ExplicitPropertiesFileTestPropertySourceTests {
ExplicitPropertiesFileInClasspathTestPropertySourceTests {
/* all tests are in superclass */