Skip to content

Commit

Permalink
[tests] Fix 'IllegalArgumentException: URI is not absolute' exceptions
Browse files Browse the repository at this point in the history
As they may prevent some diagnostics/code action/completions etc. to be generated and properly verified

```
java.lang.IllegalArgumentException: URI is not absolute
	at java.base/java.io.File.<init>(File.java:418)
	at org.eclipse.lemminx.extensions.maven.MavenProjectCache.parseAndCache(MavenProjectCache.java:239)
	at org.eclipse.lemminx.extensions.maven.MavenProjectCache.check(MavenProjectCache.java:124)
	at org.eclipse.lemminx.extensions.maven.MavenProjectCache.getLastSuccessfulMavenProject(MavenProjectCache.java:91)
	at org.eclipse.lemminx.extensions.maven.participants.codeaction.ExtractPropertyCodeAction.doCodeActionUnconditional(ExtractPropertyCodeAction.java:121)
	at org.eclipse.lemminx.services.XMLCodeActions.doCodeActions(XMLCodeActions.java:81)
	at org.eclipse.lemminx.services.XMLLanguageService.doCodeActions(XMLLanguageService.java:284)
	at org.eclipse.lemminx.XMLAssert.testCodeActionsFor(XMLAssert.java:960)
	at org.eclipse.lemminx.extensions.maven.participants.codeaction.MavenCodeActionParticipantTest.testCodeAction(MavenCodeActionParticipantTest.java:250)
	at org.eclipse.lemminx.extensions.maven.participants.codeaction.MavenCodeActionParticipantTest.testCodeActionsForParentGroupIdRemoval(MavenCodeActionParticipantTest.java:94)
```
  • Loading branch information
vrubezhny committed Jul 21, 2023
1 parent c67f0d6 commit 576cf39
Showing 1 changed file with 83 additions and 13 deletions.
Expand Up @@ -8,16 +8,17 @@
*******************************************************************************/
package org.eclipse.lemminx.extensions.maven.participants.codeaction;

import static org.eclipse.lemminx.XMLAssert.assertCodeActions;
import static org.eclipse.lemminx.XMLAssert.ca;
import static org.eclipse.lemminx.XMLAssert.d;
import static org.eclipse.lemminx.XMLAssert.teOp;
import static org.eclipse.lemminx.XMLAssert.testCodeActionsFor;

import static org.eclipse.lemminx.extensions.maven.participants.codeaction.MavenNoGrammarConstraintsCodeAction.XSI_VALUE_PATTERN;
import static org.eclipse.lemminx.extensions.maven.utils.MavenLemminxTestsUtils.createDOMDocument;
import static org.eclipse.lemminx.extensions.maven.utils.ParticipantUtils.getDocumentLineSeparator;

import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.net.URI;
Expand All @@ -30,7 +31,9 @@

import org.eclipse.lemminx.XMLAssert.SettingsSaveContext;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.commons.TextDocument;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMParser;
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSyntaxErrorCode;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
Expand All @@ -39,16 +42,23 @@
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lemminx.utils.StringUtils;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionContext;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticRelatedInformation;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.google.gson.Gson;

import org.eclipse.lemminx.extensions.maven.MavenSyntaxErrorCode;

@ExtendWith(NoMavenCentralExtension.class)
public class MavenCodeActionParticipantTest {
private static final String POM_FILE = "file:///pom.xml";

private XMLLanguageService xmlLanguageService = new XMLLanguageService();
private SharedSettings sharedSettings = new SharedSettings();

Expand All @@ -61,7 +71,7 @@ public void testCodeActionsForNoGrammarConstraints() throws Exception {
Diagnostic expectedDiagnostic = d(1, 1, 1, 8,
XMLSyntaxErrorCode.NoGrammarConstraints,
"No grammar constraints (DTD or XML Schema).");
CodeAction expectedCodeAction = ca(expectedDiagnostic, teOp("pom.xml", 1, 8, 1, 8,
CodeAction expectedCodeAction = ca(expectedDiagnostic, teOp(POM_FILE, 1, 8, 1, 8,
String.format(XSI_VALUE_PATTERN, getDocumentLineSeparator(xmlDocument))));
testCodeAction(xmlDocument,false, expectedDiagnostic, expectedCodeAction);
}
Expand All @@ -77,7 +87,7 @@ public void testCodeActionsForParentVersionRemoval() throws Exception {
Diagnostic expectedDiagnostic = d(11, 11, 11, 16,
MavenSyntaxErrorCode.DuplicationOfParentVersion,
"Version is duplicate of parent version");
CodeAction expectedCodeAction = ca(expectedDiagnostic, teOp("pom.xml", 11, 2, 11, 26, ""));
CodeAction expectedCodeAction = ca(expectedDiagnostic, teOp(POM_FILE, 11, 2, 11, 26, ""));
testCodeAction(xmlDocument,true, expectedDiagnostic, expectedCodeAction);
}

Expand All @@ -90,7 +100,7 @@ public void testCodeActionsForParentGroupIdRemoval() throws Exception {
Diagnostic expectedDiagnostic = d(11, 11, 11, 27,
MavenSyntaxErrorCode.DuplicationOfParentGroupId,
"GroupId is duplicate of parent groupId");
CodeAction expectedCodeAction = ca(expectedDiagnostic, teOp("pom.xml", 11, 2, 11, 37, ""));
CodeAction expectedCodeAction = ca(expectedDiagnostic, teOp(POM_FILE, 11, 2, 11, 37, ""));
testCodeAction(xmlDocument,true, expectedDiagnostic, expectedCodeAction);
}

Expand All @@ -117,8 +127,8 @@ public void testCodeActionsForManagedDependencyOverride() throws Exception {
expectedDiagnostic.setData(data);

// Test diagnostic and code action for a different version value
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp("pom.xml", 17, 6, 17, 31, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp("pom.xml", 17, 31, 17, 31, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp(POM_FILE, 17, 6, 17, 31, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp(POM_FILE, 17, 31, 17, 31, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_3 = ca(expectedDiagnostic,
new Command("Open declaration of managed version", "xml.open.uri",
Arrays.asList(parentFile.toURI().toString() +
Expand Down Expand Up @@ -148,8 +158,8 @@ public void testCodeActionsForManagedDependencyDuplicate() throws Exception {
expectedDiagnostic.setData(data);

// Test diagnostic and code action for the same version value
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp("pom.xml", 17, 6, 17, 31, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp("pom.xml", 17, 31, 17, 31, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp(POM_FILE, 17, 6, 17, 31, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp(POM_FILE, 17, 31, 17, 31, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_3 = ca(expectedDiagnostic,
new Command("Open declaration of managed version", "xml.open.uri",
Arrays.asList(parentFile.toURI().toString() +
Expand Down Expand Up @@ -182,8 +192,8 @@ public void testCodeActionsForManagedPluginOverride() throws Exception {
expectedDiagnostic.setData(data);

// Test diagnostic and code action for a different version value
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp("pom.xml", 24, 12, 24, 36, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp("pom.xml", 24, 36, 24, 36, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp(POM_FILE, 24, 12, 24, 36, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp(POM_FILE, 24, 36, 24, 36, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_3 = ca(expectedDiagnostic,
new Command("Open declaration of managed version", "xml.open.uri",
Arrays.asList(parentFile.toURI().toString() +
Expand Down Expand Up @@ -213,8 +223,8 @@ public void testCodeActionsForManagedPluginDuplicate() throws Exception {
expectedDiagnostic.setData(data);

// Test diagnostic and code action for the same version value
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp("pom.xml", 18, 8, 18, 32, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp("pom.xml", 18, 32, 18, 32, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_1 = ca(expectedDiagnostic, teOp(POM_FILE, 18, 8, 18, 32, ""));
CodeAction expectedCodeAction_2 = ca(expectedDiagnostic, teOp(POM_FILE, 18, 32, 18, 32, "<!--$NO-MVN-MAN-VER$-->"));
CodeAction expectedCodeAction_3 = ca(expectedDiagnostic,
new Command("Open declaration of managed version", "xml.open.uri",
Arrays.asList(parentFile.toURI().toString() +
Expand Down Expand Up @@ -247,7 +257,7 @@ private void testCodeAction(DOMDocument xmlDocument, boolean ignoreNoGrammar, Di
assertDiagnostics(actual, Arrays.asList(expectedDiagnostic), true);

// Test for expected code action is returned
testCodeActionsFor(xmlDocument.getText(), expectedDiagnostic, null, (String) null, "pom.xml",
testCodeActionsFor(xmlDocument.getText(), expectedDiagnostic, null, (String) null, POM_FILE,
sharedSettings, xmlLanguageService, -1, expectedCodeAction);
}

Expand Down Expand Up @@ -288,6 +298,66 @@ public static void assertDiagnostics(List<Diagnostic> actual, List<Diagnostic> e
assertIterableEquals(expected, received, "Unexpected diagnostics:\n" + actual);
}

public static List<CodeAction> testCodeActionsFor(String xml, Diagnostic diagnostic, Range range, String catalogPath,
String fileURI, SharedSettings sharedSettings, XMLLanguageService xmlLanguageService, int index,
CodeAction... expected) throws BadLocationException {
int offset = xml.indexOf('|');
if (offset != -1) {
xml = xml.substring(0, offset) + xml.substring(offset + 1);
}
TextDocument document = new TextDocument(xml.toString(), fileURI != null ? fileURI : POM_FILE);

// Use range from the text (if marked by "|"-char or from diagnostics
if (offset != -1) {
Position position = document.positionAt(offset);
range = new Range(position, position);
} else if (range == null && diagnostic != null) {
range = diagnostic.getRange();
}

// Otherwise, range is to be specified in parameters
assertNotNull(range, "Range cannot be null");

if (xmlLanguageService == null) {
xmlLanguageService = new XMLLanguageService();
}

ContentModelSettings cmSettings = new ContentModelSettings();
cmSettings.setUseCache(false);
if (catalogPath != null) {
// Configure XML catalog for XML schema
cmSettings.setCatalogs(new String[] { catalogPath });
}
xmlLanguageService.doSave(new SettingsSaveContext(cmSettings));

CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Arrays.asList(diagnostic));
DOMDocument xmlDoc = DOMParser.getInstance().parse(document, xmlLanguageService.getResolverExtensionManager());
xmlLanguageService.setDocumentProvider((uri) -> xmlDoc);

List<CodeAction> actual = xmlLanguageService.doCodeActions(context, range, xmlDoc, sharedSettings, () -> {})
.stream().filter(ca -> ca.getDiagnostics().contains(diagnostic)).toList();

// Clone
// Creating a gson object
Gson gson = new Gson();
// Converting the list into a json string
String jsonstring = gson.toJson(actual);

// Converting the json string
// back into a list
CodeAction[] cloned_list = gson.fromJson(jsonstring, CodeAction[].class);

// Only test the code action at index if a proper index is given
if (index >= 0) {
assertCodeActions(Arrays.asList(actual.get(index)), Arrays.asList(expected).get(index));
return Arrays.asList(cloned_list);
}
assertCodeActions(actual, expected);
return Arrays.asList(cloned_list);
}


private static ContentModelSettings createContentModelSettings(boolean ignoreNoGrammar) {
ContentModelSettings settings = new ContentModelSettings();
settings.setUseCache(false);
Expand Down

0 comments on commit 576cf39

Please sign in to comment.