From b3a13b76b1733b9c613a111906bd8f52e42e1cdf Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 18 Jul 2016 13:55:09 +0200 Subject: [PATCH] fix SingleInspectionProfilePanelTest --- .../daemon/HighlightSeverityTest.java | 7 +- .../SingleInspectionProfilePanelTest.kt | 15 ++-- .../ex/InspectionProfileTest.java | 17 ++-- .../codeInspection/ex/InspectionSchemeTest.kt | 3 +- .../ex/ProjectInspectionManagerTest.kt | 5 +- .../daemon/DaemonAnalyzerTestCase.java | 8 +- .../com/intellij/codeInspection/ex/Tools.java | 6 +- .../ex/GlobalInspectionContextBase.java | 16 ++-- .../ex/InspectionProfileImpl.java | 27 ++----- .../intellij/codeInspection/ex/ToolsImpl.java | 26 ++++--- .../InspectionsConfigTreeTable.java | 15 ++-- .../testFramework/LightPlatformTestCase.java | 66 +++------------- .../impl/CodeInsightTestFixtureImpl.java | 33 +++----- .../com/intellij/testFramework/inspections.kt | 77 +++++++++++++------ 14 files changed, 149 insertions(+), 172 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/HighlightSeverityTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/HighlightSeverityTest.java index b1a8368ec333..f70060746615 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/HighlightSeverityTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/HighlightSeverityTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -19,7 +19,6 @@ import com.intellij.codeHighlighting.HighlightDisplayLevel; import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.LocalInspectionToolSession; import com.intellij.codeInspection.ProblemsHolder; -import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; import com.intellij.psi.JavaElementVisitor; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiIdentifier; @@ -32,7 +31,7 @@ public class HighlightSeverityTest extends LightDaemonAnalyzerTestCase { public void testErrorLikeUnusedSymbol() throws Exception { - enableInspectionTool(new LocalInspectionToolWrapper(new LocalInspectionTool() { + enableInspectionTool(new LocalInspectionTool() { @NotNull @Override public String getShortName() { @@ -73,7 +72,7 @@ public class HighlightSeverityTest extends LightDaemonAnalyzerTestCase { public String getGroupDisplayName() { return getDisplayName(); } - })); + }); doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false); } } diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/SingleInspectionProfilePanelTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/SingleInspectionProfilePanelTest.kt index 37955a08bab8..0cc44a0c3217 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/SingleInspectionProfilePanelTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/SingleInspectionProfilePanelTest.kt @@ -23,8 +23,10 @@ import com.intellij.openapi.project.ProjectManager import com.intellij.profile.codeInspection.ProjectInspectionProfileManager import com.intellij.profile.codeInspection.ui.SingleInspectionProfilePanel import com.intellij.testFramework.LightIdeaTestCase +import com.intellij.testFramework.configureInspections import com.intellij.testFramework.createProfile import junit.framework.TestCase +import org.assertj.core.api.Assertions.assertThat class SingleInspectionProfilePanelTest : LightIdeaTestCase() { private val myInspection = JavaDocLocalInspection() @@ -32,11 +34,10 @@ class SingleInspectionProfilePanelTest : LightIdeaTestCase() { // see IDEA-85700 fun testSettingsModification() { val project = ProjectManager.getInstance().defaultProject - val profileManager = ProjectInspectionProfileManager.getInstanceImpl(project) - val profile = profileManager.currentProfile + val profile = configureInspections(arrayOf(myInspection), project, myTestRootDisposable) val model = profile.modifiableModel - val panel = SingleInspectionProfilePanel(profileManager, profile.name, model, profile) + val panel = SingleInspectionProfilePanel(ProjectInspectionProfileManager.getInstanceImpl(project), profile.name, model, profile) panel.isVisible = true panel.reset() @@ -45,9 +46,9 @@ class SingleInspectionProfilePanelTest : LightIdeaTestCase() { tool.myAdditionalJavadocTags = "foo" model.setModified(true) panel.apply() - TestCase.assertEquals(1, InspectionProfileTest.countInitializedTools(model)) + assertThat(InspectionProfileTest.countInitializedTools(model)).isEqualTo(1) - assertEquals("foo", getInspection(profile).myAdditionalJavadocTags) + assertThat(getInspection(profile).myAdditionalJavadocTags).isEqualTo("foo") panel.disposeUI() } @@ -73,7 +74,7 @@ class SingleInspectionProfilePanelTest : LightIdeaTestCase() { model.setModified(true) panel.apply() - TestCase.assertEquals(1, InspectionProfileTest.countInitializedTools(model)) + assertThat(InspectionProfileTest.countInitializedTools(model)).isEqualTo(1) assertEquals("bar", getInspection(profile).myAdditionalJavadocTags) panel.disposeUI() @@ -86,7 +87,7 @@ class SingleInspectionProfilePanelTest : LightIdeaTestCase() { profile.initInspectionTools(project) val originalTool = getInspection(profile) - TestCase.assertEquals("", originalTool.myAdditionalJavadocTags) + assertThat(originalTool.myAdditionalJavadocTags).isEmpty() val model = profile.modifiableModel val copyTool = getInspection(model) diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java index 821daec5d584..91a9bfa8556b 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java @@ -33,12 +33,14 @@ import com.intellij.profile.codeInspection.ui.header.InspectionToolsConfigurable import com.intellij.psi.PsiModifier; import com.intellij.testFramework.LightIdeaTestCase; import com.intellij.util.JdomKt; +import com.intellij.util.SmartList; import org.jdom.Element; import org.jdom.JDOMException; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static com.intellij.profile.ProfileEx.serializeProfile; @@ -574,21 +576,24 @@ public class InspectionProfileTest extends LightIdeaTestCase { assertEquals(test, JDOMUtil.writeElement(serializeProfile(foo))); } - public static int countInitializedTools(Profile foo) { - return getInitializedTools(foo).size(); + public static int countInitializedTools(@NotNull Profile foo) { + return getInitializedTools((InspectionProfileImpl)foo).size(); } @NotNull - public static List getInitializedTools(@NotNull Profile foo) { - List initialized = new ArrayList<>(); - List tools = ((InspectionProfileImpl)foo).getAllTools(getProject()); + public static List getInitializedTools(@NotNull InspectionProfileImpl foo) { + List initialized = null; + List tools = foo.getAllTools(getProject()); for (ScopeToolState tool : tools) { InspectionToolWrapper toolWrapper = tool.getTool(); if (toolWrapper.isInitialized()) { + if (initialized == null) { + initialized = new SmartList<>(); + } initialized.add(toolWrapper); } } - return initialized; + return initialized == null ? Collections.emptyList() : initialized; } private static LocalInspectionToolWrapper createTool(String s, boolean enabled) { diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionSchemeTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionSchemeTest.kt index cbeddb5ee4f5..1b4d285ea749 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionSchemeTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionSchemeTest.kt @@ -19,6 +19,7 @@ import com.intellij.configurationStore.SchemeManagerFactoryBase import com.intellij.openapi.application.ApplicationManager import com.intellij.testFramework.InMemoryFsRule import com.intellij.testFramework.ProjectRule +import com.intellij.testFramework.runInInitMode import com.intellij.util.readText import com.intellij.util.write import org.assertj.core.api.Assertions.assertThat @@ -56,7 +57,7 @@ internal class InspectionSchemeTest { val scheme = profileManager.profiles.first() as InspectionProfileImpl assertThat(scheme.name).isEqualTo("Bar") - InspectionProfileImpl.initAndDo { scheme.initInspectionTools(null) } + runInInitMode { scheme.initInspectionTools(null) } schemeManagerFactory.save() diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt index 45cfe67f86bb..d13b9b963419 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ex/ProjectInspectionManagerTest.kt @@ -27,6 +27,7 @@ import com.intellij.testFramework.Assertions.assertThat import com.intellij.testFramework.ProjectRule import com.intellij.testFramework.RuleChain import com.intellij.testFramework.TemporaryDirectory +import com.intellij.testFramework.runInInitMode import com.intellij.util.delete import com.intellij.util.readText import com.intellij.util.write @@ -106,7 +107,7 @@ internal class ProjectInspectionManagerTest { assertThat(projectInspectionProfileManager.state).isEmpty() // cause to use app profile - InspectionProfileImpl.initAndDo { + runInInitMode { val currentProfile = projectInspectionProfileManager.currentProfile assertThat(currentProfile.isProjectLevel).isTrue() currentProfile.disableTool("Convert2Diamond", project) @@ -137,7 +138,7 @@ internal class ProjectInspectionManagerTest { project.baseDir.refresh(false, true) (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm() - InspectionProfileImpl.initAndDo { + runInInitMode { assertThat(projectInspectionProfileManager.currentProfile.getToolDefaultState("Convert2Diamond", project).level).isEqualTo(HighlightDisplayLevel.ERROR) } } diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java index 38d3adf9efe9..be9cbb4348e7 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/DaemonAnalyzerTestCase.java @@ -29,7 +29,6 @@ import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.InspectionToolProvider; import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.ex.InspectionProfileImpl; -import com.intellij.codeInspection.ex.InspectionToolRegistrar; import com.intellij.ide.highlighter.JavaFileType; import com.intellij.ide.startup.StartupManagerEx; import com.intellij.ide.startup.impl.StartupManagerImpl; @@ -59,7 +58,10 @@ import com.intellij.psi.impl.PsiManagerEx; import com.intellij.psi.impl.search.IndexPatternBuilder; import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry; import com.intellij.psi.xml.XmlFileNSInfoProvider; -import com.intellij.testFramework.*; +import com.intellij.testFramework.ExpectedHighlightingData; +import com.intellij.testFramework.FileTreeAccessFilter; +import com.intellij.testFramework.HighlightTestInfo; +import com.intellij.testFramework.InspectionsKt; import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl; import com.intellij.util.IncorrectOperationException; import com.intellij.util.containers.ContainerUtil; @@ -131,7 +133,7 @@ public abstract class DaemonAnalyzerTestCase extends CodeInsightTestCase { } protected void enableInspectionTool(@NotNull InspectionProfileEntry tool) { - LightPlatformTestCase.enableInspectionTool(getProject(), InspectionToolRegistrar.wrapTool(tool), getTestRootDisposable()); + InspectionsKt.enableInspectionTool(getProject(), tool, getTestRootDisposable()); } protected void enableInspectionTools(@NotNull InspectionProfileEntry... tools) { diff --git a/platform/analysis-api/src/com/intellij/codeInspection/ex/Tools.java b/platform/analysis-api/src/com/intellij/codeInspection/ex/Tools.java index 8fde087c1b75..d1f43756d2ab 100644 --- a/platform/analysis-api/src/com/intellij/codeInspection/ex/Tools.java +++ b/platform/analysis-api/src/com/intellij/codeInspection/ex/Tools.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -28,7 +28,7 @@ import java.util.List; public interface Tools { @NotNull - InspectionToolWrapper getInspectionTool(PsiElement element); + InspectionToolWrapper getInspectionTool(@Nullable PsiElement element); @NotNull String getShortName(); @@ -39,6 +39,8 @@ public interface Tools { @NotNull List getTools(); + void collectTools(@NotNull List result); + @NotNull ScopeToolState getDefaultState(); diff --git a/platform/analysis-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextBase.java b/platform/analysis-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextBase.java index 5acac5537b17..3e3faf2f23be 100644 --- a/platform/analysis-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextBase.java +++ b/platform/analysis-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextBase.java @@ -69,16 +69,16 @@ public class GlobalInspectionContextBase extends UserDataHolderBase implements G private AnalysisScope myCurrentScope; @NotNull private final Project myProject; - private final List myJobDescriptors = new ArrayList(); + private final List myJobDescriptors = new ArrayList<>(); private final StdJobDescriptors myStdJobDescriptors = new StdJobDescriptors(); protected ProgressIndicator myProgressIndicator = new EmptyProgressIndicator(); private InspectionProfile myExternalProfile; - protected final Map myExtensions = new HashMap(); + protected final Map myExtensions = new HashMap<>(); - protected final Map myTools = new THashMap(); + protected final Map myTools = new THashMap<>(); @NonNls public static final String LOCAL_TOOL_ATTRIBUTE = "is_local_tool"; @@ -328,7 +328,7 @@ public class GlobalInspectionContextBase extends UserDataHolderBase implements G protected List getUsedTools() { InspectionProfileImpl profile = (InspectionProfileImpl)getCurrentProfile(); List tools = profile.getAllEnabledInspectionTools(myProject); - Set dependentTools = new LinkedHashSet(); + Set dependentTools = new LinkedHashSet<>(); for (Tools tool : tools) { profile.collectDependentInspections(tool.getTool(), dependentTools, getProject()); } @@ -336,9 +336,9 @@ public class GlobalInspectionContextBase extends UserDataHolderBase implements G if (dependentTools.isEmpty()) { return tools; } - Set set = new THashSet(tools, TOOLS_HASHING_STRATEGY); + Set set = new THashSet<>(tools, TOOLS_HASHING_STRATEGY); set.addAll(ContainerUtil.map(dependentTools, toolWrapper -> new ToolsImpl(toolWrapper, toolWrapper.getDefaultLevel(), true, true))); - return new ArrayList(set); + return new ArrayList<>(set); } private static void classifyTool(@NotNull List outGlobalTools, @@ -390,7 +390,7 @@ public class GlobalInspectionContextBase extends UserDataHolderBase implements G } public static void cleanupElements(@NotNull final Project project, @Nullable final Runnable runnable, @NotNull PsiElement... scope) { - final List> elements = new ArrayList>(); + final List> elements = new ArrayList<>(); final SmartPointerManager manager = SmartPointerManager.getInstance(project); for (PsiElement element : scope) { elements.add(manager.createSmartPsiElementPointer(element)); @@ -403,7 +403,7 @@ public class GlobalInspectionContextBase extends UserDataHolderBase implements G @Nullable final Runnable runnable, final List> elements) { Runnable cleanupRunnable = () -> { - final List psiElements = new ArrayList(); + final List psiElements = new ArrayList<>(); for (SmartPsiElementPointer element : elements) { PsiElement psiElement = element.getElement(); if (psiElement != null && psiElement.isPhysical()) { diff --git a/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java b/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java index 47e1292c03b5..d3649fa806b1 100644 --- a/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java +++ b/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionProfileImpl.java @@ -49,6 +49,7 @@ import com.intellij.util.xmlb.annotations.Attribute; import com.intellij.util.xmlb.annotations.Tag; import com.intellij.util.xmlb.annotations.Transient; import gnu.trove.THashMap; +import gnu.trove.THashSet; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -56,7 +57,6 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; import java.util.*; -import java.util.function.Supplier; /** * @author max @@ -147,10 +147,6 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, return toolWrappers; } }, InspectionProfileManager.getInstance()); - initAndDo(() -> { - profile.initInspectionTools(project); - return null; - }); for (InspectionToolWrapper toolWrapper : toolWrappers) { profile.enableTool(toolWrapper.getShortName(), project); } @@ -261,7 +257,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, @NotNull public Set getUsedSeverities() { LOG.assertTrue(myInitialized); - final Set result = new HashSet<>(); + Set result = new THashSet<>(); for (Tools tools : myTools.values()) { for (ScopeToolState state : tools.getTools()) { result.add(state.getLevel().getSeverity()); @@ -882,11 +878,12 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, } @NotNull - public List getAllTools(Project project) { + public List getAllTools(@Nullable Project project) { initInspectionTools(project); - final List result = new ArrayList<>(); + + List result = new ArrayList<>(); for (Tools tools : myTools.values()) { - result.addAll(tools.getTools()); + tools.collectTools(result); } return result; } @@ -1008,16 +1005,4 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel, private static class InspectionProfileImplHolder { private static final InspectionProfileImpl DEFAULT_PROFILE = new InspectionProfileImpl(DEFAULT_PROFILE_NAME); } - - @SuppressWarnings("TestOnlyProblems") - public static T initAndDo(@NotNull Supplier runnable) { - boolean old = INIT_INSPECTIONS; - try { - INIT_INSPECTIONS = true; - return runnable.get(); - } - finally { - INIT_INSPECTIONS = old; - } - } } diff --git a/platform/analysis-impl/src/com/intellij/codeInspection/ex/ToolsImpl.java b/platform/analysis-impl/src/com/intellij/codeInspection/ex/ToolsImpl.java index 3030364c01c0..c50406bd719d 100644 --- a/platform/analysis-impl/src/com/intellij/codeInspection/ex/ToolsImpl.java +++ b/platform/analysis-impl/src/com/intellij/codeInspection/ex/ToolsImpl.java @@ -90,7 +90,7 @@ public class ToolsImpl implements Tools { @NotNull private ScopeToolState insertTool(@NotNull final ScopeToolState scopeToolState, final int idx) { if (myTools == null) { - myTools = new ArrayList(); + myTools = new ArrayList<>(); if (scopeToolState.isEnabled()) { setEnabled(true); } @@ -132,7 +132,7 @@ public class ToolsImpl implements Tools { @NotNull public List getAllTools() { - List result = new ArrayList(); + List result = new ArrayList<>(); for (ScopeToolState state : getTools()) { result.add(state.getTool()); } @@ -179,7 +179,7 @@ public class ToolsImpl implements Tools { final InspectionToolWrapper toolWrapper = myDefaultState.getTool(); final List scopeElements = toolElement.getChildren(ProfileEx.SCOPE); - final List scopeNames = new ArrayList(); + final List scopeNames = new ArrayList<>(); for (Element scopeElement : scopeElements) { final String scopeName = scopeElement.getAttributeValue(ProfileEx.NAME); if (scopeName == null) { @@ -216,7 +216,7 @@ public class ToolsImpl implements Tools { String scopeName = scopeNames.get(i); List order = dependencies.get(scopeName); if (order == null) { - order = new ArrayList(); + order = new ArrayList<>(); dependencies.put(scopeName, order); } for (int j = i + 1; j < scopeNames.size(); j++) { @@ -242,12 +242,23 @@ public class ToolsImpl implements Tools { @Override @NotNull public List getTools() { - if (myTools == null) return Collections.singletonList(myDefaultState); - List result = new ArrayList(myTools); + if (myTools == null) { + return Collections.singletonList(myDefaultState); + } + + List result = new ArrayList<>(myTools); result.add(myDefaultState); return result; } + @Override + public void collectTools(@NotNull List result) { + if (myTools != null) { + result.addAll(myTools); + } + result.add(myDefaultState); + } + @Override @NotNull public ScopeToolState getDefaultState() { @@ -325,8 +336,6 @@ public class ToolsImpl implements Tools { return myDefaultState.getLevel(); } - - public HighlightDisplayLevel getLevel() { return myDefaultState.getLevel(); } @@ -406,7 +415,6 @@ public class ToolsImpl implements Tools { } } - public void disableTool(@NotNull PsiElement element) { final Project project = element.getProject(); final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(project); diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/inspectionsTree/InspectionsConfigTreeTable.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/inspectionsTree/InspectionsConfigTreeTable.java index f7a56134aad7..4d976f8565dc 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/inspectionsTree/InspectionsConfigTreeTable.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/ui/inspectionsTree/InspectionsConfigTreeTable.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -280,7 +280,8 @@ public class InspectionsConfigTreeTable extends TreeTable { final boolean enabled = state.isEnabled(); if (isPreviousEnabled == null) { isPreviousEnabled = enabled; - } else if (!isPreviousEnabled.equals(enabled)) { + } + else if (!isPreviousEnabled.equals(enabled)) { return null; } } @@ -313,8 +314,8 @@ public class InspectionsConfigTreeTable extends TreeTable { LOG.assertTrue(myTreeTable != null); Boolean state = null; - final HashSet tools = new HashSet(); - final List nodes = new ArrayList(); + final HashSet tools = new HashSet<>(); + final List nodes = new ArrayList<>(); for (TreePath selectionPath : myTreeTable.getTree().getSelectionPaths()) { final InspectionConfigTreeNode node = (InspectionConfigTreeNode)selectionPath.getLastPathComponent(); @@ -396,7 +397,7 @@ public class InspectionsConfigTreeTable extends TreeTable { private static class SeverityAndOccurrences { private HighlightSeverity myPrimarySeverity; - private final Map myOccurrences = new HashMap(); + private final Map myOccurrences = new HashMap<>(); public void setSeverityToMixed() { myPrimarySeverity = ScopesAndSeveritiesTable.MIXED_FAKE_SEVERITY; @@ -428,7 +429,7 @@ public class InspectionsConfigTreeTable extends TreeTable { private static class MultiColoredHighlightSeverityIconSink { - private final Map myScopeToAverageSeverityMap = new HashMap(); + private final Map myScopeToAverageSeverityMap = new HashMap<>(); private String myDefaultScopeName; @@ -456,7 +457,7 @@ public class InspectionsConfigTreeTable extends TreeTable { if (myScopeToAverageSeverityMap.isEmpty()) { return null; } - final Map result = new HashMap(); + final Map result = new HashMap<>(); final Map.Entry entry = ContainerUtil.getFirstItem(myScopeToAverageSeverityMap.entrySet()); result.put(entry.getKey(), entry.getValue().getPrimarySeverity()); if (myScopeToAverageSeverityMap.size() == 1) { diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java index 9a1e49e88f35..8e4e2f54452b 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformTestCase.java @@ -17,17 +17,11 @@ package com.intellij.testFramework; import com.intellij.ProjectTopics; import com.intellij.codeInsight.completion.CompletionProgressIndicator; -import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInsight.hint.HintManager; import com.intellij.codeInsight.hint.HintManagerImpl; import com.intellij.codeInsight.lookup.LookupManager; -import com.intellij.codeInspection.InspectionEP; import com.intellij.codeInspection.InspectionProfileEntry; -import com.intellij.codeInspection.LocalInspectionEP; import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.ex.InspectionProfileImpl; -import com.intellij.codeInspection.ex.InspectionToolRegistrar; -import com.intellij.codeInspection.ex.InspectionToolWrapper; import com.intellij.ide.highlighter.ProjectFileType; import com.intellij.ide.startup.StartupManagerEx; import com.intellij.ide.startup.impl.StartupManagerImpl; @@ -78,7 +72,6 @@ import com.intellij.openapi.vfs.impl.VirtualFilePointerManagerImpl; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl; import com.intellij.openapi.vfs.pointers.VirtualFilePointerManager; -import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileFactory; @@ -97,7 +90,6 @@ import com.intellij.util.indexing.UnindexedFilesUpdater; import com.intellij.util.lang.CompoundRuntimeException; 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; import org.jetbrains.annotations.NotNull; @@ -341,26 +333,11 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da ((FileTypeManagerImpl)FileTypeManager.getInstance()).drainReDetectQueue(); } - protected void enableInspectionTools(@NotNull Class[] classes) { - final InspectionProfileEntry[] tools = new InspectionProfileEntry[classes.length]; - - final List eps = ContainerUtil.newArrayList(); - ContainerUtil.addAll(eps, Extensions.getExtensions(LocalInspectionEP.LOCAL_INSPECTION)); - ContainerUtil.addAll(eps, Extensions.getExtensions(InspectionEP.GLOBAL_INSPECTION)); - - next: - for (int i = 0; i < classes.length; i++) { - for (InspectionEP ep : eps) { - if (classes[i].getName().equals(ep.implementationClass)) { - tools[i] = ep.instantiateTool(); - continue next; - } - } - throw new IllegalArgumentException("Unable to find extension point for " + classes[i].getName()); - } - - enableInspectionTools(tools); - } + //protected void enableInspectionTools(@NotNull Class[] classes) { + // for (InspectionProfileEntry tool : InspectionTestUtil.instantiateTools(Arrays.asList(classes))) { + // enableInspectionTool(tool); + // } + //} protected void enableInspectionTools(@NotNull InspectionProfileEntry... tools) { for (InspectionProfileEntry tool : tools) { @@ -368,37 +345,12 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da } } - protected void enableInspectionTool(@NotNull InspectionToolWrapper toolWrapper) { - enableInspectionTool(getProject(), toolWrapper, myTestRootDisposable); - } + //protected void enableInspectionTool(@NotNull InspectionToolWrapper toolWrapper) { + // InspectionsKt.enableInspectionTool(getProject(), toolWrapper, myTestRootDisposable); + //} protected void enableInspectionTool(@NotNull InspectionProfileEntry tool) { - enableInspectionTool(getProject(), InspectionToolRegistrar.wrapTool(tool), myTestRootDisposable); - } - - public static void enableInspectionTool(@NotNull Project project, @NotNull InspectionToolWrapper toolWrapper, @NotNull Disposable disposable) { - InspectionProfileImpl profile = (InspectionProfileImpl)InspectionProjectProfileManager.getInstance(project).getCurrentProfile(); - final String shortName = toolWrapper.getShortName(); - final HighlightDisplayKey key = HighlightDisplayKey.find(shortName); - if (key == null) { - HighlightDisplayKey.register(shortName, toolWrapper.getDisplayName(), toolWrapper.getID()); - } - - InspectionProfileImpl.initAndDo(() -> { - InspectionToolWrapper existingWrapper = profile.getInspectionTool(shortName, project); - if (existingWrapper == null || existingWrapper.isInitialized() != toolWrapper.isInitialized() || toolWrapper.isInitialized() && toolWrapper.getTool() != existingWrapper.getTool()) { - profile.addTool(project, toolWrapper, new THashMap<>()); - } - profile.enableTool(shortName, project); - - Disposer.register(disposable, new Disposable() { - @Override - public void dispose() { - profile.disableTool(shortName, project); - } - }); - return null; - }); + InspectionsKt.enableInspectionTool(getProject(), tool, myTestRootDisposable); } @NotNull 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 08acf7e9c441..acfb53e089ec 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -38,7 +38,6 @@ import com.intellij.codeInspection.InspectionProfileEntry; import com.intellij.codeInspection.InspectionToolProvider; import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.ex.InspectionProfileImpl; -import com.intellij.codeInspection.ex.InspectionToolRegistrar; import com.intellij.codeInspection.ex.InspectionToolWrapper; import com.intellij.facet.Facet; import com.intellij.facet.FacetManager; @@ -338,7 +337,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig public void enableInspections(@NotNull InspectionProfileEntry... inspections) { assertInitialized(); for (InspectionProfileEntry inspection : inspections) { - LightPlatformTestCase.enableInspectionTool(getProject(), InspectionToolRegistrar.wrapTool(inspection), getTestRootDisposable()); + InspectionsKt.enableInspectionTool(getProject(), inspection, getTestRootDisposable()); } } @@ -349,10 +348,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig @Override public void enableInspections(@NotNull Collection> inspections) { - List tools = new SmartList<>(); - for (Class clazz : inspections) { - tools.add(ReflectionUtil.newInstance(clazz)); - } + List tools = InspectionTestUtil.instantiateTools(inspections); enableInspections(tools.toArray(new LocalInspectionTool[tools.size()])); } @@ -365,24 +361,15 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig } @Override - public void enableInspections(@NotNull InspectionToolProvider... providers) { - List tools = new ArrayList<>(); - for (InspectionToolProvider provider : providers) { - for (Class clazz : provider.getInspectionClasses()) { - try { - Object o = clazz.getConstructor().newInstance(); - if (o instanceof LocalInspectionTool) { - LocalInspectionTool inspection = (LocalInspectionTool)o; - tools.add(inspection); - } - } - catch (Exception e) { - throw new RuntimeException("Cannot instantiate " + clazz, e); - } - } + public void enableInspections(@NotNull InspectionToolProvider... providers) { + List> classes = JBIterable.of(providers) + .flatten((o) -> Arrays.asList(o.getInspectionClasses())) + .transform((Function>)o -> + LocalInspectionTool.class.isAssignableFrom(o) ? (Class) o : null) + .filter(Conditions.notNull()) + .toList(); + enableInspections(classes); } - enableInspections(tools.toArray(new LocalInspectionTool[tools.size()])); - } @Override public long testHighlighting(final boolean checkWarnings, diff --git a/platform/testFramework/src/com/intellij/testFramework/inspections.kt b/platform/testFramework/src/com/intellij/testFramework/inspections.kt index a19db2c5340d..e743f84534ce 100644 --- a/platform/testFramework/src/com/intellij/testFramework/inspections.kt +++ b/platform/testFramework/src/com/intellij/testFramework/inspections.kt @@ -16,6 +16,7 @@ package com.intellij.testFramework import com.intellij.analysis.AnalysisScope +import com.intellij.codeInsight.daemon.HighlightDisplayKey import com.intellij.codeInspection.InspectionProfileEntry import com.intellij.codeInspection.LocalInspectionTool import com.intellij.codeInspection.ex.* @@ -25,6 +26,7 @@ import com.intellij.openapi.util.Disposer import com.intellij.profile.codeInspection.ProjectInspectionProfileManager import com.intellij.testFramework.fixtures.impl.GlobalInspectionContextForTests import com.intellij.util.ReflectionUtil +import gnu.trove.THashMap import org.jetbrains.annotations.TestOnly import java.util.* @@ -32,24 +34,19 @@ fun configureInspections(tools: Array, project: Project, parentDisposable: Disposable): InspectionProfileImpl { val profile = InspectionProfileImpl.createSimple(UUID.randomUUID().toString(), project, tools.map { InspectionToolRegistrar.wrapTool(it) }) -// profile.disableToolByDefault(disabledInspections, project) - val profileManager = ProjectInspectionProfileManager.getInstanceImpl(project) - - val oldRootProfile = profileManager.currentProfile + // we don't restore old project profile because in tests it must be in any case null - app default profile Disposer.register(parentDisposable, Disposable { profileManager.deleteProfile(profile) - profileManager.setCurrentProfile(oldRootProfile) - clearAllToolsIn(InspectionProfileImpl.getDefaultProfile(), project) + profileManager.setCurrentProfile(null) + clearAllToolsIn(InspectionProfileImpl.getDefaultProfile()) }) - profileManager.setRootProfile(profile.name) - InspectionProfileImpl.initAndDo({ - profileManager.addProfile(profile) - profile.initInspectionTools(project) - profileManager.setCurrentProfile(profile) - null - }) + runInInitMode { + profileManager.addProfile(profile) + profile.initInspectionTools(project) + profileManager.setCurrentProfile(profile) + } return profile } @@ -58,23 +55,27 @@ fun configureInspections(tools: Array, fun createGlobalContextForTool(scope: AnalysisScope, project: Project, toolWrappers: List> = emptyList()): GlobalInspectionContextForTests { - val profile = InspectionProfileImpl.createSimple("test", project, toolWrappers) - val context = object : GlobalInspectionContextForTests(project, (InspectionManagerEx.getInstance(project) as InspectionManagerEx).contentManager) { - override fun getUsedTools(): List { - return InspectionProfileImpl.initAndDo { + runInInitMode { + val profile = InspectionProfileImpl.createSimple("test", project, toolWrappers) + val context = object : GlobalInspectionContextForTests(project, (InspectionManagerEx.getInstance(project) as InspectionManagerEx).contentManager) { + override fun getUsedTools(): List { for (tool in toolWrappers) { profile.enableTool(tool.shortName, project) } - profile.getAllEnabledInspectionTools(project) + return profile.getAllEnabledInspectionTools(project) } } + context.currentScope = scope + return context } - context.currentScope = scope - return context } -private fun clearAllToolsIn(profile: InspectionProfileImpl, project: Project) { - for (state in profile.getAllTools(project)) { +private fun clearAllToolsIn(profile: InspectionProfileImpl) { + if (!profile.wasInitialized()) { + return + } + + for (state in profile.getAllTools(null)) { val wrapper = state.tool if (wrapper.extension != null) { // make it not initialized @@ -85,4 +86,36 @@ private fun clearAllToolsIn(profile: InspectionProfileImpl, project: Project) { fun ProjectInspectionProfileManager.createProfile(localInspectionTool: LocalInspectionTool, disposable: Disposable): InspectionProfileImpl { return configureInspections(arrayOf(localInspectionTool), project, disposable) +} + +fun enableInspectionTool(project: Project, tool: InspectionProfileEntry, disposable: Disposable) = enableInspectionTool(project, InspectionToolRegistrar.wrapTool(tool), disposable) + +private fun enableInspectionTool(project: Project, toolWrapper: InspectionToolWrapper<*, *>, disposable: Disposable) { + val profile = ProjectInspectionProfileManager.getInstanceImpl(project).currentProfile + val shortName = toolWrapper.shortName + val key = HighlightDisplayKey.find(shortName) + if (key == null) { + HighlightDisplayKey.register(shortName, toolWrapper.displayName, toolWrapper.id) + } + + runInInitMode { + val existingWrapper = profile.getInspectionTool(shortName, project) + if (existingWrapper == null || existingWrapper.isInitialized != toolWrapper.isInitialized || toolWrapper.isInitialized && toolWrapper.tool !== existingWrapper.tool) { + profile.addTool(project, toolWrapper, THashMap>()) + } + profile.enableTool(shortName, project) + + Disposer.register(disposable, Disposable { profile.disableTool(shortName, project) }) + } +} + +inline fun runInInitMode(runnable: () -> T): T { + val old = InspectionProfileImpl.INIT_INSPECTIONS + try { + InspectionProfileImpl.INIT_INSPECTIONS = true + return runnable() + } + finally { + InspectionProfileImpl.INIT_INSPECTIONS = old + } } \ No newline at end of file