Skip to content

Commit

Permalink
Wait for 1 second in test when file changed
Browse files Browse the repository at this point in the history
Fixes #574

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Oct 17, 2019
1 parent f8a2c15 commit 792eca3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public boolean isDirty() {
// This case occurs when user delete the XML Schema / DTD file
return true;
}
FileTime currentLastMofied = Files.getLastModifiedTime(file);
if (!currentLastMofied.equals(lastModified)) {
lastModified = currentLastMofied;
FileTime currentLastModified = Files.getLastModifiedTime(file);
if (!currentLastModified.equals(lastModified)) {
lastModified = currentLastModified;
return true;
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
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 java.nio.file.StandardOpenOption;

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 {
Expand All @@ -42,7 +44,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(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}

protected static void updateFile(String fileName, String contents) throws IOException {
// 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(1050);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
createFile(fileName, contents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void testXSDCache() throws IOException, BadLocationException {
" </xs:complexType>\r\n" + //
" </xs:element>\r\n" + //
"</xs:schema>";
createFile(xsdPath, xsd);
updateFile(xsdPath, xsd);

XMLAssert.testCompletionFor(xml, null, xmlPath, 5 /* region, endregion, cdata, comment, label */,
c("label", "<label></label>"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -392,7 +389,7 @@ public void completionWithXMLSchemaContentChanged() throws Exception {
+ " </xs:sequence>\r\n"
// + " <xs:attribute name=\"variant\" type=\"xs:string\" use=\"required\"/>\r\n"
+ " </xs:complexType>\r\n" + " </xs:element>\r\n" + "</xs:schema>";
createFile(xsdPath, schema);
updateFile(xsdPath, schema);
XMLAssert.testCompletionFor(xmlLanguageService, xml, null, null, "target/resources.xml", 4, false);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void includedSchemaLocation() throws IOException {
"\r\n" + //
" <xs:element name=\"Root\" type=\"Root\"></xs:element>\r\n" + //
"</xs:schema>";
createFile(schemaAPath, schemaA);
updateFile(schemaAPath, schemaA);
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<Root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""
+ schemaAPath + "\">\r\n" + //
Expand Down Expand Up @@ -159,7 +159,7 @@ public void includedSchemaLocation() throws IOException {
"\r\n" + //
" <xs:element name=\"Root\" type=\"Root\"></xs:element>\r\n" + //
"</xs:schema>";
createFile(schemaAPath, schemaA);
updateFile(schemaAPath, schemaA);
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<Root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""
+ schemaAPath + "\">\r\n" + //
Expand All @@ -186,7 +186,7 @@ public void includedSchemaLocation() throws IOException {
" </xs:element>\r\n" + //
"\r\n" + //
"</xs:schema>";
createFile(schemaBPath, schemaB);
updateFile(schemaBPath, schemaB);

xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<Root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""
Expand Down Expand Up @@ -235,7 +235,7 @@ public void dtd() throws IOException {
dtd = "<!ELEMENT note (to,from)>\r\n" + //
"<!ELEMENT to (#PCDATA)>\r\n" + //
"";
createFile(dtdPath, dtd);
updateFile(dtdPath, dtd);
d = d(2, 1, 5, DTDErrorCode.MSG_CONTENT_INCOMPLETE);
testDiagnosticsFor(xmlLanguageService, xml, d);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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, "<root />");
tracker.addFileURI("file://" + fileURI);

Assert.assertFalse("No dirty after file creation", tracker.isDirty());

updateFile(fileURI, "<root />");
Assert.assertTrue("Dirty after file modification on isDirty first call", tracker.isDirty());
Assert.assertFalse("NO Dirty after file modification on isDirty second call", tracker.isDirty());

}
}

0 comments on commit 792eca3

Please sign in to comment.