|
| 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 | +import static org.junit.Assert.assertTrue; |
| 15 | + |
| 16 | +import java.io.File; |
| 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.Position; |
| 23 | +import org.eclipse.lsp4j.Range; |
| 24 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Import; |
| 30 | +import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; |
| 31 | +import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest; |
| 32 | +import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf; |
| 33 | +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
| 34 | +import org.springframework.ide.vscode.commons.util.text.LanguageId; |
| 35 | +import org.springframework.ide.vscode.languageserver.testharness.Editor; |
| 36 | +import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness; |
| 37 | +import org.springframework.ide.vscode.project.harness.ProjectsHarness; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Martin Lippert |
| 42 | + */ |
| 43 | +@ExtendWith(SpringExtension.class) |
| 44 | +@BootLanguageServerTest |
| 45 | +@Import(SymbolProviderTestConf.class) |
| 46 | +public class ProfileReferencesProviderTest { |
| 47 | + |
| 48 | + @Autowired private BootLanguageServerHarness harness; |
| 49 | + @Autowired private JavaProjectFinder projectFinder; |
| 50 | + @Autowired private SpringSymbolIndex indexer; |
| 51 | + |
| 52 | + private File directory; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + public void setup() throws Exception { |
| 56 | + harness.intialize(null); |
| 57 | + |
| 58 | + directory = new File(ProjectsHarness.class.getResource("/test-projects/test-spring-indexing/").toURI()); |
| 59 | + |
| 60 | + String projectDir = directory.toURI().toString(); |
| 61 | + projectFinder.find(new TextDocumentIdentifier(projectDir)).get(); |
| 62 | + |
| 63 | + CompletableFuture<Void> initProject = indexer.waitOperation(); |
| 64 | + initProject.get(5, TimeUnit.SECONDS); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testProfileRefersToOtherProfiles() throws Exception { |
| 69 | + String tempJavaDocUri = directory.toPath().resolve("src/main/java/org/test/TempClass.java").toUri().toString(); |
| 70 | + |
| 71 | + Editor editor = harness.newEditor(LanguageId.JAVA, """ |
| 72 | + package org.test; |
| 73 | +
|
| 74 | + import org.springframework.stereotype.Component; |
| 75 | + import org.springframework.context.annotation.Profile; |
| 76 | +
|
| 77 | + @Component |
| 78 | + @Profile("pro<*>file1") |
| 79 | + public class TestDependsOnClass { |
| 80 | + }""", tempJavaDocUri); |
| 81 | + |
| 82 | + List<? extends Location> references = editor.getReferences(); |
| 83 | + assertEquals(2, references.size()); |
| 84 | + |
| 85 | + String expectedDefinitionUri1 = directory.toPath().resolve("src/main/java/org/test/profiles/ProfilesClass1.java").toUri().toString(); |
| 86 | + Location expectedLocation1 = new Location(expectedDefinitionUri1, |
| 87 | + new Range(new Position(6, 0), new Position(6, 20))); |
| 88 | + |
| 89 | + assertTrue(references.contains(expectedLocation1)); |
| 90 | + |
| 91 | + String expectedDefinitionUri2 = directory.toPath().resolve("src/main/java/org/test/profiles/ProfilesClassWithArray.java").toUri().toString(); |
| 92 | + Location expectedLocation2 = new Location(expectedDefinitionUri2, |
| 93 | + new Range(new Position(6, 0), new Position(6, 42))); |
| 94 | + |
| 95 | + assertTrue(references.contains(expectedLocation2)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testProfileWithinArrayRefersToOtherProfiles() throws Exception { |
| 100 | + String tempJavaDocUri = directory.toPath().resolve("src/main/java/org/test/TempClass.java").toUri().toString(); |
| 101 | + |
| 102 | + Editor editor = harness.newEditor(LanguageId.JAVA, """ |
| 103 | + package org.test; |
| 104 | +
|
| 105 | + import org.springframework.stereotype.Component; |
| 106 | + import org.springframework.context.annotation.Profile; |
| 107 | +
|
| 108 | + @Component |
| 109 | + @Profile(value = {"profile2", "pro<*>file1"}) |
| 110 | + public class TestDependsOnClass { |
| 111 | + }""", tempJavaDocUri); |
| 112 | + |
| 113 | + List<? extends Location> references = editor.getReferences(); |
| 114 | + assertEquals(2, references.size()); |
| 115 | + |
| 116 | + String expectedDefinitionUri1 = directory.toPath().resolve("src/main/java/org/test/profiles/ProfilesClass1.java").toUri().toString(); |
| 117 | + Location expectedLocation1 = new Location(expectedDefinitionUri1, |
| 118 | + new Range(new Position(6, 0), new Position(6, 20))); |
| 119 | + |
| 120 | + assertTrue(references.contains(expectedLocation1)); |
| 121 | + |
| 122 | + String expectedDefinitionUri2 = directory.toPath().resolve("src/main/java/org/test/profiles/ProfilesClassWithArray.java").toUri().toString(); |
| 123 | + Location expectedLocation2 = new Location(expectedDefinitionUri2, |
| 124 | + new Range(new Position(6, 0), new Position(6, 42))); |
| 125 | + |
| 126 | + assertTrue(references.contains(expectedLocation2)); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments