mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-01 11:20:44 +07:00
Cleanup a test and it for the future
This commit is contained in:
@@ -35,11 +35,13 @@ import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.intellij.openapi.util.io.IoTestUtil.assertTimestampsEqual;
|
||||
import static com.intellij.openapi.util.io.IoTestUtil.assertTimestampsNotEqual;
|
||||
|
||||
public class VfsUtilTest extends PlatformLangTestCase {
|
||||
@Override
|
||||
protected void runBareRunnable(Runnable runnable) throws Throwable {
|
||||
@@ -79,16 +81,6 @@ public class VfsUtilTest extends PlatformLangTestCase {
|
||||
public void testFindFileByUrl() throws Exception {
|
||||
VirtualFile file0;
|
||||
|
||||
// Should not find in jre jars - it creates a lot of VirtualFileImpl!
|
||||
|
||||
// file0 = VfsUtil.findFileByURL(ClassLoader.getSystemResource("java/lang/Object.class"));
|
||||
// assertNotNull(file0);
|
||||
// assertFalse(file0.isDirectory());
|
||||
//
|
||||
// file0 = VfsUtil.findFileByURL(ClassLoader.getSystemResource("com/sun"));
|
||||
// assertNotNull(file0);
|
||||
// assertTrue(file0.isDirectory());
|
||||
|
||||
File file1 = new File(PathManagerEx.getTestDataPath());
|
||||
file1 = new File(file1, "vfs");
|
||||
file1 = new File(file1, "findFileByUrl");
|
||||
@@ -121,7 +113,7 @@ public class VfsUtilTest extends PlatformLangTestCase {
|
||||
|
||||
File file3 = new File(file1, "1.txt");
|
||||
file0 = VfsUtil.findFileByURL(file3.toURI().toURL());
|
||||
String content = VfsUtil.loadText(file0);
|
||||
String content = VfsUtilCore.loadText(file0);
|
||||
assertNotNull(file0);
|
||||
assertFalse(file0.isDirectory());
|
||||
assertEquals(content, "test text");
|
||||
@@ -157,38 +149,41 @@ public class VfsUtilTest extends PlatformLangTestCase {
|
||||
final VirtualFile vSubDir = LocalFileSystem.getInstance().findFileByIoFile(subDir);
|
||||
assertNotNull(vSubDir);
|
||||
|
||||
VfsUtil.visitChildrenRecursively(data.getBase(), new VirtualFileVisitor() {
|
||||
VfsUtilCore.visitChildrenRecursively(data.getBase(), new VirtualFileVisitor() {
|
||||
@Override
|
||||
public boolean visitFile(@NotNull VirtualFile file) {
|
||||
Assert.assertTrue(! VfsUtil.isAncestor(vSubDir, file, true));
|
||||
return ! vSubDir.equals(file);
|
||||
Assert.assertTrue(!VfsUtilCore.isAncestor(vSubDir, file, true));
|
||||
return !vSubDir.equals(file);
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
data.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void testAsyncRefresh() throws Throwable {
|
||||
final Throwable[] ex = {null};
|
||||
JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Arrays.asList(new Object[8]), ProgressManager.getInstance().getProgressIndicator(), false,
|
||||
new Processor<Object>() {
|
||||
@Override
|
||||
public boolean process(Object o) {
|
||||
try {
|
||||
doAsyncRefreshTest();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
ex[0] = t;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
JobLauncher.getInstance().invokeConcurrentlyUnderProgress(
|
||||
Arrays.asList(new Object[8]), ProgressManager.getInstance().getProgressIndicator(), false, new Processor<Object>() {
|
||||
@Override
|
||||
public boolean process(Object o) {
|
||||
try {
|
||||
doAsyncRefreshTest();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
ex[0] = t;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
if (ex[0] != null) throw ex[0];
|
||||
}
|
||||
|
||||
private void doAsyncRefreshTest() throws Exception {
|
||||
int N = 1000;
|
||||
final int N = 1000;
|
||||
final byte[] data = "xxx".getBytes();
|
||||
|
||||
File temp = new WriteAction<File>() {
|
||||
@Override
|
||||
protected void run(Result<File> result) throws Throwable {
|
||||
@@ -198,13 +193,13 @@ public class VfsUtilTest extends PlatformLangTestCase {
|
||||
}.execute().getResultObject();
|
||||
LocalFileSystem fs = LocalFileSystem.getInstance();
|
||||
VirtualFile vTemp = fs.findFileByIoFile(temp);
|
||||
assert vTemp != null;
|
||||
assertNotNull(vTemp);
|
||||
VirtualFile[] children = new VirtualFile[N];
|
||||
|
||||
long[] timestamp = new long[N];
|
||||
for (int i=0;i< N;i++) {
|
||||
for (int i = 0; i < N; i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
FileUtil.writeToFile(file, "xxx".getBytes());
|
||||
FileUtil.writeToFile(file, data);
|
||||
VirtualFile child = fs.refreshAndFindFileByIoFile(file);
|
||||
assertNotNull(child);
|
||||
children[i] = child;
|
||||
@@ -213,29 +208,22 @@ public class VfsUtilTest extends PlatformLangTestCase {
|
||||
|
||||
vTemp.refresh(false, true);
|
||||
|
||||
for (int i=0;i< N;i++) {
|
||||
for (int i = 0; i < N; i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
assertEquals(timestamp[i], file.lastModified());
|
||||
VirtualFile child = fs.findFileByIoFile(file);
|
||||
assertNotNull(child);
|
||||
assertTimestampsEqual(timestamp[i], child.getTimeStamp());
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
VirtualFile child = fs.findFileByIoFile(file);
|
||||
assertNotNull(child);
|
||||
|
||||
long mod = child.getTimeStamp();
|
||||
assertEquals("File:" + child.getPath() + "; mod:" + new Date(mod) + "; io:" + new File(child.getPath()).lastModified(),
|
||||
timestamp[i], mod);
|
||||
}
|
||||
|
||||
Thread.sleep(2000); // todo[r.sh] find a way to get timestamps with millisecond granularity on Linux ?
|
||||
for (int i=0;i< N;i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
FileUtil.writeToFile(file, "xxx".getBytes());
|
||||
assertTrue(file.setLastModified(timestamp[i] - 2000));
|
||||
long modified = file.lastModified();
|
||||
assertTrue("File:" + file.getPath() + "; time:" + modified, timestamp[i] != modified);
|
||||
timestamp[i] = modified;
|
||||
assertTrue(children[i].getTimeStamp() != modified);
|
||||
assertTimestampsNotEqual(children[i].getTimeStamp(), modified);
|
||||
}
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(N);
|
||||
@@ -255,8 +243,7 @@ public class VfsUtilTest extends PlatformLangTestCase {
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
VirtualFile child = children[i];
|
||||
long mod = child.getTimeStamp();
|
||||
assertEquals(timestamp[i], mod);
|
||||
assertTimestampsEqual(timestamp[i], child.getTimeStamp());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.junit.Test;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static com.intellij.openapi.util.io.IoTestUtil.assertTimestampsEqual;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
@@ -292,13 +293,6 @@ public class FileAttributesReadingTest {
|
||||
assertTrue(attributes.isWritable());
|
||||
}
|
||||
|
||||
private static void assertTimestampsEqual(final long expected, final long actual) {
|
||||
final long roundedExpected = (expected / 1000) * 1000;
|
||||
final long roundedActual = (actual / 1000) * 1000;
|
||||
assertEquals("expected: " + expected + ", actual: " + actual,
|
||||
roundedExpected, roundedActual);
|
||||
}
|
||||
|
||||
private static void assertDirectoriesEqual(@NotNull final File dir) {
|
||||
final String[] list1 = dir.list();
|
||||
assertNotNull(list1);
|
||||
|
||||
@@ -183,4 +183,18 @@ public class IoTestUtil {
|
||||
}).start();
|
||||
return process.waitFor();
|
||||
}
|
||||
|
||||
public static void assertTimestampsEqual(final long expected, final long actual) {
|
||||
final long roundedExpected = (expected / 1000) * 1000;
|
||||
final long roundedActual = (actual / 1000) * 1000;
|
||||
assertEquals("expected: " + expected + ", actual: " + actual,
|
||||
roundedExpected, roundedActual);
|
||||
}
|
||||
|
||||
public static void assertTimestampsNotEqual(final long expected, final long actual) {
|
||||
final long roundedExpected = (expected / 1000) * 1000;
|
||||
final long roundedActual = (actual / 1000) * 1000;
|
||||
assertTrue("(un)expected: " + expected + ", actual: " + actual,
|
||||
roundedExpected != roundedActual);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user