Browse Source

Register hints for @TestPropertySource factory

This commit updates MergedContextConfigurationRuntimeHints so that it
registers hints for a custom PropertySourceFactory.

Closes gh-31160
pull/31172/head
Sam Brannen 2 years ago
parent
commit
e8fd29091c
  1. 6
      spring-test/src/main/java/org/springframework/test/context/aot/MergedContextConfigurationRuntimeHints.java
  2. 13
      spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java
  3. 11
      spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests.java
  4. 23
      spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java
  5. 2
      spring-test/src/test/java/org/springframework/test/context/env/YamlPropertySourceFactory.java
  6. 2
      spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test1.yaml
  7. 2
      spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test2.yaml

6
spring-test/src/main/java/org/springframework/test/context/aot/MergedContextConfigurationRuntimeHints.java

@ -76,6 +76,12 @@ class MergedContextConfigurationRuntimeHints { @@ -76,6 +76,12 @@ class MergedContextConfigurationRuntimeHints {
for (PropertySourceDescriptor descriptor : mergedConfig.getPropertySourceDescriptors()) {
// @TestPropertySource(locations = ...)
registerClasspathResources(descriptor.locations().stream(), runtimeHints, classLoader);
// @TestPropertySource(factory = ...)
Class<?> factoryClass = descriptor.propertySourceFactory();
if (factoryClass != null) {
registerDeclaredConstructors(factoryClass, runtimeHints);
}
}
// @WebAppConfiguration(value = ...)

13
spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java

@ -55,6 +55,7 @@ import org.springframework.test.context.aot.samples.web.WebSpringVintageTests; @@ -55,6 +55,7 @@ import org.springframework.test.context.aot.samples.web.WebSpringVintageTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests;
import org.springframework.test.context.env.YamlPropertySourceFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.function.ThrowingConsumer;
import org.springframework.web.context.WebApplicationContext;
@ -213,8 +214,20 @@ class TestContextAotGeneratorTests extends AbstractAotTests { @@ -213,8 +214,20 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
// @TestPropertySource(locations = ...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
.as("@TestPropertySource(locations)")
.accepts(runtimeHints);
// @YamlTestProperties(...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test1.yaml"))
.as("@YamlTestProperties: test1.yaml")
.accepts(runtimeHints);
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test2.yaml"))
.as("@YamlTestProperties: test2.yaml")
.accepts(runtimeHints);
// @TestPropertySource(factory = ...)
assertReflectionRegistered(runtimeHints, YamlPropertySourceFactory.class.getName(), INVOKE_DECLARED_CONSTRUCTORS);
// @WebAppConfiguration(value = ...)
assertThat(resource().forResource("META-INF/web-resources/resources/Spring.js")).accepts(runtimeHints);
assertThat(resource().forResource("META-INF/web-resources/WEB-INF/views/home.jsp")).accepts(runtimeHints);

11
spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests.java

@ -22,6 +22,7 @@ import org.springframework.context.ApplicationContext; @@ -22,6 +22,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.aot.samples.management.ManagementConfiguration;
import org.springframework.test.context.env.YamlTestProperties;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@ -34,9 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -34,9 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Sam Brannen
* @since 6.0
* @see BasicSpringJupiterTests
*/
@SpringJUnitConfig({BasicTestConfiguration.class, ManagementConfiguration.class})
@TestPropertySource(properties = "test.engine = jupiter")
// We cannot use `classpath*:` in AOT tests until gh-31088 is resolved.
// @YamlTestProperties("classpath*:org/springframework/test/context/aot/samples/basic/**/test?.yaml")
@YamlTestProperties({
"classpath:org/springframework/test/context/aot/samples/basic/test1.yaml",
"classpath:org/springframework/test/context/aot/samples/basic/test2.yaml"
})
public class BasicSpringJupiterSharedConfigTests {
@Autowired
@ -52,8 +60,7 @@ public class BasicSpringJupiterSharedConfigTests { @@ -52,8 +60,7 @@ public class BasicSpringJupiterSharedConfigTests {
void test() {
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
assertThat(testEngine).isEqualTo("jupiter");
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("@TestPropertySource").isEqualTo("jupiter");
BasicSpringJupiterTests.assertEnvProperties(context);
}
}

23
spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java

@ -21,12 +21,14 @@ import org.junit.jupiter.api.Nested; @@ -21,12 +21,14 @@ import org.junit.jupiter.api.Nested;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests.DummyTestExecutionListener;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.aot.samples.management.ManagementConfiguration;
import org.springframework.test.context.env.YamlTestProperties;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AbstractTestExecutionListener;
@ -42,10 +44,17 @@ import static org.springframework.test.context.TestExecutionListeners.MergeMode. @@ -42,10 +44,17 @@ import static org.springframework.test.context.TestExecutionListeners.MergeMode.
*
* @author Sam Brannen
* @since 6.0
* @see BasicSpringJupiterSharedConfigTests
*/
@SpringJUnitConfig({BasicTestConfiguration.class, ManagementConfiguration.class})
@TestExecutionListeners(listeners = DummyTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
@TestPropertySource(properties = "test.engine = jupiter")
// We cannot use `classpath*:` in AOT tests until gh-31088 is resolved.
// @YamlTestProperties("classpath*:org/springframework/test/context/aot/samples/basic/**/test?.yaml")
@YamlTestProperties({
"classpath:org/springframework/test/context/aot/samples/basic/test1.yaml",
"classpath:org/springframework/test/context/aot/samples/basic/test2.yaml"
})
public class BasicSpringJupiterTests {
@org.junit.jupiter.api.Test
@ -53,8 +62,7 @@ public class BasicSpringJupiterTests { @@ -53,8 +62,7 @@ public class BasicSpringJupiterTests {
@Value("${test.engine}") String testEngine) {
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
assertThat(testEngine).isEqualTo("jupiter");
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("@TestPropertySource").isEqualTo("jupiter");
assertEnvProperties(context);
}
@Nested
@ -68,12 +76,19 @@ public class BasicSpringJupiterTests { @@ -68,12 +76,19 @@ public class BasicSpringJupiterTests {
assertThat(messageService.generateMessage()).isEqualTo("¡Hola, AOT!");
assertThat(foo).isEqualTo("bar");
assertThat(testEngine).isEqualTo("jupiter");
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("@TestPropertySource").isEqualTo("jupiter");
assertEnvProperties(context);
}
}
static void assertEnvProperties(ApplicationContext context) {
Environment env = context.getEnvironment();
assertThat(env.getProperty("test.engine")).as("@TestPropertySource").isEqualTo("jupiter");
assertThat(env.getProperty("test1.prop")).as("@TestPropertySource").isEqualTo("yaml");
assertThat(env.getProperty("test2.prop")).as("@TestPropertySource").isEqualTo("yaml");
}
public static class DummyTestExecutionListener extends AbstractTestExecutionListener {
}

2
spring-test/src/test/java/org/springframework/test/context/env/YamlPropertySourceFactory.java vendored

@ -32,7 +32,7 @@ import org.springframework.util.StringUtils; @@ -32,7 +32,7 @@ import org.springframework.util.StringUtils;
* @author Sam Brannen
* @since 6.1
*/
class YamlPropertySourceFactory implements PropertySourceFactory {
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {

2
spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test1.yaml

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
test1:
prop: yaml

2
spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test2.yaml

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
test2:
prop: yaml
Loading…
Cancel
Save