Browse Source

Remove deprecated GenericPropertiesContextLoader from the TCF

Since GenericPropertiesContextLoader was deprecated in Spring Framework
5.3, we have decided to remove it in Spring Framework 6.0.

Closes gh-28911
pull/28912/head
Sam Brannen 2 years ago
parent
commit
9c91375d75
  1. 74
      spring-test/src/main/java/org/springframework/test/context/support/GenericPropertiesContextLoader.java
  2. 60
      spring-test/src/test/java/org/springframework/test/context/configuration/ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests.java
  3. 61
      spring-test/src/test/java/org/springframework/test/context/configuration/ContextConfigurationWithPropertiesExtendingPropertiesTests.java
  4. 70
      spring-test/src/test/java/org/springframework/test/context/junit4/CustomDefaultContextLoaderClassSpringRunnerTests.java
  5. 77
      spring-test/src/test/java/org/springframework/test/context/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests.java
  6. 4
      spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4TestSuite.java
  7. 14
      spring-test/src/test/java/org/springframework/test/context/support/AbstractContextConfigurationUtilsTests.java
  8. 23
      spring-test/src/test/java/org/springframework/test/context/support/BootstrapTestUtilsMergedConfigTests.java
  9. 47
      spring-test/src/test/java/org/springframework/test/context/support/GenericPropertiesContextLoaderTests.java
  10. 5
      spring-test/src/test/resources/org/springframework/test/context/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties

74
spring-test/src/main/java/org/springframework/test/context/support/GenericPropertiesContextLoader.java

@ -1,74 +0,0 @@ @@ -1,74 +0,0 @@
/*
* Copyright 2002-2020 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.support;
import java.util.Properties;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.util.ObjectUtils;
/**
* Concrete implementation of {@link AbstractGenericContextLoader} that reads
* bean definitions from Java {@link Properties} resources.
*
* @author Sam Brannen
* @since 2.5
* @deprecated as of 5.3, in favor of Spring's common bean definition formats
* and/or custom loader implementations
*/
@Deprecated
public class GenericPropertiesContextLoader extends AbstractGenericContextLoader {
/**
* Creates a new {@link org.springframework.beans.factory.support.PropertiesBeanDefinitionReader}.
* @return a new PropertiesBeanDefinitionReader
* @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
*/
@Override
protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) {
return new org.springframework.beans.factory.support.PropertiesBeanDefinitionReader(context);
}
/**
* Returns "{@code -context.properties}".
*/
@Override
protected String getResourceSuffix() {
return "-context.properties";
}
/**
* Ensure that the supplied {@link MergedContextConfiguration} does not
* contain {@link MergedContextConfiguration#getClasses() classes}.
* @since 4.0.4
* @see AbstractGenericContextLoader#validateMergedContextConfiguration
*/
@Override
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
if (mergedConfig.hasClasses()) {
String msg = String.format(
"Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, "
+ "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(),
ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName());
logger.error(msg);
throw new IllegalStateException(msg);
}
}
}

60
spring-test/src/test/java/org/springframework/test/context/configuration/ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests.java

@ -1,60 +0,0 @@ @@ -1,60 +0,0 @@
/*
* 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.configuration;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.junit4.PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests which verify that the same custom {@link ContextLoader} can
* be used at all levels within a test class hierarchy when the
* {@code loader} is <i>inherited</i> (i.e., not explicitly declared) via
* {@link ContextConfiguration &#064;ContextConfiguration}.
*
* @author Sam Brannen
* @since 3.0
* @see PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests
* @see ContextConfigurationWithPropertiesExtendingPropertiesTests
*/
@ContextConfiguration
public class ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests extends
PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests {
@Autowired
private Pet dog;
@Autowired
private String testString2;
@Test
public void verifyExtendedAnnotationAutowiredFields() {
assertThat(this.dog).as("The dog field should have been autowired.").isNotNull();
assertThat(this.dog.getName()).isEqualTo("Fido");
assertThat(this.testString2).as("The testString2 field should have been autowired.").isNotNull();
assertThat(this.testString2).isEqualTo("Test String #2");
}
}

61
spring-test/src/test/java/org/springframework/test/context/configuration/ContextConfigurationWithPropertiesExtendingPropertiesTests.java

@ -1,61 +0,0 @@ @@ -1,61 +0,0 @@
/*
* Copyright 2002-2020 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.configuration;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.junit4.PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests which verify that the same custom {@link ContextLoader} can
* be used at all levels within a test class hierarchy when the
* {@code loader} is explicitly declared via {@link ContextConfiguration
* &#064;ContextConfiguration}.
*
* @author Sam Brannen
* @since 3.0
* @see PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests
* @see ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests
*/
@SuppressWarnings("deprecation")
@ContextConfiguration(loader = org.springframework.test.context.support.GenericPropertiesContextLoader.class)
public class ContextConfigurationWithPropertiesExtendingPropertiesTests extends
PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests {
@Autowired
private Pet dog;
@Autowired
private String testString2;
@Test
public void verifyExtendedAnnotationAutowiredFields() {
assertThat(this.dog).as("The dog field should have been autowired.").isNotNull();
assertThat(this.dog.getName()).isEqualTo("Fido");
assertThat(this.testString2).as("The testString2 field should have been autowired.").isNotNull();
assertThat(this.testString2).isEqualTo("Test String #2");
}
}

70
spring-test/src/test/java/org/springframework/test/context/junit4/CustomDefaultContextLoaderClassSpringRunnerTests.java

@ -1,70 +0,0 @@ @@ -1,70 +0,0 @@
/*
* Copyright 2002-2020 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.junit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests which verify that a subclass of {@link DefaultTestContextBootstrapper}
* can specify a custom <em>default ContextLoader class</em> that overrides the standard
* default class name.
*
* @author Sam Brannen
* @since 3.0
*/
@RunWith(SpringRunner.class)
@BootstrapWith(CustomDefaultContextLoaderClassSpringRunnerTests.PropertiesBasedTestContextBootstrapper.class)
@ContextConfiguration("PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties")
public class CustomDefaultContextLoaderClassSpringRunnerTests {
@Autowired
private Pet cat;
@Autowired
private String testString;
@Test
public void verifyAnnotationAutowiredFields() {
assertThat(this.cat).as("The cat field should have been autowired.").isNotNull();
assertThat(this.cat.getName()).isEqualTo("Garfield");
assertThat(this.testString).as("The testString field should have been autowired.").isNotNull();
assertThat(this.testString).isEqualTo("Test String");
}
public static class PropertiesBasedTestContextBootstrapper extends DefaultTestContextBootstrapper {
@Override
@SuppressWarnings("deprecation")
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
return org.springframework.test.context.support.GenericPropertiesContextLoader.class;
}
}
}

77
spring-test/src/test/java/org/springframework/test/context/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests.java

@ -1,77 +0,0 @@ @@ -1,77 +0,0 @@
/*
* 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.junit4;
import java.util.Properties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* <p>
* JUnit 4 based test class, which verifies the expected functionality of
* {@link SpringRunner} in conjunction with support for application contexts
* loaded from Java {@link Properties} files. Specifically, the
* {@link ContextConfiguration#loader() loader} attribute of {@code ContextConfiguration}
* and the
* {@link org.springframework.test.context.support.GenericPropertiesContextLoader#getResourceSuffix()
* resourceSuffix} property of {@code GenericPropertiesContextLoader} are tested.
* </p>
* <p>
* Since no {@link ContextConfiguration#locations() locations} are explicitly defined, the
* {@code resourceSuffix} is set to &quot;-context.properties&quot;, and since default
* resource locations will be detected by default, this test class's dependencies will be
* injected via {@link Autowired annotation-based autowiring} from beans defined in the
* {@link ApplicationContext} loaded from the default classpath resource: &quot;
* {@code /org/springframework/test/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties}
* &quot;.
* </p>
*
* @author Sam Brannen
* @since 2.5
* @see org.springframework.test.context.support.GenericPropertiesContextLoader
* @see SpringJUnit4ClassRunnerAppCtxTests
*/
@RunWith(SpringRunner.class)
@SuppressWarnings("deprecation")
@ContextConfiguration(loader = org.springframework.test.context.support.GenericPropertiesContextLoader.class)
public class PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests {
@Autowired
private Pet cat;
@Autowired
private String testString;
@Test
public void verifyAnnotationAutowiredFields() {
assertThat(this.cat).as("The cat field should have been autowired.").isNotNull();
assertThat(this.cat.getName()).isEqualTo("Garfield");
assertThat(this.testString).as("The testString field should have been autowired.").isNotNull();
assertThat(this.testString).isEqualTo("Test String");
}
}

4
spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4TestSuite.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -93,8 +93,6 @@ StandardJUnit4FeaturesTests.class,// @@ -93,8 +93,6 @@ StandardJUnit4FeaturesTests.class,//
RelativePathSpringJUnit4ClassRunnerAppCtxTests.class,//
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.class,//
InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests.class,//
CustomDefaultContextLoaderClassSpringRunnerTests.class,//
ParameterizedDependencyInjectionTests.class,//
ConcreteTransactionalJUnit4SpringContextTests.class,//
ClassLevelTransactionalSpringRunnerTests.class,//

14
spring-test/src/test/java/org/springframework/test/context/support/AbstractContextConfigurationUtilsTests.java

@ -223,20 +223,6 @@ abstract class AbstractContextConfigurationUtilsTests { @@ -223,20 +223,6 @@ abstract class AbstractContextConfigurationUtilsTests {
static class OverriddenClassesBar extends ClassesFoo {
}
@SuppressWarnings("deprecation")
@ContextConfiguration(locations = "/foo.properties", loader = org.springframework.test.context.support.GenericPropertiesContextLoader.class)
@ActiveProfiles("foo")
static class PropertiesLocationsFoo {
}
// Combining @Configuration classes with a Properties based loader doesn't really make
// sense, but that's OK for unit testing purposes.
@SuppressWarnings("deprecation")
@ContextConfiguration(classes = FooConfig.class, loader = org.springframework.test.context.support.GenericPropertiesContextLoader.class)
@ActiveProfiles("foo")
static class PropertiesClassesFoo {
}
@ContextConfiguration(classes = FooConfig.class, loader = AnnotationConfigContextLoader.class)
@NestedTestConfiguration(INHERIT)
static class OuterTestCase {

23
spring-test/src/test/java/org/springframework/test/context/support/BootstrapTestUtilsMergedConfigTests.java

@ -29,7 +29,6 @@ import org.springframework.core.annotation.AliasFor; @@ -29,7 +29,6 @@ import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.ActiveProfiles;
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.support.BootstrapTestUtilsMergedConfigTests.EmptyConfigTestCase.Nested;
@ -140,28 +139,6 @@ class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigurationUt @@ -140,28 +139,6 @@ class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigurationUt
array(FooConfig.class), DelegatingSmartContextLoader.class);
}
@Test
@SuppressWarnings("deprecation")
void buildMergedConfigWithLocalAnnotationAndOverriddenContextLoaderAndLocations() {
Class<?> testClass = PropertiesLocationsFoo.class;
Class<? extends ContextLoader> expectedContextLoaderClass = org.springframework.test.context.support.GenericPropertiesContextLoader.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, array("classpath:/foo.properties"), EMPTY_CLASS_ARRAY,
expectedContextLoaderClass);
}
@Test
@SuppressWarnings("deprecation")
void buildMergedConfigWithLocalAnnotationAndOverriddenContextLoaderAndClasses() {
Class<?> testClass = PropertiesClassesFoo.class;
Class<? extends ContextLoader> expectedContextLoaderClass = org.springframework.test.context.support.GenericPropertiesContextLoader.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, array(FooConfig.class),
expectedContextLoaderClass);
}
@Test
void buildMergedConfigWithLocalAndInheritedAnnotationsAndLocations() {
Class<?> testClass = LocationsBar.class;

47
spring-test/src/test/java/org/springframework/test/context/support/GenericPropertiesContextLoaderTests.java

@ -1,47 +0,0 @@ @@ -1,47 +0,0 @@
/*
* Copyright 2002-2020 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.support;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.MergedContextConfiguration;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Unit tests for {@link GenericPropertiesContextLoader}.
*
* @author Sam Brannen
* @since 4.0.4
*/
@SuppressWarnings("deprecation")
class GenericPropertiesContextLoaderTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
@Test
void configMustNotContainAnnotatedClasses() throws Exception {
GenericPropertiesContextLoader loader = new GenericPropertiesContextLoader();
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
assertThatIllegalStateException()
.isThrownBy(() -> loader.loadContext(mergedConfig))
.withMessageContaining("does not support annotated classes");
}
}

5
spring-test/src/test/resources/org/springframework/test/context/junit4/PropertiesBasedSpringJUnit4ClassRunnerAppCtxTests-context.properties

@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
cat.(class)=org.springframework.beans.testfixture.beans.Pet
cat.$0=Garfield
testString.(class)=java.lang.String
testString.$0=Test String
Loading…
Cancel
Save