Skip to content

Commit

Permalink
Easily generate all-quarkus-properties.json
Browse files Browse the repository at this point in the history
Fixes redhat-developer#182

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jan 20, 2020
1 parent 63364fa commit bc25bfe
Show file tree
Hide file tree
Showing 8 changed files with 12,130 additions and 12,321 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Require-Bundle: org.junit,
com.google.guava,
com.redhat.microprofile.jdt.quarkus,
org.eclipse.lsp4j
Import-Package: com.google.gson
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package com.redhat.microprofile.jdt.core;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.Location;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.redhat.microprofile.commons.ClasspathKind;
import com.redhat.microprofile.commons.DocumentFormat;
import com.redhat.microprofile.commons.MicroProfileProjectInfo;
import com.redhat.microprofile.commons.MicroProfilePropertiesScope;
import com.redhat.microprofile.commons.metadata.ItemHint;
import com.redhat.microprofile.commons.metadata.ItemHint.ValueHint;
import com.redhat.microprofile.jdt.core.utils.IJDTUtils;
import com.redhat.microprofile.jdt.internal.core.ls.JDTUtilsLSImpl;

public class GenerateAllDefinition extends BasePropertiesManagerTest {

class PropertyDefinition {

private String sourceType;

private String sourceField;

private String sourceMethod;

private Location location;

public String getSourceType() {
return sourceType;
}

public void setSourceType(String sourceType) {
this.sourceType = sourceType;
}

public String getSourceField() {
return sourceField;
}

public void setSourceField(String sourceField) {
this.sourceField = sourceField;
}

public String getSourceMethod() {
return sourceMethod;
}

public void setSourceMethod(String sourceMethod) {
this.sourceMethod = sourceMethod;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}
}

@Test
public void generateAllQuarkusExtensionProperties() throws JavaModelException, CoreException, Exception {
generateJsonFiles(MavenProjectName.all_quarkus_extensions, JDTUtilsLSImpl.getInstance(), false);
}

@Test
public void generateAllQuarkusExtensionPropertiesAndDefinitions()
throws JavaModelException, CoreException, Exception {
generateJsonFiles(MavenProjectName.all_quarkus_extensions, JDTUtilsLSImpl.getInstance(), true);
}

private void generateJsonFiles(MavenProjectName mavenProject, IJDTUtils utils, boolean generateDefinition)
throws JavaModelException, CoreException, Exception {

Gson gson = new GsonBuilder().setPrettyPrinting().create();

long start = System.currentTimeMillis();
System.out.println("Start generating all-quarkus-properties.json");
IJavaProject javaProject = loadMavenProject(mavenProject);
MicroProfileProjectInfo info = PropertiesManager.getInstance().getMicroProfileProjectInfo(javaProject,
MicroProfilePropertiesScope.SOURCES_AND_DEPENDENCIES, ClasspathKind.SRC, utils, DocumentFormat.Markdown,
new NullProgressMonitor());

String baseDir = "../../microprofile.ls/com.redhat.microprofile.ls/src/test/resources/com/redhat/microprofile/services/";
// Generate all-quarkus-properties.json
String propertiesAsJson = gson.toJson(info);
Files.write(Paths.get(baseDir + "all-quarkus-properties.json"), propertiesAsJson.getBytes());

System.out.println(
"End generating all-quarkus-properties.json in " + (System.currentTimeMillis() - start) + "ms");

if (generateDefinition) {
start = System.currentTimeMillis();
System.out.println("Start generating all-quarkus-definitions.json");
// Generate all-quarkus-definitions.json

// Enable classFileContentsSupport to generate jdt Location
enableClassFileContentsSupport();

IFile applicationPropertiesFile = javaProject.getProject()
.getFile("src/main/resources/application.properties");
List<PropertyDefinition> definitions = info.getProperties().stream().map(item -> {
PropertyDefinition definition = new PropertyDefinition();
Location location = null;
try {
location = PropertiesManager.getInstance().findPropertyLocation(applicationPropertiesFile,
item.getSourceType(), item.getSourceField(), item.getSourceMethod(), utils,
new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
definition.setSourceType(item.getSourceType());
definition.setSourceField(item.getSourceField());
definition.setSourceMethod(item.getSourceMethod());
definition.setLocation(location);
return definition;
}).collect(Collectors.toList());

for (ItemHint hint : info.getHints()) {
for (ValueHint value : hint.getValues()) {
PropertyDefinition definition = new PropertyDefinition();
String sourceType = value.getSourceType() != null ? value.getSourceType() : hint.getSourceType();
if (sourceType != null) {
String sourceField = value.getValue();
String sourceMethod = null;
Location location = null;
try {
location = PropertiesManager.getInstance().findPropertyLocation(applicationPropertiesFile,
sourceType, sourceField, sourceMethod, utils, new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
definition.setSourceType(sourceType);
definition.setSourceField(sourceField);
definition.setSourceMethod(sourceMethod);
definition.setLocation(location);
definitions.add(definition);
}
}
}

String definitionsAsJson = gson.toJson(definitions);
Files.write(Paths.get(baseDir + "all-quarkus-definitions.json"), definitionsAsJson.getBytes());

System.out.println(
"End generating all-quarkus-definitions.json in " + (System.currentTimeMillis() - start) + "ms");
}
}

@Test
public void generateAllDefinitions() throws JavaModelException, CoreException, Exception {
MicroProfileProjectInfo info = getMicroProfileProjectInfoFromMavenProject(
MavenProjectName.all_quarkus_extensions, MicroProfilePropertiesScope.SOURCES_AND_DEPENDENCIES);
String s = new Gson().toJson(info);
System.err.println(s);
}

private static void enableClassFileContentsSupport() {
Map<String, Object> extendedClientCapabilities = new HashMap<>();
extendedClientCapabilities.put("classFileContentsSupport", "true");
JavaLanguageServerPlugin.getPreferencesManager().updateClientPrefences(new ClientCapabilities(),
extendedClientCapabilities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@
*/
public class MockMicroProfilePropertyDefinitionProvider implements MicroProfilePropertyDefinitionProvider {

private static class PropertyDefinition {

private String propertySource;
private static class PropertyDefinition extends MicroProfilePropertyDefinitionParams {

private Location location;

public String getPropertySource() {
return propertySource;
}

public Location getLocation() {
return location;
}
Expand All @@ -53,13 +47,17 @@ public MockMicroProfilePropertyDefinitionProvider() {
new InputStreamReader(MicroProfileAssert.class.getResourceAsStream("all-quarkus-definitions.json")),
PropertyDefinition[].class);
for (PropertyDefinition propertyDefinition : definitions) {
cache.put(propertyDefinition.getPropertySource(), propertyDefinition.getLocation());
cache.put(getKey(propertyDefinition), propertyDefinition.getLocation());
}
}

@Override
public CompletableFuture<Location> getPropertyDefinition(MicroProfilePropertyDefinitionParams params) {
return CompletableFuture.completedFuture(cache.get(params.getSourceType() + "#" + params.getSourceField()));
return CompletableFuture.completedFuture(cache.get(getKey(params)));
}

private static String getKey(MicroProfilePropertyDefinitionParams params) {
return params.getSourceType() + "#" + params.getSourceField() + "#" + params.getSourceMethod();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,55 @@ public void codeActionsForUnknownProperties() throws BadLocationException {

@Test
public void codeActionsForUnknownPropertiesParentKey() throws BadLocationException {
String value = "kubernetes.group=myUser\n" + //
"kubernetes.registry=http://my.docker-registry.net\n" + //
"kubernetes.labels[0].key=foo\n" + //
"kubernetes.labels[0].value=bar\n" + //
"kubernetes.readiness-probe.initial-delay-seconds=20\n" + //
"kubernetes.readiness-probe.period-seconds=45";
String value = "abcdefghij.group=myUser\n" + //
"abcdefghij.registry=http://my.docker-registry.net\n" + //
"abcdefghij.labels[0].key=foo\n" + //
"abcdefghij.labels[0].value=bar\n" + //
"abcdefghij.readiness-probe.initial-delay-seconds=20\n" + //
"abcdefghij.readiness-probe.period-seconds=45";


Diagnostic d1 = d(0, 0, 16, "Unknown property 'kubernetes.group'", DiagnosticSeverity.Warning,
Diagnostic d1 = d(0, 0, 16, "Unknown property 'abcdefghij.group'", DiagnosticSeverity.Warning,
ValidationType.unknown);
Diagnostic d2 = d(1, 0, 19, "Unknown property 'kubernetes.registry'", DiagnosticSeverity.Warning,
Diagnostic d2 = d(1, 0, 19, "Unknown property 'abcdefghij.registry'", DiagnosticSeverity.Warning,
ValidationType.unknown);
Diagnostic d3 = d(2, 0, 24, "Unknown property 'kubernetes.labels[0].key'", DiagnosticSeverity.Warning,
Diagnostic d3 = d(2, 0, 24, "Unknown property 'abcdefghij.labels[0].key'", DiagnosticSeverity.Warning,
ValidationType.unknown);
Diagnostic d4 = d(3, 0, 26, "Unknown property 'kubernetes.labels[0].value'", DiagnosticSeverity.Warning,
Diagnostic d4 = d(3, 0, 26, "Unknown property 'abcdefghij.labels[0].value'", DiagnosticSeverity.Warning,
ValidationType.unknown);
Diagnostic d5 = d(4, 0, 48, "Unknown property 'kubernetes.readiness-probe.initial-delay-seconds'", DiagnosticSeverity.Warning,
Diagnostic d5 = d(4, 0, 48, "Unknown property 'abcdefghij.readiness-probe.initial-delay-seconds'", DiagnosticSeverity.Warning,
ValidationType.unknown);
Diagnostic d6 = d(5, 0, 41, "Unknown property 'kubernetes.readiness-probe.period-seconds'", DiagnosticSeverity.Warning,
Diagnostic d6 = d(5, 0, 41, "Unknown property 'abcdefghij.readiness-probe.period-seconds'", DiagnosticSeverity.Warning,
ValidationType.unknown);

testDiagnosticsFor(value, d1, d2, d3, d4, d5, d6);
testCodeActionsFor(value, d1,
caAddToExcluded("kubernetes.group", d1),
caAddToExcluded("kubernetes.*", d1));
caAddToExcluded("abcdefghij.group", d1),
caAddToExcluded("abcdefghij.*", d1));

testCodeActionsFor(value, d2,
caAddToExcluded("kubernetes.registry", d2),
caAddToExcluded("kubernetes.*", d2));
caAddToExcluded("abcdefghij.registry", d2),
caAddToExcluded("abcdefghij.*", d2));

testCodeActionsFor(value, d3,
caAddToExcluded("kubernetes.labels[0].key", d3),
caAddToExcluded("kubernetes.labels[0].*", d3),
caAddToExcluded("kubernetes.*", d3));
caAddToExcluded("abcdefghij.labels[0].key", d3),
caAddToExcluded("abcdefghij.labels[0].*", d3),
caAddToExcluded("abcdefghij.*", d3));

testCodeActionsFor(value, d4,
caAddToExcluded("kubernetes.labels[0].value", d4),
caAddToExcluded("kubernetes.labels[0].*", d4),
caAddToExcluded("kubernetes.*", d4));
caAddToExcluded("abcdefghij.labels[0].value", d4),
caAddToExcluded("abcdefghij.labels[0].*", d4),
caAddToExcluded("abcdefghij.*", d4));

testCodeActionsFor(value, d5,
caAddToExcluded("kubernetes.readiness-probe.initial-delay-seconds", d5),
caAddToExcluded("kubernetes.readiness-probe.*", d5),
caAddToExcluded("kubernetes.*", d5));
caAddToExcluded("abcdefghij.readiness-probe.initial-delay-seconds", d5),
caAddToExcluded("abcdefghij.readiness-probe.*", d5),
caAddToExcluded("abcdefghij.*", d5));

testCodeActionsFor(value, d6,
caAddToExcluded("kubernetes.readiness-probe.period-seconds", d6),
caAddToExcluded("kubernetes.readiness-probe.*", d6),
caAddToExcluded("kubernetes.*", d6));
caAddToExcluded("abcdefghij.readiness-probe.period-seconds", d6),
caAddToExcluded("abcdefghij.readiness-probe.*", d6),
caAddToExcluded("abcdefghij.*", d6));
};

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void definitionOnKey() throws BadLocationException, InterruptedException,
public void definitionOnEnumValue() throws BadLocationException, InterruptedException, ExecutionException {
String value = "quarkus.log.syslog.async.overflow=BLO|CK";
testDefinitionFor(value, ll(
"jdt://contents/jboss-logmanager-embedded-1.0.4.jar/org.jboss.logmanager.handlers/AsyncHandler$OverflowAction.class?=abc/%5C/home%5C/dakwon%5C/.m2%5C/repository%5C/org%5C/jboss%5C/logmanager%5C/jboss-logmanager-embedded%5C/1.0.4%5C/jboss-logmanager-embedded-1.0.4.jar=/maven.pomderived=/true=/=/maven.pomderived=/true=/=/javadoc_location=/jar:file:%5C/home%5C/dakwon%5C/.m2%5C/repository%5C/org%5C/jboss%5C/logmanager%5C/jboss-logmanager-embedded%5C/1.0.4%5C/jboss-logmanager-embedded-1.0.4-javadoc.jar%5C!%5C/=/=/maven.groupId=/org.jboss.logmanager=/=/maven.artifactId=/jboss-logmanager-embedded=/=/maven.version=/1.0.4=/=/maven.scope=/compile=/%3Corg.jboss.logmanager.handlers(AsyncHandler$OverflowAction.class",
"jdt://contents/jboss-logmanager-embedded-1.0.3.jar/org.jboss.logmanager.handlers/AsyncHandler$OverflowAction.class?\u003dall-quarkus-extensions/C:%5C/Users%5C/azerr%5C/.m2%5C/repository%5C/org%5C/jboss%5C/logmanager%5C/jboss-logmanager-embedded%5C/1.0.3%5C/jboss-logmanager-embedded-1.0.3.jar%3Corg.jboss.logmanager.handlers(AsyncHandler$OverflowAction.class",
r(0, 34, 39), r(222, 8, 13)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static MicroProfileProjectInfo getDefaultMicroProfileProjectInfo() {
return DEFAULT_PROJECT;
}

public static MicroProfilePropertyDefinitionProvider getDefaultQuarkusPropertyDefinitionProvider() {
public static MicroProfilePropertyDefinitionProvider getDefaultMicroProfilePropertyDefinitionProvider() {
if (DEFAULT_DEFINITION_PROVIDER == null) {
DEFAULT_DEFINITION_PROVIDER = new MockMicroProfilePropertyDefinitionProvider();
}
Expand Down Expand Up @@ -356,7 +356,7 @@ public static void assertDocumentSymbols(List<DocumentSymbol> actual, DocumentSy

public static void testDefinitionFor(String value, LocationLink... expected)
throws BadLocationException, InterruptedException, ExecutionException {
testDefinitionFor(value, getDefaultMicroProfileProjectInfo(), getDefaultQuarkusPropertyDefinitionProvider(),
testDefinitionFor(value, getDefaultMicroProfileProjectInfo(), getDefaultMicroProfilePropertyDefinitionProvider(),
expected);
}

Expand Down
Loading

0 comments on commit bc25bfe

Please sign in to comment.