From f3109ec71934f028813bc7ec365a3027f8f6b465 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 23 Jan 2020 16:53:50 +0100 Subject: [PATCH] Support microprofile-config.properties on JDT LS side See https://github.com/redhat-developer/vscode-quarkus/issues/181 Signed-off-by: azerr --- .../MicroProfileJavaCodeLensParams.java | 1 + .../jdt/core/PropertiesManagerForJava.java | 148 +--------------- .../core/project/AbstractConfigSource.java | 167 ++++++++++++++++++ .../internal/core/project/IConfigSource.java | 37 ++++ .../core/project/JDTMicroProfileProject.java | 68 +++++++ .../core/project/PropertiesConfigSource.java | 42 +++++ .../jdt/core/BasePropertiesManagerTest.java | 19 ++ .../jdt/core/JavaCodeLensTest.java | 42 ++++- .../MicroProfileJavaCodeLensParams.java | 1 + 9 files changed, 381 insertions(+), 144 deletions(-) create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/AbstractConfigSource.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/IConfigSource.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/JDTMicroProfileProject.java create mode 100644 microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/PropertiesConfigSource.java diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java index 36dff297c..ede857dc1 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java @@ -86,6 +86,7 @@ public Integer getLocalServerPort() { */ public void setLocalServerPort(Integer localServerPort) { this.localServerPort = localServerPort; + this.localBaseURL = null; } /** diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java index be372d04f..6cd0b79bf 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/core/PropertiesManagerForJava.java @@ -12,10 +12,7 @@ import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotation; import static com.redhat.microprofile.jdt.core.utils.AnnotationUtils.getAnnotationMemberValue; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Socket; import java.util.ArrayList; @@ -27,18 +24,13 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jdt.core.Flags; import org.eclipse.jdt.core.IAnnotatable; import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.IClassFile; -import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaElement; @@ -49,7 +41,6 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.ITypeRoot; import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.internal.core.JavaProject; import org.eclipse.lsp4j.CodeLens; import org.eclipse.lsp4j.Command; import org.eclipse.lsp4j.Position; @@ -60,6 +51,7 @@ import com.redhat.microprofile.commons.MicroProfileJavaHoverInfo; import com.redhat.microprofile.commons.MicroProfileJavaHoverParams; import com.redhat.microprofile.jdt.core.utils.IJDTUtils; +import com.redhat.microprofile.jdt.internal.core.project.JDTMicroProfileProject; /** * JDT quarkus manager for Java files. @@ -69,8 +61,6 @@ */ public class PropertiesManagerForJava { - private static final Logger LOGGER = Logger.getLogger(PropertiesManagerForJava.class.getName()); - private static final String LOCALHOST = "localhost"; private static final int PING_TIMEOUT = 2000; @@ -83,131 +73,11 @@ public class PropertiesManagerForJava { private static final PropertiesManagerForJava INSTANCE = new PropertiesManagerForJava(); - private static class JDTQuarkusProjectInfo { - - private static final String APPLICATION_PROPERTIES_FILE = "application.properties"; - - private static final int DEFAULT_PORT = 8080; - - private Integer serverPort; - - private long lastModified; - - private File applicationPropertiesFile; - - private Properties properties; - - private final IJavaProject javaProject; - - public JDTQuarkusProjectInfo(IJavaProject javaProject) { - this.javaProject = javaProject; - } - - /** - * Returns the target/classes/application.properties and null otherwise. - * - *

- * Using this file instead of using src/main/resources/application.properties - * gives the capability to get the filtered value. - *

- * - * @return the target/classes/application.properties and null otherwise. - */ - private File getApplicationPropertiesFile() { - if (applicationPropertiesFile != null && applicationPropertiesFile.exists()) { - return applicationPropertiesFile; - } - try { - List outputs = Stream.of(((JavaProject) javaProject).getResolvedClasspath(true)) // - .filter(entry -> !entry.isTest()) // - .filter(entry -> entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) // - .map(entry -> entry.getOutputLocation()) // - .filter(output -> output != null) // - .distinct() // - .collect(Collectors.toList()); - for (IPath output : outputs) { - File file = javaProject.getProject().getLocation().append(output.removeFirstSegments(1)) - .append(APPLICATION_PROPERTIES_FILE).toFile(); - if (file.exists()) { - applicationPropertiesFile = file; - - return applicationPropertiesFile; - } - } - return null; - } catch (JavaModelException e) { - return null; - } - } - - /** - * Returns the loaded application.properties and null otherwise. - * - * @return the loaded application.properties and null otherwise - * @throws JavaModelException - */ - public Properties getApplicationProperties() throws JavaModelException { - File applicationPropertiesFile = getApplicationPropertiesFile(); - if (applicationPropertiesFile == null) { - reset(); - return null; - } - if (applicationPropertiesFile.lastModified() != lastModified) { - reset(); - try (InputStream input = new FileInputStream(applicationPropertiesFile)) { - properties = new Properties(); - // load a properties file - properties.load(input); - lastModified = applicationPropertiesFile.lastModified(); - } catch (IOException e) { - reset(); - LOGGER.log(Level.SEVERE, - "Error while loading properties from '" + applicationPropertiesFile.getPath() + "'.", e); - } - } - return properties; - } - - private void reset() { - properties = null; - serverPort = null; - } - - /** - * Returns the server port. - * - * @return the server port. - */ - public int getServerPort() { - try { - try { - // Get application properties and reset the cache if needs - Properties properties = getApplicationProperties(); - if (serverPort == null) { - if (properties != null) { - String port = properties.getProperty("quarkus.http.port", ""); - if (!port.trim().isEmpty()) { - serverPort = Integer.parseInt(port.trim()); - } - } - } - } catch (JavaModelException e) { - LOGGER.log(Level.SEVERE, "Error while getting 'quarkus.http.port", e); - } - } finally { - if (serverPort == null) { - serverPort = DEFAULT_PORT; - } - } - return serverPort; - } - } - public static PropertiesManagerForJava getInstance() { return INSTANCE; } - private final Map infos; + private final Map infos; private PropertiesManagerForJava() { this.infos = new HashMap<>(); @@ -268,10 +138,10 @@ private static ITypeRoot resolveTypeRoot(String uri, IJDTUtils utils, IProgressM return unit != null ? unit : classFile; } - private JDTQuarkusProjectInfo getJDTQuarkusProjectInfo(IJavaProject project) throws JavaModelException { - JDTQuarkusProjectInfo info = infos.get(project); + private JDTMicroProfileProject getJDTQuarkusProjectInfo(IJavaProject project) throws JavaModelException { + JDTMicroProfileProject info = infos.get(project); if (info == null) { - info = new JDTQuarkusProjectInfo(project); + info = new JDTMicroProfileProject(project); infos.put(project, info); } return info; @@ -444,13 +314,7 @@ public MicroProfileJavaHoverInfo hover(MicroProfileJavaHoverParams params, IJDTU return null; } - Properties properties = getJDTQuarkusProjectInfo(javaProject).getApplicationProperties(); - - if (properties == null) { - return null; - } - - String propertyValue = properties.getProperty(propertyKey); + String propertyValue = getJDTQuarkusProjectInfo(javaProject).getProperty(propertyKey, null); if (propertyValue == null) { propertyValue = getAnnotationMemberValue(annotation, MicroProfileConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE); diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/AbstractConfigSource.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/AbstractConfigSource.java new file mode 100644 index 000000000..14714d64d --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/AbstractConfigSource.java @@ -0,0 +1,167 @@ +/******************************************************************************* +* Copyright (c) 2020 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.microprofile.jdt.internal.core.project; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.internal.core.JavaProject; + +/** + * Abstract class for config file. + * + * @author Angelo ZERR + * + * @param the config model (ex: Properties for *.properties file) + */ +public abstract class AbstractConfigSource implements IConfigSource { + + private static final Logger LOGGER = Logger.getLogger(AbstractConfigSource.class.getName()); + + private final String configFileName; + private final IJavaProject javaProject; + private Path configFile; + + private FileTime lastModified; + + private T config; + + public AbstractConfigSource(String configFileName, IJavaProject javaProject) { + this.configFileName = configFileName; + this.javaProject = javaProject; + } + + /** + * Returns the target/classes/$configFile and null otherwise. + * + *

+ * Using this file instead of using src/main/resources/$configFile gives the + * capability to get the filtered value. + *

+ * + * @return the target/classes/$configFile and null otherwise. + */ + private Path getConfigFile() { + if (configFile != null && Files.exists(configFile)) { + return configFile; + } + try { + List outputs = Stream.of(((JavaProject) javaProject).getResolvedClasspath(true)) // + .filter(entry -> !entry.isTest()) // + .filter(entry -> entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) // + .map(entry -> entry.getOutputLocation()) // + .filter(output -> output != null) // + .distinct() // + .collect(Collectors.toList()); + for (IPath output : outputs) { + File file = javaProject.getProject().getLocation().append(output.removeFirstSegments(1)) + .append(configFileName).toFile(); + if (file.exists()) { + configFile = file.toPath(); + return configFile; + } + } + return null; + } catch (JavaModelException e) { + LOGGER.log(Level.SEVERE, "Error while getting configuration", e); + return null; + } + } + + /** + * Returns the loaded config and null otherwise. + * + * @return the loaded config and null otherwise + */ + private T getConfig() { + Path configFile = getConfigFile(); + if (configFile == null) { + reset(); + return null; + } + try { + FileTime currentLastModified = Files.getLastModifiedTime(configFile); + if (!currentLastModified.equals(lastModified)) { + reset(); + try (InputStream input = new FileInputStream(configFile.toFile())) { + config = loadConfig(input); + lastModified = Files.getLastModifiedTime(configFile); + } catch (IOException e) { + reset(); + LOGGER.log(Level.SEVERE, "Error while loading properties from '" + configFile + "'.", e); + } + } + } catch (IOException e1) { + LOGGER.log(Level.SEVERE, "Error while getting last modified time for '" + configFile + "'.", e1); + } + return config; + } + + @Override + public final String getProperty(String key) { + T config = getConfig(); + if (config == null) { + return null; + } + return getProperty(key, config); + } + + @Override + public Integer getPropertyAsInt(String key) { + String property = getProperty(key); + if (property != null && !property.trim().isEmpty()) { + try { + return Integer.parseInt(property.trim()); + } catch (NumberFormatException e) { + LOGGER.log(Level.SEVERE, + "Error while converting '" + property.trim() + "' as Integer for key '" + key + "'", e); + return null; + } + } + return null; + } + + private void reset() { + config = null; + } + + /** + * Load the config model from the given input stream input. + * + * @param input the input stream + * @return he config model from the given input stream input. + * @throws IOException + */ + protected abstract T loadConfig(InputStream input) throws IOException; + + /** + * Returns the property from the given key and null otherwise. + * + * @param key + * @param config + * @return the property from the given key and null otherwise. + */ + protected abstract String getProperty(String key, T config); + +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/IConfigSource.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/IConfigSource.java new file mode 100644 index 000000000..071c58ca3 --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/IConfigSource.java @@ -0,0 +1,37 @@ +/******************************************************************************* +* Copyright (c) 2020 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.microprofile.jdt.internal.core.project; + +/** + * Configuration file API + * + * @author Angelo ZERR + * + */ +public interface IConfigSource { + + /** + * Returns the property from the given key and null otherwise. + * + * @param key the key + * @return the property from the given key and null otherwise. + */ + String getProperty(String key); + + /** + * Returns the property as Integer from the given key and null + * otherwise. + * + * @param key the key + * @return the property as Integer from the given key and null + * otherwise. + */ + Integer getPropertyAsInt(String key); +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/JDTMicroProfileProject.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/JDTMicroProfileProject.java new file mode 100644 index 000000000..cad67d858 --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/JDTMicroProfileProject.java @@ -0,0 +1,68 @@ +/******************************************************************************* +* Copyright (c) 2020 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.microprofile.jdt.internal.core.project; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.core.IJavaProject; + +/** + * JDT MicroProfile project. + * + * @author Angelo ZERR + * + */ +public class JDTMicroProfileProject { + + public static final String MICROPROFILE_CONFIG_PROPERTIES_FILE = "META-INF/microprofile-config.properties"; + public static final String APPLICATION_PROPERTIES_FILE = "application.properties"; + + private final List configSources; + + private static final String QUARKUS_HTTP_PORT = "quarkus.http.port"; + + private static final int DEFAULT_PORT = 8080; + + public JDTMicroProfileProject(IJavaProject javaProject) { + this.configSources = new ArrayList(2); + configSources.add(new PropertiesConfigSource(APPLICATION_PROPERTIES_FILE, javaProject)); + configSources.add(new PropertiesConfigSource(MICROPROFILE_CONFIG_PROPERTIES_FILE, javaProject)); + } + + /** + * Returns the server port. + * + * @return the server port. + */ + public int getServerPort() { + return getPropertyAsInteger(QUARKUS_HTTP_PORT, DEFAULT_PORT); + } + + public String getProperty(String key, String defaultValue) { + for (IConfigSource configSource : configSources) { + String property = configSource.getProperty(key); + if (property != null) { + return property; + } + } + return defaultValue; + } + + public Integer getPropertyAsInteger(String key, Integer defaultValue) { + for (IConfigSource configSource : configSources) { + Integer property = configSource.getPropertyAsInt(key); + if (property != null) { + return property; + } + } + return defaultValue; + } +} \ No newline at end of file diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/PropertiesConfigSource.java b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/PropertiesConfigSource.java new file mode 100644 index 000000000..bc3ca10e1 --- /dev/null +++ b/microprofile.jdt/com.redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/PropertiesConfigSource.java @@ -0,0 +1,42 @@ +/******************************************************************************* +* Copyright (c) 2020 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.microprofile.jdt.internal.core.project; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.eclipse.jdt.core.IJavaProject; + +/** + * {@link Properties} config file implementation. + * + * @author Angelo ZERR + * + */ +public class PropertiesConfigSource extends AbstractConfigSource { + + public PropertiesConfigSource(String configFileName, IJavaProject javaProject) { + super(configFileName, javaProject); + } + + @Override + protected String getProperty(String key, Properties properties) { + return properties.getProperty(key); + } + + @Override + protected Properties loadConfig(InputStream input) throws IOException { + Properties properties = new Properties(); + properties.load(input); + return properties; + } + +} diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/BasePropertiesManagerTest.java b/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/BasePropertiesManagerTest.java index 6266eb883..31541d942 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/BasePropertiesManagerTest.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/BasePropertiesManagerTest.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -164,4 +165,22 @@ private static File copyProjectToWorkingDirectory(String projectName) throws IOE private static void waitForBackgroundJobs(IProgressMonitor monitor) throws Exception { JobHelpers.waitForJobsToComplete(monitor); } + + protected static void createFile(File file, String content) throws IOException { + Files.createDirectories(file.getParentFile().toPath()); + Files.write(file.toPath(), content.getBytes()); + } + + protected static void updateFile(File file, String content) throws IOException { + // For Mac OS, Linux OS, the call of Files.getLastModifiedTime is working for 1 + // second. + // Here we wait for > 1s to be sure that call of Files.getLastModifiedTime will + // work. + try { + Thread.sleep(1050); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + createFile(file, content); + } } diff --git a/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/JavaCodeLensTest.java b/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/JavaCodeLensTest.java index 375347825..e0895fd00 100644 --- a/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/JavaCodeLensTest.java +++ b/microprofile.jdt/com.redhat.microprofile.jdt.test/src/main/java/com/redhat/microprofile/jdt/core/JavaCodeLensTest.java @@ -9,12 +9,16 @@ *******************************************************************************/ package com.redhat.microprofile.jdt.core; +import java.io.File; +import java.io.IOException; import java.util.List; import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaModelException; import org.eclipse.lsp4j.CodeLens; import org.junit.Assert; import org.junit.Test; @@ -22,6 +26,7 @@ import com.redhat.microprofile.commons.MicroProfileJavaCodeLensParams; import com.redhat.microprofile.jdt.core.utils.IJDTUtils; import com.redhat.microprofile.jdt.internal.core.ls.JDTUtilsLSImpl; +import com.redhat.microprofile.jdt.internal.core.project.JDTMicroProfileProject; /** * JDT Quarkus manager test for Java file. @@ -43,6 +48,30 @@ public void urlCodeLens() throws Exception { params.setUri(javaFile.getLocation().toFile().toURI().toString()); params.setUrlCodeLensEnabled(true); + // Default port + assertCodeLenses(8080, params, utils); + + // META-INF/microprofile-config.properties : 8081 + saveFile(JDTMicroProfileProject.MICROPROFILE_CONFIG_PROPERTIES_FILE, "quarkus.http.port = 8081", javaProject); + assertCodeLenses(8081, params, utils); + + // application.properties : 8082 -> it overrides 8081 coming from the + // META-INF/microprofile-config.properties + saveFile(JDTMicroProfileProject.APPLICATION_PROPERTIES_FILE, "quarkus.http.port = 8082", javaProject); + assertCodeLenses(8082, params, utils); + + // application.properties : 8083 + // META-INF/microprofile-config.properties + saveFile(JDTMicroProfileProject.APPLICATION_PROPERTIES_FILE, "quarkus.http.port = 8083", javaProject); + assertCodeLenses(8083, params, utils); + + // remove quarkus.http.port from application.properties + saveFile(JDTMicroProfileProject.APPLICATION_PROPERTIES_FILE, "", javaProject); + assertCodeLenses(8081, params, utils); // here port is 8081 coming from META-INF/microprofile-config.properties + } + + private static void assertCodeLenses(int port, MicroProfileJavaCodeLensParams params, IJDTUtils utils) + throws JavaModelException { List lenses = PropertiesManagerForJava.getInstance().codeLens(params, utils, new NullProgressMonitor()); Assert.assertEquals(2, lenses.size()); @@ -51,13 +80,22 @@ public void urlCodeLens() throws Exception { // public Fruit[] get() { CodeLens lensForGet = lenses.get(0); Assert.assertNotNull(lensForGet.getCommand()); - Assert.assertEquals("http://localhost:8080/fruits", lensForGet.getCommand().getTitle()); + Assert.assertEquals("http://localhost:" + port + "/fruits", lensForGet.getCommand().getTitle()); // @GET // @Path("{id}") // public Fruit getSingle(@PathParam Integer id) { CodeLens lensForGetSingle = lenses.get(1); Assert.assertNotNull(lensForGetSingle.getCommand()); - Assert.assertEquals("http://localhost:8080/fruits/{id}", lensForGetSingle.getCommand().getTitle()); + Assert.assertEquals("http://localhost:" + port + "/fruits/{id}", lensForGetSingle.getCommand().getTitle()); + } + + private static void saveFile(String configFileName, String content, IJavaProject javaProject) + throws JavaModelException, IOException { + IPath output = javaProject.getOutputLocation(); + File file = javaProject.getProject().getLocation().append(output.removeFirstSegments(1)).append(configFileName) + .toFile(); + updateFile(file, content); } + } diff --git a/microprofile.ls/com.redhat.microprofile.ls/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java b/microprofile.ls/com.redhat.microprofile.ls/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java index 36dff297c..ede857dc1 100644 --- a/microprofile.ls/com.redhat.microprofile.ls/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java +++ b/microprofile.ls/com.redhat.microprofile.ls/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCodeLensParams.java @@ -86,6 +86,7 @@ public Integer getLocalServerPort() { */ public void setLocalServerPort(Integer localServerPort) { this.localServerPort = localServerPort; + this.localBaseURL = null; } /**