1919import static org .mockito .Mockito .*;
2020
2121import java .lang .annotation .Annotation ;
22- import java .util .Collection ;
2322import java .util .Collections ;
2423import java .util .List ;
2524import java .util .Set ;
26- import java .util .function .Consumer ;
2725
2826import org .jmolecules .ddd .annotation .Entity ;
29- import org .jspecify .annotations .Nullable ;
3027import org .junit .jupiter .api .Test ;
3128import org .junitpioneer .jupiter .ClearSystemProperty ;
3229import org .junitpioneer .jupiter .SetSystemProperty ;
3633import org .springframework .aot .generate .DefaultGenerationContext ;
3734import org .springframework .aot .generate .GenerationContext ;
3835import org .springframework .aot .generate .InMemoryGeneratedFiles ;
39- import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
4036import org .springframework .context .support .AbstractApplicationContext ;
4137import org .springframework .context .support .GenericApplicationContext ;
4238import org .springframework .core .annotation .MergedAnnotation ;
43- import org .springframework .core .env .Environment ;
44- import org .springframework .core .env .StandardEnvironment ;
4539import org .springframework .data .annotation .Id ;
4640import org .springframework .data .aot .AotContext ;
47- import org .springframework .data .aot .AotTypeConfiguration ;
4841import org .springframework .data .mongodb .repository .support .SimpleMongoRepository ;
4942import org .springframework .data .repository .Repository ;
50- import org .springframework .data .repository .config .AotRepositoryContext ;
43+ import org .springframework .data .repository .config .AotRepositoryContextSupport ;
5144import org .springframework .data .repository .config .AotRepositoryInformation ;
5245import org .springframework .data .repository .config .RepositoryConfigurationSource ;
5346import org .springframework .data .repository .core .RepositoryInformation ;
@@ -65,10 +58,10 @@ class AotMongoRepositoryPostProcessorUnitTests {
6558 @ SetSystemProperty (key = AotDetector .AOT_ENABLED , value = "true" )
6659 void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled () {
6760
68- GenerationContext ctx = createGenerationContext ();
6961 GenericApplicationContext context = new GenericApplicationContext ();
62+ context .refresh ();
7063
71- MongoRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
64+ MongoRepositoryContributor contributor = createContributorWithPersonTypes (context );
7265
7366 assertThat (contributor ).isNotNull ();
7467 }
@@ -77,10 +70,10 @@ void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
7770 @ ClearSystemProperty (key = AotContext .GENERATED_REPOSITORIES_ENABLED )
7871 void shouldEnableAotRepositoriesByDefault () {
7972
80- GenerationContext ctx = createGenerationContext ();
8173 GenericApplicationContext context = new GenericApplicationContext ();
74+ context .refresh ();
8275
83- MongoRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
76+ MongoRepositoryContributor contributor = createContributorWithPersonTypes (context );
8477
8578 assertThat (contributor ).isNotNull ();
8679 }
@@ -89,10 +82,10 @@ void shouldEnableAotRepositoriesByDefault() {
8982 @ SetSystemProperty (key = AotContext .GENERATED_REPOSITORIES_ENABLED , value = "false" )
9083 void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse () {
9184
92- GenerationContext ctx = createGenerationContext ();
9385 GenericApplicationContext context = new GenericApplicationContext ();
86+ context .refresh ();
9487
95- MongoRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
88+ MongoRepositoryContributor contributor = createContributorWithPersonTypes (context );
9689
9790 assertThat (contributor ).isNull ();
9891 }
@@ -101,10 +94,10 @@ void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse() {
10194 @ SetSystemProperty (key = "spring.aot.mongodb.repositories.enabled" , value = "false" )
10295 void shouldDisableAotRepositoriesWhenJpaGeneratedRepositoriesIsFalse () {
10396
104- GenerationContext ctx = createGenerationContext ();
10597 GenericApplicationContext context = new GenericApplicationContext ();
98+ context .refresh ();
10699
107- MongoRepositoryContributor contributor = createContributorWithPersonTypes (context , ctx );
100+ MongoRepositoryContributor contributor = createContributorWithPersonTypes (context );
108101
109102 assertThat (contributor ).isNull ();
110103 }
@@ -113,15 +106,14 @@ private GenerationContext createGenerationContext() {
113106 return new DefaultGenerationContext (new ClassNameGenerator (ClassName .OBJECT ), new InMemoryGeneratedFiles ());
114107 }
115108
116- private MongoRepositoryContributor createContributorWithPersonTypes (GenericApplicationContext context ,
117- GenerationContext ctx ) {
109+ private MongoRepositoryContributor createContributorWithPersonTypes (GenericApplicationContext context ) {
118110
119- return new AotMongoRepositoryPostProcessor ().contribute (new DummyAotRepositoryContext (context ) {
111+ return new AotMongoRepositoryPostProcessor ().contributeAotRepository (new DummyAotRepositoryContext (context ) {
120112 @ Override
121113 public Set <Class <?>> getResolvedTypes () {
122114 return Collections .singleton (Person .class );
123115 }
124- }, ctx );
116+ });
125117 }
126118
127119 @ Entity
@@ -131,22 +123,15 @@ static class Person {
131123
132124 interface PersonRepository extends Repository <Person , Long > {}
133125
134- static class DummyAotRepositoryContext implements AotRepositoryContext {
126+ static class DummyAotRepositoryContext extends AotRepositoryContextSupport {
135127
136- private final @ Nullable AbstractApplicationContext applicationContext ;
137-
138- DummyAotRepositoryContext (@ Nullable AbstractApplicationContext applicationContext ) {
139- this .applicationContext = applicationContext ;
140- }
141-
142- @ Override
143- public String getBeanName () {
144- return "jpaRepository" ;
128+ DummyAotRepositoryContext (AbstractApplicationContext applicationContext ) {
129+ super (AotContext .from (applicationContext , applicationContext .getEnvironment ()));
145130 }
146131
147132 @ Override
148133 public String getModuleName () {
149- return "JPA " ;
134+ return "MongoDB " ;
150135 }
151136
152137 @ Override
@@ -180,40 +165,6 @@ public Set<Class<?>> getResolvedTypes() {
180165 return Set .of ();
181166 }
182167
183- @ Override
184- public Set <Class <?>> getUserDomainTypes () {
185- return Set .of ();
186- }
187-
188- @ Override
189- public ConfigurableListableBeanFactory getBeanFactory () {
190- return applicationContext != null ? applicationContext .getBeanFactory () : null ;
191- }
192-
193- @ Override
194- public Environment getEnvironment () {
195- return applicationContext == null ? new StandardEnvironment () : applicationContext .getEnvironment ();
196- }
197-
198- @ Override
199- public TypeIntrospector introspectType (String typeName ) {
200- return null ;
201- }
202-
203- @ Override
204- public IntrospectedBeanDefinition introspectBeanDefinition (String beanName ) {
205- return null ;
206- }
207-
208- @ Override
209- public void typeConfiguration (Class <?> type , Consumer <AotTypeConfiguration > configurationConsumer ) {
210-
211- }
212-
213- @ Override
214- public Collection <AotTypeConfiguration > typeConfigurations () {
215- return List .of ();
216- }
217168 }
218169
219170}
0 commit comments