30
30
import org .jspecify .annotations .Nullable ;
31
31
import org .junit .jupiter .api .Test ;
32
32
33
+ import org .junitpioneer .jupiter .ClearSystemProperty ;
34
+ import org .junitpioneer .jupiter .SetSystemProperty ;
35
+ import org .springframework .aot .AotDetector ;
33
36
import org .springframework .aot .generate .ClassNameGenerator ;
34
37
import org .springframework .aot .generate .DefaultGenerationContext ;
35
38
import org .springframework .aot .generate .GenerationContext ;
56
59
57
60
/**
58
61
* @author Christoph Strobl
62
+ * @author Hyunsang Han
59
63
*/
60
64
class JpaRepositoryRegistrationAotProcessorUnitTests {
61
65
62
66
@ Test // GH-2628
63
67
void aotProcessorMustNotRegisterDomainTypes () {
64
68
65
- GenerationContext ctx = new DefaultGenerationContext (new ClassNameGenerator (ClassName .OBJECT ),
66
- new InMemoryGeneratedFiles ());
69
+ GenerationContext ctx = createGenerationContext ();
67
70
68
71
new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
69
72
.contribute (new DummyAotRepositoryContext (null ) {
@@ -79,8 +82,7 @@ public Set<Class<?>> getResolvedTypes() {
79
82
@ Test // GH-2628
80
83
void aotProcessorMustNotRegisterAnnotations () {
81
84
82
- GenerationContext ctx = new DefaultGenerationContext (new ClassNameGenerator (ClassName .OBJECT ),
83
- new InMemoryGeneratedFiles ());
85
+ GenerationContext ctx = createGenerationContext ();
84
86
85
87
new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
86
88
.contribute (new DummyAotRepositoryContext (null ) {
@@ -99,8 +101,7 @@ public Set<MergedAnnotation<Annotation>> getResolvedAnnotations() {
99
101
@ Test // GH-3838
100
102
void repositoryProcessorShouldConsiderPersistenceManagedTypes () {
101
103
102
- GenerationContext ctx = new DefaultGenerationContext (new ClassNameGenerator (ClassName .OBJECT ),
103
- new InMemoryGeneratedFiles ());
104
+ GenerationContext ctx = createGenerationContext ();
104
105
105
106
GenericApplicationContext context = new GenericApplicationContext ();
106
107
context .registerBean (PersistenceManagedTypes .class , () -> {
@@ -126,12 +127,83 @@ public List<String> getManagedPackages() {
126
127
context .getEnvironment ().getPropertySources ()
127
128
.addFirst (new MockPropertySource ().withProperty (AotContext .GENERATED_REPOSITORIES_ENABLED , "true" ));
128
129
129
- JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension .JpaRepositoryRegistrationAotProcessor ()
130
- .contribute (new DummyAotRepositoryContext (context ), ctx );
130
+ JpaRepositoryContributor contributor = createContributor (new DummyAotRepositoryContext (context ), ctx );
131
131
132
132
assertThat (contributor .getMetamodel ().managedType (Person .class )).isNotNull ();
133
133
}
134
134
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
+
135
207
@ Entity
136
208
static class Person {
137
209
@ Id Long id ;
0 commit comments