Skip to content

Commit ba01363

Browse files
committed
Test default value of spring.aot.repositories.enabled
Signed-off-by: Hyunsang Han <[email protected]>
1 parent 769ce65 commit ba01363

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/config/JpaRepositoryRegistrationAotProcessorUnitTests.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.jspecify.annotations.Nullable;
3131
import org.junit.jupiter.api.Test;
3232

33+
import org.springframework.aot.AotDetector;
3334
import org.springframework.aot.generate.ClassNameGenerator;
3435
import org.springframework.aot.generate.DefaultGenerationContext;
3536
import org.springframework.aot.generate.GenerationContext;
@@ -56,6 +57,7 @@
5657

5758
/**
5859
* @author Christoph Strobl
60+
* @author Hyunsang Han
5961
*/
6062
class JpaRepositoryRegistrationAotProcessorUnitTests {
6163

@@ -132,6 +134,104 @@ public List<String> getManagedPackages() {
132134
assertThat(contributor.getMetamodel().managedType(Person.class)).isNotNull();
133135
}
134136

137+
@Test // GH-3899
138+
void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
139+
140+
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
141+
new InMemoryGeneratedFiles());
142+
143+
GenericApplicationContext context = new GenericApplicationContext();
144+
145+
System.setProperty(AotDetector.AOT_ENABLED, "true");
146+
147+
try {
148+
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
149+
.contribute(new DummyAotRepositoryContext(context) {
150+
@Override
151+
public Set<Class<?>> getResolvedTypes() {
152+
return Collections.singleton(Person.class);
153+
}
154+
}, ctx);
155+
156+
assertThat(contributor).isNotNull();
157+
} finally {
158+
System.clearProperty(AotDetector.AOT_ENABLED);
159+
}
160+
}
161+
162+
@Test // GH-3899
163+
void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled() {
164+
165+
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
166+
new InMemoryGeneratedFiles());
167+
168+
GenericApplicationContext context = new GenericApplicationContext();
169+
170+
System.clearProperty(AotDetector.AOT_ENABLED);
171+
172+
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
173+
.contribute(new DummyAotRepositoryContext(context) {
174+
@Override
175+
public Set<Class<?>> getResolvedTypes() {
176+
return Collections.singleton(Person.class);
177+
}
178+
}, ctx);
179+
180+
assertThat(contributor).isNull();
181+
}
182+
183+
@Test // GH-3899
184+
void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty() {
185+
186+
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
187+
new InMemoryGeneratedFiles());
188+
189+
GenericApplicationContext context = new GenericApplicationContext();
190+
191+
System.setProperty(AotDetector.AOT_ENABLED, "true");
192+
193+
try {
194+
MockPropertySource propertySource = new MockPropertySource()
195+
.withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "false");
196+
context.getEnvironment().getPropertySources().addFirst(propertySource);
197+
198+
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
199+
.contribute(new DummyAotRepositoryContext(context) {
200+
@Override
201+
public Set<Class<?>> getResolvedTypes() {
202+
return Collections.singleton(Person.class);
203+
}
204+
}, ctx);
205+
206+
assertThat(contributor).isNull();
207+
} finally {
208+
System.clearProperty(AotDetector.AOT_ENABLED);
209+
}
210+
}
211+
212+
@Test // GH-3899
213+
void repositoryProcessorShouldEnableWhenExplicitlySetToTrue() {
214+
215+
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
216+
new InMemoryGeneratedFiles());
217+
218+
GenericApplicationContext context = new GenericApplicationContext();
219+
220+
MockPropertySource propertySource = new MockPropertySource()
221+
.withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "true");
222+
context.getEnvironment().getPropertySources().addFirst(propertySource);
223+
224+
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
225+
.contribute(new DummyAotRepositoryContext(context) {
226+
@Override
227+
public Set<Class<?>> getResolvedTypes() {
228+
return Collections.singleton(Person.class);
229+
}
230+
}, ctx);
231+
232+
assertThat(contributor).isNotNull();
233+
}
234+
135235
@Entity
136236
static class Person {
137237
@Id Long id;

0 commit comments

Comments
 (0)