2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Created by IntelliJ IDEA.
22 * To change template for new class use
23 * Code Style | Class Templates options (Tools | IDE Options).
25 package com
.intellij
.testFramework
;
27 import com
.intellij
.analysis
.AnalysisScope
;
28 import com
.intellij
.codeInspection
.GlobalInspectionTool
;
29 import com
.intellij
.codeInspection
.InspectionManager
;
30 import com
.intellij
.codeInspection
.LocalInspectionTool
;
31 import com
.intellij
.codeInspection
.deadCode
.UnusedDeclarationInspection
;
32 import com
.intellij
.codeInspection
.ex
.*;
33 import com
.intellij
.openapi
.application
.ApplicationManager
;
34 import com
.intellij
.openapi
.application
.ex
.PathManagerEx
;
35 import com
.intellij
.openapi
.diagnostic
.Logger
;
36 import com
.intellij
.openapi
.projectRoots
.Sdk
;
37 import com
.intellij
.openapi
.projectRoots
.impl
.JavaSdkImpl
;
38 import com
.intellij
.openapi
.roots
.ContentEntry
;
39 import com
.intellij
.openapi
.roots
.LanguageLevelProjectExtension
;
40 import com
.intellij
.openapi
.roots
.ModifiableRootModel
;
41 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
42 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
43 import com
.intellij
.openapi
.vfs
.VirtualFile
;
44 import com
.intellij
.pom
.java
.LanguageLevel
;
45 import com
.intellij
.psi
.PsiManager
;
46 import org
.jetbrains
.annotations
.NonNls
;
50 @SuppressWarnings({"HardCodedStringLiteral"})
51 public abstract class InspectionTestCase
extends PsiTestCase
{
52 private static final Logger LOG
= Logger
.getInstance("#com.intellij.testFramework.InspectionTestCase");
54 public InspectionManagerEx
getManager() {
55 return (InspectionManagerEx
) InspectionManager
.getInstance(myProject
);
58 public void doTest(@NonNls String folderName
, LocalInspectionTool tool
) throws Exception
{
59 doTest(folderName
, new LocalInspectionToolWrapper(tool
));
61 public void doTest(@NonNls String folderName
, GlobalInspectionTool tool
) throws Exception
{
62 doTest(folderName
, new GlobalInspectionToolWrapper(tool
));
64 public void doTest(@NonNls String folderName
, GlobalInspectionTool tool
, boolean checkRange
) throws Exception
{
65 doTest(folderName
, new GlobalInspectionToolWrapper(tool
), checkRange
);
68 public void doTest(@NonNls String folderName
, GlobalInspectionTool tool
, boolean checkRange
, boolean runDeadCodeFirst
) throws Exception
{
69 doTest(folderName
, new GlobalInspectionToolWrapper(tool
), "java 1.4", checkRange
, runDeadCodeFirst
);
72 public void doTest(@NonNls String folderName
, InspectionTool tool
) throws Exception
{
73 doTest(folderName
, tool
, "java 1.4");
76 public void doTest(@NonNls String folderName
, InspectionTool tool
, final boolean checkRange
) throws Exception
{
77 doTest(folderName
, tool
, "java 1.4", checkRange
);
80 public void doTest(@NonNls String folderName
, InspectionTool tool
, @NonNls final String jdkName
) throws Exception
{
81 doTest(folderName
, tool
, jdkName
, false);
84 public void doTest(@NonNls String folderName
, InspectionTool tool
, @NonNls final String jdkName
, boolean checkRange
) throws Exception
{
85 doTest(folderName
, tool
, jdkName
, checkRange
, false);
88 public void doTest(@NonNls String folderName
, InspectionTool tool
, @NonNls final String jdkName
, boolean checkRange
, boolean runDeadCodeFirst
) throws Exception
{
89 final String testDir
= getTestDataPath() + "/" + folderName
;
90 runTool(testDir
, jdkName
, tool
, runDeadCodeFirst
);
92 InspectionTestUtil
.compareToolResults(tool
, checkRange
, testDir
);
95 protected void runTool(@NonNls final String testDir
, @NonNls final String jdkName
, final InspectionTool tool
) {
96 runTool(testDir
, jdkName
, tool
, false);
99 protected void runTool(final String testDir
, final String jdkName
, final InspectionTool tool
, boolean runDeadCodeFirst
) {
100 final VirtualFile
[] sourceDir
= new VirtualFile
[1];
101 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
104 setupRootModel(testDir
, sourceDir
, jdkName
);
105 } catch (Exception e
) {
110 AnalysisScope scope
= createAnalysisScope(sourceDir
[0]);
112 InspectionManagerEx inspectionManager
= (InspectionManagerEx
) InspectionManager
.getInstance(myProject
);
113 final GlobalInspectionContextImpl globalContext
= inspectionManager
.createNewGlobalContext(true);
114 globalContext
.setCurrentScope(scope
);
116 if (runDeadCodeFirst
) {
117 runTool(new UnusedDeclarationInspection(), scope
, globalContext
, inspectionManager
);
119 runTool(tool
, scope
, globalContext
, inspectionManager
);
122 protected AnalysisScope
createAnalysisScope(VirtualFile sourceDir
) {
123 PsiManager psiManager
= PsiManager
.getInstance(myProject
);
124 return new AnalysisScope(psiManager
.findDirectory(sourceDir
));
127 private static void runTool(final InspectionTool tool
,
128 final AnalysisScope scope
,
129 final GlobalInspectionContextImpl globalContext
,
130 final InspectionManagerEx inspectionManager
) {
131 InspectionTestUtil
.runTool(tool
, scope
, globalContext
, inspectionManager
);
133 final GlobalJavaInspectionContextImpl javaInspectionContext
=
134 (GlobalJavaInspectionContextImpl
)globalContext
.getExtension(GlobalJavaInspectionContextImpl
.CONTEXT
);
135 if (javaInspectionContext
!= null) {
137 javaInspectionContext
.processSearchRequests(globalContext
);
138 } while (tool
.queryExternalUsagesRequests(inspectionManager
));
142 protected void setupRootModel(final String testDir
, final VirtualFile
[] sourceDir
, final String sdkName
) {
143 VirtualFile projectDir
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(new File(testDir
));
144 assertNotNull("could not find project dir " + testDir
, projectDir
);
145 sourceDir
[0] = projectDir
.findChild("src");
146 if (sourceDir
[0] == null) {
147 sourceDir
[0] = projectDir
;
149 VirtualFile ext_src
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(new File(testDir
+ "/ext_src"));
150 final ModuleRootManager rootManager
= ModuleRootManager
.getInstance(myModule
);
151 final ModifiableRootModel rootModel
= rootManager
.getModifiableModel();
153 // configure source and output path
154 final ContentEntry contentEntry
= rootModel
.addContentEntry(projectDir
);
155 contentEntry
.addSourceFolder(sourceDir
[0], false);
156 if (ext_src
!= null) {
157 contentEntry
.addSourceFolder(ext_src
, false);
160 // IMPORTANT! The jdk must be obtained in a way it is obtained in the normal program!
161 //ProjectJdkEx jdk = ProjectJdkTable.getInstance().getInternalJdk();
163 rootModel
.setSdk(getTestProjectSdk(sdkName
));
168 protected Sdk
getTestProjectSdk(String sdkName
) {
170 if ("java 1.5".equals(sdkName
)) {
171 sdk
= JavaSdkImpl
.getMockJdk15(sdkName
);
172 LanguageLevelProjectExtension
.getInstance(getProject()).setLanguageLevel(LanguageLevel
.JDK_1_5
);
175 sdk
= JavaSdkImpl
.getMockJdk(sdkName
);
181 protected String
getTestDataPath() {
182 return PathManagerEx
.getTestDataPath()+"/inspection/";