Skip to content

Commit 0bcbf06

Browse files
committed
stability improvements for tests around conditional on resource
1 parent 1c64204 commit 0bcbf06

File tree

2 files changed

+46
-116
lines changed

2 files changed

+46
-116
lines changed

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/conditionals/test/ConditionalOnResourceCompletionTest.java

Lines changed: 20 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2024 Broadcom, Inc.
2+
* Copyright (c) 2024, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -13,42 +13,26 @@
1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414
import static org.junit.jupiter.api.Assertions.fail;
1515

16+
import java.io.File;
1617
import java.io.InputStream;
1718
import java.nio.charset.Charset;
18-
import java.util.Collection;
19-
import java.util.Collections;
2019
import java.util.Comparator;
2120
import java.util.List;
22-
import java.util.Optional;
21+
import java.util.concurrent.CompletableFuture;
22+
import java.util.concurrent.TimeUnit;
2323

2424
import org.apache.commons.io.IOUtils;
2525
import org.eclipse.lsp4j.CompletionItem;
2626
import org.eclipse.lsp4j.TextDocumentIdentifier;
27-
import org.gradle.internal.impldep.com.google.common.collect.ImmutableList;
2827
import org.junit.jupiter.api.BeforeEach;
2928
import org.junit.jupiter.api.Test;
3029
import org.junit.jupiter.api.extension.ExtendWith;
3130
import org.springframework.beans.factory.annotation.Autowired;
32-
import org.springframework.context.annotation.Bean;
33-
import org.springframework.context.annotation.Configuration;
3431
import org.springframework.context.annotation.Import;
35-
import org.springframework.ide.vscode.boot.app.BootLanguageServerParams;
36-
import org.springframework.ide.vscode.boot.bootiful.AdHocPropertyHarnessTestConf;
32+
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
3733
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
38-
import org.springframework.ide.vscode.boot.editor.harness.AdHocPropertyHarness;
39-
import org.springframework.ide.vscode.boot.editor.harness.PropertyIndexHarness;
40-
import org.springframework.ide.vscode.boot.index.cache.IndexCache;
41-
import org.springframework.ide.vscode.boot.index.cache.IndexCacheVoid;
42-
import org.springframework.ide.vscode.boot.java.links.SourceLinkFactory;
43-
import org.springframework.ide.vscode.boot.java.links.SourceLinks;
44-
import org.springframework.ide.vscode.boot.java.utils.CompilationUnitCache;
45-
import org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry;
46-
import org.springframework.ide.vscode.commons.java.IJavaProject;
34+
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
4735
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
48-
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
49-
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
50-
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
51-
import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject;
5236
import org.springframework.ide.vscode.commons.util.text.LanguageId;
5337
import org.springframework.ide.vscode.languageserver.testharness.Editor;
5438
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
@@ -60,73 +44,29 @@
6044
*/
6145
@ExtendWith(SpringExtension.class)
6246
@BootLanguageServerTest
63-
@Import({AdHocPropertyHarnessTestConf.class, ConditionalOnResourceCompletionTest.TestConf.class})
47+
@Import(SymbolProviderTestConf.class)
6448
public class ConditionalOnResourceCompletionTest {
6549

6650
@Autowired private BootLanguageServerHarness harness;
6751
@Autowired private JavaProjectFinder projectFinder;
52+
@Autowired private SpringSymbolIndex indexer;
6853

6954
private Editor editor;
70-
71-
@Autowired private PropertyIndexHarness indexHarness;
72-
@Autowired private AdHocPropertyHarness adHocProperties;
73-
74-
@Configuration
75-
static class TestConf {
76-
77-
@Bean MavenJavaProject testProject() throws Exception {
78-
return ProjectsHarness.INSTANCE.mavenProject("test-annotation-conditionalonresource");
79-
}
80-
81-
@Bean PropertyIndexHarness indexHarness(ValueProviderRegistry valueProviders) {
82-
return new PropertyIndexHarness(valueProviders);
83-
}
84-
85-
@Bean JavaProjectFinder projectFinder(MavenJavaProject testProject) {
86-
return new JavaProjectFinder() {
87-
88-
@Override
89-
public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
90-
return Optional.ofNullable(testProject);
91-
}
92-
93-
@Override
94-
public Collection<? extends IJavaProject> all() {
95-
// TODO Auto-generated method stub
96-
return testProject == null ? Collections.emptyList() : ImmutableList.of(testProject);
97-
}
98-
};
99-
}
100-
101-
@Bean BootLanguageServerHarness harness(SimpleLanguageServer server, BootLanguageServerParams serverParams, PropertyIndexHarness indexHarness, JavaProjectFinder projectFinder) throws Exception {
102-
return new BootLanguageServerHarness(server, serverParams, indexHarness, projectFinder, LanguageId.JAVA, ".java");
103-
}
104-
105-
@Bean BootLanguageServerParams serverParams(SimpleLanguageServer server, JavaProjectFinder projectFinder, ValueProviderRegistry valueProviders, PropertyIndexHarness indexHarness) {
106-
BootLanguageServerParams testDefaults = BootLanguageServerHarness.createTestDefault(server, valueProviders);
107-
return new BootLanguageServerParams(
108-
projectFinder,
109-
ProjectObserver.NULL,
110-
indexHarness.getIndexProvider(),
111-
testDefaults.typeUtilProvider
112-
);
113-
}
114-
115-
@Bean IndexCache symbolCache() {
116-
return new IndexCacheVoid();
117-
}
118-
119-
@Bean SourceLinks sourceLinks(SimpleTextDocumentService documents, CompilationUnitCache cuCache) {
120-
return SourceLinkFactory.NO_SOURCE_LINKS;
121-
}
122-
123-
}
55+
private String testSourceUri;
12456

12557
@BeforeEach
12658
public void setup() throws Exception {
127-
IJavaProject testProject = ProjectsHarness.INSTANCE.mavenProject("test-annotation-conditionalonresource");
128-
harness.useProject(testProject);
12959
harness.intialize(null);
60+
61+
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-conditionalonresource/").toURI());
62+
63+
String projectDir = directory.toURI().toString();
64+
projectFinder.find(new TextDocumentIdentifier(projectDir)).get();
65+
66+
testSourceUri = directory.toPath().resolve("src/main/java/org/test/TestConditionalOnResourceCompletion.java").toUri().toString();
67+
68+
CompletableFuture<Void> initProject = indexer.waitOperation();
69+
initProject.get(5, TimeUnit.SECONDS);
13070
}
13171

13272
@Test
@@ -221,7 +161,7 @@ private void prepareCase(String selectedAnnotation, String annotationStatementBe
221161
String content = IOUtils.toString(resource, Charset.defaultCharset());
222162

223163
content = content.replace(selectedAnnotation, annotationStatementBeforeTest);
224-
editor = new Editor(harness, content, LanguageId.JAVA);
164+
editor = harness.newEditor(LanguageId.JAVA, content, testSourceUri);
225165
}
226166

227167
private void assertClasspathCompletions(String... completedAnnotations) throws Exception {

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/conditionals/test/ConditionalOnResourceDefinitionProviderTest.java

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2024 Broadcom, Inc.
2+
* Copyright (c) 2024, 2025 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -10,70 +10,60 @@
1010
*******************************************************************************/
1111
package org.springframework.ide.vscode.boot.java.conditionals.test;
1212

13-
import java.io.IOException;
14-
import java.nio.charset.StandardCharsets;
15-
import java.nio.file.Files;
16-
import java.nio.file.Path;
17-
import java.nio.file.Paths;
18-
import java.nio.file.StandardOpenOption;
19-
import java.util.HashSet;
13+
import java.io.File;
2014
import java.util.List;
21-
import java.util.Set;
15+
import java.util.concurrent.CompletableFuture;
16+
import java.util.concurrent.TimeUnit;
2217

2318
import org.eclipse.lsp4j.LocationLink;
2419
import org.eclipse.lsp4j.Position;
2520
import org.eclipse.lsp4j.Range;
26-
import org.junit.jupiter.api.AfterEach;
21+
import org.eclipse.lsp4j.TextDocumentIdentifier;
2722
import org.junit.jupiter.api.BeforeEach;
2823
import org.junit.jupiter.api.Test;
2924
import org.junit.jupiter.api.extension.ExtendWith;
3025
import org.springframework.beans.factory.annotation.Autowired;
3126
import org.springframework.context.annotation.Import;
32-
import org.springframework.ide.vscode.boot.bootiful.AdHocPropertyHarnessTestConf;
27+
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
3328
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
34-
import org.springframework.ide.vscode.commons.java.IJavaProject;
29+
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
30+
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
3531
import org.springframework.ide.vscode.commons.util.text.LanguageId;
3632
import org.springframework.ide.vscode.languageserver.testharness.Editor;
3733
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
34+
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
3835
import org.springframework.test.context.junit.jupiter.SpringExtension;
3936

4037
@ExtendWith(SpringExtension.class)
4138
@BootLanguageServerTest
42-
@Import({ AdHocPropertyHarnessTestConf.class, ConditionalOnResourceCompletionTest.TestConf.class })
39+
@Import(SymbolProviderTestConf.class)
4340
public class ConditionalOnResourceDefinitionProviderTest {
4441

45-
@Autowired
46-
private BootLanguageServerHarness harness;
47-
@Autowired
48-
private IJavaProject testProject;
42+
@Autowired private BootLanguageServerHarness harness;
43+
@Autowired private JavaProjectFinder projectFinder;
44+
@Autowired private SpringSymbolIndex indexer;
4945

50-
private Set<Path> createdFiles = new HashSet<>();
46+
private String testSourceUri;
47+
private String testResourceUri;
5148

5249
@BeforeEach
5350
public void setup() throws Exception {
5451
harness.intialize(null);
55-
}
5652

57-
@AfterEach
58-
public void tearDown() throws Exception {
59-
for (Path f : createdFiles) {
60-
Files.deleteIfExists(f);
61-
}
62-
createdFiles.clear();
63-
}
53+
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-conditionalonresource/").toURI());
54+
55+
String projectDir = directory.toURI().toString();
56+
projectFinder.find(new TextDocumentIdentifier(projectDir)).get();
57+
58+
testResourceUri = directory.toPath().resolve("src/main/java/a-random-resource-root.md").toUri().toASCIIString();
59+
testSourceUri = directory.toPath().resolve("src/main/java/org/test/TestConditionalOnResourceCompletion.java").toUri().toASCIIString();
6460

65-
private Path projectFile(String relativePath, String content) throws IOException {
66-
Path projectPath = Paths.get(testProject.getLocationUri());
67-
Path filePath = projectPath.resolve(relativePath);
68-
Files.createDirectories(filePath.getParent());
69-
Files.write(filePath, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
70-
createdFiles.add(filePath);
71-
return filePath;
61+
CompletableFuture<Void> initProject = indexer.waitOperation();
62+
initProject.get(5, TimeUnit.SECONDS);
7263
}
7364

7465
@Test
7566
void testFindClasspathResource() throws Exception {
76-
Path randomResourceFilePath = projectFile("src/main/java/a-random-resource-root.md", "");
7767
Editor editor = harness.newEditor(LanguageId.JAVA, """
7868
package org.test;
7969
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
@@ -83,9 +73,9 @@ void testFindClasspathResource() throws Exception {
8373
@ConditionalOnResource("classpath:a-random-resource-root.md")
8474
public class TestConditionalOnResourceCompletion {
8575
private String value1;
86-
}""");
76+
}""", testSourceUri);
8777

88-
LocationLink expectedLocation = new LocationLink(randomResourceFilePath.toUri().toASCIIString(),
78+
LocationLink expectedLocation = new LocationLink(testResourceUri,
8979
new Range(new Position(0, 0), new Position(0, 0)),
9080
new Range(new Position(0, 0), new Position(0, 0)),
9181
new Range(new Position(5, 23), new Position(5, 60)));

0 commit comments

Comments
 (0)