mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
vfs tests moved to community
This commit is contained in:
BIN
java/java-tests/testData/psi/generics22/collect-2.2.jar
Normal file
BIN
java/java-tests/testData/psi/generics22/collect-2.2.jar
Normal file
Binary file not shown.
13
java/java-tests/testData/vfs/changes/PsiChangesTest.java
Normal file
13
java/java-tests/testData/vfs/changes/PsiChangesTest.java
Normal file
@@ -0,0 +1,13 @@
|
||||
public class PsiChangesTest {
|
||||
private int a;
|
||||
private int b;
|
||||
|
||||
PsiChangesTest() {
|
||||
}
|
||||
|
||||
void foo() {
|
||||
}
|
||||
|
||||
void bar() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
public class PsiChangesTest {
|
||||
protected int a;
|
||||
private int c;
|
||||
|
||||
PsiChangesTest() {
|
||||
int i = 0;
|
||||
}
|
||||
|
||||
void foo() {
|
||||
int i = 0;
|
||||
}
|
||||
|
||||
void bar2() {
|
||||
}
|
||||
}
|
||||
3
java/java-tests/testData/vfs/changes/results/fields.txt
Normal file
3
java/java-tests/testData/vfs/changes/results/fields.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
a : Modified
|
||||
b : Deleted
|
||||
c : Added
|
||||
4
java/java-tests/testData/vfs/changes/results/methods.txt
Normal file
4
java/java-tests/testData/vfs/changes/results/methods.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
PsiChangesTest : Modified
|
||||
foo : Modified
|
||||
bar : Deleted
|
||||
bar2 : Added
|
||||
1
java/java-tests/testData/vfs/encoding/UTF16BOM.txt
Normal file
1
java/java-tests/testData/vfs/encoding/UTF16BOM.txt
Normal file
@@ -0,0 +1 @@
|
||||
<EFBFBD><EFBFBD>@8
|
||||
1
java/java-tests/testData/vfs/encoding/defaultHtml.html
Normal file
1
java/java-tests/testData/vfs/encoding/defaultHtml.html
Normal file
@@ -0,0 +1 @@
|
||||
<html></html>
|
||||
3
java/java-tests/testData/vfs/encoding/xDefault.xml
Normal file
3
java/java-tests/testData/vfs/encoding/xDefault.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0"?>
|
||||
<web-app>
|
||||
</web-app>
|
||||
4
java/java-tests/testData/vfs/encoding/xIbm866.xml
Normal file
4
java/java-tests/testData/vfs/encoding/xIbm866.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="IBM866"?>
|
||||
<web-app>
|
||||
<!--<EFBFBD><EFBFBD><EFBFBD><EFBFBD>-->
|
||||
</web-app>
|
||||
4
java/java-tests/testData/vfs/encoding/xNotepadUtf8.xml
Normal file
4
java/java-tests/testData/vfs/encoding/xNotepadUtf8.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app>
|
||||
<!--папа-->
|
||||
</web-app>
|
||||
4
java/java-tests/testData/vfs/encoding/xTrickyProlog.xml
Normal file
4
java/java-tests/testData/vfs/encoding/xTrickyProlog.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
<?xml encoding='Big5' version='1.0' ?>
|
||||
<web-app>
|
||||
</web-app>
|
||||
4
java/java-tests/testData/vfs/encoding/xWin1251.xml
Normal file
4
java/java-tests/testData/vfs/encoding/xWin1251.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="windows-1251"?>
|
||||
<web-app>
|
||||
<!--<EFBFBD><EFBFBD><EFBFBD><EFBFBD>-->
|
||||
</web-app>
|
||||
1
java/java-tests/testData/vfs/findFileByUrl/1.txt
Normal file
1
java/java-tests/testData/vfs/findFileByUrl/1.txt
Normal file
@@ -0,0 +1 @@
|
||||
test text
|
||||
BIN
java/java-tests/testData/vfs/findFileByUrl/test.zip
Normal file
BIN
java/java-tests/testData/vfs/findFileByUrl/test.zip
Normal file
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Empty file
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.vfs.ex.dummy.DummyFileSystem;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
|
||||
public class DummyFileSystemTest extends IdeaTestCase {
|
||||
DummyFileSystem fs;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
fs = new DummyFileSystem();
|
||||
}
|
||||
|
||||
public void testDeletionEvents() throws Exception {
|
||||
final VirtualFile root = fs.createRoot("root");
|
||||
VirtualFile f = new WriteAction<VirtualFile>() {
|
||||
@Override
|
||||
protected void run(Result<VirtualFile> result) throws Throwable {
|
||||
VirtualFile res = root.createChildData(null, "f");
|
||||
result.setResult(res);
|
||||
}
|
||||
}.execute().getResultObject();
|
||||
|
||||
final VirtualFileEvent[] events = new VirtualFileEvent[2];
|
||||
fs.addVirtualFileListener(new VirtualFileAdapter() {
|
||||
@Override
|
||||
public void fileDeleted(VirtualFileEvent e) {
|
||||
events[0] = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeFileDeletion(VirtualFileEvent e) {
|
||||
events[1] = e;
|
||||
}
|
||||
});
|
||||
|
||||
f.delete(null);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertNotNull(events[i]);
|
||||
assertEquals(f, events[i].getFile());
|
||||
assertEquals("f", events[i].getFileName());
|
||||
assertEquals(root, events[i].getParent());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class GetPathPerformanceTest extends IdeaTestCase {
|
||||
public void testGetPath() throws IOException {
|
||||
File dir = createTempDirectory();
|
||||
try {
|
||||
File subdir1 = new File(dir, "1");
|
||||
File subdir2 = new File(dir, "2");
|
||||
subdir1.mkdir();
|
||||
subdir2.mkdir();
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
new File(subdir1, "" + i).createNewFile();
|
||||
new File(subdir2, "" + i).createNewFile();
|
||||
}
|
||||
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(dir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(file);
|
||||
|
||||
final VirtualFile[] children = file.getChildren();
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for( int i = 0; i < 1000000; ++i ) {
|
||||
for( VirtualFile child: children) {
|
||||
child.getPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
IdeaTestUtil.assertTiming("Performance failed", 3000, runnable);
|
||||
}
|
||||
finally {
|
||||
FileUtil.delete(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class JarFileSystemTest extends IdeaTestCase{
|
||||
public void testFindFile() throws Exception{
|
||||
Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk();
|
||||
VirtualFile jdkHome = jdk.getHomeDirectory();
|
||||
String rtJarPath = new File(jdkHome.getPath() + "/src.zip").getCanonicalPath().replace(File.separator, "/");
|
||||
|
||||
VirtualFile file1 = findByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR);
|
||||
assertTrue(file1.isDirectory());
|
||||
|
||||
VirtualFile file2 = findByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR + "java");
|
||||
assertTrue(file2.isDirectory());
|
||||
|
||||
VirtualFile file3 = file1.findChild("java");
|
||||
assertEquals(file2, file3);
|
||||
|
||||
VirtualFile file4 = findByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR + "java/lang/Object.java");
|
||||
assertTrue(!file4.isDirectory());
|
||||
|
||||
byte[] bytes = file4.contentsToByteArray();
|
||||
assertNotNull(bytes);
|
||||
assertTrue(bytes.length > 10);
|
||||
}
|
||||
|
||||
public void testMetaInf() throws Exception{
|
||||
Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk();
|
||||
VirtualFile jdkHome = jdk.getHomeDirectory();
|
||||
String rtJarPath = jdkHome.getPath() + "/jre/lib/rt.jar";
|
||||
|
||||
VirtualFile jarRoot = JarFileSystem.getInstance().findFileByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR);
|
||||
assertNotNull(jarRoot);
|
||||
|
||||
VirtualFile metaInf = jarRoot.findChild("META-INF");
|
||||
assertNotNull(metaInf);
|
||||
|
||||
VirtualFile[] children = metaInf.getChildren();
|
||||
assertEquals(1, children.length);
|
||||
}
|
||||
|
||||
private static VirtualFile findByPath(String path) {
|
||||
VirtualFile file = JarFileSystem.getInstance().findFileByPath(path);
|
||||
assertNotNull(file);
|
||||
final String filePath = file.getPath();
|
||||
final String message = "paths are not equal, path1 = " + path + " found: " + filePath;
|
||||
if (SystemInfo.isFileSystemCaseSensitive) {
|
||||
assertEquals(message, path, file.getPath());
|
||||
}
|
||||
else {
|
||||
assertTrue(message, path.equalsIgnoreCase(filePath));
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.vfs.impl.win32.Win32LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.newvfs.ManagingFS;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class LocalFileSystemTest extends IdeaTestCase{
|
||||
private static final String KEY = "filesystem.useNative";
|
||||
|
||||
public void testChildrenAccessedButNotCached() throws Exception{
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
File dir = createTempDirectory();
|
||||
final ManagingFS managingFS = ManagingFS.getInstance();
|
||||
|
||||
VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(dir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(vFile);
|
||||
assertFalse(managingFS.areChildrenLoaded(vFile));
|
||||
assertFalse(managingFS.wereChildrenAccessed(vFile));
|
||||
|
||||
final File child = new File(dir, "child");
|
||||
final boolean created = child.createNewFile();
|
||||
assertTrue(created);
|
||||
|
||||
final File subdir = new File(dir, "subdir");
|
||||
final boolean subdirCreated = subdir.mkdir();
|
||||
assertTrue(subdirCreated);
|
||||
|
||||
final File subChild = new File(subdir, "subdir");
|
||||
final boolean subChildCreated = subChild.createNewFile();
|
||||
assertTrue(subChildCreated);
|
||||
|
||||
final VirtualFile childVFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(child.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(childVFile);
|
||||
assertFalse(managingFS.areChildrenLoaded(vFile));
|
||||
assertTrue(managingFS.wereChildrenAccessed(vFile));
|
||||
|
||||
final VirtualFile subdirVFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(subdir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(subdirVFile);
|
||||
assertFalse(managingFS.areChildrenLoaded(subdirVFile));
|
||||
assertFalse(managingFS.wereChildrenAccessed(subdirVFile));
|
||||
|
||||
assertFalse(managingFS.areChildrenLoaded(vFile));
|
||||
assertTrue(managingFS.wereChildrenAccessed(vFile));
|
||||
|
||||
|
||||
vFile.getChildren();
|
||||
assertTrue(managingFS.areChildrenLoaded(vFile));
|
||||
assertTrue(managingFS.wereChildrenAccessed(vFile));
|
||||
assertFalse(managingFS.areChildrenLoaded(subdirVFile));
|
||||
assertFalse(managingFS.wereChildrenAccessed(subdirVFile));
|
||||
|
||||
final VirtualFile subChildVFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(subChild.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(subChildVFile);
|
||||
assertTrue(managingFS.areChildrenLoaded(vFile));
|
||||
assertTrue(managingFS.wereChildrenAccessed(vFile));
|
||||
assertFalse(managingFS.areChildrenLoaded(subdirVFile));
|
||||
assertTrue(managingFS.wereChildrenAccessed(subdirVFile));
|
||||
}
|
||||
catch(IOException e){
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void testRefreshAndFindFile() throws Exception{
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
File dir = createTempDirectory();
|
||||
|
||||
VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(dir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(vFile);
|
||||
vFile.getChildren();
|
||||
|
||||
for(int i = 0; i < 100; i++){
|
||||
File subdir = new File(dir, "a" + i);
|
||||
assertTrue(subdir.mkdir());
|
||||
}
|
||||
|
||||
File subdir = new File(dir, "aaa");
|
||||
assertTrue(subdir.mkdir());
|
||||
|
||||
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(subdir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(file);
|
||||
}
|
||||
catch(IOException e){
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void testCopyFile() throws Exception{
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
File fromDir = createTempDirectory();
|
||||
File toDir = createTempDirectory();
|
||||
|
||||
VirtualFile fromVDir = LocalFileSystem.getInstance().findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
|
||||
VirtualFile toVDir = LocalFileSystem.getInstance().findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(fromVDir);
|
||||
assertNotNull(toVDir);
|
||||
final VirtualFile fileToCopy = fromVDir.createChildData(null, "temp_file");
|
||||
final byte[] byteContent = {0, 1, 2, 3};
|
||||
fileToCopy.setBinaryContent(byteContent);
|
||||
final String newName = "new_temp_file";
|
||||
final VirtualFile copy = fileToCopy.copy(null, toVDir, newName);
|
||||
assertEquals(newName, copy.getName());
|
||||
assertTrue(Arrays.equals(byteContent, copy.contentsToByteArray()));
|
||||
}
|
||||
catch(Exception e){
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void testCopyDir() throws Exception{
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
File fromDir = createTempDirectory();
|
||||
File toDir = createTempDirectory();
|
||||
|
||||
VirtualFile fromVDir = LocalFileSystem.getInstance().findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
|
||||
VirtualFile toVDir = LocalFileSystem.getInstance().findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
|
||||
assertNotNull(fromVDir);
|
||||
assertNotNull(toVDir);
|
||||
final VirtualFile dirToCopy = fromVDir.createChildDirectory(null, "dir");
|
||||
final VirtualFile file = dirToCopy.createChildData(null, "temp_file");
|
||||
file.setBinaryContent(new byte[]{0, 1, 2, 3});
|
||||
final String newName = "dir";
|
||||
final VirtualFile dirCopy = dirToCopy.copy(null, toVDir, newName);
|
||||
assertEquals(newName, dirCopy.getName());
|
||||
IdeaTestUtil.assertDirectoriesEqual(toVDir, fromVDir, null);
|
||||
}
|
||||
catch(Exception e){
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void testGermanLetters() throws Exception{
|
||||
final File dirFile = createTempDirectory();
|
||||
|
||||
final String name = "te\u00dft123123123.txt";
|
||||
final File childFile = new File(dirFile, name);
|
||||
childFile.createNewFile();
|
||||
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
final VirtualFile dir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(dirFile);
|
||||
assertNotNull(dir);
|
||||
|
||||
final VirtualFile child = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(childFile);
|
||||
assertNotNull(child);
|
||||
|
||||
}
|
||||
catch(Exception e){
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
assertTrue(childFile.delete());
|
||||
}
|
||||
|
||||
public void _testSymLinks() throws Exception {
|
||||
ApplicationManager.getApplication().runWriteAction(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
boolean b = Registry.get(KEY).asBoolean();
|
||||
try{
|
||||
Registry.get(KEY).setValue(false);
|
||||
final VirtualFile dir = LocalFileSystem.getInstance().refreshAndFindFileByPath("C:/Documents and Settings");
|
||||
Win32LocalFileSystem system = Win32LocalFileSystem.getWin32Instance();
|
||||
system.exists(dir);
|
||||
String[] strings = system.list(dir);
|
||||
System.out.println(Arrays.asList(strings));
|
||||
}
|
||||
catch(Exception e){
|
||||
fail(e.getMessage());
|
||||
}
|
||||
finally {
|
||||
Registry.get(KEY).setValue(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class RefreshChildrenTest extends IdeaTestCase {
|
||||
private File testDir;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
|
||||
// the superclass sets tmpdir on every run and cleans up, but we want to do it our way
|
||||
|
||||
String baseTempDir = FileUtil.getTempDirectory();
|
||||
testDir = new File(baseTempDir, "RefreshChildrenTest." + getName());
|
||||
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
FileUtil.delete(testDir);
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testRefreshSeesLatestDirectoryContents() throws Exception {
|
||||
assertFalse(testDir.exists());
|
||||
testDir.mkdir();
|
||||
assertTrue(testDir.isDirectory());
|
||||
writeFile(testDir, "Foo.java", "");
|
||||
|
||||
LocalFileSystem local = LocalFileSystem.getInstance();
|
||||
VirtualFile virtualDir = local.findFileByIoFile(testDir);
|
||||
virtualDir.getChildren();
|
||||
virtualDir.refresh(false, true);
|
||||
|
||||
checkChildCount(virtualDir, 1);
|
||||
|
||||
writeFile(testDir, "Bar.java", "");
|
||||
virtualDir.refresh(false, true);
|
||||
|
||||
checkChildCount(virtualDir, 2);
|
||||
}
|
||||
|
||||
private void writeFile(File dir, String filename, String contents) throws IOException {
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(new File(dir, filename)), "UTF-8");
|
||||
writer.write(contents);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private void checkChildCount(VirtualFile virtualDir, int expectedCount) {
|
||||
VirtualFile[] children = virtualDir.getChildren();
|
||||
if (children.length != expectedCount) {
|
||||
System.err.println("children:");
|
||||
for (VirtualFile child : children) {
|
||||
System.err.println(child.getPath());
|
||||
}
|
||||
}
|
||||
assertEquals(expectedCount, children.length);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.concurrency.JobUtil;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.application.ex.PathManagerEx;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
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;
|
||||
|
||||
public class VfsUtilTest extends IdeaTestCase {
|
||||
@Override
|
||||
protected void runBareRunnable(Runnable runnable) throws Throwable {
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
VfsUtilTest.super.setUp();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
VfsUtilTest.super.tearDown();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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");
|
||||
file0 = VfsUtil.findFileByURL(file1.toURL());
|
||||
assertNotNull(file0);
|
||||
assertTrue(file0.isDirectory());
|
||||
final VirtualFile[] children = file0.getChildren();
|
||||
List<VirtualFile> list = new ArrayList<VirtualFile>();
|
||||
final VirtualFileFilter fileFilter = new VirtualFileFilter() {
|
||||
@Override
|
||||
public boolean accept(VirtualFile file) {
|
||||
return IdeaTestUtil.CVS_FILE_FILTER.accept(file) && !file.getName().endsWith(".new");
|
||||
}
|
||||
};
|
||||
for (VirtualFile child : children) {
|
||||
if (fileFilter.accept(child)) {
|
||||
list.add(child);
|
||||
}
|
||||
}
|
||||
assertEquals(2, list.size()); // "CVS" dir ignored
|
||||
|
||||
File file2 = new File(file1, "test.zip");
|
||||
URL url2 = file2.toURL();
|
||||
url2 = new URL("jar", "", url2.toExternalForm() + "!/");
|
||||
url2 = new URL(url2, "com/intellij/installer");
|
||||
url2 = new URL(url2.toExternalForm());
|
||||
file0 = VfsUtil.findFileByURL(url2);
|
||||
assertNotNull(file0);
|
||||
assertTrue(file0.isDirectory());
|
||||
|
||||
File file3 = new File(file1, "1.txt");
|
||||
file0 = VfsUtil.findFileByURL(file3.toURL());
|
||||
String content = VfsUtil.loadText(file0);
|
||||
assertNotNull(file0);
|
||||
assertFalse(file0.isDirectory());
|
||||
assertEquals(content, "test text");
|
||||
}
|
||||
|
||||
public void testRelativePath() throws Exception {
|
||||
final File root = new File(PathManagerEx.getTestDataPath());
|
||||
final File testRoot = new File(new File(root, "vfs"), "relativePath");
|
||||
VirtualFile vTestRoot = LocalFileSystem.getInstance().findFileByIoFile(testRoot);
|
||||
assertNotNull(vTestRoot);
|
||||
assertTrue(vTestRoot.isDirectory());
|
||||
|
||||
final File subDir = new File(testRoot, "subDir");
|
||||
final VirtualFile vSubDir = LocalFileSystem.getInstance().findFileByIoFile(subDir);
|
||||
assertNotNull(vSubDir);
|
||||
|
||||
final File subSubDir = new File(subDir, "subSubDir");
|
||||
final VirtualFile vSubSubDir = LocalFileSystem.getInstance().findFileByIoFile(subSubDir);
|
||||
assertNotNull(vSubSubDir);
|
||||
|
||||
assertEquals("subDir", VfsUtil.getRelativePath(vSubDir, vTestRoot, '/'));
|
||||
assertEquals("subDir/subSubDir", VfsUtil.getRelativePath(vSubSubDir, vTestRoot, '/'));
|
||||
assertEquals("", VfsUtil.getRelativePath(vTestRoot, vTestRoot, '/'));
|
||||
}
|
||||
|
||||
public void testAsyncRefresh() throws Throwable {
|
||||
final Throwable[] ex = {null};
|
||||
JobUtil.invokeConcurrentlyUnderProgress(Arrays.asList(new Object[8]), new Processor<Object>() {
|
||||
@Override
|
||||
public boolean process(Object o) {
|
||||
try {
|
||||
doAsyncRefreshTest();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
ex[0] = t;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}, false, ProgressManager.getInstance().getProgressIndicator());
|
||||
if (ex[0] != null) throw ex[0];
|
||||
}
|
||||
|
||||
private void doAsyncRefreshTest() throws Exception {
|
||||
int N = 1000;
|
||||
File temp = new WriteAction<File>() {
|
||||
@Override
|
||||
protected void run(Result<File> result) throws Throwable {
|
||||
File res = createTempDirectory();
|
||||
result.setResult(res);
|
||||
}
|
||||
}.execute().getResultObject();
|
||||
LocalFileSystem fs = LocalFileSystem.getInstance();
|
||||
VirtualFile vtemp = fs.findFileByIoFile(temp);
|
||||
assert vtemp != null;
|
||||
VirtualFile[] children = new VirtualFile[N];
|
||||
|
||||
long[] timestamp = new long[N];
|
||||
for (int i=0;i< N;i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
FileUtil.writeToFile(file, "xxx".getBytes());
|
||||
VirtualFile child = fs.findFileByIoFile(file);
|
||||
assertNotNull(child);
|
||||
children[i] = child;
|
||||
timestamp[i] = file.lastModified();
|
||||
}
|
||||
|
||||
vtemp.refresh(false,true);
|
||||
|
||||
for (int i=0;i< N;i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
assertEquals(timestamp[i], file.lastModified());
|
||||
}
|
||||
|
||||
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(300);
|
||||
for (int i=0;i< N;i++) {
|
||||
File file = new File(temp, i + ".txt");
|
||||
FileUtil.writeToFile(file, "xxx".getBytes());
|
||||
long modified = file.lastModified();
|
||||
assertTrue(timestamp[i] != modified);
|
||||
timestamp[i] = modified;
|
||||
assertTrue(children[i].getTimeStamp() != modified);
|
||||
}
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(N);
|
||||
for (final VirtualFile child : children) {
|
||||
child.refresh(true, true, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
while (true) {
|
||||
latch.await(100, TimeUnit.MILLISECONDS);
|
||||
if (SwingUtilities.isEventDispatchThread()) {
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
}
|
||||
else {
|
||||
UIUtil.pump();
|
||||
}
|
||||
if (latch.getCount() == 0) break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
VirtualFile child = children[i];
|
||||
long mod = child.getTimeStamp();
|
||||
assertEquals(timestamp[i], mod);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFindChildWithTrailingSpace() throws IOException {
|
||||
File tempDir = new WriteAction<File>() {
|
||||
@Override
|
||||
protected void run(Result<File> result) throws Throwable {
|
||||
File res = createTempDirectory();
|
||||
result.setResult(res);
|
||||
}
|
||||
}.execute().getResultObject();
|
||||
VirtualFile vdir = LocalFileSystem.getInstance().findFileByIoFile(tempDir);
|
||||
assertNotNull(vdir);
|
||||
assertTrue(vdir.isDirectory());
|
||||
|
||||
VirtualFile child = vdir.findChild(" ");
|
||||
assertNull(child);
|
||||
|
||||
assertEmpty(vdir.getChildren());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
package com.intellij.openapi.vfs;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ex.PathManagerEx;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.pointers.VirtualFilePointer;
|
||||
import com.intellij.openapi.vfs.pointers.VirtualFilePointerListener;
|
||||
import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author dsl
|
||||
*/
|
||||
public class VirtualFilePointerTest extends IdeaTestCase {
|
||||
private VirtualFilePointerManager myVirtualFilePointerManager;
|
||||
|
||||
static class LoggingListener implements VirtualFilePointerListener {
|
||||
private final ArrayList<String> myLog = new ArrayList<String>();
|
||||
|
||||
@Override
|
||||
public void beforeValidityChanged(VirtualFilePointer[] pointers) {
|
||||
verifyPointersInCorrectState(pointers);
|
||||
myLog.add(buildMessage("before", pointers));
|
||||
}
|
||||
|
||||
private static String buildMessage(@NonNls final String startMsg, VirtualFilePointer[] pointers) {
|
||||
StringBuilder buffer = new StringBuilder(startMsg);
|
||||
buffer.append(":");
|
||||
for (int i = 0; i < pointers.length; i++) {
|
||||
VirtualFilePointer pointer = pointers[i];
|
||||
final String s = Boolean.toString(pointer.isValid());
|
||||
if (i > 0) buffer.append(":");
|
||||
buffer.append(s);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validityChanged(VirtualFilePointer[] pointers) {
|
||||
verifyPointersInCorrectState(pointers);
|
||||
myLog.add(buildMessage("after", pointers));
|
||||
}
|
||||
|
||||
public ArrayList<String> getLog() {
|
||||
return myLog;
|
||||
}
|
||||
}
|
||||
|
||||
public void testDelete() throws Exception {
|
||||
File tempDirectory = createTempDirectory();
|
||||
final File fileToDelete = new File(tempDirectory, "toDelete.txt");
|
||||
fileToDelete.createNewFile();
|
||||
final LoggingListener fileToDeleteListener = new LoggingListener();
|
||||
final VirtualFilePointer fileToDeletePointer = createPointerByFile(fileToDelete, fileToDeleteListener);
|
||||
assertTrue(fileToDeletePointer.isValid());
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final VirtualFile virtualFile = getVirtualFile(fileToDelete);
|
||||
try {
|
||||
virtualFile.delete(null);
|
||||
} catch (IOException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
});
|
||||
assertFalse(fileToDeletePointer.isValid());
|
||||
assertEquals("[before:true, after:false]", fileToDeleteListener.getLog().toString());
|
||||
myFilesToDelete.add(tempDirectory);
|
||||
}
|
||||
|
||||
public void testCreate() throws Exception {
|
||||
final File tempDirectory = createTempDirectory();
|
||||
final File fileToCreate = new File(tempDirectory, "toCreate.txt");
|
||||
final LoggingListener fileToCreateListener = new LoggingListener();
|
||||
final VirtualFilePointer fileToCreatePointer = createPointerByFile(fileToCreate, fileToCreateListener);
|
||||
assertFalse(fileToCreatePointer.isValid());
|
||||
fileToCreate.createNewFile();
|
||||
final Runnable postRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
assertTrue(fileToCreatePointer.isValid());
|
||||
assertEquals("[before:false, after:true]", fileToCreateListener.getLog().toString());
|
||||
try {
|
||||
String expectedUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, fileToCreate.getCanonicalPath().replace(File.separatorChar, '/'));
|
||||
assertEquals(expectedUrl.toUpperCase(), fileToCreatePointer.getUrl().toUpperCase());
|
||||
} catch (IOException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
};
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run () {
|
||||
VirtualFileManager.getInstance().refresh(false);
|
||||
final VirtualFile virtualFile = getVirtualFile(tempDirectory);
|
||||
virtualFile.refresh(false, true);
|
||||
}
|
||||
});
|
||||
postRunnable.run();
|
||||
myFilesToDelete.add(fileToCreate);
|
||||
myFilesToDelete.add(tempDirectory);
|
||||
}
|
||||
|
||||
public void testMove() throws Exception {
|
||||
File tempDirectory = createTempDirectory();
|
||||
final File moveTarget = new File(tempDirectory, "moveTarget");
|
||||
moveTarget.mkdir();
|
||||
final File fileToMove = new File(tempDirectory, "toMove.txt");
|
||||
fileToMove.createNewFile();
|
||||
|
||||
final LoggingListener fileToMoveListener = new LoggingListener();
|
||||
final VirtualFilePointer fileToMovePointer = createPointerByFile(fileToMove, fileToMoveListener);
|
||||
assertTrue(fileToMovePointer.isValid());
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final VirtualFile virtualFile = getVirtualFile(fileToMove);
|
||||
assertTrue(virtualFile.isValid());
|
||||
final VirtualFile target = getVirtualFile(moveTarget);
|
||||
assertTrue(target.isValid());
|
||||
try {
|
||||
virtualFile.move(null, target);
|
||||
} catch (IOException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
});
|
||||
assertTrue(fileToMovePointer.isValid());
|
||||
assertEquals("[]", fileToMoveListener.getLog().toString());
|
||||
final File fileAfterMove = new File(moveTarget, fileToMove.getName());
|
||||
myFilesToDelete.add(fileAfterMove);
|
||||
myFilesToDelete.add(moveTarget);
|
||||
myFilesToDelete.add(tempDirectory);
|
||||
}
|
||||
|
||||
public void testCreate1() throws Exception {
|
||||
final File tempDirectory = createTempDirectory();
|
||||
final File fileToCreate = new File(tempDirectory, "toCreate1.txt");
|
||||
final LoggingListener fileToCreateListener = new LoggingListener();
|
||||
final VirtualFilePointer fileToCreatePointer = createPointerByFile(fileToCreate, fileToCreateListener);
|
||||
assertFalse(fileToCreatePointer.isValid());
|
||||
fileToCreate.createNewFile();
|
||||
final Runnable postRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
assertTrue(fileToCreatePointer.isValid());
|
||||
assertEquals("[before:false, after:true]", fileToCreateListener.getLog().toString());
|
||||
try {
|
||||
String expectedUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, fileToCreate.getCanonicalPath().replace(File.separatorChar, '/'));
|
||||
assertEquals(expectedUrl.toUpperCase(), fileToCreatePointer.getUrl().toUpperCase());
|
||||
} catch (IOException e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
};
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run () {
|
||||
VirtualFileManager.getInstance().refresh(false);
|
||||
final VirtualFile virtualFile = getVirtualFile(tempDirectory);
|
||||
virtualFile.refresh(false, true);
|
||||
}
|
||||
});
|
||||
postRunnable.run();
|
||||
myFilesToDelete.add(fileToCreate);
|
||||
myFilesToDelete.add(tempDirectory);
|
||||
}
|
||||
|
||||
public void testMultipleNotifications() throws Exception {
|
||||
final File tempDir = createTempDirectory();
|
||||
final File file_f1 = new File(tempDir, "f1");
|
||||
final File file_f2 = new File(tempDir, "f2");
|
||||
final LoggingListener listener = new LoggingListener();
|
||||
final VirtualFilePointer pointer_f1 = createPointerByFile(file_f1, listener);
|
||||
final VirtualFilePointer pointer_f2 = createPointerByFile(file_f2, listener);
|
||||
assertFalse(pointer_f1.isValid());
|
||||
assertFalse(pointer_f2.isValid());
|
||||
file_f1.createNewFile();
|
||||
file_f2.createNewFile();
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LocalFileSystem.getInstance().refresh(false);
|
||||
}
|
||||
});
|
||||
assertEquals("[before:false:false, after:true:true]", listener.getLog().toString());
|
||||
myFilesToDelete.add(file_f1);
|
||||
myFilesToDelete.add(file_f2);
|
||||
}
|
||||
|
||||
public void testJars() throws Exception {
|
||||
final File tempDir = createTempDirectory();
|
||||
final File jarParent = new File(tempDir, "jarParent");
|
||||
jarParent.mkdir();
|
||||
final File jar = new File(jarParent, "x.jar");
|
||||
final File originalJar = new File(PathManagerEx.getTestDataPath() + "/psi/generics22/collect-2.2.jar".replace('/', File.separatorChar));
|
||||
FileUtil.copy(originalJar, jar);
|
||||
|
||||
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar); // Make sure we receive events when jar changes
|
||||
|
||||
final VirtualFilePointer[] pointersToWatch = new VirtualFilePointer[2];
|
||||
final VirtualFilePointerListener listener = new VirtualFilePointerListener() {
|
||||
@Override
|
||||
public void beforeValidityChanged(VirtualFilePointer[] pointers) {
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validityChanged(VirtualFilePointer[] pointers) {
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
}
|
||||
};
|
||||
final VirtualFilePointer jarParentPointer = createPointerByFile(jarParent, listener);
|
||||
final String pathInJar = jar.getPath().replace(File.separatorChar, '/') + JarFileSystem.JAR_SEPARATOR;
|
||||
final String jarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL,
|
||||
pathInJar);
|
||||
final VirtualFilePointer jarPointer = myVirtualFilePointerManager.create(jarUrl, myProject, listener);
|
||||
pointersToWatch[0] = jarParentPointer;
|
||||
pointersToWatch[1] = jarPointer;
|
||||
assertTrue(jarParentPointer.isValid());
|
||||
assertTrue(jarPointer.isValid());
|
||||
|
||||
jar.delete();
|
||||
jarParent.delete();
|
||||
refreshVFS();
|
||||
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
assertFalse(jarParentPointer.isValid());
|
||||
assertFalse(jarPointer.isValid());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
|
||||
jarParent.mkdir();
|
||||
FileUtil.copy(originalJar, jar);
|
||||
|
||||
refreshVFS();
|
||||
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
assertTrue(jarParentPointer.isValid());
|
||||
assertTrue(jarPointer.isValid());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
|
||||
jar.delete();
|
||||
jarParent.delete();
|
||||
refreshVFS();
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
assertFalse(jarParentPointer.isValid());
|
||||
assertFalse(jarPointer.isValid());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
}
|
||||
|
||||
public void testJars2() throws Exception {
|
||||
final File tempDir = createTempDirectory();
|
||||
final File jarParent = new File(tempDir, "jarParent");
|
||||
jarParent.mkdir();
|
||||
final File jar = new File(jarParent, "x.jar");
|
||||
final File originalJar = new File(PathManagerEx.getTestDataPath() + "/psi/generics22/collect-2.2.jar".replace('/', File.separatorChar));
|
||||
FileUtil.copy(originalJar, jar);
|
||||
|
||||
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar); // Make sure we receive events when jar changes
|
||||
|
||||
final VirtualFilePointer[] pointersToWatch = new VirtualFilePointer[1];
|
||||
final VirtualFilePointerListener listener = new VirtualFilePointerListener() {
|
||||
@Override
|
||||
public void beforeValidityChanged(VirtualFilePointer[] pointers) {
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validityChanged(VirtualFilePointer[] pointers) {
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
}
|
||||
};
|
||||
final String pathInJar = jar.getPath().replace(File.separatorChar, '/') + JarFileSystem.JAR_SEPARATOR;
|
||||
final String jarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL,
|
||||
pathInJar);
|
||||
final VirtualFilePointer jarPointer = myVirtualFilePointerManager.create(jarUrl, myProject, listener);
|
||||
pointersToWatch[0] = jarPointer;
|
||||
assertTrue(jarPointer.isValid());
|
||||
|
||||
jar.delete();
|
||||
refreshVFS();
|
||||
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
assertFalse(jarPointer.isValid());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
|
||||
jarParent.mkdir();
|
||||
FileUtil.copy(originalJar, jar);
|
||||
|
||||
refreshVFS();
|
||||
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
assertTrue(jarPointer.isValid());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
|
||||
jar.delete();
|
||||
refreshVFS();
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
|
||||
verifyPointersInCorrectState(pointersToWatch);
|
||||
assertFalse(jarPointer.isValid());
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
}
|
||||
|
||||
private static void refreshVFS() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
VirtualFileManager.getInstance().refresh(false);
|
||||
}
|
||||
});
|
||||
UIUtil.dispatchAllInvocationEvents();
|
||||
}
|
||||
|
||||
private static void verifyPointersInCorrectState(VirtualFilePointer[] pointers) {
|
||||
for (VirtualFilePointer pointer : pointers) {
|
||||
final VirtualFile file = pointer.getFile();
|
||||
assertTrue(file == null || file.isValid());
|
||||
}
|
||||
}
|
||||
|
||||
private VirtualFilePointer createPointerByFile(final File file, final VirtualFilePointerListener fileListener) throws IOException {
|
||||
final VirtualFile[] vFile = new VirtualFile[1];
|
||||
final String url = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, file.getCanonicalPath().replace(File.separatorChar, '/'));
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
vFile[0] = VirtualFileManager.getInstance().refreshAndFindFileByUrl(url);
|
||||
}
|
||||
});
|
||||
final VirtualFilePointer fileToDeletePointer;
|
||||
if (vFile[0] == null) {
|
||||
fileToDeletePointer = myVirtualFilePointerManager.create(url, myProject, fileListener);
|
||||
}
|
||||
else {
|
||||
fileToDeletePointer = myVirtualFilePointerManager.create(vFile[0], myProject, fileListener);
|
||||
}
|
||||
return fileToDeletePointer;
|
||||
}
|
||||
|
||||
public void testFilePointerUpdate() throws Exception {
|
||||
final File tempDir = createTempDirectory();
|
||||
final File file_f1 = new File(tempDir, "f1");
|
||||
|
||||
final VirtualFilePointer pointer_f1 = createPointerByFile(file_f1, null);
|
||||
|
||||
assertFalse(pointer_f1.isValid());
|
||||
|
||||
file_f1.createNewFile();
|
||||
|
||||
doVfsRefresh();
|
||||
|
||||
assertTrue(pointer_f1.isValid());
|
||||
|
||||
file_f1.delete();
|
||||
|
||||
doVfsRefresh();
|
||||
assertFalse(pointer_f1.isValid());
|
||||
}
|
||||
|
||||
public void testContainerDeletePerformance() throws Exception {
|
||||
PlatformTestUtil.assertTiming("", 3000, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Disposable parent = Disposer.newDisposable();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
myVirtualFilePointerManager.createContainer(parent);
|
||||
}
|
||||
Disposer.dispose(parent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void doVfsRefresh() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LocalFileSystem.getInstance().refresh(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myVirtualFilePointerManager = VirtualFilePointerManager.getInstance();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.intellij.openapi.vfs.changes;
|
||||
|
||||
import com.intellij.openapi.application.ex.PathManagerEx;
|
||||
import com.intellij.psi.util.PsiFilter;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vcs.FileStatus;
|
||||
import com.intellij.openapi.vcs.changes.PsiChangeTracker;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.testFramework.IdeaTestCase;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Konstantin Bulenkov
|
||||
*/
|
||||
public class PsiChangeTrackerTest extends IdeaTestCase {
|
||||
private PsiFile myOriginalFile;
|
||||
private PsiFile myChangedFile;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
final VirtualFile testRoot = getTestRoot();
|
||||
final VirtualFile child = testRoot.findChild("PsiChangesTest.java");
|
||||
assert child != null : "Can't find PsiChangesTest.java";
|
||||
final PsiFile original = this.getPsiManager().findFile(child);
|
||||
assert original != null : "Can't create PsiFile from VirtualFile " + child.getName();
|
||||
myOriginalFile = original;
|
||||
|
||||
final VirtualFile changedFile = testRoot.findChild("PsiChangesTest_changed.java");
|
||||
assert changedFile != null : "Can't find PsiChangesTest_changed.java";
|
||||
|
||||
myChangedFile = PsiFileFactory.getInstance(myProject)
|
||||
.createFileFromText(new String(changedFile.contentsToByteArray()), myOriginalFile);
|
||||
assert myChangedFile != null : "Can't create PsiFile from " + changedFile.getPath();
|
||||
}
|
||||
|
||||
public void testMethods() throws Exception {
|
||||
doTest(new PsiFilter<PsiMethod>(PsiMethod.class));
|
||||
}
|
||||
|
||||
public void testFields() throws Exception {
|
||||
doTest(new PsiFilter<PsiField>(PsiField.class));
|
||||
}
|
||||
|
||||
private <T extends PsiElement> void doTest(PsiFilter<T> filter) throws IOException {
|
||||
final Map<T,FileStatus> map = PsiChangeTracker.getElementsChanged(myChangedFile, myOriginalFile, filter);
|
||||
final Map<String, String> changes = convert(map);
|
||||
final Map<String, String> expected = getExpectedResults();
|
||||
assert expected.equals(changes) : "Changes are not equal \n Expected:\n" + expected + "\nFound:\n" + changes;
|
||||
}
|
||||
|
||||
private static <T extends PsiElement> Map<String, String> convert(Map<T, FileStatus> map) {
|
||||
final Map<String, String> result = new HashMap<String, String>();
|
||||
for (T key : map.keySet()) {
|
||||
final String newKey;
|
||||
if (key instanceof PsiMember) {
|
||||
newKey = ((PsiMember)key).getName();
|
||||
} else {
|
||||
newKey = key.getText();
|
||||
}
|
||||
result.put(newKey, map.get(key).getText());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, String> getExpectedResults() throws IOException {
|
||||
final String resultFileName = getTestName(true) + ".txt";
|
||||
final VirtualFile resultsDir = getTestRoot().findChild("results");
|
||||
assert resultsDir != null : "Can't find results dir";
|
||||
final VirtualFile result = resultsDir.findChild(resultFileName);
|
||||
assert result != null : "File not found " + resultsDir.getPath() + "/" + resultFileName;
|
||||
String res = new String(result.contentsToByteArray());
|
||||
final Map<String, String> map = new HashMap<String, String>();
|
||||
for (String s : res.replace("\r\n", "\n").split("\n")) {
|
||||
if (!StringUtil.isEmptyOrSpaces(s)) {
|
||||
final String[] strings = s.split(":");
|
||||
map.put(strings[0].trim(), strings[1].trim());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
protected static VirtualFile getTestRoot() {
|
||||
final File root = new File(PathManagerEx.getTestDataPath());
|
||||
final File testRoot = new File(new File(root, "vfs"), "changes");
|
||||
final VirtualFile ioFile = LocalFileSystem.getInstance().findFileByIoFile(testRoot);
|
||||
assert ioFile != null : "Can't find directory vfs/changes";
|
||||
return ioFile;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user