diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/BaseFileTempTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/BaseFileTempTest.java index 93c5543714..7ec566d648 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/BaseFileTempTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/BaseFileTempTest.java @@ -4,20 +4,21 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import org.junit.AfterClass; import org.junit.BeforeClass; -import com.google.common.base.Charsets; -import com.google.common.io.Files; import com.google.common.io.MoreFiles; import com.google.common.io.RecursiveDeleteOption; public class BaseFileTempTest { - protected static final String tempDirPath = "target/temp/"; - protected static final URI tempDirUri = Paths.get(tempDirPath).toAbsolutePath().toUri(); + private static final Path tempDirPath = Paths.get("target/temp/"); + protected static final URI tempDirUri = tempDirPath.toAbsolutePath().toUri(); @BeforeClass public static void setup() throws FileNotFoundException, IOException { @@ -42,7 +43,26 @@ private static void createTempDir() { tempDir.mkdir(); } - protected static void createFile(String path, String contents) throws IOException { - Files.asCharSink(new File(path), Charsets.UTF_8).write(contents); + protected static void createFile(String fileName, String contents) throws IOException { + try { + URI uri = new URI("file://" + fileName); + Path path = Paths.get(uri); + Files.write(path, contents.getBytes()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } + + protected static void updateFile(String path, String contents) throws IOException { + createFile(path, contents); + // For Mac OS, Linux OS, the call of Files.getLastModifiedTime is working for 1 + // second. + // Here we wait for > 1s to be sure that call of Files.getLastModifiedTime will + // work. + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelManagerCacheTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelManagerCacheTest.java index 01dbef77e1..e0d3b20ef3 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelManagerCacheTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/ContentModelManagerCacheTest.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import org.eclipse.lsp4xml.XMLAssert; import org.eclipse.lsp4xml.commons.BadLocationException; @@ -74,7 +75,7 @@ public void testXSDCache() throws IOException, BadLocationException { " \r\n" + // " \r\n" + // ""; - createFile(xsdPath, xsd); + updateFile(xsdPath, xsd); XMLAssert.testCompletionFor(xml, null, xmlPath, 5 /* region, endregion, cdata, comment, label */, c("label", "")); diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java index d39b30571e..ad0b49c58b 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaCompletionExtensionsTest.java @@ -31,9 +31,6 @@ import org.eclipse.lsp4xml.settings.XMLCompletionSettings; import org.junit.Test; -import com.google.common.io.MoreFiles; -import com.google.common.io.RecursiveDeleteOption; - /** * XSD completion tests. * @@ -392,7 +389,7 @@ public void completionWithXMLSchemaContentChanged() throws Exception { + " \r\n" // + " \r\n" + " \r\n" + " \r\n" + ""; - createFile(xsdPath, schema); + updateFile(xsdPath, schema); XMLAssert.testCompletionFor(xmlLanguageService, xml, null, null, "target/resources.xml", 4, false); } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/model/FilesChangedTrackerTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/model/FilesChangedTrackerTest.java new file mode 100644 index 0000000000..0f209c7dc8 --- /dev/null +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/model/FilesChangedTrackerTest.java @@ -0,0 +1,40 @@ +/******************************************************************************* +* Copyright (c) 2019 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package org.eclipse.lsp4xml.extensions.contentmodel.model; + +import java.io.IOException; + +import org.eclipse.lsp4xml.extensions.contentmodel.BaseFileTempTest; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link FilesChangedTracker} + * + * @author Angelo ZERR + * + */ +public class FilesChangedTrackerTest extends BaseFileTempTest { + + @Test + public void trackFile() throws IOException { + FilesChangedTracker tracker = new FilesChangedTracker(); + String fileURI = tempDirUri.getPath() + "/track.xml"; + createFile(fileURI, ""); + tracker.addFileURI("file://" + fileURI); + + Assert.assertFalse(tracker.isDirty()); + + updateFile(fileURI, ""); + Assert.assertTrue(tracker.isDirty()); + Assert.assertFalse(tracker.isDirty()); + + } +}