Skip to content

Commit

Permalink
Add diagnostic for multiple versions of a feature (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylking authored Jan 30, 2024
1 parent 64bccad commit 819ae43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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

0 comments on commit 819ae43

Please sign in to comment.