From 665cd6c3e0fdf3ea2faee7dabbed2f9a261ac2d7 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 4 Apr 2011 18:37:20 +0400 Subject: [PATCH] fix tests for GlobalSimpleInspectionTool running concurrently --- .../testFramework/InspectionTestCase.java | 29 ++------ .../GlobalInspectionContext.java | 3 +- .../GlobalSimpleInspectionTool.java | 6 ++ .../ex/GlobalInspectionContextImpl.java | 68 ++++++++++++------- .../ex/InspectionManagerEx.java | 6 ++ .../ex/LocalInspectionToolWrapper.java | 2 +- .../testFramework/InspectionTestUtil.java | 24 ++----- .../impl/CodeInsightTestFixtureImpl.java | 37 ++++++++-- .../InconsistentResourceBundleInspection.java | 14 +++- 9 files changed, 114 insertions(+), 75 deletions(-) diff --git a/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java b/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java index 85fead9b176a..1a0b9443380c 100644 --- a/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java +++ b/java/testFramework/src/com/intellij/testFramework/InspectionTestCase.java @@ -20,9 +20,9 @@ import com.intellij.analysis.AnalysisScope; import com.intellij.codeInspection.GlobalInspectionTool; import com.intellij.codeInspection.InspectionManager; import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.reference.EntryPoint; import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection; import com.intellij.codeInspection.ex.*; +import com.intellij.codeInspection.reference.EntryPoint; import com.intellij.codeInspection.reference.RefElement; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ex.PathManagerEx; @@ -42,6 +42,7 @@ import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiManager; import com.intellij.psi.util.PsiUtil; +import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -118,14 +119,11 @@ public abstract class InspectionTestCase extends PsiTestCase { }); AnalysisScope scope = createAnalysisScope(sourceDir[0].getParent()); - InspectionManagerEx inspectionManager = (InspectionManagerEx) InspectionManager.getInstance(myProject); - final GlobalInspectionContextImpl globalContext = inspectionManager.createNewGlobalContext(true); - globalContext.setCurrentScope(scope); + InspectionManagerEx inspectionManager = (InspectionManagerEx) InspectionManager.getInstance(getProject()); + InspectionTool[] tools = runDeadCodeFirst ? new InspectionTool[]{new UnusedDeclarationInspection(), tool} : new InspectionTool[]{tool}; + final GlobalInspectionContextImpl globalContext = CodeInsightTestFixtureImpl.createGlobalContextForTool(scope, getProject(), inspectionManager, tools); - if (runDeadCodeFirst) { - runTool(new UnusedDeclarationInspection(), scope, globalContext, inspectionManager); - } - runTool(tool, scope, globalContext, inspectionManager); + InspectionTestUtil.runTool(tool, scope, globalContext, inspectionManager); } protected AnalysisScope createAnalysisScope(VirtualFile sourceDir) { @@ -133,21 +131,6 @@ public abstract class InspectionTestCase extends PsiTestCase { return new AnalysisScope(psiManager.findDirectory(sourceDir)); } - private static void runTool(final InspectionTool tool, - final AnalysisScope scope, - final GlobalInspectionContextImpl globalContext, - final InspectionManagerEx inspectionManager) { - InspectionTestUtil.runTool(tool, scope, globalContext, inspectionManager); - - final GlobalJavaInspectionContextImpl javaInspectionContext = - (GlobalJavaInspectionContextImpl)globalContext.getExtension(GlobalJavaInspectionContextImpl.CONTEXT); - if (javaInspectionContext != null) { - do { - javaInspectionContext.processSearchRequests(globalContext); - } while (tool.queryExternalUsagesRequests(inspectionManager)); - } - } - protected void setupRootModel(final String testDir, final VirtualFile[] sourceDir, final String sdkName) { VirtualFile projectDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir)); assertNotNull("could not find project dir " + testDir, projectDir); diff --git a/platform/lang-api/src/com/intellij/codeInspection/GlobalInspectionContext.java b/platform/lang-api/src/com/intellij/codeInspection/GlobalInspectionContext.java index b26ab828e328..ad8fa4b18d5e 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/GlobalInspectionContext.java +++ b/platform/lang-api/src/com/intellij/codeInspection/GlobalInspectionContext.java @@ -21,6 +21,7 @@ import com.intellij.codeInspection.reference.RefEntity; import com.intellij.codeInspection.reference.RefManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; +import com.intellij.openapi.util.UserDataHolder; import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; @@ -33,7 +34,7 @@ import org.jetbrains.annotations.NotNull; * @see GlobalInspectionTool#queryExternalUsagesRequests * @since 6.0 */ -public interface GlobalInspectionContext { +public interface GlobalInspectionContext extends UserDataHolder { /** * Returns the reference graph for the global inspection run. * diff --git a/platform/lang-api/src/com/intellij/codeInspection/GlobalSimpleInspectionTool.java b/platform/lang-api/src/com/intellij/codeInspection/GlobalSimpleInspectionTool.java index d937b8f0c4a8..d302b82b5f18 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/GlobalSimpleInspectionTool.java +++ b/platform/lang-api/src/com/intellij/codeInspection/GlobalSimpleInspectionTool.java @@ -25,6 +25,12 @@ import org.jetbrains.annotations.NotNull; * Basically it is a local inspection tool which cannot be selected in the inspection profile to be run on-the-fly. */ public abstract class GlobalSimpleInspectionTool extends GlobalInspectionTool { + public void inspectionStarted(@NotNull InspectionManager manager, + @NotNull GlobalInspectionContext globalContext, + @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {} + public void inspectionFinished(@NotNull InspectionManager manager, + @NotNull GlobalInspectionContext globalContext, + @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {} public abstract void checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, @NotNull ProblemsHolder problemsHolder, diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java index 0207bb2a62fa..c5271749f496 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java @@ -42,10 +42,7 @@ import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectUtil; import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.Computable; -import com.intellij.openapi.util.JDOMUtil; -import com.intellij.openapi.util.Key; -import com.intellij.openapi.util.NotNullLazyValue; +import com.intellij.openapi.util.*; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ToolWindowId; @@ -76,7 +73,7 @@ import java.util.List; import java.util.Map; import java.util.Set; -public class GlobalInspectionContextImpl implements GlobalInspectionContext { +public class GlobalInspectionContextImpl extends UserDataHolderBase implements GlobalInspectionContext { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.GlobalInspectionContextImpl"); private RefManager myRefManager; @@ -98,6 +95,7 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { private static final JobDescriptor LOCAL_ANALYSIS = new JobDescriptor(InspectionsBundle.message("inspection.processing.job.descriptor2")); + public static final JobDescriptor[] LOCAL_ANALYSIS_ARRAY = {LOCAL_ANALYSIS}; private InspectionProfile myExternalProfile = null; @@ -428,7 +426,7 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { }); } - private void performInspectionsWithProgress(@NotNull final AnalysisScope scope, @NotNull final InspectionManager manager) { + public void performInspectionsWithProgress(@NotNull final AnalysisScope scope, @NotNull final InspectionManager manager) { final PsiManager psiManager = PsiManager.getInstance(myProject); myProgressIndicator = ProgressManager.getInstance().getProgressIndicator(); //init manager in read action @@ -464,11 +462,11 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { } private void runTools(@NotNull AnalysisScope scope, @NotNull final InspectionManager manager) { - final List needRepeatSearchRequest = new ArrayList(); final List globalTools = new ArrayList(); final List localTools = new ArrayList(); final List globalSimpleTools = new ArrayList(); - initializeTools(globalTools, localTools,globalSimpleTools); + initializeTools(globalTools, localTools, globalSimpleTools); + final List needRepeatSearchRequest = new ArrayList(); ((RefManagerImpl)getRefManager()).initializeAnnotators(); for (Tools tools : globalTools) { for (ScopeToolState state : tools.getTools()) { @@ -511,6 +509,11 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { final PsiManager psiManager = PsiManager.getInstance(myProject); final Set localScopeFiles = scope.toSearchScope() instanceof LocalSearchScope ? new THashSet() : null; + for (Tools tools : globalSimpleTools) { + GlobalInspectionToolWrapper toolWrapper = (GlobalInspectionToolWrapper)tools.getTool(); + GlobalSimpleInspectionTool tool = (GlobalSimpleInspectionTool)toolWrapper.getTool(); + tool.inspectionStarted(manager, this, toolWrapper); + } scope.accept(new PsiElementVisitor() { @Override public void visitFile(final PsiFile file) { @@ -563,7 +566,13 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { } } }); + for (Tools tools : globalSimpleTools) { + GlobalInspectionToolWrapper toolWrapper = (GlobalInspectionToolWrapper)tools.getTool(); + GlobalSimpleInspectionTool tool = (GlobalSimpleInspectionTool)toolWrapper.getTool(); + tool.inspectionFinished(manager, this, toolWrapper); + } } + private static final TripleFunction CONVERT = new TripleFunction() { @Override @@ -583,26 +592,12 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { public void initializeTools(@NotNull List outGlobalTools, @NotNull List outLocalTools, @NotNull List outGlobalSimpleTools) { myJobDescriptors = new ArrayList(); - final InspectionProfileImpl profile = new InspectionProfileImpl((InspectionProfileImpl)getCurrentProfile()); - final List usedTools = profile.getAllEnabledInspectionTools(); + final List usedTools = getUsedTools(); for (Tools currentTools : usedTools) { final String shortName = currentTools.getShortName(); myTools.put(shortName, currentTools); final InspectionTool tool = (InspectionTool)currentTools.getTool(); - if (tool instanceof LocalInspectionToolWrapper) { - outLocalTools.add(currentTools); - appendJobDescriptor(LOCAL_ANALYSIS); - } - else if (tool instanceof GlobalInspectionToolWrapper && ((GlobalInspectionToolWrapper)tool).getTool() instanceof GlobalSimpleInspectionTool) { - outGlobalSimpleTools.add(currentTools); - } - else { - outGlobalTools.add(currentTools); - JobDescriptor[] jobDescriptors = tool.getJobDescriptors(); - for (JobDescriptor jobDescriptor : jobDescriptors) { - appendJobDescriptor(jobDescriptor); - } - } + classifyTool(outGlobalTools, outLocalTools, outGlobalSimpleTools, currentTools, tool); for (ScopeToolState state : currentTools.getTools()) { ((InspectionTool)state.getTool()).initialize(this); @@ -613,6 +608,31 @@ public class GlobalInspectionContextImpl implements GlobalInspectionContext { } } + protected List getUsedTools() { + final InspectionProfileImpl profile = new InspectionProfileImpl((InspectionProfileImpl)getCurrentProfile()); + return profile.getAllEnabledInspectionTools(); + } + + private void classifyTool(List outGlobalTools, + List outLocalTools, + List outGlobalSimpleTools, + Tools currentTools, + InspectionTool tool) { + if (tool instanceof LocalInspectionToolWrapper) { + outLocalTools.add(currentTools); + } + else if (tool instanceof GlobalInspectionToolWrapper && ((GlobalInspectionToolWrapper)tool).getTool() instanceof GlobalSimpleInspectionTool) { + outGlobalSimpleTools.add(currentTools); + } + else { + outGlobalTools.add(currentTools); + } + JobDescriptor[] jobDescriptors = tool.getJobDescriptors(); + for (JobDescriptor jobDescriptor : jobDescriptors) { + appendJobDescriptor(jobDescriptor); + } + } + public Map getTools() { return myTools; } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionManagerEx.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionManagerEx.java index 8c4c5e0e1c9b..9a7f9bb8040e 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionManagerEx.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/InspectionManagerEx.java @@ -44,6 +44,7 @@ import com.intellij.ui.content.TabbedPaneContentUI; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; import java.util.HashSet; import java.util.Set; @@ -282,4 +283,9 @@ public class InspectionManagerEx extends InspectionManager { return new ProblemDescriptorImpl(psiElement, psiElement, descriptionTemplate, fixes, highlightType, false, null, showTooltip, null, true); } + + @TestOnly + public NotNullLazyValue getContentManager() { + return myContentManager; + } } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/LocalInspectionToolWrapper.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/LocalInspectionToolWrapper.java index 0dc1b5a39b45..801d42b7b316 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/LocalInspectionToolWrapper.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/LocalInspectionToolWrapper.java @@ -81,7 +81,7 @@ public final class LocalInspectionToolWrapper extends DescriptorProviderInspecti @NotNull public JobDescriptor[] getJobDescriptors() { - return JobDescriptor.EMPTY_ARRAY; + return GlobalInspectionContextImpl.LOCAL_ANALYSIS_ARRAY; } public void addProblemDescriptors(List descriptors, final boolean filterSuppressed) { diff --git a/platform/testFramework/src/com/intellij/testFramework/InspectionTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/InspectionTestUtil.java index f4147f020f4c..e660b4087a8e 100644 --- a/platform/testFramework/src/com/intellij/testFramework/InspectionTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/InspectionTestUtil.java @@ -20,8 +20,6 @@ import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInspection.ex.GlobalInspectionContextImpl; import com.intellij.codeInspection.ex.InspectionManagerEx; import com.intellij.codeInspection.ex.InspectionTool; -import com.intellij.codeInspection.ex.ToolsImpl; -import com.intellij.codeInspection.reference.RefManagerImpl; import com.intellij.openapi.progress.EmptyProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.impl.ProgressManagerImpl; @@ -145,28 +143,18 @@ expected: } } - public static void runTool(final InspectionTool tool, final AnalysisScope scope, GlobalInspectionContextImpl globalContext, final InspectionManagerEx inspectionManager) { + public static void runTool(final InspectionTool tool, final AnalysisScope scope, final GlobalInspectionContextImpl globalContext, final InspectionManagerEx inspectionManager) { final String shortName = tool.getShortName(); final HighlightDisplayKey key = HighlightDisplayKey.find(shortName); if (key == null){ HighlightDisplayKey.register(shortName); } - globalContext.getTools().put(tool.getShortName(), new ToolsImpl(tool, tool.getDefaultLevel(), true)); - tool.initialize(globalContext); - ((RefManagerImpl)globalContext.getRefManager()).initializeAnnotators(); - if (tool.isGraphNeeded()){ - ((RefManagerImpl)tool.getRefManager()).findAllDeclarations(); - } - ((ProgressManagerImpl)ProgressManager.getInstance()).executeProcessUnderProgress(new Runnable() { - @Override - public void run() { - tool.runInspection(scope, inspectionManager); - } - }, new EmptyProgressIndicator()); - - - tool.queryExternalUsagesRequests(inspectionManager); + @Override + public void run() { + globalContext.performInspectionsWithProgress(scope, inspectionManager); + } + }, new EmptyProgressIndicator()); } } 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 499cc9c2d8e6..0b5b29c8615e 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -45,6 +45,8 @@ import com.intellij.find.findUsages.FindUsagesHandler; import com.intellij.find.findUsages.FindUsagesOptions; import com.intellij.find.impl.FindManagerImpl; import com.intellij.ide.DataManager; +import com.intellij.ide.startup.StartupManagerEx; +import com.intellij.ide.startup.impl.StartupManagerImpl; import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.AnAction; @@ -417,16 +419,38 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig VirtualFile sourceDir = copyDirectoryToProject(new File(testDir, "src").getPath(), "src"); AnalysisScope scope = new AnalysisScope(getPsiManager().findDirectory(sourceDir)); - InspectionManagerEx inspectionManager = (InspectionManagerEx)InspectionManager.getInstance(getProject()); - final GlobalInspectionContextImpl globalContext = - inspectionManager.createNewGlobalContext(!(myProjectFixture instanceof LightIdeaTestFixture)); - globalContext.setCurrentScope(scope); scope.invalidate(); + InspectionManagerEx inspectionManager = (InspectionManagerEx)InspectionManager.getInstance(getProject()); + GlobalInspectionContextImpl globalContext = createGlobalContextForTool(scope, getProject(), inspectionManager, tool); + InspectionTestUtil.runTool(tool, scope, globalContext, inspectionManager); InspectionTestUtil.compareToolResults(tool, false, new File(getTestDataPath(), testDir).getPath()); } + public static GlobalInspectionContextImpl createGlobalContextForTool(AnalysisScope scope, + final Project project, + final InspectionManagerEx inspectionManager, + final InspectionTool... tools) { + final GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()){ + @Override + protected List getUsedTools() { + List result = new ArrayList(); + for (InspectionTool tool : tools) { + result.add(new ToolsImpl(tool, tool.getDefaultLevel(), true)); + } + return result; + } + + @Override + public boolean isToCheckMember(PsiElement element, InspectionProfileEntry tool) { + return true; + } + }; + globalContext.setCurrentScope(scope); + return globalContext; + } + @Override @Nullable public PsiReference getReferenceAtCaretPosition(final String... filePaths) { @@ -995,6 +1019,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(false); ensureIndexesUpToDate(getProject()); + ((StartupManagerImpl)StartupManagerEx.getInstanceEx(getProject())).runPostStartupActivities(); } @Override @@ -1472,7 +1497,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig final RangeMarker selEndMarker; static SelectionAndCaretMarkupLoader fromFile(String path, Project project) throws IOException { - return new SelectionAndCaretMarkupLoader(StringUtil.convertLineSeparators(new String(FileUtil.loadFileText(new File(path)))), + return new SelectionAndCaretMarkupLoader(StringUtil.convertLineSeparators(FileUtil.loadFile(new File(path))), project); } @@ -1685,7 +1710,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig private void testFoldingRegions(final String verificationFileName, boolean doCheckCollapseStatus) { String expectedContent; try { - expectedContent = new String(FileUtil.loadFileText(new File(verificationFileName))); + expectedContent = FileUtil.loadFile(new File(verificationFileName)); } catch (IOException e) { throw new RuntimeException(e); diff --git a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InconsistentResourceBundleInspection.java b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InconsistentResourceBundleInspection.java index 77c213c1d96f..e07fb22ad567 100644 --- a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InconsistentResourceBundleInspection.java +++ b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InconsistentResourceBundleInspection.java @@ -25,6 +25,7 @@ import com.intellij.lang.properties.ResourceBundle; import com.intellij.lang.properties.psi.PropertiesFile; import com.intellij.lang.properties.psi.Property; import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.Key; import com.intellij.psi.PsiFile; import com.intellij.util.containers.BidirectionalMap; import gnu.trove.THashMap; @@ -94,18 +95,27 @@ public class InconsistentResourceBundleInspection extends GlobalSimpleInspection } + private static final Key> VISITED_BUNDLES_KEY = Key.create("VISITED_BUNDLES_KEY"); + @Override + public void inspectionStarted(@NotNull InspectionManager manager, + @NotNull GlobalInspectionContext globalContext, + @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) { + globalContext.putUserData(VISITED_BUNDLES_KEY, new THashSet()); + } + @Override public void checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) { - checkFile(file, manager, new THashSet(), globalContext.getRefManager(), problemDescriptionsProcessor); + Set visitedBundles = globalContext.getUserData(VISITED_BUNDLES_KEY); + checkFile(file, manager, visitedBundles, globalContext.getRefManager(), problemDescriptionsProcessor); } private void checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, - final Set visitedBundles, + @NotNull Set visitedBundles, RefManager refManager, ProblemDescriptionsProcessor processor) { if (!(file instanceof PropertiesFile)) return; final PropertiesFile propertiesFile = (PropertiesFile)file;