real GlobalInspectionContext for tests

This commit is contained in:
Dmitry Avdeev
2012-02-17 10:10:45 +04:00
parent 1d5b82afd7
commit df6da2d822
10 changed files with 73 additions and 93 deletions
@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems />
@@ -1,6 +0,0 @@
public class Foo {
public void emptyMethod() {
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 2000-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.codeInspection.ex.InspectionTool;
import com.intellij.profile.codeInspection.InspectionProfileManager;
import com.intellij.testFramework.IdeaTestCase;
import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase;
/**
* @author Dmitry Avdeev
* Date: 2/15/12
*/
public class GlobalInspectionsTest extends CodeInsightFixtureTestCase {
public void testUnusedDeclarationInspection() throws Exception {
InspectionProfileImpl profile = (InspectionProfileImpl)InspectionProfileManager.getInstance().createProfile();
try {
InspectionProfileImpl.INIT_INSPECTIONS = true;
profile.initInspectionTools(getProject());
}
finally {
InspectionProfileImpl.INIT_INSPECTIONS = false;
}
InspectionProfileEntry tool = profile.getInspectionTool(new UnusedDeclarationInspection().getShortName());
myFixture.testInspection("unusedDeclaration", (InspectionTool)tool);
}
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/global/";
}
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public GlobalInspectionsTest() {
IdeaTestCase.initPlatformPrefix();
}
}
@@ -1,35 +1,41 @@
package com.intellij.codeInspection;
import com.intellij.codeInspection.ex.GlobalInspectionToolWrapper;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.codeInspection.ex.InspectionToolRegistrar;
import com.intellij.codeInspection.emptyMethod.EmptyMethodInspection;
import com.intellij.codeInspection.ex.*;
import com.intellij.codeInspection.i18n.I18nInspection;
import com.intellij.psi.PsiElement;
import com.intellij.testFramework.InspectionTestCase;
public class RedundantSuppressTest extends InspectionTestCase {
private GlobalInspectionToolWrapper myWrapper;
private InspectionTool[] myInspectionTools;
@Override
protected void setUp() throws Exception {
super.setUp();
InspectionToolRegistrar.getInstance().ensureInitialized();
myWrapper = new GlobalInspectionToolWrapper(new RedundantSuppressInspection());
myInspectionTools = new InspectionTool[]{new LocalInspectionToolWrapper(new I18nInspection()),
new GlobalInspectionToolWrapper(new EmptyMethodInspection())};
myWrapper = new GlobalInspectionToolWrapper(new RedundantSuppressInspection() {
@Override
protected InspectionTool[] getInspectionTools(PsiElement psiElement, InspectionManager manager) {
return myInspectionTools;
}
});
}
public void testDefaultFile() throws Exception {
InspectionProfileImpl.INIT_INSPECTIONS = true;
doTest();
InspectionProfileImpl.INIT_INSPECTIONS = false;
}
public void testSuppressAll() throws Exception {
InspectionProfileImpl.INIT_INSPECTIONS = true;
try {
((RedundantSuppressInspection)myWrapper.getTool()).IGNORE_ALL = true;
doTest();
}
finally {
((RedundantSuppressInspection)myWrapper.getTool()).IGNORE_ALL = false;
InspectionProfileImpl.INIT_INSPECTIONS = false;
}
}
@@ -43,6 +43,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl;
import com.intellij.util.ArrayUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -94,18 +95,27 @@ public abstract class InspectionTestCase extends PsiTestCase {
doTest(folderName, tool, jdkName, checkRange, false);
}
public void doTest(@NonNls String folderName, InspectionTool tool, @NonNls final String jdkName, boolean checkRange, boolean runDeadCodeFirst) throws Exception {
public void doTest(@NonNls String folderName,
InspectionTool tool,
@NonNls final String jdkName,
boolean checkRange,
boolean runDeadCodeFirst,
InspectionTool... additional) throws Exception {
final String testDir = getTestDataPath() + "/" + folderName;
runTool(testDir, jdkName, tool, runDeadCodeFirst);
runTool(testDir, jdkName, runDeadCodeFirst, tool, additional);
InspectionTestUtil.compareToolResults(tool, checkRange, testDir);
}
protected void runTool(@NonNls final String testDir, @NonNls final String jdkName, final InspectionTool tool) {
runTool(testDir, jdkName, tool, false);
runTool(testDir, jdkName, false, tool);
}
protected void runTool(final String testDir, final String jdkName, final InspectionTool tool, boolean runDeadCodeFirst) {
protected void runTool(final String testDir,
final String jdkName,
boolean runDeadCodeFirst,
final InspectionTool tool,
InspectionTool... additional) {
final VirtualFile[] sourceDir = new VirtualFile[1];
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
@@ -121,6 +131,7 @@ public abstract class InspectionTestCase extends PsiTestCase {
InspectionManagerEx inspectionManager = (InspectionManagerEx) InspectionManager.getInstance(getProject());
InspectionTool[] tools = runDeadCodeFirst ? new InspectionTool[]{new UnusedDeclarationInspection(), tool} : new InspectionTool[]{tool};
tools = ArrayUtil.mergeArrays(tools, additional);
final GlobalInspectionContextImpl globalContext = CodeInsightTestFixtureImpl.createGlobalContextForTool(scope, getProject(), inspectionManager, tools);
InspectionTestUtil.runTool(tool, scope, globalContext, inspectionManager);
@@ -40,6 +40,7 @@ public class CommonInspectionToolWrapper extends InspectionToolWrapper<Inspectio
super(ep, tool);
}
@Override
public CommonInspectionToolWrapper createCopy(InspectionToolWrapper<InspectionTool, InspectionEP> from) {
return new CommonInspectionToolWrapper(from.myEP, from.myTool);
@@ -102,4 +103,11 @@ public class CommonInspectionToolWrapper extends InspectionToolWrapper<Inspectio
boolean showStructure) {
return getTool().createToolNode(provider, parentNode, showStructure);
}
@Override
public boolean queryExternalUsagesRequests(InspectionManager manager) {
return getTool().queryExternalUsagesRequests(manager);
}
}
@@ -168,7 +168,7 @@ public class GlobalInspectionContextImpl extends UserDataHolderBase implements G
for (ScopeToolState state : tools.getTools()) {
final NamedScope namedScope = state.getScope(project);
if (namedScope == null || namedScope.getValue().contains(file, getCurrentProfile().getProfileManager().getScopesManager())) {
return state.isEnabled() && ((GlobalInspectionToolWrapper)state.getTool()).getTool() == tool;
return state.isEnabled() && ((InspectionToolWrapper)state.getTool()).getTool() == tool;
}
}
}
@@ -458,7 +458,7 @@ public class InspectionProfileImpl extends ProfileEx implements ModifiableModel,
catch (ProcessCanceledException e) {
return false;
}
for (InspectionTool tool : tools) {
for (InspectionToolWrapper tool : tools) {
final String shortName = tool.getShortName();
HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
if (key == null) {
@@ -101,13 +101,7 @@ public class InspectionToolRegistrar {
for (final InspectionProfileEntry profileEntry : factory.createTools()) {
myInspectionToolFactories.add(new Factory<InspectionToolWrapper>() {
public InspectionToolWrapper create() {
if (profileEntry instanceof LocalInspectionTool) {
return new LocalInspectionToolWrapper((LocalInspectionTool)profileEntry);
}
else if (profileEntry instanceof GlobalInspectionTool) {
return new GlobalInspectionToolWrapper((GlobalInspectionTool)profileEntry);
}
return new CommonInspectionToolWrapper((InspectionTool)profileEntry);
return wrapTool(profileEntry);
}
});
}
@@ -115,6 +109,16 @@ public class InspectionToolRegistrar {
}
}
protected static InspectionToolWrapper wrapTool(InspectionProfileEntry profileEntry) {
if (profileEntry instanceof LocalInspectionTool) {
return new LocalInspectionToolWrapper((LocalInspectionTool)profileEntry);
}
else if (profileEntry instanceof GlobalInspectionTool) {
return new GlobalInspectionToolWrapper((GlobalInspectionTool)profileEntry);
}
return new CommonInspectionToolWrapper((InspectionTool)profileEntry);
}
public void registerTools(final InspectionToolProvider[] providers) {
for (InspectionToolProvider provider : providers) {
// System.out.println("***** " + provider.getClass().getName());
@@ -449,19 +449,32 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
final Project project,
final InspectionManagerEx inspectionManager,
final InspectionTool... tools) {
final GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()){
InspectionToolRegistrar registrar = new InspectionToolRegistrar(null) {
@Override
public List<InspectionToolWrapper> createTools() {
return ContainerUtil.map(tools, new Function<InspectionTool, InspectionToolWrapper>() {
@Override
public InspectionToolWrapper fun(InspectionTool tool) {
return tool instanceof InspectionToolWrapper ? (InspectionToolWrapper)tool : wrapTool(tool);
}
});
}
};
final InspectionProfileImpl profile = new InspectionProfileImpl("test", registrar, InspectionProfileManager.getInstance());
GlobalInspectionContextImpl globalContext = new GlobalInspectionContextImpl(project, inspectionManager.getContentManager()) {
@Override
protected List<ToolsImpl> getUsedTools() {
List<ToolsImpl> result = new ArrayList<ToolsImpl>();
for (InspectionTool tool : tools) {
result.add(new ToolsImpl(tool, tool.getDefaultLevel(), true));
try {
InspectionProfileImpl.INIT_INSPECTIONS = true;
for (InspectionTool tool : tools) {
profile.enableTool(tool.getShortName());
}
return profile.getAllEnabledInspectionTools(project);
}
finally {
InspectionProfileImpl.INIT_INSPECTIONS = false;
}
return result;
}
@Override
public boolean isToCheckMember(PsiElement element, InspectionProfileEntry tool) {
return true;
}
};
globalContext.setCurrentScope(scope);