From bea9d678a080eed29aebf2082bbe547f3bb81440 Mon Sep 17 00:00:00 2001 From: Cheryl King Date: Fri, 26 Jan 2024 16:14:40 -0600 Subject: [PATCH] Add diagnostic for multiple versions of a feature --- .../lemminx/LibertyDiagnosticParticipant.java | 16 +++++++++++++--- .../io/openliberty/LibertyDiagnosticTest.java | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/LibertyDiagnosticParticipant.java b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/LibertyDiagnosticParticipant.java index 50f49ec8..f68c54d8 100644 --- a/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/LibertyDiagnosticParticipant.java +++ b/lemminx-liberty/src/main/java/io/openliberty/tools/langserver/lemminx/LibertyDiagnosticParticipant.java @@ -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 @@ -109,6 +109,8 @@ private void validateFeature(DOMDocument domDocument, List list, DOM final int requestDelay = SettingsService.getInstance().getRequestDelay(); + Set featuresWithoutVersions = new HashSet(); + // Search for duplicate features // or features that do not exist List features = featureManager.getChildren(); @@ -126,14 +128,22 @@ private void validateFeature(DOMDocument domDocument, List 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); } } } diff --git a/lemminx-liberty/src/test/java/io/openliberty/LibertyDiagnosticTest.java b/lemminx-liberty/src/test/java/io/openliberty/LibertyDiagnosticTest.java index cdcd5547..c1a0b188 100644 --- a/lemminx-liberty/src/test/java/io/openliberty/LibertyDiagnosticTest.java +++ b/lemminx-liberty/src/test/java/io/openliberty/LibertyDiagnosticTest.java @@ -78,6 +78,25 @@ public void testFeatureDuplicateDiagnostic() { XMLAssert.testDiagnosticsFor(serverXML, null, null, serverXMLURI, dup1, dup2); } + @Test + public void testAnotherVersionOfFeatureDuplicateDiagnostic() { + String serverXML = String.join(newLine, // + "", // + " ", // + " jaxrs-2.0", // + " jaxrs-2.1", // + " jsonp-1.1", // + " ", // + "" // + ); + + 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, //