Stale directory contents in unit test

Hi, I have a standalone test case that seems to show that the virtual filesystem is mysteriously keeping a stale cache for getChildren() from a previous run, even though refresh() was called. Is there some way to get it to clear the cache completely? The test passes the first time but fails with the following error on every second run:

children:
/tmp/RefreshChildrenTest.testRefreshSeesLatestDirectoryContents/Foo.java
/tmp/RefreshChildrenTest.testRefreshSeesLatestDirectoryContents/Bar.java

junit.framework.AssertionFailedError: expected: but was:<2>]]>
at somepackage.RefreshChildrenTest.checkChildCount(RefreshChildrenTest.java:70)
at somepackage.RefreshChildrenTest.testRefreshSeesLatestDirectoryContents(RefreshChildrenTest.java:48)

Here is the source code (with the package name changed compared to when I ran it):

package somepackage;

import com.intellij.testFramework.IdeaTestCase;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.Writer;

/**

  • Verifies that the contents of a directory changes after a refresh.

*

  • @author skybrian

*/
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 = System.getProperty("java.io.tmpdir");
testDir = new File(baseTempDir, "RefreshChildrenTest." + getName());
if (testDir.exists()) {
// remove directory that's left over from the previous run
removeDir(testDir);
}

super.setUp();
}

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.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);
}

private void removeDir(File testDir) {
File[] children = testDir.listFiles();
for (File child : children) {
child.delete();
}
testDir.delete();
}
}

(This is with IntelliJ 7.0.3.)

2 comments
Comment actions Permalink

An update on this: I happened to notice when using IDEA that a newly checked-in jar file didn't show up, even after I clicked 'refresh'. It was only after I visited the directory in the project view that it showed up. So I think this may be a real bug, not just a unit testing issue.

0

Please sign in to leave a comment.