From 8865601d5601e0c5e0b9fd95b4caed27094249f7 Mon Sep 17 00:00:00 2001 From: Yi Cheng Date: Thu, 8 Aug 2019 12:47:35 -0700 Subject: [PATCH] Fix resource not disposed error (#412) Followup for https://github.com/pantsbuild/intellij-pants-plugin/pull/409#discussion_r311701451 as it suppressed the error. --- .../impl/PantsProjectCacheTest.java | 61 ++++++++----------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/tests/com/twitter/intellij/pants/components/impl/PantsProjectCacheTest.java b/tests/com/twitter/intellij/pants/components/impl/PantsProjectCacheTest.java index 38ac4f0eb..5e577c003 100644 --- a/tests/com/twitter/intellij/pants/components/impl/PantsProjectCacheTest.java +++ b/tests/com/twitter/intellij/pants/components/impl/PantsProjectCacheTest.java @@ -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 @@ -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(); @@ -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()); } }); }