Browse Source

Add reflection hints for JsonTesters

Closes gh-32858
pull/33098/head
Andy Wilkinson 3 years ago
parent
commit
fcbc7dacf1
  1. 1
      spring-boot-project/spring-boot-test-autoconfigure/build.gradle
  2. 66
      spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java
  3. 108
      spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfigurationTests.java

1
spring-boot-project/spring-boot-test-autoconfigure/build.gradle

@ -94,6 +94,7 @@ dependencies { @@ -94,6 +94,7 @@ dependencies {
testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter")
testImplementation("org.skyscreamer:jsonassert")
testImplementation("org.springframework:spring-core-test")
testImplementation("org.springframework.hateoas:spring-hateoas")
testImplementation("org.springframework.plugin:spring-plugin-core")
testImplementation("org.testcontainers:cassandra")

66
spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfiguration.java

@ -23,6 +23,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; @@ -23,6 +23,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import jakarta.json.bind.Jsonb;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
@ -42,6 +47,7 @@ import org.springframework.boot.test.json.JacksonTester; @@ -42,6 +47,7 @@ import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.context.annotation.Scope;
import org.springframework.core.ResolvableType;
import org.springframework.test.util.ReflectionTestUtils;
@ -68,6 +74,7 @@ public class JsonTestersAutoConfiguration { @@ -68,6 +74,7 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ImportRuntimeHints(BasicJsonTesterRuntimeHints.class)
public FactoryBean<BasicJsonTester> basicJsonTesterFactoryBean() {
return new JsonTesterFactoryBean<BasicJsonTester, Void>(BasicJsonTester.class, null);
}
@ -79,10 +86,19 @@ public class JsonTestersAutoConfiguration { @@ -79,10 +86,19 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(ObjectMapper.class)
@ImportRuntimeHints(JacksonTesterRuntimeHints.class)
FactoryBean<JacksonTester<?>> jacksonTesterFactoryBean(ObjectMapper mapper) {
return new JsonTesterFactoryBean<>(JacksonTester.class, mapper);
}
static class JacksonTesterRuntimeHints extends AbstractJsonMarshalTesterRuntimeHints {
JacksonTesterRuntimeHints() {
super(JacksonTester.class);
}
}
}
@Configuration(proxyBeanMethods = false)
@ -92,10 +108,19 @@ public class JsonTestersAutoConfiguration { @@ -92,10 +108,19 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(Gson.class)
@ImportRuntimeHints(GsonTesterRuntimeHints.class)
FactoryBean<GsonTester<?>> gsonTesterFactoryBean(Gson gson) {
return new JsonTesterFactoryBean<>(GsonTester.class, gson);
}
static class GsonTesterRuntimeHints extends AbstractJsonMarshalTesterRuntimeHints {
GsonTesterRuntimeHints() {
super(GsonTester.class);
}
}
}
@Configuration(proxyBeanMethods = false)
@ -105,10 +130,19 @@ public class JsonTestersAutoConfiguration { @@ -105,10 +130,19 @@ public class JsonTestersAutoConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnBean(Jsonb.class)
@ImportRuntimeHints(JsonbJsonTesterRuntimeHints.class)
FactoryBean<JsonbTester<?>> jsonbTesterFactoryBean(Jsonb jsonb) {
return new JsonTesterFactoryBean<>(JsonbTester.class, jsonb);
}
static class JsonbJsonTesterRuntimeHints extends AbstractJsonMarshalTesterRuntimeHints {
JsonbJsonTesterRuntimeHints() {
super(JsonbTester.class);
}
}
}
/**
@ -189,4 +223,36 @@ public class JsonTestersAutoConfiguration { @@ -189,4 +223,36 @@ public class JsonTestersAutoConfiguration {
}
@SuppressWarnings("rawtypes")
static class AbstractJsonMarshalTesterRuntimeHints implements RuntimeHintsRegistrar {
private final Class<? extends AbstractJsonMarshalTester> tester;
AbstractJsonMarshalTesterRuntimeHints(Class<? extends AbstractJsonMarshalTester> tester) {
this.tester = tester;
}
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
ReflectionHints reflection = hints.reflection();
reflection.registerType(this.tester, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
reflection.registerMethod(
ReflectionUtils.findMethod(this.tester, "initialize", Class.class, ResolvableType.class),
ExecutableMode.INVOKE);
}
}
static class BasicJsonTesterRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
ReflectionHints reflection = hints.reflection();
reflection.registerType(BasicJsonTester.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
reflection.registerMethod(ReflectionUtils.findMethod(BasicJsonTester.class, "initialize", Class.class),
ExecutableMode.INVOKE);
}
}
}

108
spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestersAutoConfigurationTests.java

@ -0,0 +1,108 @@ @@ -0,0 +1,108 @@
/*
* Copyright 2012-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.
* 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.boot.test.autoconfigure.json;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.ReflectionHintsPredicates;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.json.JsonbTester;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.aot.ApplicationContextAotGenerator;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JsonTestersAutoConfiguration}.
*
* @author Andy Wilkinson
*/
class JsonTestersAutoConfigurationTests {
@Test
void withNoMarshallersOnlyBasicJsonTesterHintsAreContributed() {
jsonTesters((runtimeHints) -> {
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
assertThat(reflection.onType(BasicJsonTester.class)).accepts(runtimeHints);
assertThat(reflection.onType(JacksonTester.class).negate()).accepts(runtimeHints);
assertThat(reflection.onType(JsonbTester.class).negate()).accepts(runtimeHints);
assertThat(reflection.onType(GsonTester.class).negate()).accepts(runtimeHints);
});
}
@Test
void withObjectMapperBeanJacksonTesterHintsAreContributed() {
jsonTestersWith(JacksonAutoConfiguration.class, (runtimeHints) -> {
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
assertThat(reflection.onType(BasicJsonTester.class)).accepts(runtimeHints);
assertThat(reflection.onType(JacksonTester.class)).accepts(runtimeHints);
assertThat(reflection.onType(JsonbTester.class).negate()).accepts(runtimeHints);
assertThat(reflection.onType(GsonTester.class).negate()).accepts(runtimeHints);
});
}
@Test
void withGsonBeanGsonTesterHintsAreContributed() {
jsonTestersWith(GsonAutoConfiguration.class, (runtimeHints) -> {
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
assertThat(reflection.onType(BasicJsonTester.class)).accepts(runtimeHints);
assertThat(reflection.onType(JacksonTester.class).negate()).accepts(runtimeHints);
assertThat(reflection.onType(JsonbTester.class).negate()).accepts(runtimeHints);
assertThat(reflection.onType(GsonTester.class)).accepts(runtimeHints);
});
}
@Test
void withJsonbBeanJsonbTesterHintsAreContributed() {
jsonTestersWith(JsonbAutoConfiguration.class, (runtimeHints) -> {
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
assertThat(reflection.onType(BasicJsonTester.class)).accepts(runtimeHints);
assertThat(reflection.onType(JacksonTester.class).negate()).accepts(runtimeHints);
assertThat(reflection.onType(JsonbTester.class)).accepts(runtimeHints);
assertThat(reflection.onType(GsonTester.class).negate()).accepts(runtimeHints);
});
}
private void jsonTesters(Consumer<RuntimeHints> hintsConsumer) {
jsonTestersWith(null, hintsConsumer);
}
private void jsonTestersWith(Class<?> configuration, Consumer<RuntimeHints> hintsConsumer) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
TestPropertyValues.of("spring.test.jsontesters.enabled=true").applyTo(context);
if (configuration != null) {
context.register(configuration);
}
context.register(JsonTestersAutoConfiguration.class);
TestGenerationContext generationContext = new TestGenerationContext();
new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
hintsConsumer.accept(generationContext.getRuntimeHints());
}
}
}
Loading…
Cancel
Save