|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Broadcom |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.vscode.boot.java.beans.test; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import org.eclipse.lsp4j.Location; |
| 22 | +import org.eclipse.lsp4j.LocationLink; |
| 23 | +import org.eclipse.lsp4j.Position; |
| 24 | +import org.eclipse.lsp4j.Range; |
| 25 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.context.annotation.Import; |
| 31 | +import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; |
| 32 | +import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest; |
| 33 | +import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf; |
| 34 | +import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; |
| 35 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 36 | +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
| 37 | +import org.springframework.ide.vscode.commons.protocol.spring.Bean; |
| 38 | +import org.springframework.ide.vscode.commons.util.text.LanguageId; |
| 39 | +import org.springframework.ide.vscode.languageserver.testharness.Editor; |
| 40 | +import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness; |
| 41 | +import org.springframework.ide.vscode.project.harness.ProjectsHarness; |
| 42 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Martin Lippert |
| 46 | + */ |
| 47 | +@ExtendWith(SpringExtension.class) |
| 48 | +@BootLanguageServerTest |
| 49 | +@Import(SymbolProviderTestConf.class) |
| 50 | +public class DependsOnDefinitionProviderTest { |
| 51 | + |
| 52 | + @Autowired private BootLanguageServerHarness harness; |
| 53 | + @Autowired private JavaProjectFinder projectFinder; |
| 54 | + @Autowired private SpringMetamodelIndex springIndex; |
| 55 | + @Autowired private SpringSymbolIndex indexer; |
| 56 | + |
| 57 | + private File directory; |
| 58 | + private IJavaProject project; |
| 59 | + |
| 60 | + @BeforeEach |
| 61 | + public void setup() throws Exception { |
| 62 | + harness.intialize(null); |
| 63 | + |
| 64 | + directory = new File(ProjectsHarness.class.getResource("/test-projects/test-spring-indexing/").toURI()); |
| 65 | + |
| 66 | + String projectDir = directory.toURI().toString(); |
| 67 | + project = projectFinder.find(new TextDocumentIdentifier(projectDir)).get(); |
| 68 | + |
| 69 | + CompletableFuture<Void> initProject = indexer.waitOperation(); |
| 70 | + initProject.get(5, TimeUnit.SECONDS); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testSingleDependsOnBeanDefinitionLink() throws Exception { |
| 75 | + String tempJavaDocUri = directory.toPath().resolve("src/main/java/org/test/TempClass.java").toUri().toString(); |
| 76 | + |
| 77 | + Editor editor = harness.newEditor(LanguageId.JAVA, """ |
| 78 | + package org.test; |
| 79 | +
|
| 80 | + import org.springframework.context.annotation.DependsOn; |
| 81 | +
|
| 82 | + @Component |
| 83 | + @DependsOn("bean1") |
| 84 | + public class TestDependsOnClass { |
| 85 | + }""", tempJavaDocUri); |
| 86 | + |
| 87 | + String expectedDefinitionUri = directory.toPath().resolve("src/main/java/org/test/MainClass.java").toUri().toString(); |
| 88 | + |
| 89 | + Bean[] beans = springIndex.getBeansWithName(project.getElementName(), "bean1"); |
| 90 | + assertEquals(1, beans.length); |
| 91 | + |
| 92 | + LocationLink expectedLocation = new LocationLink(expectedDefinitionUri, |
| 93 | + beans[0].getLocation().getRange(), beans[0].getLocation().getRange(), |
| 94 | + null); |
| 95 | + |
| 96 | + editor.assertLinkTargets("bean1", List.of(expectedLocation)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testMultipleDependsOnBeanDefinitionLink() throws Exception { |
| 101 | + String tempJavaDocUri = directory.toPath().resolve("src/main/java/org/test/TempClass.java").toUri().toString(); |
| 102 | + |
| 103 | + Editor editor = harness.newEditor(LanguageId.JAVA, """ |
| 104 | + package org.test; |
| 105 | +
|
| 106 | + import org.springframework.context.annotation.DependsOn; |
| 107 | +
|
| 108 | + @Component |
| 109 | + @DependsOn({"bean1", "bean2"}) |
| 110 | + public class TestDependsOnClass { |
| 111 | + }""", tempJavaDocUri); |
| 112 | + |
| 113 | + String expectedDefinitionUri = directory.toPath().resolve("src/main/java/org/test/MainClass.java").toUri().toString(); |
| 114 | + |
| 115 | + Bean[] beans = springIndex.getBeansWithName(project.getElementName(), "bean1"); |
| 116 | + assertEquals(1, beans.length); |
| 117 | + |
| 118 | + LocationLink expectedLocation = new LocationLink(expectedDefinitionUri, |
| 119 | + beans[0].getLocation().getRange(), beans[0].getLocation().getRange(), |
| 120 | + null); |
| 121 | + |
| 122 | + editor.assertLinkTargets("bean1", List.of(expectedLocation)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testDependsOnWithMultipleBeanDefinitionLinks() throws Exception { |
| 127 | + String tempJavaDocUri = directory.toPath().resolve("src/main/java/org/test/TempClass.java").toUri().toString(); |
| 128 | + |
| 129 | + Editor editor = harness.newEditor(LanguageId.JAVA, """ |
| 130 | + package org.test; |
| 131 | +
|
| 132 | + import org.springframework.context.annotation.DependsOn; |
| 133 | +
|
| 134 | + @Component |
| 135 | + @DependsOn("bean1") |
| 136 | + public class TestDependsOnClass { |
| 137 | + }""", tempJavaDocUri); |
| 138 | + |
| 139 | + String expectedDefinitionUri = directory.toPath().resolve("src/main/java/org/test/MainClass.java").toUri().toString(); |
| 140 | + |
| 141 | + List<Bean> beansOfDoc = new ArrayList<>(List.of(springIndex.getBeansOfDocument(expectedDefinitionUri))); |
| 142 | + beansOfDoc.add(new Bean("bean1", "type", new Location(expectedDefinitionUri, new Range(new Position(20, 1), new Position(20, 10))), null, null, null)); |
| 143 | + springIndex.updateBeans(project.getElementName(), expectedDefinitionUri, beansOfDoc.toArray(new Bean[0])); |
| 144 | + |
| 145 | + Bean[] beans = springIndex.getBeansWithName(project.getElementName(), "bean1"); |
| 146 | + assertEquals(2, beans.length); |
| 147 | + |
| 148 | + LocationLink expectedLocation1 = new LocationLink(expectedDefinitionUri, |
| 149 | + beans[0].getLocation().getRange(), beans[0].getLocation().getRange(), |
| 150 | + null); |
| 151 | + |
| 152 | + LocationLink expectedLocation2 = new LocationLink(expectedDefinitionUri, |
| 153 | + beans[1].getLocation().getRange(), beans[1].getLocation().getRange(), |
| 154 | + null); |
| 155 | + |
| 156 | + editor.assertLinkTargets("bean1", List.of(expectedLocation1, expectedLocation2)); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments