From b24e41acd4c52d0239ebfc64aeb98f274597485d Mon Sep 17 00:00:00 2001 From: Nikolas Date: Fri, 18 Jan 2019 09:54:47 -0500 Subject: [PATCH] Tests for fileAssociations updates Closes #143 Signed-off-by: Nikolas --- .../lsp4xml/XMLTextDocumentService.java | 2 +- .../contentmodel/ContentModelPlugin.java | 3 +- .../XMLFileAssociationResolverExtension.java | 3 +- .../java/org/eclipse/lsp4xml/XMLAssert.java | 29 ++++ .../XMLFileAssociationsDiagnosticsTest.java | 145 ++++++++++++++++++ 5 files changed, 179 insertions(+), 3 deletions(-) diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java index 262012f2a9..9f1550bfcf 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java @@ -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() { diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelPlugin.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelPlugin.java index a0f641b4e5..7a50cf0c14 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelPlugin.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelPlugin.java @@ -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; @@ -38,7 +39,7 @@ public class ContentModelPlugin implements IXMLExtension { private final ContentModelDocumentLinkParticipant documentLinkParticipant; - private ContentModelManager contentModelManager; + ContentModelManager contentModelManager; private ContentModelSettings cmSettings; diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/uriresolver/XMLFileAssociationResolverExtension.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/uriresolver/XMLFileAssociationResolverExtension.java index 3ccb164271..c1d2b7ef94 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/uriresolver/XMLFileAssociationResolverExtension.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/uriresolver/XMLFileAssociationResolverExtension.java @@ -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; @@ -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 diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/XMLAssert.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/XMLAssert.java index ceacd42a5f..18561edf8f 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/XMLAssert.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/XMLAssert.java @@ -78,6 +78,11 @@ public DOMDocument getDocument(String uri) { public void collectDocumentToValidate(Predicate validateDocumentPredicate) { } + + @Override + public Object getSettings() { + return super.getSettings(); + } } public static void testCompletionFor(String value, CompletionItem... expectedItems) throws BadLocationException { @@ -269,6 +274,30 @@ public static void testDiagnosticsFor(String xml, String catalogPath, Consumer 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 actual, Diagnostic... expected) { assertDiagnostics(actual, Arrays.asList(expected), true); } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLFileAssociationsDiagnosticsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLFileAssociationsDiagnosticsTest.java index 198b0ffe66..1137069842 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLFileAssociationsDiagnosticsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLFileAssociationsDiagnosticsTest.java @@ -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; /** @@ -47,6 +61,130 @@ public void validationOnRoot() throws BadLocationException { " "; 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 @@ -111,4 +249,11 @@ private static void testDiagnosticsFor(String xml, String fileURI, Consumer