diff --git a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/xml/test/XMLBeanRefContentAssistTest.java b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/xml/test/XMLBeanRefContentAssistTest.java new file mode 100644 index 0000000000..60bdcb10bf --- /dev/null +++ b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/xml/test/XMLBeanRefContentAssistTest.java @@ -0,0 +1,163 @@ +/******************************************************************************* + * Copyright (c) 2024 Broadcom + * 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 + * https://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Broadcom - initial API and implementation + *******************************************************************************/ +package org.springframework.ide.vscode.boot.xml.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import org.eclipse.lsp4j.CompletionItem; +import org.eclipse.lsp4j.TextDocumentIdentifier; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Import; +import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; +import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest; +import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf; +import org.springframework.ide.vscode.commons.java.IJavaProject; +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; +import org.springframework.ide.vscode.commons.languageserver.util.Settings; +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; + +import com.google.gson.Gson; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; + +/** + * @author Martin Lippert + */ +@ExtendWith(SpringExtension.class) +@BootLanguageServerTest +@Import(SymbolProviderTestConf.class) +public class XMLBeanRefContentAssistTest { + + private static final org.slf4j.Logger log = LoggerFactory.getLogger(XMLBeanRefContentAssistTest.class); + + @Autowired private BootLanguageServerHarness harness; + @Autowired private JavaProjectFinder projectFinder; + @Autowired private SpringSymbolIndex indexer; + + private IJavaProject project; + private Level originalLevel; + private File directory; + + private String tempJavaDocUri; + + @BeforeEach + public void setup() throws Exception { + final Logger logger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + originalLevel = logger.getLevel(); + logger.setLevel(Level.INFO); + + log.info("-------------------------------------------------"); + harness.intialize(null); + + Map supportXML = new HashMap<>(); + supportXML.put("on", true); + supportXML.put("hyperlinks", true); + supportXML.put("content-assist", true); + supportXML.put("scan-folders", "/src/main/"); + Map bootJavaObj = new HashMap<>(); + bootJavaObj.put("support-spring-xml-config", supportXML); + Map settings = new HashMap<>(); + settings.put("boot-java", bootJavaObj); + + harness.changeConfiguration(new Settings(new Gson().toJsonTree(settings))); + + directory = new File(ProjectsHarness.class.getResource("/test-projects/test-xml-hyperlinks/").toURI()); + + String projectDir = directory.toURI().toString(); + project = projectFinder.find(new TextDocumentIdentifier(projectDir)).get(); + + tempJavaDocUri = directory.toPath().resolve("src/main/java/tempdoc.xml").toUri().toString(); + + CompletableFuture initProject = indexer.waitOperation(); + initProject.get(5, TimeUnit.SECONDS); + } + + @AfterEach + public void tearDown() { + log.debug("-------------------------------------------------"); + final Logger logger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); + logger.setLevel(originalLevel); + } + + @Test + void testSimpleBeanRefCompletion() throws Exception { + Editor editor = harness.newEditor(LanguageId.XML, """ + + + + + + + """, + tempJavaDocUri); + + List completions = editor.getCompletions(); + + assertEquals(4, completions.size()); + assertTrue(containsCompletion("testBean", completions)); + assertTrue(containsCompletion("homeController", completions)); + assertTrue(containsCompletion("simpleObj", completions)); + assertTrue(containsCompletion("internalResourceViewResolver", completions)); + CompletionItem completionItem = getCompletion("homeController", completions); + + editor.apply(completionItem); + String editorContent = editor.getText(); + + assertEquals(""" + + + + + + + """, + editorContent); + } + + private CompletionItem getCompletion(String completionLabel, List completions) { + for (CompletionItem completionItem : completions) { + if (completionItem.getLabel().equals(completionLabel)) return completionItem; + } + + return null; + } + + private boolean containsCompletion(String completionLabel, List completions) { + for (CompletionItem completionItem : completions) { + if (completionItem.getLabel().equals(completionLabel)) return true; + } + + return false; + } + +}