Skip to content

Commit 9ab6384

Browse files
committed
Test default value of spring.aot.repositories.enabled
Signed-off-by: Hyunsang Han <gustkd3@gmail.com>
1 parent db6a002 commit 9ab6384

File tree

1 file changed

+80
-8
lines changed

1 file changed

+80
-8
lines changed

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

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

33+
import org.junitpioneer.jupiter.ClearSystemProperty;
34+
import org.junitpioneer.jupiter.SetSystemProperty;
35+
import org.springframework.aot.AotDetector;
3336
import org.springframework.aot.generate.ClassNameGenerator;
3437
import org.springframework.aot.generate.DefaultGenerationContext;
3538
import org.springframework.aot.generate.GenerationContext;
@@ -56,14 +59,14 @@
5659

5760
/**
5861
* @author Christoph Strobl
62+
* @author Hyunsang Han
5963
*/
6064
class JpaRepositoryRegistrationAotProcessorUnitTests {
6165

6266
@Test // GH-2628
6367
void aotProcessorMustNotRegisterDomainTypes() {
6468

65-
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
66-
new InMemoryGeneratedFiles());
69+
GenerationContext ctx = createGenerationContext();
6770

6871
new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
6972
.contribute(new DummyAotRepositoryContext(null) {
@@ -79,8 +82,7 @@ public Set<Class<?>> getResolvedTypes() {
7982
@Test // GH-2628
8083
void aotProcessorMustNotRegisterAnnotations() {
8184

82-
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
83-
new InMemoryGeneratedFiles());
85+
GenerationContext ctx = createGenerationContext();
8486

8587
new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
8688
.contribute(new DummyAotRepositoryContext(null) {
@@ -99,8 +101,7 @@ public Set<MergedAnnotation<Annotation>> getResolvedAnnotations() {
99101
@Test // GH-3838
100102
void repositoryProcessorShouldConsiderPersistenceManagedTypes() {
101103

102-
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
103-
new InMemoryGeneratedFiles());
104+
GenerationContext ctx = createGenerationContext();
104105

105106
GenericApplicationContext context = new GenericApplicationContext();
106107
context.registerBean(PersistenceManagedTypes.class, () -> {
@@ -126,12 +127,83 @@ public List<String> getManagedPackages() {
126127
context.getEnvironment().getPropertySources()
127128
.addFirst(new MockPropertySource().withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "true"));
128129

129-
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
130-
.contribute(new DummyAotRepositoryContext(context), ctx);
130+
JpaRepositoryContributor contributor = createContributor(new DummyAotRepositoryContext(context), ctx);
131131

132132
assertThat(contributor.getMetamodel().managedType(Person.class)).isNotNull();
133133
}
134134

135+
@Test // GH-3899
136+
@SetSystemProperty(key = AotDetector.AOT_ENABLED, value = "true")
137+
void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
138+
139+
GenerationContext ctx = createGenerationContext();
140+
GenericApplicationContext context = createApplicationContext();
141+
142+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
143+
144+
assertThat(contributor).isNotNull();
145+
}
146+
147+
@Test // GH-3899
148+
@ClearSystemProperty(key = AotDetector.AOT_ENABLED)
149+
void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled() {
150+
151+
GenerationContext ctx = createGenerationContext();
152+
GenericApplicationContext context = createApplicationContext();
153+
154+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
155+
156+
assertThat(contributor).isNull();
157+
}
158+
159+
@Test // GH-3899
160+
@SetSystemProperty(key = AotDetector.AOT_ENABLED, value = "true")
161+
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "false")
162+
void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty() {
163+
164+
GenerationContext ctx = createGenerationContext();
165+
GenericApplicationContext context = createApplicationContext();
166+
167+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
168+
169+
assertThat(contributor).isNull();
170+
}
171+
172+
@Test // GH-3899
173+
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "true")
174+
void repositoryProcessorShouldEnableWhenExplicitlySetToTrue() {
175+
176+
GenerationContext ctx = createGenerationContext();
177+
GenericApplicationContext context = createApplicationContext();
178+
179+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
180+
181+
assertThat(contributor).isNotNull();
182+
}
183+
184+
private GenerationContext createGenerationContext() {
185+
return new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
186+
new InMemoryGeneratedFiles());
187+
}
188+
189+
private GenericApplicationContext createApplicationContext() {
190+
return new GenericApplicationContext();
191+
}
192+
193+
private JpaRepositoryContributor createContributor(AotRepositoryContext repositoryContext, GenerationContext ctx) {
194+
return new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
195+
.contribute(repositoryContext, ctx);
196+
}
197+
198+
private JpaRepositoryContributor createContributorWithPersonTypes(GenericApplicationContext context, GenerationContext ctx) {
199+
return createContributor(new DummyAotRepositoryContext(context) {
200+
@Override
201+
public Set<Class<?>> getResolvedTypes() {
202+
return Collections.singleton(Person.class);
203+
}
204+
}, ctx);
205+
}
206+
135207
@Entity
136208
static class Person {
137209
@Id Long id;

0 commit comments

Comments
 (0)