Skip to content

Commit

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

Signed-off-by: Nikolas <[email protected]>
  • Loading branch information
NikolasKomonen authored and fbricon committed Jan 23, 2019
1 parent 05895c4 commit 92f7f65
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 @@ -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<XMLLanguageService> customConfiguration, String fileURI, Integer expectedCount,
CompletionSettings completionSettings, CompletionItem... expectedItems) throws BadLocationException {
Expand Down Expand Up @@ -267,7 +266,6 @@ public static void testDiagnosticsFor(String xml, String catalogPath, Consumer<X
List<Diagnostic> actual = xmlLanguageService.doDiagnostics(xmlDocument, () -> {
});
assertDiagnostics(actual, Arrays.asList(expected), filter);

}

public static void assertDiagnostics(List<Diagnostic> actual, Diagnostic... expected) {
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 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
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 92f7f65

Please sign in to comment.