From 92f7f6541de83b846c61401904e455a50e0b1e76 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 --- .../contentmodel/ContentModelPlugin.java | 2 +- .../XMLFileAssociationResolverExtension.java | 3 +- .../java/org/eclipse/lsp4xml/XMLAssert.java | 2 - .../XMLFileAssociationsDiagnosticsTest.java | 145 ++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) 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 a0f641b4e..38f78afc9 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 @@ -38,7 +38,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 3ccb16427..c1d2b7ef9 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 93c670ac0..abba825f5 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 @@ -112,7 +112,6 @@ public static void testCompletionFor(XMLLanguageService xmlLanguageService, Stri new CompletionSettings(autoCloseTags), expectedItems); } - public static void testCompletionFor(XMLLanguageService xmlLanguageService, String value, String catalogPath, Consumer customConfiguration, String fileURI, Integer expectedCount, CompletionSettings completionSettings, CompletionItem... expectedItems) throws BadLocationException { @@ -267,7 +266,6 @@ public static void testDiagnosticsFor(String xml, String catalogPath, Consumer actual = xmlLanguageService.doDiagnostics(xmlDocument, () -> { }); assertDiagnostics(actual, Arrays.asList(expected), filter); - } public static void assertDiagnostics(List actual, Diagnostic... expected) { 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 198b0ffe6..733b517eb 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 testNoValidationOnFileAssociationNotChanged() 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 testValidationOnFileAssociationUpdate() 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