diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java index d60978a1d2c0..2d691cbdc4f9 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java @@ -83,6 +83,7 @@ import com.intellij.util.containers.CollectionFactory; import com.intellij.util.indexing.FileBasedIndex; import com.intellij.util.indexing.IndexableFileSet; import com.intellij.util.messages.MessageBusConnection; +import com.intellij.util.ui.UIUtil; import gnu.trove.THashMap; import junit.framework.TestCase; import org.jetbrains.annotations.NonNls; @@ -170,7 +171,7 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da ((PersistentFS)ManagingFS.getInstance()).cleanPersistedContents(); } - private static void initProject(final LightProjectDescriptor descriptor) throws Exception { + private static void initProject(final LightProjectDescriptor descriptor, final UsefulTestCase usefulTestCase) throws Exception { ourProjectDescriptor = descriptor; final File projectFile = File.createTempFile("lighttemp", ProjectFileType.DOT_DEFAULT_EXTENSION); ApplicationManager.getApplication().runWriteAction(new Runnable() { @@ -188,7 +189,7 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da ByteArrayOutputStream buffer = new ByteArrayOutputStream(); new Throwable(projectFile.getPath()).printStackTrace(new PrintStream(buffer)); - ourProject = PlatformTestCase.createProject(projectFile, buffer.toString()); + ourProject = PlatformTestCase.createProject(projectFile, buffer.toString(), usefulTestCase); if (!ourHaveShutdownHook) { ourHaveShutdownHook = true; @@ -288,7 +289,8 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da protected void setUp() throws Exception { super.setUp(); initApplication(this); - doSetup(new SimpleLightProjectDescriptor(getModuleType(), getProjectJDK()), configureLocalInspectionTools(), myAvailableInspectionTools); + doSetup(new SimpleLightProjectDescriptor(getModuleType(), getProjectJDK()), configureLocalInspectionTools(), myAvailableInspectionTools, + this); ((InjectedLanguageManagerImpl)InjectedLanguageManager.getInstance(getProject())).pushInjectors(); storeSettings(); @@ -297,12 +299,14 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da } public static void doSetup(final LightProjectDescriptor descriptor, - final LocalInspectionTool[] localInspectionTools, final Map availableInspectionTools) throws Exception { + final LocalInspectionTool[] localInspectionTools, + final Map availableInspectionTools, + UsefulTestCase usefulTestCase) throws Exception { assertNull("Previous test " + ourTestCase + " hasn't called tearDown(). Probably overriden without super call.", ourTestCase); IdeaLogger.ourErrorsOccurred = null; if (ourProject == null || !ourProjectDescriptor.equals(descriptor)) { - initProject(descriptor); + initProject(descriptor, usefulTestCase); } ProjectManagerEx.getInstanceEx().setCurrentTestProject(ourProject); @@ -462,6 +466,12 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da if (checkForEditors) { checkEditorsReleased(); } + if (SwingUtilities.isEventDispatchThread()) { + UIUtil.dispatchAllInvocationEvents(); + } + else { + UIUtil.pump(); + } } public static void checkEditorsReleased() { diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java index 7143907b1132..f2f16d594b55 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java @@ -56,9 +56,7 @@ import com.intellij.openapi.vfs.newvfs.ManagingFS; import com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS; import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiManager; +import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.impl.PsiDocumentManagerImpl; @@ -68,6 +66,7 @@ import com.intellij.util.indexing.IndexableSetContributor; import com.intellij.util.indexing.IndexedRootsProvider; import junit.framework.TestCase; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -180,7 +179,7 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro File projectFile = getIprFile(); LocalFileSystem.getInstance().refreshIoFiles(myFilesToDelete); - myProject = createProject(projectFile, getClass().getName() + "." + getName()); + myProject = createProject(projectFile, getClass().getName() + "." + getName(), this); setUpModule(); @@ -193,13 +192,21 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro } @Nullable - public static Project createProject(File projectFile, String creationPlace) { + public static Project createProject(@NotNull File projectFile, @NotNull String creationPlace, @NotNull final UsefulTestCase usefulTestCase) { try { - Project project = - ProjectManagerEx.getInstanceEx().newProject(FileUtil.getNameWithoutExtension(projectFile), projectFile.getPath(), false, false); + Project project = ProjectManagerEx.getInstanceEx().newProject(FileUtil.getNameWithoutExtension(projectFile), projectFile.getPath(), false, false); assert project != null; project.putUserData(CREATION_PLACE, creationPlace); + + + PsiManager.getInstance(project).addPsiTreeChangeListener(new PsiTreeChangeAdapter() { + @Override + public void childrenChanged(PsiTreeChangeEvent event) { + PsiFile psiFile = event.getFile(); + usefulTestCase.checkPsiElementsAreStillValid(psiFile); + } + }); return project; } catch (TooManyProjectLeakedException e) { diff --git a/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java b/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java index 6cd2908b3dce..2b1c9820e0a7 100644 --- a/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/UsefulTestCase.java @@ -28,6 +28,9 @@ import com.intellij.openapi.util.Key; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiRecursiveElementVisitor; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.impl.source.PostprocessReformattingAspect; @@ -602,4 +605,15 @@ public abstract class UsefulTestCase extends TestCase { } } + public void checkPsiElementsAreStillValid(PsiFile psiFile) { + if (psiFile == null || !psiFile.isValid() || isPerformanceTest()) return; + + psiFile.accept(new PsiRecursiveElementVisitor() { + @Override + public void visitElement(PsiElement element) { + assertTrue(element.toString(), element.isValid()); + super.visitElement(element); + } + }); + } } diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java index 7ff1b0d91313..4ce57a38e159 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/HeavyIdeaTestFixtureImpl.java @@ -133,7 +133,7 @@ class HeavyIdeaTestFixtureImpl extends BaseFixture implements HeavyIdeaTestFixtu LocalFileSystem.getInstance().refreshAndFindFileByIoFile(projectFile); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); new Throwable(projectFile.getPath()).printStackTrace(new PrintStream(buffer)); - myProject = PlatformTestCase.createProject(projectFile, buffer.toString()); + myProject = PlatformTestCase.createProject(projectFile, buffer.toString(), this); for (ModuleFixtureBuilder moduleFixtureBuilder: myModuleFixtureBuilders) { moduleFixtureBuilder.getFixture().setUp(); diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightIdeaTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightIdeaTestFixtureImpl.java index 995b6cafd1b6..2bfaf7165acd 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightIdeaTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightIdeaTestFixtureImpl.java @@ -45,7 +45,7 @@ class LightIdeaTestFixtureImpl extends BaseFixture implements LightIdeaTestFixtu super.setUp(); LightPlatformTestCase.initApplication(new MyDataProvider()); - LightPlatformTestCase.doSetup(myProjectDescriptor, new LocalInspectionTool[0], null); + LightPlatformTestCase.doSetup(myProjectDescriptor, new LocalInspectionTool[0], null, this); storeSettings(); }