Skip to content

Commit

Permalink
Tests for fileAssociations updates
Browse files Browse the repository at this point in the history
Closes eclipse-lemminx#143

Signed-off-by: Nikolas <[email protected]>
  • Loading branch information
NikolasKomonen committed Jan 18, 2019
1 parent 006f630 commit b24e41a
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private void doTriggerValidation(String uri, int version, CancelChecker monitor)
getXMLLanguageService().publishDiagnostics(xmlDocument,
params -> xmlLanguageServer.getLanguageClient().publishDiagnostics(params),
(u, v) -> triggerValidation(u, v), monitor);
}
}
}

private XMLLanguageService getXMLLanguageService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.lsp4xml.services.extensions.IXMLExtension;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.services.extensions.save.ISaveContext;
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtensionManager;
import org.eclipse.lsp4xml.utils.DOMUtils;

Expand All @@ -38,7 +39,7 @@ public class ContentModelPlugin implements IXMLExtension {

private final ContentModelDocumentLinkParticipant documentLinkParticipant;

private ContentModelManager contentModelManager;
ContentModelManager contentModelManager;

private ContentModelSettings cmSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.lsp4xml.extensions.contentmodel.uriresolver;

import java.net.URI;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -39,7 +40,7 @@ public boolean setFileAssociations(XMLFileAssociation[] fileAssociations) {
XMLFileAssociation[] oldFileAssociations = this.fileAssociations;
this.fileAssociations = fileAssociations;
expandSystemId();
return !Objects.equals(oldFileAssociations, fileAssociations);
return !Arrays.equals(oldFileAssociations, fileAssociations);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public DOMDocument getDocument(String uri) {
public void collectDocumentToValidate(Predicate<DOMDocument> validateDocumentPredicate) {

}

@Override
public Object getSettings() {
return super.getSettings();
}
}

public static void testCompletionFor(String value, CompletionItem... expectedItems) throws BadLocationException {
Expand Down Expand Up @@ -269,6 +274,30 @@ public static void testDiagnosticsFor(String xml, String catalogPath, Consumer<X

}

public static void testSettingsDiagnosticsFor(String xml, String catalogPath,
String fileURI, boolean filter, Diagnostic... expected) {
TextDocument document = new TextDocument(xml, fileURI != null ? fileURI : "test.xml");

XMLLanguageService xmlLanguageService = new XMLLanguageService();
DOMDocument xmlDocument = DOMParser.getInstance().parse(document,
xmlLanguageService.getResolverExtensionManager());
xmlLanguageService.setDocumentProvider((uri) -> xmlDocument);

ContentModelSettings settings = new ContentModelSettings();
settings.setUseCache(false);
XMLProblems problems = new XMLProblems();
problems.setNoGrammar("ignore");
settings.setProblems(problems);
if (catalogPath != null) {
// Configure XML catalog for XML schema
settings.setCatalogs(new String[] { catalogPath });
}
xmlLanguageService.doSave(new SettingsSaveContext(settings));

//assertDiagnostics(actual, Arrays.asList(expected), filter);

}

public static void assertDiagnostics(List<Diagnostic> actual, Diagnostic... expected) {
assertDiagnostics(actual, Arrays.asList(expected), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@
package org.eclipse.lsp4xml.extensions.contentmodel;

import static org.eclipse.lsp4xml.XMLAssert.d;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.util.function.Consumer;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.XMLAssert;
import org.eclipse.lsp4xml.XMLTextDocumentService;
import org.eclipse.lsp4xml.XMLAssert.SettingsSaveContext;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.commons.TextDocument;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.XMLSchemaErrorCode;
import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lsp4xml.extensions.contentmodel.settings.XMLFileAssociation;
import org.eclipse.lsp4xml.services.XMLLanguageService;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.AllXMLSettings;
import org.eclipse.lsp4xml.settings.InitializationOptionsSettings;
import org.junit.Test;

/**
Expand All @@ -47,6 +61,130 @@ public void validationOnRoot() throws BadLocationException {
" <Configuration></Configuration>";
testDiagnosticsFor(xml, "file:///test/Test.Format.ps1xml", configuration);

}

@Test
public void validationOnRootOnUpdate() throws BadLocationException {
String json1 =
"{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + //
" \"pattern\": \"projectDescription.xml\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ]\r\n" + //
" }\r\n" +
" }\r\n" +
"}";

// Emulate InitializeParams#getInitializationOptions() object created as
// JSONObject when XMLLanguageServer#initialize(InitializeParams params) is
// called
InitializeParams params = createInitializeParams(json1);
Object initializationOptionsSettings = InitializationOptionsSettings.getSettings(params);
Object settings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings);

//Create content model settings, which include fileAssociations
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(settings);

SettingsSaveContext context = new SettingsSaveContext(settings);
ContentModelPlugin cmPlugin = new ContentModelPlugin();
//Initializes values in cmPlugin
cmPlugin.start(null, new XMLExtensionsRegistry());
//Set initial fileAssociations
cmPlugin.contentModelManager.setFileAssociations(cmSettings.getFileAssociations());
//Simulate an update of settings
cmPlugin.doSave(context);
//Try to set associations, should be false since they are the same
boolean last = cmPlugin.contentModelManager.setFileAssociations(cmSettings.getFileAssociations());
assertFalse(last);



}

@Test
public void validationOnRootDoesntUpdate() throws BadLocationException {
String json1 =
"{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + //
" \"pattern\": \"**/test*.xml\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + //
" \"pattern\": \"projectDescription.xml\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ]\r\n" + //
" }\r\n" +
" }\r\n" +
"}";

String json2 =
"{\r\n" + //
" \"settings\": {\r\n" + //
// Content model settings
" \"xml\": {\r\n" +
" \"fileAssociations\": [\r\n" + //
" {\r\n" + //
" \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-6000.0.xsd\",\r\n" + // <- Changed
" \"pattern\": \"**/test*.xml\"\r\n" + //
" }\r\n" + //

" ],\r\n" + //
" \"catalogs\": [\r\n" + //
" \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + //
" ]\r\n" + //
" }\r\n" +
" }\r\n" +
"}";
// Emulate InitializeParams#getInitializationOptions() object created as
// JSONObject when XMLLanguageServer#initialize(InitializeParams params) is
// called
InitializeParams params = createInitializeParams(json1);
Object initializationOptionsSettings = InitializationOptionsSettings.getSettings(params);
Object settings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings);

//Create content model settings, which include fileAssociations
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(settings);

SettingsSaveContext context = new SettingsSaveContext(settings);
ContentModelPlugin cmPlugin = new ContentModelPlugin();
//Initalize values in cmPlugin
cmPlugin.start(null, new XMLExtensionsRegistry());
//Set initial fileAssociations
cmPlugin.contentModelManager.setFileAssociations(cmSettings.getFileAssociations());
//Simulate an update of settings
cmPlugin.doSave(context);

//Create cmSettings with new fileAssociations settings
params = createInitializeParams(json2);
initializationOptionsSettings = InitializationOptionsSettings.getSettings(params);
settings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings);
cmSettings = ContentModelSettings.getContentModelXMLSettings(settings);
//Try to set associations, should be true since new fileAssociations were detected
boolean last = cmPlugin.contentModelManager.setFileAssociations(cmSettings.getFileAssociations());
assertTrue(last);



}

@Test
Expand Down Expand Up @@ -111,4 +249,11 @@ private static void testDiagnosticsFor(String xml, String fileURI, Consumer<XMLL
Diagnostic... expected) {
XMLAssert.testDiagnosticsFor(xml, null, configuration, fileURI, expected);
}

private static InitializeParams createInitializeParams(String json) {
InitializeParams initializeParams = new InitializeParams();
Object initializationOptions = new Gson().fromJson(json, JsonObject.class);
initializeParams.setInitializationOptions(initializationOptions);
return initializeParams;
}
}

0 comments on commit b24e41a

Please sign in to comment.