|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2025 the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | +package org.springframework.data.aot; | 
|  | 17 | + | 
|  | 18 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 19 | +import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection; | 
|  | 20 | + | 
|  | 21 | +import java.util.List; | 
|  | 22 | + | 
|  | 23 | +import org.junit.jupiter.api.Test; | 
|  | 24 | +import org.mockito.Mock; | 
|  | 25 | +import org.mockito.junit.jupiter.MockitoSettings; | 
|  | 26 | +import org.springframework.aot.hint.MemberCategory; | 
|  | 27 | +import org.springframework.aot.hint.annotation.Reflective; | 
|  | 28 | +import org.springframework.aot.test.generate.TestGenerationContext; | 
|  | 29 | +import org.springframework.beans.factory.BeanFactory; | 
|  | 30 | +import org.springframework.mock.env.MockEnvironment; | 
|  | 31 | + | 
|  | 32 | +/** | 
|  | 33 | + * Unit tests targeting {@link DefaultAotContext}; | 
|  | 34 | + * | 
|  | 35 | + * @author Christoph Strobl | 
|  | 36 | + */ | 
|  | 37 | +@MockitoSettings(strictness = org.mockito.quality.Strictness.LENIENT) | 
|  | 38 | +public class DefaultAotContextUnitTests { | 
|  | 39 | + | 
|  | 40 | +	@Mock BeanFactory beanFactory; | 
|  | 41 | + | 
|  | 42 | +	@Mock AotMappingContext mappingContext; | 
|  | 43 | + | 
|  | 44 | +	MockEnvironment mockEnvironment = new MockEnvironment(); | 
|  | 45 | + | 
|  | 46 | +	@Test // GH-3387 | 
|  | 47 | +	void doesNotRegisterReflectionWhenThereIsNothingToRegister() { | 
|  | 48 | + | 
|  | 49 | +		DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext); | 
|  | 50 | +		context.typeConfiguration(Dummy.class, it -> { | 
|  | 51 | +			// no specific action | 
|  | 52 | +		}); | 
|  | 53 | + | 
|  | 54 | +		TestGenerationContext generationContext = new TestGenerationContext(); | 
|  | 55 | +		context.contributeTypeConfigurations(generationContext); | 
|  | 56 | + | 
|  | 57 | +		assertThat(generationContext.getRuntimeHints()).matches(reflection().onType(Dummy.class).negate()); | 
|  | 58 | +	} | 
|  | 59 | + | 
|  | 60 | +	@Test // GH-3387 | 
|  | 61 | +	void doesNotRegisterReflectionWithCategoryAccordingly() { | 
|  | 62 | + | 
|  | 63 | +		DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext); | 
|  | 64 | +		context.typeConfiguration(Dummy.class, it -> it.forReflectiveAccess(MemberCategory.ACCESS_DECLARED_FIELDS)); | 
|  | 65 | + | 
|  | 66 | +		TestGenerationContext generationContext = new TestGenerationContext(); | 
|  | 67 | +		context.contributeTypeConfigurations(generationContext); | 
|  | 68 | + | 
|  | 69 | +		assertThat(generationContext.getRuntimeHints()) | 
|  | 70 | +				.matches(reflection().onType(Dummy.class).withAnyMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)); | 
|  | 71 | +	} | 
|  | 72 | + | 
|  | 73 | +	@Test // GH-3387 | 
|  | 74 | +	void registerReflectionIfThereIsAnAtReflectiveAnnotation() throws NoSuchMethodException { | 
|  | 75 | + | 
|  | 76 | +		DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext); | 
|  | 77 | +		context.typeConfiguration(DummyWithAtReflective.class, it -> { | 
|  | 78 | + | 
|  | 79 | +		}); | 
|  | 80 | + | 
|  | 81 | +		TestGenerationContext generationContext = new TestGenerationContext(); | 
|  | 82 | +		context.contributeTypeConfigurations(generationContext); | 
|  | 83 | + | 
|  | 84 | +		assertThat(generationContext.getRuntimeHints()) | 
|  | 85 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("reflectiveAnnotated"))) | 
|  | 86 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("getValue")).negate()) | 
|  | 87 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("justAMethod")).negate()); | 
|  | 88 | +	} | 
|  | 89 | + | 
|  | 90 | +	@Test // GH-3387 | 
|  | 91 | +	void registerReflectionForGetterSetterIfDataBindingRequested() throws NoSuchMethodException { | 
|  | 92 | + | 
|  | 93 | +		DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext); | 
|  | 94 | +		context.typeConfiguration(DummyWithAtReflective.class, AotTypeConfiguration::forDataBinding); | 
|  | 95 | + | 
|  | 96 | +		TestGenerationContext generationContext = new TestGenerationContext(); | 
|  | 97 | +		context.contributeTypeConfigurations(generationContext); | 
|  | 98 | + | 
|  | 99 | +		assertThat(generationContext.getRuntimeHints()) | 
|  | 100 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("reflectiveAnnotated"))) | 
|  | 101 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("getValue"))) | 
|  | 102 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("justAMethod")).negate()); | 
|  | 103 | +	} | 
|  | 104 | + | 
|  | 105 | +	@Test // GH-3387 | 
|  | 106 | +	void registerReflectionIfThereIsAnAtReflectiveAnnotationInTheSuperType() throws NoSuchMethodException { | 
|  | 107 | +		DefaultAotContext context = new DefaultAotContext(beanFactory, mockEnvironment, mappingContext); | 
|  | 108 | +		context.typeConfiguration(ExtendingDummyWithAtReflective.class, it -> { | 
|  | 109 | + | 
|  | 110 | +		}); | 
|  | 111 | + | 
|  | 112 | +		TestGenerationContext generationContext = new TestGenerationContext(); | 
|  | 113 | +		context.contributeTypeConfigurations(generationContext); | 
|  | 114 | + | 
|  | 115 | +		assertThat(generationContext.getRuntimeHints()) | 
|  | 116 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("reflectiveAnnotated"))) | 
|  | 117 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("justAMethod")).negate()) | 
|  | 118 | +				.matches(reflection().onMethodInvocation(DummyWithAtReflective.class.getMethod("getValue")).negate()); | 
|  | 119 | +	} | 
|  | 120 | + | 
|  | 121 | +	static class Dummy { | 
|  | 122 | + | 
|  | 123 | +		String value; | 
|  | 124 | + | 
|  | 125 | +		public List<String> justAMethod() { | 
|  | 126 | +			return null; | 
|  | 127 | +		} | 
|  | 128 | + | 
|  | 129 | +		public String getValue() { | 
|  | 130 | +			return value; | 
|  | 131 | +		} | 
|  | 132 | + | 
|  | 133 | +		public void setValue(String value) { | 
|  | 134 | +			this.value = value; | 
|  | 135 | +		} | 
|  | 136 | +	} | 
|  | 137 | + | 
|  | 138 | +	static class DummyWithAtReflective extends Dummy { | 
|  | 139 | + | 
|  | 140 | +		@Reflective | 
|  | 141 | +		public List<String> reflectiveAnnotated() { | 
|  | 142 | +			return null; | 
|  | 143 | +		} | 
|  | 144 | +	} | 
|  | 145 | + | 
|  | 146 | +	static class ExtendingDummyWithAtReflective extends DummyWithAtReflective {} | 
|  | 147 | + | 
|  | 148 | +} | 
0 commit comments