|
20 | 20 | import org.junit.jupiter.api.AfterEach;
|
21 | 21 | import org.junit.jupiter.api.BeforeEach;
|
22 | 22 | import org.junit.jupiter.api.Test;
|
| 23 | +import org.openrewrite.java.RemoveAnnotation; |
23 | 24 | import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
|
24 | 25 | import org.springframework.ide.vscode.boot.java.Annotations;
|
| 26 | +import org.springframework.ide.vscode.boot.java.Boot4JavaProblemType; |
25 | 27 | import org.springframework.ide.vscode.boot.java.reconcilers.BeanRegistrarDeclarationReconciler;
|
26 | 28 | import org.springframework.ide.vscode.boot.java.reconcilers.JdtAstReconciler;
|
27 | 29 | import org.springframework.ide.vscode.commons.java.IClasspathUtil;
|
| 30 | +import org.springframework.ide.vscode.commons.languageserver.quickfix.Quickfix.QuickfixData; |
28 | 31 | import org.springframework.ide.vscode.commons.languageserver.quickfix.QuickfixRegistry;
|
29 | 32 | import org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileProblem;
|
30 | 33 | import org.springframework.ide.vscode.commons.protocol.spring.AnnotationAttributeValue;
|
31 | 34 | import org.springframework.ide.vscode.commons.protocol.spring.AnnotationMetadata;
|
32 | 35 | import org.springframework.ide.vscode.commons.protocol.spring.Bean;
|
| 36 | +import org.springframework.ide.vscode.commons.rewrite.java.FixDescriptor; |
33 | 37 |
|
34 | 38 | public class BeanRegistrarDeclarationReconcilerTest extends BaseReconcilerTest {
|
35 | 39 |
|
@@ -268,4 +272,121 @@ public void register(BeanRegistry registry, Environment env) {
|
268 | 272 |
|
269 | 273 | assertEquals(1, problems.size());
|
270 | 274 | }
|
| 275 | + |
| 276 | + @Test |
| 277 | + void componentAnnotationOver() throws Throwable { |
| 278 | + String source = """ |
| 279 | + package com.example.demo; |
| 280 | + |
| 281 | + import org.springframework.beans.factory.BeanRegistrar; |
| 282 | + import org.springframework.beans.factory.BeanRegistry; |
| 283 | + import org.springframework.core.env.Environment; |
| 284 | + import org.springframework.stereotype.Component; |
| 285 | + |
| 286 | + @Component |
| 287 | + public class MyBeanRegistrar implements BeanRegistrar { |
| 288 | + |
| 289 | + public void register(BeanRegistry registry, Environment env) { |
| 290 | + } |
| 291 | + |
| 292 | + } |
| 293 | + """; |
| 294 | + List<ReconcileProblem> problems = reconcile(() -> { |
| 295 | + SpringMetamodelIndex springIndex = new SpringMetamodelIndex(); |
| 296 | + |
| 297 | + Location l = new Location(); |
| 298 | + Path sourceFolder = IClasspathUtil.getSourceFolders(project.getClasspath()).map(f -> f.toPath()).filter(p -> p.endsWith(Path.of("src", "main", "java"))).findFirst().orElseThrow(); |
| 299 | + l.setUri(sourceFolder.resolve("com/example/demo/B.java").toUri().toASCIIString()); |
| 300 | + |
| 301 | + AnnotationMetadata annotationMetadata = new AnnotationMetadata(Annotations.CONFIGURATION, false, null, Map.of()); |
| 302 | + AnnotationMetadata importMetadata = new AnnotationMetadata(Annotations.IMPORT, false, null, |
| 303 | + Map.of("value", new AnnotationAttributeValue[] { |
| 304 | + new AnnotationAttributeValue("com.example.demo.MyBeanRegistrar", null) })); |
| 305 | + AnnotationMetadata[] annotations = new AnnotationMetadata[] {annotationMetadata, importMetadata}; |
| 306 | + Bean configBean = new Bean("conf", "com.example.demo.Conf", l, null, null, annotations, true, "symbolLabel"); |
| 307 | + Bean[] beans = new Bean[] {configBean}; |
| 308 | + springIndex.updateBeans(getProjectName(), beans); |
| 309 | + |
| 310 | + BeanRegistrarDeclarationReconciler r = new BeanRegistrarDeclarationReconciler(new QuickfixRegistry(), springIndex); |
| 311 | + |
| 312 | + return r; |
| 313 | + }, "A.java", source, false); |
| 314 | + |
| 315 | + assertEquals(1, problems.size()); |
| 316 | + |
| 317 | + ReconcileProblem p = problems.get(0); |
| 318 | + |
| 319 | + assertEquals(Boot4JavaProblemType.REGISTRAR_BEAN_INVALID_ANNOTATION.getLabel(), p.getType().getLabel()); |
| 320 | + |
| 321 | + assertEquals(1, p.getQuickfixes().size()); |
| 322 | + |
| 323 | + QuickfixData<?> qf = p.getQuickfixes().get(0); |
| 324 | + |
| 325 | + assertEquals("Remove `@Component`", qf.title); |
| 326 | + |
| 327 | + FixDescriptor fd = (FixDescriptor) qf.params; |
| 328 | + |
| 329 | + assertEquals(RemoveAnnotation.class.getName(), fd.getRecipeId()); |
| 330 | + assertEquals("@org.springframework.stereotype.Component", fd.getParameters().get("annotationPattern")); |
| 331 | + |
| 332 | + } |
| 333 | + |
| 334 | + @Test |
| 335 | + void serviceAnnotationOver() throws Throwable { |
| 336 | + String source = """ |
| 337 | + package com.example.demo; |
| 338 | + |
| 339 | + import org.springframework.beans.factory.BeanRegistrar; |
| 340 | + import org.springframework.beans.factory.BeanRegistry; |
| 341 | + import org.springframework.core.env.Environment; |
| 342 | + import org.springframework.stereotype.Service; |
| 343 | + |
| 344 | + @Service("myService") |
| 345 | + public class MyBeanRegistrar implements BeanRegistrar { |
| 346 | + |
| 347 | + public void register(BeanRegistry registry, Environment env) { |
| 348 | + } |
| 349 | + |
| 350 | + } |
| 351 | + """; |
| 352 | + List<ReconcileProblem> problems = reconcile(() -> { |
| 353 | + SpringMetamodelIndex springIndex = new SpringMetamodelIndex(); |
| 354 | + |
| 355 | + Location l = new Location(); |
| 356 | + Path sourceFolder = IClasspathUtil.getSourceFolders(project.getClasspath()).map(f -> f.toPath()).filter(p -> p.endsWith(Path.of("src", "main", "java"))).findFirst().orElseThrow(); |
| 357 | + l.setUri(sourceFolder.resolve("com/example/demo/B.java").toUri().toASCIIString()); |
| 358 | + |
| 359 | + AnnotationMetadata annotationMetadata = new AnnotationMetadata(Annotations.CONFIGURATION, false, null, Map.of()); |
| 360 | + AnnotationMetadata importMetadata = new AnnotationMetadata(Annotations.IMPORT, false, null, |
| 361 | + Map.of("value", new AnnotationAttributeValue[] { |
| 362 | + new AnnotationAttributeValue("com.example.demo.MyBeanRegistrar", null) })); |
| 363 | + AnnotationMetadata[] annotations = new AnnotationMetadata[] {annotationMetadata, importMetadata}; |
| 364 | + Bean configBean = new Bean("conf", "com.example.demo.Conf", l, null, null, annotations, true, "symbolLabel"); |
| 365 | + Bean[] beans = new Bean[] {configBean}; |
| 366 | + springIndex.updateBeans(getProjectName(), beans); |
| 367 | + |
| 368 | + BeanRegistrarDeclarationReconciler r = new BeanRegistrarDeclarationReconciler(new QuickfixRegistry(), springIndex); |
| 369 | + |
| 370 | + return r; |
| 371 | + }, "A.java", source, false); |
| 372 | + |
| 373 | + assertEquals(1, problems.size()); |
| 374 | + |
| 375 | + ReconcileProblem p = problems.get(0); |
| 376 | + |
| 377 | + assertEquals(Boot4JavaProblemType.REGISTRAR_BEAN_INVALID_ANNOTATION.getLabel(), p.getType().getLabel()); |
| 378 | + |
| 379 | + assertEquals(1, p.getQuickfixes().size()); |
| 380 | + |
| 381 | + QuickfixData<?> qf = p.getQuickfixes().get(0); |
| 382 | + |
| 383 | + assertEquals("Remove `@Service`", qf.title); |
| 384 | + |
| 385 | + FixDescriptor fd = (FixDescriptor) qf.params; |
| 386 | + |
| 387 | + assertEquals(RemoveAnnotation.class.getName(), fd.getRecipeId()); |
| 388 | + assertEquals("@org.springframework.stereotype.Service", fd.getParameters().get("annotationPattern")); |
| 389 | + |
| 390 | + } |
| 391 | + |
271 | 392 | }
|
0 commit comments