Skip to content

Commit

Permalink
Add reflection hints for JsonTesters
Browse files Browse the repository at this point in the history
Closes gh-32858
  • Loading branch information
wilkinsona committed Nov 9, 2022
1 parent 41e2423 commit fcbc7da
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
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;
Expand All @@ -42,6 +47,7 @@
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;
Expand All @@ -68,6 +74,7 @@ public static JsonMarshalTestersBeanPostProcessor jsonMarshalTestersBeanPostProc

@Bean
@Scope("prototype")
@ImportRuntimeHints(BasicJsonTesterRuntimeHints.class)
public FactoryBean<BasicJsonTester> basicJsonTesterFactoryBean() {
return new JsonTesterFactoryBean<BasicJsonTester, Void>(BasicJsonTester.class, null);
}
Expand All @@ -79,10 +86,19 @@ static class JacksonJsonTestersConfiguration {
@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)
Expand All @@ -92,10 +108,19 @@ static class GsonJsonTestersConfiguration {
@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)
Expand All @@ -105,10 +130,19 @@ static class JsonbJsonTesterConfiguration {
@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);
}

}

}

/**
Expand Down Expand Up @@ -189,4 +223,36 @@ private void initializeTester(Object bean, Field field, Object... args) {

}

@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);
}

}

}
Original file line number Diff line number Diff line change
@@ -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());
}
}

}

0 comments on commit fcbc7da

Please sign in to comment.