Polishing

This commit is contained in:
Sam Brannen
2023-02-02 14:38:05 +01:00
parent e170c16b02
commit ac6385025b
8 changed files with 95 additions and 89 deletions

View File

@@ -21,7 +21,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.framework.Advised;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -32,6 +32,7 @@ import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.proxies;
/**
* Tests for {@link HttpExchangeBeanRegistrationAotProcessor}.
@@ -40,23 +41,25 @@ import static org.mockito.Mockito.mock;
*/
class HttpExchangeBeanRegistrationAotProcessorTests {
private final HttpExchangeBeanRegistrationAotProcessor processor = new HttpExchangeBeanRegistrationAotProcessor();
private final GenerationContext generationContext = new TestGenerationContext();
private final RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
@Test
void shouldSkipNonAnnotatedInterface() {
process(NonAnnotatedInterface.class);
assertThat(this.generationContext.getRuntimeHints().proxies().jdkProxyHints()).isEmpty();
assertThat(this.runtimeHints.proxies().jdkProxyHints()).isEmpty();
}
@Test
void shouldProcessAnnotatedInterface() {
process(AnnotatedInterface.class);
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(AnnotatedInterface.class, SpringProxy.class, Advised.class,
DecoratingProxy.class)).accepts(this.generationContext.getRuntimeHints());
assertThat(proxies().forInterfaces(AnnotatedInterface.class, SpringProxy.class, Advised.class, DecoratingProxy.class))
.accepts(this.runtimeHints);
}
private void process(Class<?> beanClass) {
BeanRegistrationAotContribution contribution = createContribution(beanClass);
if (contribution != null) {
@@ -65,12 +68,14 @@ class HttpExchangeBeanRegistrationAotProcessorTests {
}
@Nullable
private BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
private static BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
return new HttpExchangeBeanRegistrationAotProcessor()
.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
}
interface NonAnnotatedInterface {
void notExchange();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -21,10 +21,10 @@ import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.web.bind.annotation.RequestBody;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
/**
* Tests for {@link HttpExchangeReflectiveProcessor}.
@@ -37,26 +37,27 @@ class HttpExchangeReflectiveProcessorTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
void registerReflectiveHintsForMethodWithReturnValue() throws NoSuchMethodException {
Method method = SampleService.class.getDeclaredMethod("get");
processor.registerReflectionHints(hints.reflection(), method);
assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "get")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Response.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "getMessage")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Response.class, "setMessage")).accepts(hints);
assertThat(reflection().onType(SampleService.class)).accepts(hints);
assertThat(reflection().onMethod(SampleService.class, "get")).accepts(hints);
assertThat(reflection().onType(Response.class)).accepts(hints);
assertThat(reflection().onMethod(Response.class, "getMessage")).accepts(hints);
assertThat(reflection().onMethod(Response.class, "setMessage")).accepts(hints);
}
@Test
void registerReflectiveHintsForMethodWithRequestBodyParameter() throws NoSuchMethodException {
Method method = SampleService.class.getDeclaredMethod("post", Request.class);
processor.registerReflectionHints(hints.reflection(), method);
assertThat(RuntimeHintsPredicates.reflection().onType(SampleService.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleService.class, "post")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(Request.class)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "getMessage")).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onMethod(Request.class, "setMessage")).accepts(hints);
assertThat(reflection().onType(SampleService.class)).accepts(hints);
assertThat(reflection().onMethod(SampleService.class, "post")).accepts(hints);
assertThat(reflection().onType(Request.class)).accepts(hints);
assertThat(reflection().onMethod(Request.class, "getMessage")).accepts(hints);
assertThat(reflection().onMethod(Request.class, "setMessage")).accepts(hints);
}