diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java index 3d0e35bd8585..39bfcda42d56 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java @@ -167,8 +167,7 @@ public abstract class DaemonAnalyzerTestCase extends CodeInsightTestCase { InspectionProjectProfileManager.getInstance(getProject()).updateProfile(profile); InspectionProjectProfileManager.getInstance(getProject()).setProjectProfile(profile.getName()); DaemonCodeAnalyzerImpl daemonCodeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject()); - toInitializeDaemon = !daemonCodeAnalyzer.isInitialized(); - daemonCodeAnalyzer.prepareForTest(toInitializeDaemon); + daemonCodeAnalyzer.prepareForTest(); final StartupManagerImpl startupManager = (StartupManagerImpl)StartupManagerEx.getInstanceEx(getProject()); startupManager.runStartupActivities(); startupManager.startCacheUpdate(); @@ -194,9 +193,7 @@ public abstract class DaemonAnalyzerTestCase extends CodeInsightTestCase { @Override protected void tearDown() throws Exception { ((StartupManagerImpl)StartupManager.getInstance(getProject())).checkCleared(); - if (toInitializeDaemon) { - ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).cleanupAfterTest(); - } + ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).cleanupAfterTest(!LightPlatformTestCase.isLight(getProject())); super.tearDown(); ((VirtualFilePointerManagerImpl)VirtualFilePointerManager.getInstance()).assertPointersDisposed(); } diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/LightDaemonAnalyzerTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/LightDaemonAnalyzerTestCase.java index c3d7b10c4165..1be48ad506ec 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/LightDaemonAnalyzerTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/LightDaemonAnalyzerTestCase.java @@ -42,13 +42,13 @@ public abstract class LightDaemonAnalyzerTestCase extends LightCodeInsightTestCa @Override protected void setUp() throws Exception { super.setUp(); - ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).prepareForTest(true); + ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).prepareForTest(); DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(false); } @Override protected void tearDown() throws Exception { - ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).cleanupAfterTest(); // has to cleanup by hand since light project does not get disposed any time soon + ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).cleanupAfterTest(!isLight(getProject())); super.tearDown(); } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java index cc24301a205a..c69b108b008d 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java @@ -179,7 +179,7 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx @NotNull int[] toIgnore, boolean canChangeDocument, @Nullable Runnable callbackWhileWaiting) { - assert isInitialized(); + assert myInitialized; assert !myDisposed; Application application = ApplicationManager.getApplication(); application.assertIsDispatchThread(); @@ -246,8 +246,8 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx } @TestOnly - public void prepareForTest(boolean initialize) { - if (initialize) { + public void prepareForTest() { + if (!myInitialized) { projectOpened(); } setUpdateByTimerEnabled(false); @@ -255,12 +255,16 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx } @TestOnly - public void cleanupAfterTest() { - projectClosed(); - Disposer.dispose(myStatusBarUpdater); - myStatusBarUpdater = null; - Disposer.dispose(myDaemonListeners); - myDaemonListeners = null; + public void cleanupAfterTest(boolean dispose) { + if (!myProject.isOpen()) return; + stopProcess(false); + if (dispose) { + projectClosed(); + Disposer.dispose(myStatusBarUpdater); + myStatusBarUpdater = null; + Disposer.dispose(myDaemonListeners); + myDaemonListeners = null; + } setUpdateByTimerEnabled(false); waitForTermination(); } @@ -303,12 +307,6 @@ public class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzer implements JDOMEx myDisposed = true; myLastSettings = null; - myInitialized = false; - } - - @TestOnly - public boolean isInitialized() { - return myInitialized; } void repaintErrorStripeRenderer(Editor editor) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java index 445153e03d4e..f804710ee52d 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java @@ -96,7 +96,7 @@ class DaemonListeners implements Disposable { private final DaemonCodeAnalyzer.DaemonListener myDaemonEventPublisher; private static final Key DAEMON_INITIALIZED = Key.create("DAEMON_INITIALIZED"); - public DaemonListeners(Project project, DaemonCodeAnalyzerImpl daemonCodeAnalyzer, EditorTracker editorTracker) { + public DaemonListeners(@NotNull Project project, @NotNull DaemonCodeAnalyzerImpl daemonCodeAnalyzer, @NotNull EditorTracker editorTracker) { myProject = project; myDaemonCodeAnalyzer = daemonCodeAnalyzer; LOG.assertTrue(((UserDataHolderEx)myProject).replace(DAEMON_INITIALIZED, null, Boolean.TRUE), "Daemon listeners already initialized for the project "+myProject); @@ -284,7 +284,7 @@ class DaemonListeners implements Disposable { } public void dispose() { - LOG.assertTrue(((UserDataHolderEx)myProject).replace(DAEMON_INITIALIZED, Boolean.TRUE, null), "Daemon listeners already disposed for the project "+myProject); + LOG.assertTrue(((UserDataHolderEx)myProject).replace(DAEMON_INITIALIZED, Boolean.TRUE, Boolean.FALSE), "Daemon listeners already disposed for the project "+myProject); } boolean canChangeFileSilently(@NotNull PsiFileSystemItem file) { diff --git a/platform/lang-impl/src/com/intellij/psi/impl/PsiDocumentManagerImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/PsiDocumentManagerImpl.java index adb6277362d7..0a9583e0058a 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/PsiDocumentManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/PsiDocumentManagerImpl.java @@ -137,7 +137,7 @@ public class PsiDocumentManagerImpl extends PsiDocumentManager implements Projec if (ApplicationManager.getApplication().isUnitTestMode()) { Collection projects = ProjectLocator.getInstance().getProjectsForFile(virtualFile); - LOG.assertTrue(projects.isEmpty() || projects.contains(myProject), "Trying to get PSI for an alien project. VirtualFile=" + virtualFile + "; myProject=" + myProject); + LOG.assertTrue(projects.isEmpty() || projects.contains(myProject), "Trying to get PSI for an alien project. VirtualFile=" + virtualFile + ";\n myProject=" + myProject+";\n projects returned: "+projects); } psiFile = getPsiFile(virtualFile); diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java index 6d7b5dbe8368..93057150d426 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectManagerImpl.java @@ -55,6 +55,7 @@ import com.intellij.openapi.vfs.VirtualFileManagerListener; import com.intellij.openapi.vfs.ex.VirtualFileManagerEx; import com.intellij.openapi.vfs.newvfs.NewVirtualFile; import com.intellij.util.Alarm; +import com.intellij.util.ArrayUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.HashMap; import com.intellij.util.io.fs.IFile; @@ -242,8 +243,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt return message; } - private ProjectImpl - createAndInitProject(String projectName, String filePath, boolean isDefault, boolean isOptimiseTestLoadSpeed, + private ProjectImpl createAndInitProject(String projectName, String filePath, boolean isDefault, boolean isOptimiseTestLoadSpeed, @Nullable Project template) throws IOException { final ProjectImpl project = isDefault ? new DefaultProject(this, filePath, isOptimiseTestLoadSpeed, projectName) : new ProjectImpl(this, filePath, isOptimiseTestLoadSpeed, projectName); @@ -348,29 +348,32 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt @NotNull public Project[] getOpenProjects() { - if (ApplicationManager.getApplication().isUnitTestMode()) { - final Project currentTestProject = myCurrentTestProject; - if (myOpenProjects.isEmpty() && currentTestProject != null && !currentTestProject.isDisposed()) { - return new Project[] {currentTestProject}; - } - } if (myOpenProjectsArrayCache.length != myOpenProjects.size()) { LOG.error("Open projects: "+myOpenProjects+"; cache: "+Arrays.asList(myOpenProjectsArrayCache)); } if (myOpenProjectsArrayCache.length > 0 && myOpenProjectsArrayCache[0] != myOpenProjects.get(0)) { LOG.error("Open projects cache corrupted. Open projects: "+myOpenProjects+"; cache: "+Arrays.asList(myOpenProjectsArrayCache)); } + if (ApplicationManager.getApplication().isUnitTestMode()) { + Project currentTestProject = myCurrentTestProject; + if (currentTestProject != null && !currentTestProject.isDisposed()) { + return ArrayUtil.append(myOpenProjectsArrayCache, currentTestProject); + } + } return myOpenProjectsArrayCache; } public boolean isProjectOpened(Project project) { - if (ApplicationManager.getApplication().isUnitTestMode() && myOpenProjects.isEmpty() && myCurrentTestProject != null) { - return project == myCurrentTestProject; + if (ApplicationManager.getApplication().isUnitTestMode() && myCurrentTestProject != null) { + return project == myCurrentTestProject || myOpenProjects.contains(project); } return myOpenProjects.contains(project); } public boolean openProject(final Project project) { + if (ApplicationManager.getApplication().isUnitTestMode() && project.toString().contains("lighttemp")) { + throw new AssertionError("must not open light project"); + } if (myOpenProjects.contains(project)) return false; if (!ApplicationManager.getApplication().isUnitTestMode() && !((ProjectEx)project).getStateStore().checkVersion()) return false; @@ -891,9 +894,11 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt } private boolean closeProject(final Project project, final boolean save, final boolean dispose) { + if (ApplicationManager.getApplication().isUnitTestMode() && project.toString().contains("lighttemp")) { + throw new AssertionError("must not close light project"); + } if (!isProjectOpened(project)) return true; if (!canClose(project)) return false; - final ShutDownTracker shutDownTracker = ShutDownTracker.getInstance(); shutDownTracker.registerStopperThread(Thread.currentThread()); try { @@ -912,6 +917,7 @@ public class ProjectManagerImpl extends ProjectManagerEx implements NamedJDOMExt public void run() { myOpenProjects.remove(project); cacheOpenProjects(); + myCurrentTestProject = null; myChangedProjectFiles.remove(project); diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java index 4f2c8bbe95f6..a5a14cdb6ccf 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java @@ -29,6 +29,7 @@ import com.intellij.codeInspection.ex.InspectionTool; import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.codeInspection.ex.ToolsImpl; import com.intellij.ide.highlighter.ProjectFileType; +import com.intellij.ide.startup.StartupManagerEx; import com.intellij.ide.startup.impl.StartupManagerImpl; import com.intellij.idea.IdeaLogger; import com.intellij.idea.IdeaTestApplication; @@ -101,10 +102,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @author yole @@ -408,7 +406,15 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da InspectionProjectProfileManager.getInstance(getProject()).setProjectProfile(profile.getName()); assertFalse(getPsiManager().isDisposed()); - assertTrue(getProject().isInitialized()); + Boolean passed = null; + try { + passed = StartupManagerEx.getInstanceEx(getProject()).startupActivityPassed(); + } + catch (Exception e) { + + } + assertTrue("open: "+getProject().isOpen()+"; disposed:"+getProject().isDisposed()+"; startup passed:"+ passed+"; testProjectIsOurProject:"+(getProject() == ProjectManagerEx.getInstanceEx().getCurrentTestProject())+"; all open projects: "+ + Arrays.asList(ProjectManager.getInstance().getOpenProjects()), getProject().isInitialized()); CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(new CodeStyleSettings()); @@ -451,7 +457,7 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da ((InjectedLanguageManagerImpl)InjectedLanguageManager.getInstance(getProject())).checkInjectorsAreDisposed(); } - public static void doTearDown(Project project, IdeaTestApplication application, boolean checkForEditors) throws Exception { + public static void doTearDown(final Project project, IdeaTestApplication application, boolean checkForEditors) throws Exception { CodeStyleSettingsManager.getInstance(project).dropTemporarySettings(); checkAllTimersAreDisposed(); UsefulTestCase.doPostponedFormatting(project); @@ -506,6 +512,7 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da @Override public void run() { ((UndoManagerImpl)UndoManager.getGlobalInstance()).dropHistoryInTests(); + ((UndoManagerImpl)UndoManager.getInstance(project)).dropHistoryInTests(); } }); diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java index ba22374ef556..b59a9974e1c6 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -512,7 +512,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig @Override public IntentionAction findSingleIntention(@NotNull final String hint) { final List list = filterAvailableIntentions(hint); - if (list.size() == 0) { + if (list.isEmpty()) { Assert.fail("\"" + hint + "\" not in [" + StringUtil.join(getAvailableIntentions(), INTENTION_NAME_FUN, ", ") + "]"); } else if (list.size() > 1) { @@ -719,7 +719,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig DataContext context = DataManager.getInstance().getDataContext(); TestActionEvent e = new TestActionEvent(context, action); action.beforeActionPerformedUpdate(e); - if (e.getPresentation().isVisible() && e.getPresentation().isVisible()) { + if (e.getPresentation().isEnabled() && e.getPresentation().isVisible()) { action.actionPerformed(e); } return e.getPresentation(); @@ -1016,8 +1016,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig configureInspections(myInspections == null ? LocalInspectionTool.EMPTY_ARRAY : myInspections); DaemonCodeAnalyzerImpl daemonCodeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject()); - toInitializeDaemon = !daemonCodeAnalyzer.isInitialized(); - daemonCodeAnalyzer.prepareForTest(toInitializeDaemon); + daemonCodeAnalyzer.prepareForTest(); DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(false); ensureIndexesUpToDate(getProject()); @@ -1048,7 +1047,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig editorManager.closeFile(openFile); } if (toInitializeDaemon) { - ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).cleanupAfterTest(); + ((DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject())).cleanupAfterTest(!LightPlatformTestCase.isLight(getProject())); } myEditor = null;