diff --git a/spring-test/src/test/java/org/springframework/test/context/env/AbstractExplicitPropertiesFileTests.java b/spring-test/src/test/java/org/springframework/test/context/env/AbstractExplicitPropertiesFileTests.java new file mode 100644 index 0000000000..9b8a7ca3a6 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/AbstractExplicitPropertiesFileTests.java @@ -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 */ + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileInClasspathTestPropertySourceTests.java b/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileInClasspathTestPropertySourceTests.java new file mode 100644 index 0000000000..c2d8bcfce3 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileInClasspathTestPropertySourceTests.java @@ -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 { +} diff --git a/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileTestPropertySourceTests.java b/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileTestPropertySourceTests.java index 31da089a47..07e2cd2cba 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileTestPropertySourceTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/ExplicitPropertiesFileTestPropertySourceTests.java @@ -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 { + } + } } diff --git a/spring-test/src/test/java/org/springframework/test/context/env/InheritedRelativePathPropertiesFileTestPropertySourceTests.java b/spring-test/src/test/java/org/springframework/test/context/env/InheritedRelativePathPropertiesFileTestPropertySourceTests.java index 61ee2c5701..b0c9593453 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/InheritedRelativePathPropertiesFileTestPropertySourceTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/InheritedRelativePathPropertiesFileTestPropertySourceTests.java @@ -27,7 +27,7 @@ import org.springframework.test.context.TestPropertySource; * @since 4.1 */ class InheritedRelativePathPropertiesFileTestPropertySourceTests extends - ExplicitPropertiesFileTestPropertySourceTests { + ExplicitPropertiesFileInClasspathTestPropertySourceTests { /* all tests are in superclass */ diff --git a/spring-test/src/test/java/org/springframework/test/context/env/MergedPropertiesFilesTestPropertySourceTests.java b/spring-test/src/test/java/org/springframework/test/context/env/MergedPropertiesFilesTestPropertySourceTests.java index d6b5e6b4c7..2c953926cb 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/MergedPropertiesFilesTestPropertySourceTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/MergedPropertiesFilesTestPropertySourceTests.java @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ @TestPropertySource("extended.properties") class MergedPropertiesFilesTestPropertySourceTests extends - ExplicitPropertiesFileTestPropertySourceTests { + ExplicitPropertiesFileInClasspathTestPropertySourceTests { @Test void verifyExtendedPropertiesAreAvailableInEnvironment() { diff --git a/spring-test/src/test/java/org/springframework/test/context/env/subpackage/SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests.java b/spring-test/src/test/java/org/springframework/test/context/env/subpackage/SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests.java index 64d32df8bb..687f348987 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/subpackage/SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/subpackage/SubpackageInheritedRelativePathPropertiesFileTestPropertySourceTests.java @@ -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 */