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

Add diagnostic for multiple versions of a feature #271

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation and others.
* Copyright (c) 2020, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -109,6 +109,8 @@ private void validateFeature(DOMDocument domDocument, List<Diagnostic> list, DOM

final int requestDelay = SettingsService.getInstance().getRequestDelay();

Set<String> featuresWithoutVersions = new HashSet<String>();

// Search for duplicate features
// or features that do not exist
List<DOMNode> features = featureManager.getChildren();
Expand All @@ -126,14 +128,22 @@ private void validateFeature(DOMDocument domDocument, List<Diagnostic> list, DOM
list.add(new Diagnostic(range, message, DiagnosticSeverity.Error, LIBERTY_LEMMINX_SOURCE, INCORRECT_FEATURE_CODE));
} else {
String featureNameLower = featureName.toLowerCase();
String featureNameNoVersionLower = featureNameLower.substring(0,featureNameLower.lastIndexOf("-"));
// 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(),
featureTextNode.getEnd(), domDocument);
String message = "ERROR: " + featureName + " is already included.";
list.add(new Diagnostic(range, message, DiagnosticSeverity.Error, LIBERTY_LEMMINX_SOURCE));
} else {
includedFeatures.add(featureNameLower);
} else if (featuresWithoutVersions.contains(featureNameNoVersionLower)) {
Range range = XMLPositionUtility.createRange(featureTextNode.getStart(),
featureTextNode.getEnd(), domDocument);
String featureNameNoVersion = featureName.substring(0,featureName.lastIndexOf("-"));
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));
}
includedFeatures.add(featureNameLower);
featuresWithoutVersions.add(featureNameNoVersionLower);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ public void testFeatureDuplicateDiagnostic() {
XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI, dup1, dup2);
}

@Test
public void testAnotherVersionOfFeatureDuplicateDiagnostic() {
String serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <feature>jaxrs-2.0</feature>", //
" <feature>jaxrs-2.1</feature>", //
" <feature>jsonp-1.1</feature>", //
" </featureManager>", //
"</server>" //
);

Diagnostic dup1 = new Diagnostic();
dup1.setRange(r(3, 24, 3, 33));
dup1.setMessage("ERROR: More than one version of feature jaxrs is included. Only one version of a feature may be specified.");

XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI, dup1);
}

@Test
public void testInvalidFeatureDiagnostic() throws BadLocationException{
String serverXML = String.join(newLine, //
Expand Down
Loading