Skip to content
Open
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
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.lemminx.customservice.ActionableNotification;
import org.eclipse.lemminx.customservice.XMLLanguageClientAPI;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class MockXMLLanguageClient implements XMLLanguageClientAPI {
private final List<MessageParams> logMessages;

public MockXMLLanguageClient() {
publishDiagnostics = new ArrayList<>();
publishDiagnostics = new CopyOnWriteArrayList<>();
showMessages = new ArrayList<>();
logMessages = new ArrayList<>();
actionableNotifications = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public static void testCompletionItemResolveFor(XMLLanguageService xmlLanguageSe
CompletionList resolved = new CompletionList(
list.getItems().stream() //
.map((item) -> {
return (CompletionItem) xmlLanguageService.resolveCompletionItem(item, htmlDoc,
return xmlLanguageService.resolveCompletionItem(item, htmlDoc,
sharedSettings,
() -> {
});
Expand Down Expand Up @@ -860,6 +860,24 @@ public static void testPublishDiagnosticsFor(String xml, String fileURI, XMLLang
testPublishDiagnosticsFor(xml, fileURI, null, xmlLanguageService, expected);
}

public static void testPublishDiagnosticsFor(long timeout, String xml, String fileURI,
XMLValidationRootSettings validationSettings, XMLLanguageService xmlLanguageService,
PublishDiagnosticsParams... expected) {
long deadline = System.currentTimeMillis() + timeout;
while (true) {
try {
testPublishDiagnosticsFor(xml, fileURI, validationSettings, xmlLanguageService, expected);
return;
} catch (AssertionError e) {
if (System.currentTimeMillis() < deadline) {
Thread.yield();
continue;
}
throw e;
}
}
}

public static void testPublishDiagnosticsFor(String xml, String fileURI,
XMLValidationRootSettings validationSettings,
XMLLanguageService xmlLanguageService, PublishDiagnosticsParams... expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.eclipse.lemminx.MockXMLLanguageServer;
import org.eclipse.lemminx.XMLLanguageServer;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.FileChangeType;
Expand Down Expand Up @@ -81,15 +85,15 @@ public void externalDTDTest() throws InterruptedException, IOException {

Thread.sleep(threadSleepMs);

List<PublishDiagnosticsParams> actualDiagnostics = languageServer.getPublishDiagnostics();
List<PublishDiagnosticsParams> actualDiagnostics = getDiagnostic(1);
assertEquals(1, actualDiagnostics.size());
assertEquals(0, actualDiagnostics.get(0).getDiagnostics().size());

editFile(testDtd, 2, "");
didChangedWatchedFiles(languageServer, testDtd);

Thread.sleep(threadSleepMs);

actualDiagnostics = getDiagnostic(2);
assertEquals(2, actualDiagnostics.size());
assertFalse(actualDiagnostics.get(1).getDiagnostics().isEmpty());
assertEquals("MSG_ELEMENT_NOT_DECLARED", actualDiagnostics.get(1).getDiagnostics().get(0).getCode().getLeft());
Expand Down Expand Up @@ -134,19 +138,31 @@ public void externalXSDTest() throws InterruptedException, IOException {

clientOpenFile(languageServer, xmlTextDocument);

Thread.sleep(threadSleepMs);

List<PublishDiagnosticsParams> actualDiagnostics = languageServer.getPublishDiagnostics();
List<PublishDiagnosticsParams> actualDiagnostics = getDiagnostic(1);
assertEquals(1, actualDiagnostics.size());
assertEquals(0, actualDiagnostics.get(0).getDiagnostics().size());

editFile(testXsd, 12, " maxOccurs=\"2\"/>");
didChangedWatchedFiles(languageServer, testXsd);

Thread.sleep(threadSleepMs);

actualDiagnostics = getDiagnostic(2);
assertEquals(2, actualDiagnostics.size());
assertEquals("cvc-complex-type.2.4.f", actualDiagnostics.get(1).getDiagnostics().get(0).getCode().getLeft());
PublishDiagnosticsParams params = actualDiagnostics.get(1);
List<Diagnostic> diagnostics = params.getDiagnostics();
assertTrue(diagnostics.size() > 0);
assertEquals("cvc-complex-type.2.4.f", diagnostics.get(0).getCode().getLeft());
}

private List<PublishDiagnosticsParams> getDiagnostic(int wanted) {
long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
while (System.currentTimeMillis() < deadline) {
List<PublishDiagnosticsParams> list = new ArrayList<>(languageServer.getPublishDiagnostics());
if (list.size() >= wanted) {
return list;
}
Thread.yield();
}
fail("Did not receive at least " + wanted + " diagnostics within time frame!");
return new ArrayList<>();
}

private TextDocumentItem getXMLTextDocumentItem(String filename, String xmlContents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void entityRefDownloadProblem() throws Exception {
String dtdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.dtd").toString();
String fileURI = "test.xml";
// Downloading...
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls,
pd(fileURI,
new Diagnostic(r(2, 32, 2, 64),
"The resource 'http://localhost:8080/sample.dtd' is downloading in the cache path '"
Expand All @@ -177,10 +177,8 @@ public void entityRefDownloadProblem() throws Exception {
new Diagnostic(r(6, 1, 6, 7), "The entity \"abcd\" was referenced, but not declared.",
DiagnosticSeverity.Error, "xml", DTDErrorCode.EntityNotDeclared.getCode())));

TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines

// Downloaded error
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls,
pd(fileURI,
new Diagnostic(r(2, 32, 2, 64),
"Error while downloading 'http://localhost:8080/sample.dtd' to '" + dtdCachePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSchemaErrorCode;
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.DownloadDisabledResourceCodeAction;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lemminx.uriresolver.CacheResourcesManager;
import org.eclipse.lsp4j.Diagnostic;
Expand Down Expand Up @@ -91,18 +90,17 @@ public void noNamespaceSchemaLocationDownloadProblem() throws Exception {
String xsdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.xsd").toString();
String fileURI = "test.xml";
// Downloading...
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
new Diagnostic(r(2, 32, 2, 64),
"The resource 'http://localhost:8080/sample.xsd' is downloading in the cache path '"
+ xsdCachePath + "'.",
DiagnosticSeverity.Information, "xml", ExternalResourceErrorCode.DownloadingResource.getCode()),
new Diagnostic(r(0, 1, 0, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.",
DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode())));

TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines

// Downloaded error
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
new Diagnostic(r(2, 32, 2, 64),
"Error while downloading 'http://localhost:8080/sample.xsd' to '" + xsdCachePath
+ "' : '[java.net.ConnectException] Connection refused'.",
Expand Down Expand Up @@ -165,18 +163,16 @@ public void schemaLocationDownloadProblem() throws Exception {
String xsdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.xsd").toString();
String fileURI = "test.xml";
// Downloading...
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
new Diagnostic(r(3, 37, 3, 69),
"The resource 'http://localhost:8080/sample.xsd' is downloading in the cache path '"
+ xsdCachePath + "'.",
DiagnosticSeverity.Information, "xml", ExternalResourceErrorCode.DownloadingResource.getCode()),
new Diagnostic(r(0, 1, 0, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.",
DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode())));

TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines

// Downloaded error
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
new Diagnostic(r(3, 37, 3, 69),
"Error while downloading 'http://localhost:8080/sample.xsd' to '" + xsdCachePath
+ "' : '[java.net.ConnectException] Connection refused'.",
Expand Down
Loading