29
29
30
30
import org .jspecify .annotations .Nullable ;
31
31
import org .junit .jupiter .api .Test ;
32
-
33
32
import org .junitpioneer .jupiter .ClearSystemProperty ;
34
33
import org .junitpioneer .jupiter .SetSystemProperty ;
34
+
35
35
import org .springframework .aot .AotDetector ;
36
36
import org .springframework .aot .generate .ClassNameGenerator ;
37
37
import org .springframework .aot .generate .DefaultGenerationContext ;
58
58
import org .springframework .orm .jpa .persistenceunit .PersistenceManagedTypes ;
59
59
60
60
/**
61
+ * Unit tests for {@link JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor}.
62
+ *
61
63
* @author Christoph Strobl
62
64
* @author Hyunsang Han
65
+ * @author Mark Paluch
63
66
*/
64
67
class JpaRepositoryRegistrationAotProcessorUnitTests {
65
68
66
69
@ Test // GH-2628
67
70
void aotProcessorMustNotRegisterDomainTypes () {
68
71
69
72
GenerationContext ctx = createGenerationContext ();
73
+ GenericApplicationContext context = new GenericApplicationContext ();
70
74
71
75
new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
72
- .contribute (new DummyAotRepositoryContext (null ) {
76
+ .contribute (new DummyAotRepositoryContext (context ) {
73
77
@ Override
74
78
public Set <Class <?>> getResolvedTypes () {
75
79
return Collections .singleton (Person .class );
@@ -84,8 +88,9 @@ void aotProcessorMustNotRegisterAnnotations() {
84
88
85
89
GenerationContext ctx = createGenerationContext ();
86
90
91
+ GenericApplicationContext context = new GenericApplicationContext ();
87
92
new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
88
- .contribute (new DummyAotRepositoryContext (null ) {
93
+ .contribute (new DummyAotRepositoryContext (context ) {
89
94
90
95
@ Override
91
96
public Set <MergedAnnotation <Annotation >> getResolvedAnnotations () {
@@ -127,7 +132,8 @@ public List<String> getManagedPackages() {
127
132
context .getEnvironment ().getPropertySources ()
128
133
.addFirst (new MockPropertySource ().withProperty (AotContext .GENERATED_REPOSITORIES_ENABLED , "true" ));
129
134
130
- JpaRepositoryContributor contributor = createContributor (new DummyAotRepositoryContext (context ), ctx );
135
+ JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
136
+ .contribute (new DummyAotRepositoryContext (context ), ctx );
131
137
132
138
assertThat (contributor .getMetamodel ().managedType (Person .class )).isNotNull ();
133
139
}
@@ -137,66 +143,58 @@ public List<String> getManagedPackages() {
137
143
void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled () {
138
144
139
145
GenerationContext ctx = createGenerationContext ();
140
- GenericApplicationContext context = createApplicationContext ();
146
+ GenericApplicationContext context = new GenericApplicationContext ();
141
147
142
148
JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
143
149
144
150
assertThat (contributor ).isNotNull ();
145
151
}
146
152
147
153
@ Test // GH-3899
148
- @ ClearSystemProperty (key = AotDetector . AOT_ENABLED )
149
- void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled () {
154
+ @ ClearSystemProperty (key = AotContext . GENERATED_REPOSITORIES_ENABLED )
155
+ void shouldEnableAotRepositoriesByDefault () {
150
156
151
157
GenerationContext ctx = createGenerationContext ();
152
- GenericApplicationContext context = createApplicationContext ();
158
+ GenericApplicationContext context = new GenericApplicationContext ();
153
159
154
160
JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
155
161
156
- assertThat (contributor ).isNull ();
162
+ assertThat (contributor ).isNotNull ();
157
163
}
158
164
159
165
@ Test // GH-3899
160
- @ SetSystemProperty (key = AotDetector .AOT_ENABLED , value = "true" )
161
166
@ SetSystemProperty (key = AotContext .GENERATED_REPOSITORIES_ENABLED , value = "false" )
162
- void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty () {
167
+ void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse () {
163
168
164
169
GenerationContext ctx = createGenerationContext ();
165
- GenericApplicationContext context = createApplicationContext ();
170
+ GenericApplicationContext context = new GenericApplicationContext ();
166
171
167
172
JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
168
173
169
174
assertThat (contributor ).isNull ();
170
175
}
171
176
172
177
@ Test // GH-3899
173
- @ SetSystemProperty (key = AotContext . GENERATED_REPOSITORIES_ENABLED , value = "true " )
174
- void repositoryProcessorShouldEnableWhenExplicitlySetToTrue () {
178
+ @ SetSystemProperty (key = "spring.aot.jpa.repositories.enabled" , value = "false " )
179
+ void shouldDisableAotRepositoriesWhenJpaGeneratedRepositoriesIsFalse () {
175
180
176
181
GenerationContext ctx = createGenerationContext ();
177
- GenericApplicationContext context = createApplicationContext ();
182
+ GenericApplicationContext context = new GenericApplicationContext ();
178
183
179
184
JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
180
185
181
- assertThat (contributor ).isNotNull ();
186
+ assertThat (contributor ).isNull ();
182
187
}
183
188
184
189
private GenerationContext createGenerationContext () {
185
190
return new DefaultGenerationContext (new ClassNameGenerator (ClassName .OBJECT ),
186
191
new InMemoryGeneratedFiles ());
187
192
}
188
193
189
- private GenericApplicationContext createApplicationContext () {
190
- return new GenericApplicationContext ();
191
- }
194
+ private JpaRepositoryContributor createContributorWithPersonTypes (GenericApplicationContext context , GenerationContext ctx ) {
192
195
193
- private JpaRepositoryContributor createContributor (AotRepositoryContext repositoryContext , GenerationContext ctx ) {
194
196
return new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
195
- .contribute (repositoryContext , ctx );
196
- }
197
-
198
- private JpaRepositoryContributor createContributorWithPersonTypes (GenericApplicationContext context , GenerationContext ctx ) {
199
- return createContributor (new DummyAotRepositoryContext (context ) {
197
+ .contribute (new DummyAotRepositoryContext (context ) {
200
198
@ Override
201
199
public Set <Class <?>> getResolvedTypes () {
202
200
return Collections .singleton (Person .class );
@@ -252,12 +250,12 @@ public RepositoryInformation getRepositoryInformation() {
252
250
253
251
@ Override
254
252
public Set <MergedAnnotation <Annotation >> getResolvedAnnotations () {
255
- return null ;
253
+ return Set . of () ;
256
254
}
257
255
258
256
@ Override
259
257
public Set <Class <?>> getResolvedTypes () {
260
- return null ;
258
+ return Set . of () ;
261
259
}
262
260
263
261
@ Override
@@ -279,5 +277,7 @@ public TypeIntrospector introspectType(String typeName) {
279
277
public IntrospectedBeanDefinition introspectBeanDefinition (String beanName ) {
280
278
return null ;
281
279
}
280
+
282
281
}
282
+
283
283
}
0 commit comments