stability improvements for tests around conditional on resource

This commit is contained in:
Martin Lippert
2025-01-24 16:38:15 +01:00
parent 1c642049ff
commit 0bcbf0625e
2 changed files with 46 additions and 116 deletions

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom, Inc.
* Copyright (c) 2024, 2025 Broadcom, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -13,42 +13,26 @@ package org.springframework.ide.vscode.boot.java.conditionals.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.gradle.internal.impldep.com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.ide.vscode.boot.app.BootLanguageServerParams;
import org.springframework.ide.vscode.boot.bootiful.AdHocPropertyHarnessTestConf;
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
import org.springframework.ide.vscode.boot.editor.harness.AdHocPropertyHarness;
import org.springframework.ide.vscode.boot.editor.harness.PropertyIndexHarness;
import org.springframework.ide.vscode.boot.index.cache.IndexCache;
import org.springframework.ide.vscode.boot.index.cache.IndexCacheVoid;
import org.springframework.ide.vscode.boot.java.links.SourceLinkFactory;
import org.springframework.ide.vscode.boot.java.links.SourceLinks;
import org.springframework.ide.vscode.boot.java.utils.CompilationUnitCache;
import org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject;
import org.springframework.ide.vscode.commons.util.text.LanguageId;
import org.springframework.ide.vscode.languageserver.testharness.Editor;
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
@@ -60,73 +44,29 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
*/
@ExtendWith(SpringExtension.class)
@BootLanguageServerTest
@Import({AdHocPropertyHarnessTestConf.class, ConditionalOnResourceCompletionTest.TestConf.class})
@Import(SymbolProviderTestConf.class)
public class ConditionalOnResourceCompletionTest {
@Autowired private BootLanguageServerHarness harness;
@Autowired private JavaProjectFinder projectFinder;
@Autowired private SpringSymbolIndex indexer;
private Editor editor;
@Autowired private PropertyIndexHarness indexHarness;
@Autowired private AdHocPropertyHarness adHocProperties;
@Configuration
static class TestConf {
@Bean MavenJavaProject testProject() throws Exception {
return ProjectsHarness.INSTANCE.mavenProject("test-annotation-conditionalonresource");
}
@Bean PropertyIndexHarness indexHarness(ValueProviderRegistry valueProviders) {
return new PropertyIndexHarness(valueProviders);
}
@Bean JavaProjectFinder projectFinder(MavenJavaProject testProject) {
return new JavaProjectFinder() {
@Override
public Optional<IJavaProject> find(TextDocumentIdentifier doc) {
return Optional.ofNullable(testProject);
}
@Override
public Collection<? extends IJavaProject> all() {
// TODO Auto-generated method stub
return testProject == null ? Collections.emptyList() : ImmutableList.of(testProject);
}
};
}
@Bean BootLanguageServerHarness harness(SimpleLanguageServer server, BootLanguageServerParams serverParams, PropertyIndexHarness indexHarness, JavaProjectFinder projectFinder) throws Exception {
return new BootLanguageServerHarness(server, serverParams, indexHarness, projectFinder, LanguageId.JAVA, ".java");
}
@Bean BootLanguageServerParams serverParams(SimpleLanguageServer server, JavaProjectFinder projectFinder, ValueProviderRegistry valueProviders, PropertyIndexHarness indexHarness) {
BootLanguageServerParams testDefaults = BootLanguageServerHarness.createTestDefault(server, valueProviders);
return new BootLanguageServerParams(
projectFinder,
ProjectObserver.NULL,
indexHarness.getIndexProvider(),
testDefaults.typeUtilProvider
);
}
@Bean IndexCache symbolCache() {
return new IndexCacheVoid();
}
@Bean SourceLinks sourceLinks(SimpleTextDocumentService documents, CompilationUnitCache cuCache) {
return SourceLinkFactory.NO_SOURCE_LINKS;
}
}
private String testSourceUri;
@BeforeEach
public void setup() throws Exception {
IJavaProject testProject = ProjectsHarness.INSTANCE.mavenProject("test-annotation-conditionalonresource");
harness.useProject(testProject);
harness.intialize(null);
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-conditionalonresource/").toURI());
String projectDir = directory.toURI().toString();
projectFinder.find(new TextDocumentIdentifier(projectDir)).get();
testSourceUri = directory.toPath().resolve("src/main/java/org/test/TestConditionalOnResourceCompletion.java").toUri().toString();
CompletableFuture<Void> initProject = indexer.waitOperation();
initProject.get(5, TimeUnit.SECONDS);
}
@Test
@@ -221,7 +161,7 @@ public class ConditionalOnResourceCompletionTest {
String content = IOUtils.toString(resource, Charset.defaultCharset());
content = content.replace(selectedAnnotation, annotationStatementBeforeTest);
editor = new Editor(harness, content, LanguageId.JAVA);
editor = harness.newEditor(LanguageId.JAVA, content, testSourceUri);
}
private void assertClasspathCompletions(String... completedAnnotations) throws Exception {

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom, Inc.
* Copyright (c) 2024, 2025 Broadcom, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,70 +10,60 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.conditionals.test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.AfterEach;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.ide.vscode.boot.bootiful.AdHocPropertyHarnessTestConf;
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.util.text.LanguageId;
import org.springframework.ide.vscode.languageserver.testharness.Editor;
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@BootLanguageServerTest
@Import({ AdHocPropertyHarnessTestConf.class, ConditionalOnResourceCompletionTest.TestConf.class })
@Import(SymbolProviderTestConf.class)
public class ConditionalOnResourceDefinitionProviderTest {
@Autowired
private BootLanguageServerHarness harness;
@Autowired
private IJavaProject testProject;
@Autowired private BootLanguageServerHarness harness;
@Autowired private JavaProjectFinder projectFinder;
@Autowired private SpringSymbolIndex indexer;
private Set<Path> createdFiles = new HashSet<>();
private String testSourceUri;
private String testResourceUri;
@BeforeEach
public void setup() throws Exception {
harness.intialize(null);
}
@AfterEach
public void tearDown() throws Exception {
for (Path f : createdFiles) {
Files.deleteIfExists(f);
}
createdFiles.clear();
}
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-conditionalonresource/").toURI());
private Path projectFile(String relativePath, String content) throws IOException {
Path projectPath = Paths.get(testProject.getLocationUri());
Path filePath = projectPath.resolve(relativePath);
Files.createDirectories(filePath.getParent());
Files.write(filePath, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
createdFiles.add(filePath);
return filePath;
String projectDir = directory.toURI().toString();
projectFinder.find(new TextDocumentIdentifier(projectDir)).get();
testResourceUri = directory.toPath().resolve("src/main/java/a-random-resource-root.md").toUri().toASCIIString();
testSourceUri = directory.toPath().resolve("src/main/java/org/test/TestConditionalOnResourceCompletion.java").toUri().toASCIIString();
CompletableFuture<Void> initProject = indexer.waitOperation();
initProject.get(5, TimeUnit.SECONDS);
}
@Test
void testFindClasspathResource() throws Exception {
Path randomResourceFilePath = projectFile("src/main/java/a-random-resource-root.md", "");
Editor editor = harness.newEditor(LanguageId.JAVA, """
package org.test;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
@@ -83,9 +73,9 @@ public class ConditionalOnResourceDefinitionProviderTest {
@ConditionalOnResource("classpath:a-random-resource-root.md")
public class TestConditionalOnResourceCompletion {
private String value1;
}""");
}""", testSourceUri);
LocationLink expectedLocation = new LocationLink(randomResourceFilePath.toUri().toASCIIString(),
LocationLink expectedLocation = new LocationLink(testResourceUri,
new Range(new Position(0, 0), new Position(0, 0)),
new Range(new Position(0, 0), new Position(0, 0)),
new Range(new Position(5, 23), new Position(5, 60)));