test framework: stability improvements

This commit is contained in:
Roman Shevchenko
2013-09-19 12:51:35 +04:00
parent 76d664d9f9
commit fe00f73a2f
2 changed files with 68 additions and 47 deletions
@@ -74,6 +74,7 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.encoding.EncodingManager;
import com.intellij.openapi.vfs.encoding.EncodingManagerImpl;
import com.intellij.openapi.vfs.ex.temp.TempFileSystem;
import com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl;
import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS;
import com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl;
@@ -89,6 +90,7 @@ import com.intellij.psi.impl.PsiDocumentManagerImpl;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl;
import com.intellij.psi.templateLanguages.TemplateDataLanguageMappings;
import com.intellij.util.Consumer;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.LocalTimeCounter;
import com.intellij.util.containers.ContainerUtil;
@@ -109,6 +111,8 @@ import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
import static com.intellij.openapi.roots.ModuleRootModificationUtil.updateModel;
/**
* @author yole
*/
@@ -202,9 +206,11 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
private static void initProject(@NotNull final LightProjectDescriptor descriptor) throws Exception {
ourProjectDescriptor = descriptor;
final File projectFile = FileUtil.createTempFile("light_temp_", ProjectFileType.DOT_DEFAULT_EXTENSION);
new WriteCommandAction.Simple(null) {
@SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod")
@Override
protected void run() throws Throwable {
if (ourProject != null) {
@@ -227,14 +233,13 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
ourPsiManager = null;
ourModule = createMainModule(descriptor.getModuleType());
//ourSourceRoot = DummyFileSystem.getInstance().createRoot("src");
final VirtualFile dummyRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///");
VirtualFile dummyRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///");
assert dummyRoot != null;
dummyRoot.refresh(false, false);
try {
ourSourceRoot = dummyRoot.createChildDirectory(this, "src");
cleanSourceRoot();
}
catch (IOException e) {
throw new RuntimeException(e);
@@ -255,39 +260,35 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
@Override
public boolean visitFile(@NotNull VirtualFile file) {
iterator.processFile(file);
return true;
}
});
}
}, null);
final ModuleRootManager rootManager = ModuleRootManager.getInstance(ourModule);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
if (descriptor.getSdk() != null) {
rootModel.setSdk(descriptor.getSdk());
}
final ContentEntry contentEntry = rootModel.addContentEntry(ourSourceRoot);
contentEntry.addSourceFolder(ourSourceRoot, false);
descriptor.configureModule(ourModule, rootModel, contentEntry);
rootModel.commit();
final MessageBusConnection connection = ourProject.getMessageBus().connect();
connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() {
updateModel(ourModule, new Consumer<ModifiableRootModel>() {
@Override
public void beforeRootsChange(ModuleRootEvent event) {
if (!event.isCausedByFileTypesChange()) {
//TODO: uncomment fail("Root modification in LightIdeaTestCase is not allowed.");
public void consume(ModifiableRootModel model) {
if (descriptor.getSdk() != null) {
model.setSdk(descriptor.getSdk());
}
ContentEntry contentEntry = model.addContentEntry(ourSourceRoot);
contentEntry.addSourceFolder(ourSourceRoot, false);
descriptor.configureModule(ourModule, model, contentEntry);
}
});
MessageBusConnection connection = ourProject.getMessageBus().connect();
connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() {
@Override
public void beforeRootsChange(ModuleRootEvent event) {
/*if (!event.isCausedByFileTypesChange()) {
fail("Root modification in LightIdeaTestCase is not allowed.");
}*/
}
});
connection.subscribe(ProjectTopics.MODULES, new ModuleAdapter() {
@Override
public void moduleAdded(Project project, Module module) {
@@ -295,12 +296,22 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
}
});
final StartupManagerImpl startupManager = (StartupManagerImpl)StartupManager.getInstance(ourProject);
StartupManagerImpl startupManager = (StartupManagerImpl)StartupManager.getInstance(ourProject);
startupManager.runStartupActivities();
startupManager.startCacheUpdate();
}
private void cleanSourceRoot() throws IOException {
TempFileSystem tempFs = (TempFileSystem)ourSourceRoot.getFileSystem();
for (VirtualFile child : ourSourceRoot.getChildren()) {
if (!tempFs.exists(child)) {
tempFs.createChildFile(this, ourSourceRoot, child.getName());
}
child.delete(this);
}
}
}.execute().throwException();
// project creation may make a lot of pointers, do not regard them as leak
((VirtualFilePointerManagerImpl)VirtualFilePointerManager.getInstance()).storePointers();
}
@@ -339,12 +350,11 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
public static void doSetup(@NotNull LightProjectDescriptor descriptor,
@NotNull LocalInspectionTool[] localInspectionTools,
@NotNull final Map<String, InspectionToolWrapper> availableInspectionTools)
throws Exception {
@NotNull final Map<String, InspectionToolWrapper> availableInspectionTools) throws Exception {
assertNull("Previous test " + ourTestCase + " hasn't called tearDown(). Probably overridden without super call.", ourTestCase);
IdeaLogger.ourErrorsOccurred = null;
if (ourProject == null || !ourProjectDescriptor.equals(descriptor)) {
if (ourProject == null || ourProjectDescriptor == null || !ourProjectDescriptor.equals(descriptor)) {
initProject(descriptor);
}
((ProjectImpl)ourProject).setTemporarilyDisposed(false);
@@ -762,17 +772,22 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
return PsiDocumentManager.getInstance(getProject()).getDocument(file);
}
@SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
public static synchronized void closeAndDeleteProject() {
if (ourProject != null) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
ourApplication.setDataProvider(null);
((ProjectImpl)ourProject).setTemporarilyDisposed(false);
final VirtualFile projFile = ((ProjectEx)ourProject).getStateStore().getProjectFile();
final File projectFile = projFile == null ? null : VfsUtilCore.virtualToIoFile(projFile);
VirtualFile projectFile = ((ProjectEx)ourProject).getStateStore().getProjectFile();
File ioFile = projectFile == null ? null : VfsUtilCore.virtualToIoFile(projectFile);
if (!ourProject.isDisposed()) Disposer.dispose(ourProject);
if (projectFile != null) {
FileUtil.delete(projectFile);
if (ioFile != null) {
FileUtil.delete(ioFile);
}
ProjectManagerEx.getInstanceEx().closeTestProject(ourProject);
ourProject = null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.mvc;
import com.intellij.ProjectTopics;
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.DumbAwareRunnable;
import com.intellij.openapi.project.ModuleAdapter;
import com.intellij.openapi.project.Project;
@@ -246,12 +246,18 @@ public class MvcModuleStructureSynchronizer extends AbstractProjectComponent {
StartupManager.getInstance(myProject).runWhenProjectIsInitialized(new DumbAwareRunnable() {
@Override
public void run() {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
runActions();
}
}, ModalityState.NON_MODAL);
Application app = ApplicationManager.getApplication();
if (!app.isUnitTestMode()) {
app.invokeLater(new Runnable() {
@Override
public void run() {
runActions();
}
}, ModalityState.NON_MODAL);
}
else {
runActions();
}
}
});
}
@@ -270,7 +276,7 @@ public class MvcModuleStructureSynchronizer extends AbstractProjectComponent {
if (o instanceof VirtualFile) {
final VirtualFile file = (VirtualFile)o;
if (file.isValid()) {
final Module module = ModuleUtil.findModuleForFile(file, myProject);
final Module module = ModuleUtilCore.findModuleForFile(file, myProject);
if (module == null) {
return Collections.emptyList();
}
@@ -296,7 +302,7 @@ public class MvcModuleStructureSynchronizer extends AbstractProjectComponent {
return;
}
Pair<Object, SyncAction>[] actions = myActions.toArray(new Pair[myActions.size()]);
@SuppressWarnings("unchecked") Pair<Object, SyncAction>[] actions = myActions.toArray(new Pair[myActions.size()]);
//get module by object and kill duplicates
final Set<Trinity<Module, SyncAction, MvcFramework>> rawActions = new LinkedHashSet<Trinity<Module, SyncAction, MvcFramework>>();
@@ -379,7 +385,7 @@ public class MvcModuleStructureSynchronizer extends AbstractProjectComponent {
@Override
void doAction(Module module, MvcFramework framework) {
final Project project = module.getProject();
final MvcModuleStructureSynchronizer mvcModuleStructureSynchronizer = MvcModuleStructureSynchronizer.getInstance(project);
final MvcModuleStructureSynchronizer mvcModuleStructureSynchronizer = getInstance(project);
if (mvcModuleStructureSynchronizer.myOutOfModuleDirectoryCreatedActionAdded) {
mvcModuleStructureSynchronizer.myOutOfModuleDirectoryCreatedActionAdded = false;