Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing feature uniqueness diagnostic issue for renamed features #305

Merged
merged 4 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static io.openliberty.tools.langserver.lemminx.util.LibertyConstants.changedFeatureNameDiagMessage;

public class LibertyDiagnosticParticipant implements IDiagnosticsParticipant {
private static final Logger LOGGER = Logger.getLogger(LibertyDiagnosticParticipant.class.getName());

Expand Down Expand Up @@ -163,14 +167,31 @@ private void validateFeature(DOMDocument domDocument, List<Diagnostic> list, Set
*/
private void checkForFeatureUniqueness(DOMDocument domDocument, List<Diagnostic> list, Set<String> includedFeatures, DOMNode featureTextNode, Set<String> versionedFeatures, Set<String> versionlessFeatures, Set<String> featuresWithoutVersions, String featureName) {
String featureNameLower = featureName.toLowerCase();
String featureNameNoVersionLower=featureNameLower;
String featureNameNoVersionLower;
if(featureNameLower.contains("-")){
featureNameNoVersionLower = featureNameLower.substring(0,
featureNameLower.lastIndexOf("-"));
versionedFeatures.add(featureName);
}else{
featureNameNoVersionLower = featureNameLower;
versionlessFeatures.add(featureName);
}
String featureNameNoVersion = LibertyUtils.stripVersion(featureName);
Map<String, String> changedFeatureNameMapLower = LibertyConstants.changedFeatureNameMap.entrySet().parallelStream().collect(
Collectors.toMap(entry -> entry.getKey().toLowerCase(),
entry -> entry.getValue().toLowerCase()));
Map<String, String> changedFeatureNameMapLowerReversed =
changedFeatureNameMapLower.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
Set<String> featuresWithOldNames = featuresWithoutVersions.stream().map(
v -> changedFeatureNameMapLower.get(v + "-"))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
Set<String> featuresWithChangedNames = featuresWithoutVersions.stream().map(
cherylking marked this conversation as resolved.
Show resolved Hide resolved
v -> changedFeatureNameMapLowerReversed.get(v + "-"))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
// if this exact feature already exists, or another version of this feature already exists, then show a diagnostic
if (includedFeatures.contains(featureNameLower)) {
Range range = XMLPositionUtility.createRange(featureTextNode.getStart(),
Expand All @@ -180,9 +201,25 @@ private void checkForFeatureUniqueness(DOMDocument domDocument, List<Diagnostic>
} else if (featuresWithoutVersions.contains(featureNameNoVersionLower)) {
Range range = XMLPositionUtility.createRange(featureTextNode.getStart(),
featureTextNode.getEnd(), domDocument);
String featureNameNoVersion = LibertyUtils.stripVersion(featureName);
String message = "ERROR: More than one version of feature " + featureNameNoVersion + " is included. Only one version of a feature may be specified.";
list.add(new Diagnostic(range, message, DiagnosticSeverity.Error, LIBERTY_LEMMINX_SOURCE));
} else if (featuresWithOldNames.contains(featureNameNoVersionLower + "-")) {
String otherFeatureName = getOtherFeatureName(includedFeatures, changedFeatureNameMapLowerReversed, featureNameNoVersionLower);
//check for features whose name is changed such as jsp is changed to pages
Range range = XMLPositionUtility.createRange(featureTextNode.getStart(),
featureTextNode.getEnd(), domDocument);
String message = String.format(changedFeatureNameDiagMessage, featureName, otherFeatureName,
LibertyUtils.stripVersion(otherFeatureName),
LibertyUtils.stripVersion(featureName));
list.add(new Diagnostic(range, message, DiagnosticSeverity.Error, LIBERTY_LEMMINX_SOURCE));
} else if (featuresWithChangedNames.contains(featureNameNoVersionLower + "-")) {
String otherFeatureName = getOtherFeatureName(includedFeatures, changedFeatureNameMapLower, featureNameNoVersionLower);
Range range = XMLPositionUtility.createRange(featureTextNode.getStart(),
featureTextNode.getEnd(), domDocument);
String message = String.format(changedFeatureNameDiagMessage, featureName, otherFeatureName,
LibertyUtils.stripVersion(featureName),
LibertyUtils.stripVersion(otherFeatureName));
list.add(new Diagnostic(range, message, DiagnosticSeverity.Error, LIBERTY_LEMMINX_SOURCE));
}
includedFeatures.add(featureNameLower);
featuresWithoutVersions.add(featureNameNoVersionLower);
Expand Down Expand Up @@ -520,4 +557,20 @@ private void checkForVersionlessPlatforms(DOMDocument domDocument,
list.add(new Diagnostic(range, message, DiagnosticSeverity.Error, LIBERTY_LEMMINX_SOURCE, INCORRECT_FEATURE_CODE));
}
}

/**
* Get conflicting featurename for any feature
* @param includedFeatures
* @param changedFeatureNameMap
* @param featureNameNoVersionLower
* @return
*/
private static String getOtherFeatureName(Set<String> includedFeatures, Map<String, String> changedFeatureNameMapLowerReversed, String featureNameNoVersionLower) {
String otherFeatureNameWithoutVersion = changedFeatureNameMapLowerReversed.get(featureNameNoVersionLower + "-");
String otherFeatureName = includedFeatures
.stream()
.filter(f -> f.toLowerCase().contains(LibertyUtils.stripVersion(otherFeatureNameWithoutVersion)))
.findFirst().orElse(null);
return otherFeatureName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ private LibertyConstants() {
put("wasJmsClient-", "messagingClient-");
put("wasJmsSecurity-", "messagingSecurity-");
}});

public static String changedFeatureNameDiagMessage="ERROR: The %s feature cannot be configured with the %s feature because they are two different versions of the same feature. " +
"The feature name changed from %s to %s for Jakarta EE. Remove one of the features.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,67 @@ public void testConfigElementVersionLess() throws JAXBException {
configForMissingFeature.setMessage(LibertyDiagnosticParticipant.MISSING_CONFIGURED_FEATURE_MESSAGE);
XMLAssert.testDiagnosticsFor(serverXML1, null, null, serverXMLURI,configForMissingFeature);
}

@Test
public void testUniquenessForNameChangedFeatureDiagnostic() throws BadLocationException {
String serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <feature>jms-2.0</feature>", //
" <feature>messaging-3.0</feature>", //
" </featureManager>", //
"</server>" //
);
Diagnostic invalid = new Diagnostic();
invalid.setRange(r(3, 24, 3, 37));
invalid.setMessage("ERROR: The messaging-3.0 feature cannot be configured with the jms-2.0 feature because they are two different versions of the same feature. The feature name changed from jms to messaging for Jakarta EE. Remove one of the features.");
cherylking marked this conversation as resolved.
Show resolved Hide resolved
XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI,
invalid);

serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <feature>messaging-3.0</feature>", //
" <feature>jms-2.0</feature>", //
" </featureManager>", //
"</server>" //
);
invalid = new Diagnostic();
invalid.setRange(r(3, 24, 3, 31));
invalid.setMessage("ERROR: The jms-2.0 feature cannot be configured with the messaging-3.0 feature because they are two different versions of the same feature. The feature name changed from jms to messaging for Jakarta EE. Remove one of the features.");
XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI,
invalid);

serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <platform>javaee-7.0</platform>", //
" <feature>enterpriseBeans-4.0</feature>", //
" <feature>ejb-3.2</feature>", //
" </featureManager>", //

"</server>" //
);
invalid = new Diagnostic();
invalid.setRange(r(4, 24, 4, 31));
invalid.setMessage("ERROR: The ejb-3.2 feature cannot be configured with the enterprisebeans-4.0 feature because they are two different versions of the same feature. The feature name changed from ejb to enterprisebeans for Jakarta EE. Remove one of the features.");
cherylking marked this conversation as resolved.
Show resolved Hide resolved
XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI,
invalid);

serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <platform>javaee-7.0</platform>", //
" <feature>enterpriseBeans</feature>", //
" <feature>ejb</feature>", //
" </featureManager>", //

"</server>" //
);
invalid = new Diagnostic();
invalid.setRange(r(4, 24, 4, 27));
invalid.setMessage("ERROR: The ejb feature cannot be configured with the enterprisebeans feature because they are two different versions of the same feature. The feature name changed from ejb to enterprisebeans for Jakarta EE. Remove one of the features.");
XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI,
invalid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ public void didOpen(DidOpenTextDocumentParams params) {
LibertyTextDocument document = documents.onDidOpenTextDocument(params);
String uri = document.getUri();
validate(Arrays.asList(uri));
if (uri == null) {
arunvenmany-ibm marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.severe("Liberty text document URI is null for " + params);
}
new DiagnosticRunner(libertyLanguageServer).compute(params);
}

@Override
public void didChange(DidChangeTextDocumentParams params) {
LibertyTextDocument document = documents.onDidChangeTextDocument(params);
String uri = document.getUri();
if (uri == null) {
LOGGER.severe("Liberty text document URI is null for " + params);
}
validate(Arrays.asList(uri));
new DiagnosticRunner(libertyLanguageServer).compute(params);
}
Expand All @@ -97,6 +103,9 @@ public void didChange(DidChangeTextDocumentParams params) {
public void didClose(DidCloseTextDocumentParams params) {
documents.onDidCloseTextDocument(params);
String uri = params.getTextDocument().getUri();
if (uri == null) {
LOGGER.severe("Liberty text document URI is null for " + params);
}
libertyLanguageServer.getLanguageClient()
.publishDiagnostics(new PublishDiagnosticsParams(uri, new ArrayList<Diagnostic>()));
}
Expand All @@ -117,8 +126,16 @@ private void validateAll() {
@Override
public CompletableFuture<Hover> hover(HoverParams hoverParams) {
String uri = hoverParams.getTextDocument().getUri();
if (uri == null) {
LOGGER.severe("Liberty text document URI is null for " + hoverParams);
}
LibertyTextDocument textDocumentItem = documents.get(uri);
return new LibertyPropertiesHoverProvider(textDocumentItem).getHover(hoverParams.getPosition());
if (textDocumentItem != null) {
return new LibertyPropertiesHoverProvider(textDocumentItem).getHover(hoverParams.getPosition());
} else {
LOGGER.severe("The document with uri " + uri + " has not been found in opened documents. Cannot provide hover.");
return CompletableFuture.completedFuture(new Hover());
}
}

@Override
Expand Down
Loading