Skip to content

Commit

Permalink
Fix resource not disposed error (#412)
Browse files Browse the repository at this point in the history
Followup for #409 (comment) as it suppressed the error.
  • Loading branch information
wisechengyi authored Aug 8, 2019
1 parent 6333b39 commit 8865601
Showing 1 changed file with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,30 @@ public class PantsProjectCacheTest extends PantsCodeInsightFixtureTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
cleanUpsourceFolders();
PantsProjectCacheImpl.getInstance(getProject()).projectOpened();
}

private void cleanUpsourceFolders() {
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
final ModifiableRootModel modifiableRootModel = ModuleRootManager.getInstance(getModule()).getModifiableModel();
for (ContentEntry contentEntry : modifiableRootModel.getContentEntries()) {
final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
for (SourceFolder sourceFolder : sourceFolders) {
contentEntry.removeSourceFolder(sourceFolder);
}
() -> {
final ModifiableRootModel modifiableRootModel = ModuleRootManager.getInstance(getModule()).getModifiableModel();
for (ContentEntry contentEntry : modifiableRootModel.getContentEntries()) {
final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
for (SourceFolder sourceFolder : sourceFolders) {
contentEntry.removeSourceFolder(sourceFolder);
}
modifiableRootModel.commit();
}
modifiableRootModel.commit();
}
);
PantsProjectCacheImpl.getInstance(getProject()).projectOpened();
}

@Override
protected void tearDown() throws Exception {
cleanUpsourceFolders();
PantsProjectCacheImpl.getInstance(getProject()).projectClosed();
try {
super.tearDown();
}
catch (Exception e) {
// TODO: something in the test framework isn't disposed.
if (!e.getMessage().contains("hasn't been disposed")) {
throw e;
}
}
super.tearDown();
}

@NotNull
Expand All @@ -66,7 +60,7 @@ public void testEmpty() {
assertFalse(cache.folderContainsSourceRoot(myFixture.getProject().getBaseDir()));
}

public void testLastOne() throws IOException {
public void testLastOne() {
final PantsProjectCache cache = PantsProjectCacheImpl.getInstance(myFixture.getProject());
final VirtualFile root = getMainContentRoot();

Expand All @@ -88,23 +82,20 @@ public void run() {
});
}

public void testFirstOne() throws IOException {
public void testFirstOne() {
final PantsProjectCache cache = PantsProjectCacheImpl.getInstance(myFixture.getProject());
final VirtualFile root = getMainContentRoot();

ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
try {
PsiTestUtil.addSourceRoot(getModule(), VfsUtil.createDirectoryIfMissing(root, "abc"));
PsiTestUtil.addSourceRoot(getModule(), VfsUtil.createDirectoryIfMissing(root, "foo/bar"));
PsiTestUtil.addSourceRoot(getModule(), VfsUtil.createDirectoryIfMissing(root, "foo/baz"));
assertTrue(cache.folderContainsSourceRoot(VfsUtil.createDirectoryIfMissing(root, "abc")));
assertTrue(cache.folderContainsSourceRoot(VfsUtil.createDirectoryIfMissing(root, "foo")));
}
catch (IOException e) {
fail(e.getMessage());
}
ApplicationManager.getApplication().runWriteAction(() -> {
try {
PsiTestUtil.addSourceRoot(getModule(), VfsUtil.createDirectoryIfMissing(root, "abc"));
PsiTestUtil.addSourceRoot(getModule(), VfsUtil.createDirectoryIfMissing(root, "foo/bar"));
PsiTestUtil.addSourceRoot(getModule(), VfsUtil.createDirectoryIfMissing(root, "foo/baz"));
assertTrue(cache.folderContainsSourceRoot(VfsUtil.createDirectoryIfMissing(root, "abc")));
assertTrue(cache.folderContainsSourceRoot(VfsUtil.createDirectoryIfMissing(root, "foo")));
}
catch (IOException e) {
fail(e.getMessage());
}
});
}
Expand Down

0 comments on commit 8865601

Please sign in to comment.