1111package org .springframework .ide .vscode .boot .java .beans .test ;
1212
1313import static org .junit .Assert .assertEquals ;
14+ import static org .junit .Assert .assertNull ;
1415import static org .junit .Assert .assertTrue ;
1516
1617import java .io .File ;
1718import java .util .List ;
19+ import java .util .Map ;
1820import java .util .concurrent .CompletableFuture ;
1921import java .util .concurrent .TimeUnit ;
2022
21- import org .apache .commons .lang3 .ArrayUtils ;
2223import org .eclipse .lsp4j .Location ;
2324import org .eclipse .lsp4j .Position ;
2425import org .eclipse .lsp4j .Range ;
3435import org .springframework .ide .vscode .boot .index .SpringMetamodelIndex ;
3536import org .springframework .ide .vscode .commons .java .IJavaProject ;
3637import org .springframework .ide .vscode .commons .languageserver .java .JavaProjectFinder ;
38+ import org .springframework .ide .vscode .commons .protocol .spring .AnnotationAttributeValue ;
3739import org .springframework .ide .vscode .commons .protocol .spring .AnnotationMetadata ;
3840import org .springframework .ide .vscode .commons .protocol .spring .Bean ;
41+ import org .springframework .ide .vscode .commons .protocol .spring .InjectionPoint ;
3942import org .springframework .ide .vscode .commons .util .text .LanguageId ;
4043import org .springframework .ide .vscode .languageserver .testharness .Editor ;
4144import org .springframework .ide .vscode .project .harness .BootLanguageServerHarness ;
@@ -61,6 +64,11 @@ public class NamedReferencesProviderTest {
6164
6265 private String tempJavaDocUri1 ;
6366 private String tempJavaDocUri ;
67+ private String tempJavaDocUri2 ;
68+ private Location locationNamedAnnotation1 ;
69+ private Location locationNamedAnnotation2 ;
70+ private Bean bean2 ;
71+ private Location point1NamedAnnotationLocation ;
6472
6573 @ BeforeEach
6674 public void setup () throws Exception {
@@ -74,63 +82,76 @@ public void setup() throws Exception {
7482 CompletableFuture <Void > initProject = indexer .waitOperation ();
7583 initProject .get (5 , TimeUnit .SECONDS );
7684
77- tempJavaDocUri = directory .toPath ().resolve ("src/main/java/org/test/TestDependsOnClass.java" ).toUri ().toString ();
85+ tempJavaDocUri = directory .toPath ().resolve ("src/main/java/org/test/TestDependsOnClass.java" ).toUri ().toString ();
7886 tempJavaDocUri1 = directory .toPath ().resolve ("src/main/java/org/test/TempClass1.java" ).toUri ().toString ();
87+ tempJavaDocUri2 = directory .toPath ().resolve ("src/main/java/org/test/TempClass2.java" ).toUri ().toString ();
7988
80- bean1 = new Bean ("bean1" , "type1" , new Location (tempJavaDocUri1 , new Range (new Position (1 ,1 ), new Position (1 , 20 ))), null , null , new AnnotationMetadata [] {});
89+ locationNamedAnnotation1 = new Location (tempJavaDocUri1 , new Range (new Position (1 , 10 ), new Position (1 , 20 )));
90+ locationNamedAnnotation2 = new Location (tempJavaDocUri2 , new Range (new Position (2 , 10 ), new Position (2 , 20 )));
8191
82- Bean [] beans = ArrayUtils .add (springIndex .getBeansOfProject (project .getElementName ()), bean1 );
83- springIndex .updateBeans (project .getElementName (), beans );
92+ AnnotationMetadata annotationBean1 = new AnnotationMetadata ("jakarta.inject.Named" , false , null , Map .of ("value" , new AnnotationAttributeValue [] {new AnnotationAttributeValue ("named1" , locationNamedAnnotation1 )}));
93+ AnnotationMetadata annotationBean2 = new AnnotationMetadata ("jakarta.inject.Named" , false , null , Map .of ("value" , new AnnotationAttributeValue [] {new AnnotationAttributeValue ("named2" , locationNamedAnnotation2 )}));
94+
95+ point1NamedAnnotationLocation = new Location (tempJavaDocUri1 , new Range (new Position (20 , 10 ), new Position (20 , 20 )));
96+ AnnotationMetadata point1Metadata = new AnnotationMetadata ("jakarta.inject.Named" , false , null , Map .of ("value" , new AnnotationAttributeValue [] {new AnnotationAttributeValue ("namedAtPoint1" , point1NamedAnnotationLocation )}));
97+
98+ InjectionPoint point1 = new InjectionPoint ("point1" , "type1" , null , new AnnotationMetadata [] {point1Metadata });
99+
100+ bean1 = new Bean ("bean1" , "type1" , new Location (tempJavaDocUri1 , new Range (new Position (1 ,1 ), new Position (1 , 20 ))), new InjectionPoint [] {point1 }, null , new AnnotationMetadata [] {annotationBean1 });
101+ bean2 = new Bean ("bean2" , "type2" , new Location (tempJavaDocUri2 , new Range (new Position (1 ,1 ), new Position (1 , 20 ))), null , null , new AnnotationMetadata [] {annotationBean2 });
102+
103+ springIndex .updateBeans (project .getElementName (), new Bean [] {bean1 , bean2 });
84104 }
85105
86106 @ Test
87- public void testNamedRefersToBean () throws Exception {
107+ public void testNamedRefersToNamedBean () throws Exception {
88108 Editor editor = harness .newEditor (LanguageId .JAVA , """
89109 package org.test;
90110
91111 import jakarta.inject.Named;
92112
93- @Named("be <*>an1 ")
113+ @Named("na <*>med1 ")
94114 public class TestDependsOnClass {
95115 }""" , tempJavaDocUri );
96116
97- Bean [] beans = springIndex .getBeansWithName (project .getElementName (), "bean1" );
98- assertEquals (1 , beans .length );
99-
100- Location expectedLocation = new Location (tempJavaDocUri1 ,
101- beans [0 ].getLocation ().getRange ());
102-
103117 List <? extends Location > references = editor .getReferences ();
104118 assertEquals (1 , references .size ());
105119
106120 Location foundLocation = references .get (0 );
107- assertEquals (expectedLocation , foundLocation );
121+ assertEquals (locationNamedAnnotation1 , foundLocation );
108122 }
109123
110124 @ Test
111- public void testNamedRefersToOtherNamedValues () throws Exception {
125+ public void testNamedNotRefersToPureSpringBean () throws Exception {
112126 Editor editor = harness .newEditor (LanguageId .JAVA , """
113127 package org.test;
114128
115129 import jakarta.inject.Named;
116130
117- @Named("specificFin <*>der ")
131+ @Named("be <*>an1 ")
118132 public class TestDependsOnClass {
119133 }""" , tempJavaDocUri );
120134
121- String expectedDefinitionUri1 = directory . toPath (). resolve ( "src/main/java/org/test/jakarta/SimpleMovieLister.java" ). toUri (). toString ();
122- Location expectedLocation1 = new Location ( expectedDefinitionUri1 ,
123- new Range ( new Position ( 27 , 45 ), new Position ( 27 , 61 )));
135+ List <? extends Location > references = editor . getReferences ();
136+ assertNull ( references );
137+ }
124138
125- String expectedDefinitionUri2 = directory .toPath ().resolve ("src/main/java/org/test/javax/SimpleMovieLister.java" ).toUri ().toString ();
126- Location expectedLocation2 = new Location (expectedDefinitionUri2 ,
127- new Range (new Position (27 , 45 ), new Position (27 , 61 )));
139+ @ Test
140+ public void testNamedRefersToNamedInjectionPoints () throws Exception {
141+ Editor editor = harness .newEditor (LanguageId .JAVA , """
142+ package org.test;
143+
144+ import jakarta.inject.Named;
128145
146+ @Named("namedAt<*>Point1")
147+ public class TestDependsOnClass {
148+ }""" , tempJavaDocUri );
149+
129150 List <? extends Location > references = editor .getReferences ();
130- assertEquals (2 , references .size ());
151+ assertEquals (1 , references .size ());
131152
132- assertTrue ( references .contains ( expectedLocation1 ) );
133- assertTrue ( references . contains ( expectedLocation2 ) );
153+ Location foundLocation = references .get ( 0 );
154+ assertEquals ( point1NamedAnnotationLocation , foundLocation );
134155 }
135156
136157}
0 commit comments