First, simple implementation of active profiles hover

This commit is contained in:
Kris De Volder
2017-10-24 10:48:02 -07:00
parent b8aa5e484c
commit 5652e8402e
6 changed files with 264 additions and 11 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.ide.vscode.boot.java.handlers.HoverProvider;
import org.springframework.ide.vscode.boot.java.handlers.ReferenceProvider;
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
import org.springframework.ide.vscode.boot.java.handlers.SymbolProvider;
import org.springframework.ide.vscode.boot.java.profiles.ActiveProfilesProvider;
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingHoverProvider;
import org.springframework.ide.vscode.boot.java.requestmapping.RequestMappingSymbolProvider;
import org.springframework.ide.vscode.boot.java.scope.ScopeCompletionProcessor;
@@ -256,6 +257,7 @@ public class BootJavaLanguageServer extends SimpleLanguageServer {
providers.put(org.springframework.ide.vscode.boot.java.requestmapping.Constants.SPRING_PUT_MAPPING, new RequestMappingHoverProvider());
providers.put(org.springframework.ide.vscode.boot.java.requestmapping.Constants.SPRING_DELETE_MAPPING, new RequestMappingHoverProvider());
providers.put(org.springframework.ide.vscode.boot.java.requestmapping.Constants.SPRING_PATCH_MAPPING, new RequestMappingHoverProvider());
providers.put(ActiveProfilesProvider.ANNOTATION, new ActiveProfilesProvider());
providers.put(org.springframework.ide.vscode.boot.java.autowired.Constants.SPRING_AUTOWIRED, new AutowiredHoverProvider());
providers.put(org.springframework.ide.vscode.boot.java.beans.Constants.SPRING_COMPONENT, new ComponentHoverProvider());

View File

@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.profiles;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.springframework.ide.vscode.boot.java.handlers.HoverProvider;
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
import com.google.common.collect.ImmutableList;
/**
* @author Kris De Volder
*/
public class ActiveProfilesProvider implements HoverProvider {
public static final String ANNOTATION = "org.springframework.context.annotation.Profile";
@Override
public CompletableFuture<Hover> provideHover(
ASTNode node,
Annotation annotation,
ITypeBinding type,
int offset,
TextDocument doc, SpringBootApp[] runningApps
) {
if (runningApps.length>0) {
StringBuilder markdown = new StringBuilder();
markdown.append("**Active Profiles**\n\n");
for (SpringBootApp app : runningApps) {
List<String> profiles = app.getActiveProfiles();
if (profiles==null) {
markdown.append(niceAppName(app)+" : _Unknown_\n\n");
} else if (profiles.isEmpty()) {
markdown.append(niceAppName(app)+" : _None_\n\n");
} else {
markdown.append(niceAppName(app)+" :\n");
for (String profile : profiles) {
markdown.append("- "+profile+"\n");
}
markdown.append("\n");
}
}
return CompletableFuture.completedFuture(new Hover(
ImmutableList.of(Either.forLeft(markdown.toString()))
));
}
return null;
}
private String niceAppName(SpringBootApp app) {
return "Process [PID="+app.getProcessID()+", name=`"+app.getProcessName()+"`]";
}
@Override
public Range getLiveHoverHint(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
try {
if (runningApps.length > 0) {
return doc.toRange(annotation.getStartPosition(), annotation.getLength());
}
} catch (BadLocationException e) {
Log.log(e);
}
return null;
}
}

View File

@@ -0,0 +1,109 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.profile;
import org.junit.Before;
import org.junit.Test;
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.MockRunningAppProvider;
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
public class ActiveProfilesHoverTest {
private BootLanguageServerHarness harness;
private ProjectsHarness projects = ProjectsHarness.INSTANCE;
private MockRunningAppProvider mockAppProvider;
@Before
public void setup() throws Exception {
mockAppProvider = new MockRunningAppProvider();
harness = BootLanguageServerHarness.builder()
.mockDefaults()
.runningAppProvider(mockAppProvider.provider)
.build();
harness.useProject(projects.mavenProject("empty-boot-15-web-app"));
harness.intialize(null);
}
@Test
public void testActiveProfileHover() throws Exception {
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("22022")
.processName("foo.bar.RunningApp")
.profiles("testing-profile", "local-profile")
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package hello;\n" +
"\n" +
"import org.springframework.context.annotation.Configuration;\n" +
"import org.springframework.context.annotation.Profile;\n" +
"\n" +
"@Configuration\n" +
"@Profile(\"local\")\n" +
"public class LocalConfig {\n" +
"}"
);
editor.assertHoverContains("@Profile", "testing-profile");
editor.assertHoverContains("@Profile", "local-profile");
editor.assertHoverContains("@Profile", "foo.bar.RunningApp");
editor.assertHoverContains("@Profile", "22022");
}
@Test
public void testActiveProfileHover_Unknown() throws Exception {
//Sometimes its not possible to determine active profiles for an app (e.g. no actuator dependency).
//Make sure we show something sensible
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("22022")
.processName("foo.bar.RunningApp")
.profilesUnknown()
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package hello;\n" +
"\n" +
"import org.springframework.context.annotation.Configuration;\n" +
"import org.springframework.context.annotation.Profile;\n" +
"\n" +
"@Configuration\n" +
"@Profile(\"local\")\n" +
"public class LocalConfig {\n" +
"\n" +
"}"
);
editor.assertHoverContains("@Profile", "Process [PID=22022, name=`foo.bar.RunningApp`] : _Unknown_");
}
@Test
public void testNoRunningApps() throws Exception {
Editor editor = harness.newEditor(LanguageId.JAVA,
"package hello;\n" +
"\n" +
"import org.springframework.context.annotation.Configuration;\n" +
"import org.springframework.context.annotation.Profile;\n" +
"\n" +
"@Configuration\n" +
"@Profile(\"local\")\n" +
"public class LocalConfig {\n" +
"\n" +
"}"
);
editor.assertNoHover("@Profile");
}
}

View File

@@ -20,6 +20,9 @@ import org.mockito.Mockito;
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.util.ExceptionUtil;
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness.Builder;
import com.google.common.collect.ImmutableList;
public class MockRunningAppProvider {
@@ -103,6 +106,20 @@ public class MockRunningAppProvider {
return this;
}
public MockAppBuilder profiles(String... names) {
when(app.getActiveProfiles()).thenReturn(ImmutableList.copyOf(names));
return this;
}
public MockAppBuilder profilesUnknown() {
//Note, technically, we don't have to program the mock for this case as it will return
// null by default. But it makes test code more readable. Also... how we represent the
// 'unknown' case may change in the future and having this method will help fix the tests.
when(app.getActiveProfiles()).thenReturn(null);
return this;
}
/**
* Builds the mock app and adds it to the app provider
*/