Skip to content

Commit 2f87eb6

Browse files
committed
Improve tests
1 parent ed43738 commit 2f87eb6

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

java-frontend/src/test/files/springcontext/AutowiredConstructorWithUnannotatedConstructor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class AutowiredConstructorWithUnannotatedConstructor {
1818
}
1919

2020
// Spring ignores this constructor — its parameters must not appear as dependencies
21-
AutowiredConstructorWithUnannotatedConstructor(ApplicationContext applicationContext) {
22-
this.applicationContext = applicationContext;
21+
AutowiredConstructorWithUnannotatedConstructor(ApplicationContext ignoredContext) {
22+
this.applicationContext = ignoredContext;
2323
this.environment = null;
2424
}
2525
}

java-frontend/src/test/java/org/sonar/java/model/springcontext/BeanDefinitionGathererTest.java

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ void anonymous_class_is_skipped() {
151151
// Anonymous class (no simpleName) should be skipped — it would not be registered as a bean
152152
// SpringBootApplication itself is not a stereotype bean
153153
assertThat(model.getBeanDefinitionRegistry().getByName("")).isEmpty();
154+
assertThat(model.getTypeToBeanNamesIndex().getNamesForType("")).isEmpty();
154155
}
155156

156157
@Test
@@ -332,7 +333,8 @@ void leaveFile_writes_beans_to_cache() {
332333
.contains(encodedName)
333334
.contains("checks.spring.context.SimpleComponent")
334335
.contains("checks.spring.context")
335-
.contains("false");
336+
.contains("false")
337+
.endsWith("|checks.spring.context.SimpleComponent");
336338
}
337339

338340
@Test
@@ -360,6 +362,8 @@ void scanWithoutParsing_returns_true_and_restores_beans_on_cache_hit() {
360362
assertThat(beans).hasSize(1);
361363
assertThat(beans.get(0).getType()).isEqualTo("checks.spring.context.SimpleComponent");
362364
assertThat(beans.get(0).isPrimary()).isFalse();
365+
assertThat(model.getTypeToBeanNamesIndex().getNamesForType("checks.spring.context.SimpleComponent"))
366+
.containsOnly("simpleComponent");
363367
}
364368

365369
@Test
@@ -515,14 +519,49 @@ void stereotype_bean_is_registered_under_its_own_type(String filePath) {
515519
}
516520

517521
@Test
518-
void bean_is_registered_under_implemented_interface() {
522+
void bean_is_registered_under_full_type_hierarchy() {
519523
scan("src/test/files/springcontext/ComponentImplementingInterface.java");
520524

521525
var index = model.getTypeToBeanNamesIndex();
522526
assertThat(index.getNamesForType("checks.spring.context.ComponentImplementingInterface"))
523527
.containsOnly("componentImplementingInterface");
524528
assertThat(index.getNamesForType("org.springframework.context.ApplicationContextAware"))
525529
.containsOnly("componentImplementingInterface");
530+
assertThat(index.getNamesForType("org.springframework.beans.factory.Aware"))
531+
.containsOnly("componentImplementingInterface");
532+
}
533+
534+
@Test
535+
void scanWithoutParsing_restores_full_type_hierarchy_from_cache() {
536+
InputFile inputFile = TestUtils.inputFile(new File("src/test/files/springcontext/ComponentImplementingInterface.java"));
537+
String cacheKey = "java:spring:bean-definitions:" + inputFile.key();
538+
String encodedName = Base64.getEncoder().encodeToString("componentImplementingInterface".getBytes(StandardCharsets.UTF_8));
539+
String serialized = encodedName + "|checks.spring.context.ComponentImplementingInterface|checks.spring.context|8:6:8:36|false|"
540+
+ "|checks.spring.context.ComponentImplementingInterface"
541+
+ ";org.springframework.context.ApplicationContextAware"
542+
+ ";org.springframework.beans.factory.Aware";
543+
544+
JavaReadCache readCache = mock(JavaReadCache.class);
545+
when(readCache.readBytes(cacheKey)).thenReturn(serialized.getBytes(StandardCharsets.UTF_8));
546+
CacheContext cacheContext = mockCacheContext(readCache, mock(JavaWriteCache.class));
547+
548+
InputFileScannerContext context = mock(InputFileScannerContext.class);
549+
when(context.getInputFile()).thenReturn(inputFile);
550+
when(context.getCacheContext()).thenReturn(cacheContext);
551+
552+
assertThat(gatherer.scanWithoutParsing(context)).isTrue();
553+
554+
ModuleScannerContext moduleScannerContext = mock(ModuleScannerContext.class);
555+
when(moduleScannerContext.getModuleKey()).thenReturn("");
556+
gatherer.gatherSpringContextData(moduleScannerContext, model);
557+
558+
var index = model.getTypeToBeanNamesIndex();
559+
assertThat(index.getNamesForType("checks.spring.context.ComponentImplementingInterface"))
560+
.containsOnly("componentImplementingInterface");
561+
assertThat(index.getNamesForType("org.springframework.context.ApplicationContextAware"))
562+
.containsOnly("componentImplementingInterface");
563+
assertThat(index.getNamesForType("org.springframework.beans.factory.Aware"))
564+
.containsOnly("componentImplementingInterface");
526565
}
527566

528567
@Test
@@ -583,13 +622,6 @@ void index_gatherer_skipped_when_spring_not_in_classpath() {
583622
.isEmpty();
584623
}
585624

586-
@Test
587-
void index_anonymous_class_is_skipped() {
588-
scan("src/test/files/springcontext/SpringBootAppWithAnonymousClass.java");
589-
590-
assertThat(model.getTypeToBeanNamesIndex().getNamesForType("")).isEmpty();
591-
}
592-
593625
private static String beanClassNameFrom(String filePath) {
594626
return filePath.substring(filePath.lastIndexOf('/') + 1, filePath.lastIndexOf('.'));
595627
}

0 commit comments

Comments
 (0)