2929
3030import org .jspecify .annotations .Nullable ;
3131import org .junit .jupiter .api .Test ;
32-
3332import org .junitpioneer .jupiter .ClearSystemProperty ;
3433import org .junitpioneer .jupiter .SetSystemProperty ;
34+
3535import org .springframework .aot .AotDetector ;
3636import org .springframework .aot .generate .ClassNameGenerator ;
3737import org .springframework .aot .generate .DefaultGenerationContext ;
5858import org .springframework .orm .jpa .persistenceunit .PersistenceManagedTypes ;
5959
6060/**
61+ * Unit tests for {@link JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor}.
62+ *
6163 * @author Christoph Strobl
6264 * @author Hyunsang Han
65+ * @author Mark Paluch
6366 */
6467class JpaRepositoryRegistrationAotProcessorUnitTests {
6568
6669 @ Test // GH-2628
6770 void aotProcessorMustNotRegisterDomainTypes () {
6871
6972 GenerationContext ctx = createGenerationContext ();
73+ GenericApplicationContext context = new GenericApplicationContext ();
7074
7175 new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
72- .contribute (new DummyAotRepositoryContext (null ) {
76+ .contribute (new DummyAotRepositoryContext (context ) {
7377 @ Override
7478 public Set <Class <?>> getResolvedTypes () {
7579 return Collections .singleton (Person .class );
@@ -84,8 +88,9 @@ void aotProcessorMustNotRegisterAnnotations() {
8488
8589 GenerationContext ctx = createGenerationContext ();
8690
91+ GenericApplicationContext context = new GenericApplicationContext ();
8792 new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
88- .contribute (new DummyAotRepositoryContext (null ) {
93+ .contribute (new DummyAotRepositoryContext (context ) {
8994
9095 @ Override
9196 public Set <MergedAnnotation <Annotation >> getResolvedAnnotations () {
@@ -127,7 +132,8 @@ public List<String> getManagedPackages() {
127132 context .getEnvironment ().getPropertySources ()
128133 .addFirst (new MockPropertySource ().withProperty (AotContext .GENERATED_REPOSITORIES_ENABLED , "true" ));
129134
130- JpaRepositoryContributor contributor = createContributor (new DummyAotRepositoryContext (context ), ctx );
135+ JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
136+ .contribute (new DummyAotRepositoryContext (context ), ctx );
131137
132138 assertThat (contributor .getMetamodel ().managedType (Person .class )).isNotNull ();
133139 }
@@ -137,66 +143,58 @@ public List<String> getManagedPackages() {
137143 void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled () {
138144
139145 GenerationContext ctx = createGenerationContext ();
140- GenericApplicationContext context = createApplicationContext ();
146+ GenericApplicationContext context = new GenericApplicationContext ();
141147
142148 JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
143149
144150 assertThat (contributor ).isNotNull ();
145151 }
146152
147153 @ Test // GH-3899
148- @ ClearSystemProperty (key = AotDetector . AOT_ENABLED )
149- void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled () {
154+ @ ClearSystemProperty (key = AotContext . GENERATED_REPOSITORIES_ENABLED )
155+ void shouldEnableAotRepositoriesByDefault () {
150156
151157 GenerationContext ctx = createGenerationContext ();
152- GenericApplicationContext context = createApplicationContext ();
158+ GenericApplicationContext context = new GenericApplicationContext ();
153159
154160 JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
155161
156- assertThat (contributor ).isNull ();
162+ assertThat (contributor ).isNotNull ();
157163 }
158164
159165 @ Test // GH-3899
160- @ SetSystemProperty (key = AotDetector .AOT_ENABLED , value = "true" )
161166 @ SetSystemProperty (key = AotContext .GENERATED_REPOSITORIES_ENABLED , value = "false" )
162- void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty () {
167+ void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse () {
163168
164169 GenerationContext ctx = createGenerationContext ();
165- GenericApplicationContext context = createApplicationContext ();
170+ GenericApplicationContext context = new GenericApplicationContext ();
166171
167172 JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
168173
169174 assertThat (contributor ).isNull ();
170175 }
171176
172177 @ 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 () {
175180
176181 GenerationContext ctx = createGenerationContext ();
177- GenericApplicationContext context = createApplicationContext ();
182+ GenericApplicationContext context = new GenericApplicationContext ();
178183
179184 JpaRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
180185
181- assertThat (contributor ).isNotNull ();
186+ assertThat (contributor ).isNull ();
182187 }
183188
184189 private GenerationContext createGenerationContext () {
185190 return new DefaultGenerationContext (new ClassNameGenerator (ClassName .OBJECT ),
186191 new InMemoryGeneratedFiles ());
187192 }
188193
189- private GenericApplicationContext createApplicationContext () {
190- return new GenericApplicationContext ();
191- }
194+ private JpaRepositoryContributor createContributorWithPersonTypes (GenericApplicationContext context , GenerationContext ctx ) {
192195
193- private JpaRepositoryContributor createContributor (AotRepositoryContext repositoryContext , GenerationContext ctx ) {
194196 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 ) {
200198 @ Override
201199 public Set <Class <?>> getResolvedTypes () {
202200 return Collections .singleton (Person .class );
@@ -252,12 +250,12 @@ public RepositoryInformation getRepositoryInformation() {
252250
253251 @ Override
254252 public Set <MergedAnnotation <Annotation >> getResolvedAnnotations () {
255- return null ;
253+ return Set . of () ;
256254 }
257255
258256 @ Override
259257 public Set <Class <?>> getResolvedTypes () {
260- return null ;
258+ return Set . of () ;
261259 }
262260
263261 @ Override
@@ -279,5 +277,7 @@ public TypeIntrospector introspectType(String typeName) {
279277 public IntrospectedBeanDefinition introspectBeanDefinition (String beanName ) {
280278 return null ;
281279 }
280+
282281 }
282+
283283}
0 commit comments