|
30 | 30 | import org.jspecify.annotations.Nullable;
|
31 | 31 | import org.junit.jupiter.api.Test;
|
32 | 32 |
|
| 33 | +import org.springframework.aot.AotDetector; |
33 | 34 | import org.springframework.aot.generate.ClassNameGenerator;
|
34 | 35 | import org.springframework.aot.generate.DefaultGenerationContext;
|
35 | 36 | import org.springframework.aot.generate.GenerationContext;
|
|
56 | 57 |
|
57 | 58 | /**
|
58 | 59 | * @author Christoph Strobl
|
| 60 | + * @author Hyunsang Han |
59 | 61 | */
|
60 | 62 | class JpaRepositoryRegistrationAotProcessorUnitTests {
|
61 | 63 |
|
@@ -132,6 +134,104 @@ public List<String> getManagedPackages() {
|
132 | 134 | assertThat(contributor.getMetamodel().managedType(Person.class)).isNotNull();
|
133 | 135 | }
|
134 | 136 |
|
| 137 | + @Test // GH-3899 |
| 138 | + void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() { |
| 139 | + |
| 140 | + GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT), |
| 141 | + new InMemoryGeneratedFiles()); |
| 142 | + |
| 143 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 144 | + |
| 145 | + System.setProperty(AotDetector.AOT_ENABLED, "true"); |
| 146 | + |
| 147 | + try { |
| 148 | + JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor() |
| 149 | + .contribute(new DummyAotRepositoryContext(context) { |
| 150 | + @Override |
| 151 | + public Set<Class<?>> getResolvedTypes() { |
| 152 | + return Collections.singleton(Person.class); |
| 153 | + } |
| 154 | + }, ctx); |
| 155 | + |
| 156 | + assertThat(contributor).isNotNull(); |
| 157 | + } finally { |
| 158 | + System.clearProperty(AotDetector.AOT_ENABLED); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test // GH-3899 |
| 163 | + void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled() { |
| 164 | + |
| 165 | + GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT), |
| 166 | + new InMemoryGeneratedFiles()); |
| 167 | + |
| 168 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 169 | + |
| 170 | + System.clearProperty(AotDetector.AOT_ENABLED); |
| 171 | + |
| 172 | + JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor() |
| 173 | + .contribute(new DummyAotRepositoryContext(context) { |
| 174 | + @Override |
| 175 | + public Set<Class<?>> getResolvedTypes() { |
| 176 | + return Collections.singleton(Person.class); |
| 177 | + } |
| 178 | + }, ctx); |
| 179 | + |
| 180 | + assertThat(contributor).isNull(); |
| 181 | + } |
| 182 | + |
| 183 | + @Test // GH-3899 |
| 184 | + void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty() { |
| 185 | + |
| 186 | + GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT), |
| 187 | + new InMemoryGeneratedFiles()); |
| 188 | + |
| 189 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 190 | + |
| 191 | + System.setProperty(AotDetector.AOT_ENABLED, "true"); |
| 192 | + |
| 193 | + try { |
| 194 | + MockPropertySource propertySource = new MockPropertySource() |
| 195 | + .withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "false"); |
| 196 | + context.getEnvironment().getPropertySources().addFirst(propertySource); |
| 197 | + |
| 198 | + JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor() |
| 199 | + .contribute(new DummyAotRepositoryContext(context) { |
| 200 | + @Override |
| 201 | + public Set<Class<?>> getResolvedTypes() { |
| 202 | + return Collections.singleton(Person.class); |
| 203 | + } |
| 204 | + }, ctx); |
| 205 | + |
| 206 | + assertThat(contributor).isNull(); |
| 207 | + } finally { |
| 208 | + System.clearProperty(AotDetector.AOT_ENABLED); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Test // GH-3899 |
| 213 | + void repositoryProcessorShouldEnableWhenExplicitlySetToTrue() { |
| 214 | + |
| 215 | + GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT), |
| 216 | + new InMemoryGeneratedFiles()); |
| 217 | + |
| 218 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 219 | + |
| 220 | + MockPropertySource propertySource = new MockPropertySource() |
| 221 | + .withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "true"); |
| 222 | + context.getEnvironment().getPropertySources().addFirst(propertySource); |
| 223 | + |
| 224 | + JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor() |
| 225 | + .contribute(new DummyAotRepositoryContext(context) { |
| 226 | + @Override |
| 227 | + public Set<Class<?>> getResolvedTypes() { |
| 228 | + return Collections.singleton(Person.class); |
| 229 | + } |
| 230 | + }, ctx); |
| 231 | + |
| 232 | + assertThat(contributor).isNotNull(); |
| 233 | + } |
| 234 | + |
135 | 235 | @Entity
|
136 | 236 | static class Person {
|
137 | 237 | @Id Long id;
|
|
0 commit comments