Merge branch 'master' into request-mappings

# Conflicts:
#	headless-services/boot-java-language-server/src/main/java/org/springframework/ide/vscode/boot/java/requestmapping/RequestMappingHoverProvider.java
#	headless-services/boot-java-language-server/src/test/java/org/springframework/ide/vscode/boot/java/requestmapping/test/RequestMappingLiveHoverTest.java
#	headless-services/boot-java-language-server/src/test/java/org/springframework/ide/vscode/project/harness/MockRunningAppProvider.java
#	headless-services/commons/commons-boot-app-cli/src/main/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootApp.java
#	headless-services/commons/commons-boot-app-cli/src/test/java/org/springframework/ide/vscode/commons/boot/app/cli/SpringBootAppTest.java
This commit is contained in:
BoykoAlex
2017-10-26 21:04:44 -04:00
29 changed files with 926 additions and 874 deletions

View File

@@ -34,7 +34,9 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.ide.vscode.commons.boot.app.cli.requestmappings.RequestMapping;
import org.springframework.ide.vscode.commons.boot.app.cli.requestmappings.RequestMappingImpl1;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.StringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
@@ -174,7 +176,7 @@ public class SpringBootApp {
return null;
}
public String getBeans() throws Exception {
private String getBeansJson() throws Exception {
Object result = getActuatorDataFromAttribute("org.springframework.boot:type=Endpoint,name=beansEndpoint", "Data");
if (result != null) {
String beans = new ObjectMapper().writeValueAsString(result);
@@ -190,6 +192,14 @@ public class SpringBootApp {
return null;
}
public LiveBeansModel getBeans() throws Exception {
String json = getBeansJson();
if (StringUtil.hasText(json)) {
return LiveBeansModel.parse(json);
}
return null;
}
public static Collection<RequestMapping> parseRequestMappingsJson(String json) {
JSONObject obj = new JSONObject(json);
Iterator<String> keys = obj.keys();

View File

@@ -0,0 +1,63 @@
/*******************************************************************************
* 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.commons.boot.app.cli.livebean;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* @author Martin Lippert
* @author Kris De Volder
*/
public class LiveBean {
private final String id;
private final String[] aliases;
private final String scope;
private final String type;
private final String resource;
private final String[] dependencies;
protected LiveBean(String id, String[] aliases, String scope, String type, String resource, String[] dependencies) {
super();
this.id = id;
this.aliases = aliases;
this.scope = scope;
this.type = type;
this.resource = resource;
this.dependencies = dependencies;
}
public String getId() {
return id;
}
public String[] getAliases() {
return aliases;
}
public String getScope() {
return scope;
}
public String getType() {
return type;
}
public String getResource() {
return resource;
}
public String[] getDependencies() {
return dependencies;
}
}

View File

@@ -0,0 +1,185 @@
/*******************************************************************************
* 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.commons.boot.app.cli.livebean;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Stream;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.StringUtil;
/**
* @author Martin Lippert
*/
public class LiveBeansModel {
interface Parser {
LiveBeansModel parse(String json) throws Exception;
}
private static class Boot15Parser implements Parser {
@Override
public LiveBeansModel parse(String json) throws Exception {
LiveBeansModel model = new LiveBeansModel();
JSONArray mainArray = new JSONArray(json);
for (int i = 0; i < mainArray.length(); i++) {
JSONObject appContext = mainArray.getJSONObject(i);
if (appContext == null) continue;
JSONArray beansArray = appContext.optJSONArray("beans");
if (beansArray == null) continue;
for (int j = 0; j < beansArray.length(); j++) {
JSONObject beanObject = beansArray.getJSONObject(j);
if (beanObject == null) continue;
LiveBean bean = parseBean(beanObject);
if (bean != null) {
model.add(bean);
}
}
}
return model;
}
private LiveBean parseBean(JSONObject beansJSON) {
String id = beansJSON.optString("bean");
String type = beansJSON.optString("type");
String scope = beansJSON.optString("scope");
String resource = beansJSON.optString("resource");
JSONArray aliasesJSON = beansJSON.getJSONArray("aliases");
String[] aliases = new String[aliasesJSON.length()];
for (int i = 0; i < aliasesJSON.length(); i++) {
aliases[i] = aliasesJSON.optString(i);
}
JSONArray dependenciesJSON = beansJSON.getJSONArray("dependencies");
String[] dependencies = new String[dependenciesJSON.length()];
for (int i = 0; i < dependenciesJSON.length(); i++) {
dependencies[i] = dependenciesJSON.optString(i);
}
return new LiveBean(id, aliases, scope, type, resource, dependencies);
}
}
private static class Boot20Parser implements Parser {
@Override
public LiveBeansModel parse(String json) throws Exception {
LiveBeansModel model = new LiveBeansModel();
JSONObject mainObject = new JSONObject(json);
JSONObject beansObject = mainObject.getJSONObject("beans");
for (String id : beansObject.keySet()) {
JSONObject beanObject = beansObject.getJSONObject(id);
System.out.println(beanObject.toString(3));
LiveBean bean = parseBean(id, beanObject);
if (bean!=null) {
model.add(bean);
}
}
return model;
}
private LiveBean parseBean(String id, JSONObject beansJSON) {
String type = beansJSON.optString("type");
String scope = beansJSON.optString("scope");
String resource = beansJSON.optString("resource");
JSONArray aliasesJSON = beansJSON.getJSONArray("aliases");
String[] aliases = new String[aliasesJSON.length()];
for (int i = 0; i < aliasesJSON.length(); i++) {
aliases[i] = aliasesJSON.optString(i);
}
JSONArray dependenciesJSON = beansJSON.getJSONArray("dependencies");
String[] dependencies = new String[dependenciesJSON.length()];
for (int i = 0; i < dependenciesJSON.length(); i++) {
dependencies[i] = dependenciesJSON.optString(i);
}
return new LiveBean(id, aliases, scope, type, resource, dependencies);
}
}
private static final Parser[] PARSERS = {
new Boot15Parser(),
new Boot20Parser(),
};
public static LiveBeansModel parse(String json) {
List<Exception> exceptions = new ArrayList<>(PARSERS.length);
if (StringUtil.hasText(json)) {
for (Parser parser : PARSERS) {
try {
LiveBeansModel model = parser.parse(json);
if (model==null) {
throw new NullPointerException("Parser returned a null model (it should not!)");
}
return model; //good!
} catch (Exception e) {
exceptions.add(e);
}
}
}
//Only getting here if none of the parsers worked... So if at least one parser works,
// we won't log any exceptions.
for (Exception e : exceptions) {
Log.log(e);
}
return new LiveBeansModel(); // allways return at least an empty model.
}
private final ConcurrentMap<String, List<LiveBean>> beansViaType;
private final ConcurrentMap<String, List<LiveBean>> beansViaName;
protected LiveBeansModel() {
this.beansViaType = new ConcurrentHashMap<>();
this.beansViaName = new ConcurrentHashMap<>();
}
public LiveBean[] getBeansOfType(String fullyQualifiedType) {
List<LiveBean> result = beansViaType.get(fullyQualifiedType);
return result != null ? result.toArray(new LiveBean[result.size()]) : new LiveBean[0];
}
public LiveBean[] getBeansOfName(String beanName) {
List<LiveBean> result = beansViaName.get(beanName);
return result != null ? result.toArray(new LiveBean[result.size()]) : new LiveBean[0];
}
protected void add(LiveBean bean) {
String type = bean.getType();
if (type != null) {
beansViaType.computeIfAbsent(type, (t) -> new ArrayList<>()).add(bean);
}
String name = bean.getId();
if (name != null) {
beansViaName.computeIfAbsent(name, (n) -> new ArrayList<>()).add(bean);
}
}
public Stream<LiveBean> getAllBeans() {
return beansViaName.values().stream().flatMap(Collection::stream);
}
public boolean isEmpty() {
return !getAllBeans().findAny().isPresent();
}
}

View File

@@ -0,0 +1,73 @@
/*******************************************************************************
* 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.commons.boot.app.cli;
import static org.junit.Assert.assertEquals;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel;
/**
* @author Martin Lippert
*/
public class LiveBeansModelTest {
@Test
public void testSimpleModel() throws Exception {
String json = IOUtils.toString(getResourceAsStream("/live-beans-models/simple-live-beans-model.json"));
LiveBeansModel model = LiveBeansModel.parse(json);
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(1, bean.length);
assertEquals("dependencyA", bean[0].getId());
assertEquals("singleton", bean[0].getScope());
assertEquals("org.test.DependencyA", bean[0].getType());
assertEquals("file [/test-projects/classes/org/test/DependencyA.class]", bean[0].getResource());
assertEquals(0, bean[0].getAliases().length);
assertEquals(0, bean[0].getDependencies().length);
bean = model.getBeansOfName("dependencyB");
assertEquals(1, bean.length);
assertEquals("dependencyB", bean[0].getId());
assertEquals("singleton", bean[0].getScope());
assertEquals("org.test.DependencyB", bean[0].getType());
assertEquals("file [/test-projects/classes/org/test/DependencyB.class]", bean[0].getResource());
assertEquals(0, bean[0].getAliases().length);
assertEquals(0, bean[0].getDependencies().length);
}
@Test
public void testEmptyModel() throws Exception {
String json = IOUtils.toString(getResourceAsStream("/live-beans-models/empty-live-beans-model.json"));
LiveBeansModel model = LiveBeansModel.parse(json);
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(0, bean.length);
}
@Test
public void testTotallyEmptyModel() throws Exception {
String json = IOUtils.toString(getResourceAsStream("/live-beans-models/totally-empty-live-beans-model.json"));
LiveBeansModel model = LiveBeansModel.parse(json);
LiveBean[] bean = model.getBeansOfType("org.test.DependencyA");
assertEquals(0, bean.length);
}
private InputStream getResourceAsStream(String string) {
return LiveBeansModelTest.class.getResourceAsStream(string);
}
}

View File

@@ -12,23 +12,27 @@ package org.springframework.ide.vscode.commons.boot.app.cli;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.net.URL;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.json.JSONObject;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel;
import org.springframework.ide.vscode.commons.boot.app.cli.requestmappings.RequestMapping;
import org.springframework.ide.vscode.commons.util.AsyncProcess;
import org.springframework.ide.vscode.commons.util.ExceptionUtil;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.StringUtil;
import org.springframework.ide.vscode.commons.util.test.ACondition;
@@ -37,25 +41,36 @@ import com.google.common.collect.ImmutableList;
public class SpringBootAppTest {
// private static final String appName = "actuator-client-15-test-subject"; // Boot 1.5 test app
private static final String appName = "actuator-client-20-test-subject"; //Boot 2.0 test app
// private static final String appName = "actuator-client-20-thin-test-subject"; //Boot 2.0 test app with THIN launcher
private static final String[] appNames = {
"actuator-client-15-test-subject", // Boot 1.5 test app
"actuator-client-20-test-subject", //Boot 2.0 test app
"actuator-client-20-thin-test-subject", // Like the Boot 2.0 app, but packaged with thin launcher instead of fatjar
};
private static final Duration TIMEOUT = Duration.ofSeconds(30); // in CI build starting the app takes longer than 10s sometimes.
//Output from CI build: Started ActuatorClientTestSubjectApplication in 22.962 seconds (JVM running for 26.028)
private static final Duration TIMEOUT = Duration.ofSeconds(60); // in CI build starting the app takes a while, starting several in parallel takes even longer
private static final List<String> TEST_PROFILES = ImmutableList.of("testing", "funny", "cameleon");
private static AsyncProcess testAppRunner;
private static SpringBootApp testApp;
private static List<AsyncProcess> testAppRunners;
@BeforeClass
public static void setupClass() throws Exception {
testAppRunner = startTestApplication(SpringBootAppTest.class.getResource("/"+appName+"-0.0.1-SNAPSHOT.jar"));
ACondition.waitFor(TIMEOUT, () -> {
testApp = getAppContaining(appName);
assertNotNull(testApp);
});
testAppRunners = Arrays.asList(appNames).stream().map(appName -> {
try {
return startTestApplication(SpringBootAppTest.class.getResource("/boot-apps/"+appName+"-0.0.1-SNAPSHOT.jar"));
} catch (Exception e) {
throw ExceptionUtil.unchecked(e);
}
})
.collect(Collectors.toList());
}
@AfterClass
public static void tearDownClass() throws Exception {
for (AsyncProcess process : testAppRunners) {
process.kill();
}
testAppRunners = null;
}
private static AsyncProcess startTestApplication(URL jarUrl) throws Exception {
@@ -73,95 +88,127 @@ public class SpringBootAppTest {
);
}
private static SpringBootApp getAppContaining(String nameFragment) throws Exception {
return SpringBootApp.getAllRunningJavaApps().values().stream().filter(app -> app.getProcessName().contains(nameFragment)).findAny().get();
private SpringBootApp getAppContaining(String nameFragment) {
try {
return SpringBootApp.getAllRunningJavaApps().values().stream().filter(app -> app.getProcessName().contains(nameFragment)).findAny().get();
} catch (Exception e) {
throw ExceptionUtil.unchecked(e);
}
}
@AfterClass
public static void tearDownClass() throws Exception {
testAppRunner.kill();
@Ignore @Test public void dumpJvmInfo() throws Exception {
//Ignored because this test may have timing issues. Still useful to
// run locally and inspect dump results, but may need some tweaking.
for (String appName : appNames) {
SpringBootApp testApp = getAppContaining(appName);
testApp.dumpJvmInfo();
System.out.println("======================================");
}
}
@Test
public void getAllJavaApps() throws Exception {
@Test public void getAllJavaApps() throws Exception {
Map<String, SpringBootApp> allApps = SpringBootApp.getAllRunningJavaApps();
Optional<SpringBootApp> myProcess = allApps.values().stream().filter(app -> app.getProcessName().contains(appName)).findAny();
assertTrue(myProcess.isPresent());
}
@Test public void dumpJvmInfo() throws Exception {
ACondition.waitFor(TIMEOUT, this::getRequestMappings);
testApp.dumpJvmInfo();
// SpringBootApp app = getAppContaining("language-server.jar");
// app.dumpJvmInfo();
for (String appName : appNames) {
Optional<SpringBootApp> myProcess = allApps.values().stream().filter(app -> app.getProcessName().contains(appName)).findAny();
assertTrue(appName, myProcess.isPresent());
}
}
@Test public void getAllBootApps() throws Exception {
Map<String, SpringBootApp> allApps = SpringBootApp.getAllRunningSpringApps();
Optional<SpringBootApp> myProcess = allApps.values().stream().filter(app -> app.getProcessName().contains(appName)).findAny();
assertTrue(myProcess.isPresent());
for (String appName : appNames) {
Optional<SpringBootApp> myProcess = allApps.values().stream().filter(app -> app.getProcessName().contains(appName)).findAny();
assertTrue(myProcess.isPresent());
}
}
@Test
public void getPort() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
int port = Integer.parseInt(testApp.getPort());
assertTrue(port > 0);
// System.out.println("port = "+port);
});
for (SpringBootApp testApp : getTestApps()) {
ACondition.waitFor(TIMEOUT, () -> {
int port = Integer.parseInt(testApp.getPort());
assertTrue(port > 0);
// System.out.println("port = "+port);
});
}
}
private Collection<SpringBootApp> getTestApps() throws Exception {
return ACondition.waitForValue(TIMEOUT, () -> Arrays.asList(appNames).stream()
.map(this::getAppContaining)
.collect(Collectors.toList())
);
}
@Test
public void getHost() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
String host = testApp.getHost();
assertTrue(StringUtil.hasText(host));
// System.out.println("host = "+host);
});
for (SpringBootApp testApp : getTestApps()) {
System.err.println("getHost for "+testApp);
ACondition.waitFor(TIMEOUT, () -> {
String host = testApp.getHost();
assertTrue(StringUtil.hasText(host));
System.out.println("host = "+host);
});
}
}
@Test
public void getEnvironment() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
String env = testApp.getEnvironment();
assertNonEmptyJsonObject(env);
System.out.println("env = "+new JSONObject(env).toString(3));
});
for (SpringBootApp testApp : getTestApps()) {
ACondition.waitFor(TIMEOUT, () -> {
String env = testApp.getEnvironment();
assertNonEmptyJsonObject(env);
System.out.println("env = "+new JSONObject(env).toString(3));
});
}
}
@Test
public void getBeans() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
String beans = testApp.getBeans();
assertNonEmptyJsonObject(beans);
// System.out.println("beans = "+beans);
});
for (SpringBootApp testApp : getTestApps()) {
try {
ACondition.waitFor(TIMEOUT, () -> {
LiveBeansModel beansModel = testApp.getBeans();
assertTrue(beansModel.getAllBeans().findAny().isPresent());
// System.out.println("beans = "+beans);
});
} catch (Throwable e) {
//Make it easier to identify the culprit of failing test
throw new RuntimeException("Failed for: "+testApp.getProcessName(), e);
}
}
}
@Test
public void getRequestMappings() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
Collection<RequestMapping> result = testApp.getRequestMappings();
assertTrue(result != null && !result.isEmpty());
// System.out.println("requestMappings = "+result);
});
for (SpringBootApp testApp : getTestApps()) {
ACondition.waitFor(TIMEOUT, () -> {
Collection<RequestMapping> result = testApp.getRequestMappings();
assertTrue(result != null && !result.isEmpty());
// System.out.println("requestMappings = "+result);
});
}
}
@Test
public void getAutoConfigReport() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
String result = testApp.getAutoConfigReport();
assertNonEmptyJsonObject(result);
// System.out.println("autoconfreport = "+result);
});
for (SpringBootApp testApp : getTestApps()) {
ACondition.waitFor(TIMEOUT, () -> {
String result = testApp.getAutoConfigReport();
assertNonEmptyJsonObject(result);
// System.out.println("autoconfreport = "+result);
});
}
}
@Test
public void getProfiles() throws Exception {
ACondition.waitFor(TIMEOUT, () -> {
List<String> result = testApp.getActiveProfiles();
assertEquals(ImmutableList.copyOf(TEST_PROFILES), result);
});
for (SpringBootApp testApp : getTestApps()) {
ACondition.waitFor(TIMEOUT, () -> {
List<String> result = testApp.getActiveProfiles();
assertEquals(ImmutableList.copyOf(TEST_PROFILES), result);
});
}
}
private void assertNonEmptyJsonObject(String jsonData) {

View File

@@ -0,0 +1,8 @@
[
{
"context": "application",
"parent": null,
"beans": [
]
}
]

View File

@@ -0,0 +1,35 @@
[
{
"context": "application",
"parent": null,
"beans": [
{
"bean": "dependencyA",
"aliases": [],
"scope": "singleton",
"type": "org.test.DependencyA",
"resource": "file [/test-projects/classes/org/test/DependencyA.class]",
"dependencies": []
},
{
"bean": "dependencyB",
"aliases": [],
"scope": "singleton",
"type": "org.test.DependencyB",
"resource": "file [/test-projects/classes/org/test/DependencyB.class]",
"dependencies": []
},
{
"bean": "myAutowiredComponent",
"aliases": [],
"scope": "singleton",
"type": "org.test.MyAutowiredComponent",
"resource": "file [/test-projects/classes/org/test/MyAutowiredComponent.class]",
"dependencies": [
"dependencyA",
"dependencyB"
]
}
]
}
]