fix test data
[idea/community.git] / java / java-tests / testSrc / com / intellij / codeInspection / InspectionProfileTest.java
1 /*
2  * Copyright 2000-2011 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.codeInspection;
17
18 import com.intellij.codeInsight.daemon.HighlightDisplayKey;
19 import com.intellij.codeInspection.dataFlow.DataFlowInspection;
20 import com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase;
21 import com.intellij.codeInspection.ex.*;
22 import com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase;
23 import com.intellij.openapi.util.JDOMUtil;
24 import com.intellij.openapi.util.WriteExternalException;
25 import com.intellij.profile.Profile;
26 import com.intellij.profile.codeInspection.InspectionProfileManager;
27 import com.intellij.testFramework.LightIdeaTestCase;
28 import org.jdom.Element;
29 import org.jdom.JDOMException;
30 import org.jetbrains.annotations.NotNull;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import static com.intellij.testFramework.PlatformTestUtil.assertElementsEqual;
37
38 /**
39  * @author Anna.Kozlova
40  * Date: 18-Aug-2006
41  */
42 public class InspectionProfileTest extends LightIdeaTestCase {
43   private static final String PROFILE = "ToConvert";
44
45   @Override
46   protected void setUp() throws Exception {
47     //noinspection AssignmentToStaticFieldFromInstanceMethod
48     InspectionProfileImpl.INIT_INSPECTIONS = true;
49     super.setUp();
50     InspectionToolRegistrar.getInstance().ensureInitialized();
51   }
52
53   @Override
54   protected void tearDown() throws Exception {
55     super.tearDown();
56     //noinspection AssignmentToStaticFieldFromInstanceMethod
57     InspectionProfileImpl.INIT_INSPECTIONS = false;
58     InspectionProfileManager.getInstance().deleteProfile(PROFILE);
59   }
60
61   public void testCopyProjectProfile() throws Exception {
62     final Element element = loadProfile();
63     final InspectionProfileImpl profile = createProfile();
64     profile.readExternal(element);
65     final ModifiableModel model = profile.getModifiableModel();
66     model.commit();
67     final Element copy = new Element("inspections");
68     profile.writeExternal(copy);
69     assertElementsEqual(element, copy);
70   }
71
72   private static InspectionProfileImpl createProfile() {
73     InspectionProfileImpl profile = new InspectionProfileImpl(PROFILE);
74     profile.setBaseProfile(InspectionProfileImpl.getDefaultProfile());
75     return profile;
76   }
77
78   public void testConvertOldProfile() throws Exception {
79     Element element = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
80                                             "  <option name=\"myName\" value=\"ToConvert\" />\n" +
81                                             "  <inspection_tool class=\"JavaDoc\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
82                                             "    <option name=\"TOP_LEVEL_CLASS_OPTIONS\">\n" +
83                                             "      <value>\n" +
84                                             "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
85                                             "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
86                                             "      </value>\n" +
87                                             "    </option>\n" +
88                                             "    <option name=\"INNER_CLASS_OPTIONS\">\n" +
89                                             "      <value>\n" +
90                                             "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
91                                             "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
92                                             "      </value>\n" +
93                                             "    </option>\n" +
94                                             "    <option name=\"METHOD_OPTIONS\">\n" +
95                                             "      <value>\n" +
96                                             "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
97                                             "        <option name=\"REQUIRED_TAGS\" value=\"@return@param@throws or @exception\" />\n" +
98                                             "      </value>\n" + "    </option>\n" +
99                                             "    <option name=\"FIELD_OPTIONS\">\n" +
100                                             "      <value>\n" +
101                                             "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
102                                             "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
103                                             "      </value>\n" +
104                                             "    </option>\n" +
105                                             "    <option name=\"IGNORE_DEPRECATED\" value=\"false\" />\n" +
106                                             "    <option name=\"IGNORE_JAVADOC_PERIOD\" value=\"false\" />\n" +
107                                             "    <option name=\"IGNORE_DUPLICATED_THROWS\" value=\"false\" />\n" +
108                                             "    <option name=\"IGNORE_POINT_TO_ITSELF\" value=\"false\" />\n" +
109                                             "    <option name=\"myAdditionalJavadocTags\" value=\"tag1,tag2 \" />\n" +
110                                             "  </inspection_tool>\n" +
111                                             "</inspections>").getRootElement();
112     InspectionProfileImpl profile = createProfile();
113     profile.readExternal(element);
114     ModifiableModel model = profile.getModifiableModel();
115     model.commit();
116
117     Element copy = new Element("inspections");
118     profile.writeExternal(copy);
119     assertElementsEqual(loadProfile(), copy);
120   }
121
122   private static Element loadProfile() throws IOException, JDOMException {
123     return JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
124                                  "  <option name=\"myName\" value=\"ToConvert\" />\n" +
125                                  "  <inspection_tool class=\"JavaDoc\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
126                                  "    <option name=\"TOP_LEVEL_CLASS_OPTIONS\">\n" +
127                                  "      <value>\n" +
128                                  "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
129                                  "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
130                                  "      </value>\n" +
131                                  "    </option>\n" +
132                                  "    <option name=\"INNER_CLASS_OPTIONS\">\n" +
133                                  "      <value>\n" +
134                                  "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
135                                  "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
136                                  "      </value>\n" +
137                                  "    </option>\n" +
138                                  "    <option name=\"METHOD_OPTIONS\">\n" +
139                                  "      <value>\n" +
140                                  "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
141                                  "        <option name=\"REQUIRED_TAGS\" value=\"@return@param@throws or @exception\" />\n" +
142                                  "      </value>\n" + "    </option>\n" +
143                                  "    <option name=\"FIELD_OPTIONS\">\n" +
144                                  "      <value>\n" +
145                                  "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
146                                  "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
147                                  "      </value>\n" +
148                                  "    </option>\n" +
149                                  "    <option name=\"IGNORE_DEPRECATED\" value=\"false\" />\n" +
150                                  "    <option name=\"IGNORE_JAVADOC_PERIOD\" value=\"false\" />\n" +
151                                  "    <option name=\"IGNORE_DUPLICATED_THROWS\" value=\"false\" />\n" +
152                                  "    <option name=\"IGNORE_POINT_TO_ITSELF\" value=\"false\" />\n" +
153                                  "    <option name=\"myAdditionalJavadocTags\" value=\"tag1,tag2 \" />\n" +
154                                  "  </inspection_tool>\n" +
155                                  "</inspections>").getRootElement();
156   }
157
158   public void testReloadProfileWithUnknownScopes() throws Exception {
159     final Element element = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
160                                                   "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
161                                                   "  <inspection_tool class=\"ArgNamesErrorsInspection\" enabled=\"true\" level=\"ERROR\" enabled_by_default=\"false\" />\n" +
162                                                   "  <inspection_tool class=\"ArgNamesWarningsInspection\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
163                                                   "  <inspection_tool class=\"AroundAdviceStyleInspection\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
164                                                   "  <inspection_tool class=\"DeclareParentsInspection\" enabled=\"true\" level=\"ERROR\" enabled_by_default=\"false\" />\n" +
165                                                   /*"  <inspection_tool class=\"ManifestDomInspection\" enabled=\"true\" level=\"ERROR\" enabled_by_default=\"false\" />\n" +*/
166                                                   "  <inspection_tool class=\"MissingAspectjAutoproxyInspection\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
167                                                   "  <inspection_tool class=\"UNUSED_IMPORT\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\">\n" +
168                                                   "    <scope name=\"Unknown scope name\" level=\"WARNING\" enabled=\"true\" />\n" +
169                                                   "  </inspection_tool>\n" +
170                                                   "</inspections>").getRootElement();
171     final InspectionProfileImpl profile = createProfile();
172     profile.readExternal(element);
173     final ModifiableModel model = profile.getModifiableModel();
174     model.commit();
175     final Element copy = new Element("inspections");
176     profile.writeExternal(copy);
177     assertElementsEqual(element, copy);
178   }
179
180   public void testMergeUnusedDeclarationAndUnusedSymbol() throws Exception {
181     //no specific settings
182     final Element element = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
183                                                   "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
184                                                   "</inspections>").getRootElement();
185     InspectionProfileImpl profile = createProfile();
186     profile.setBaseProfile(new InspectionProfileImpl("foo"));
187     profile.readExternal(element);
188     ModifiableModel model = profile.getModifiableModel();
189     model.commit();
190     final Element copy = new Element("inspections");
191     profile.writeExternal(copy);
192     assertElementsEqual(element, copy);
193
194
195     //settings to merge
196     final Element unusedProfile = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
197                                                         "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
198                                                         "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
199                                                         "      <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" +
200                                                         "      <option name=\"FIELD\" value=\"true\" />\n" +
201                                                         "      <option name=\"METHOD\" value=\"true\" />\n" +
202                                                         "      <option name=\"CLASS\" value=\"true\" />\n" +
203                                                         "      <option name=\"PARAMETER\" value=\"true\" />\n" +
204                                                         "      <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" +
205                                                         "  </inspection_tool>\n" +
206                                                         "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
207                                                         "      <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" +
208                                                         "      <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" +
209                                                         "      <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
210                                                         "      <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
211                                                         "   </inspection_tool>\n" +
212                                                         "</inspections>").getRootElement();
213     profile.readExternal(unusedProfile);
214     model = profile.getModifiableModel();
215     model.commit();
216     assertEquals("<profile version=\"1.0\">\n" +
217                  "  <option name=\"myName\" value=\"ToConvert\" />\n" +
218                  "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
219                  "    <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" +
220                  "    <option name=\"FIELD\" value=\"true\" />\n" +
221                  "    <option name=\"METHOD\" value=\"true\" />\n" +
222                  "    <option name=\"CLASS\" value=\"true\" />\n" +
223                  "    <option name=\"PARAMETER\" value=\"true\" />\n" +
224                  "    <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" +
225                  "  </inspection_tool>\n" +
226                  "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
227                  "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" +
228                  "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" +
229                  "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
230                  "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
231                  "  </inspection_tool>\n" +
232                  "</profile>", serialize(profile));
233
234     //make them default
235     profile = createProfile();
236     profile.setBaseProfile(new InspectionProfileImpl("foo"));
237     profile.readExternal(unusedProfile);
238     model = profile.getModifiableModel();
239     InspectionToolWrapper toolWrapper = ((InspectionProfileImpl)model).getInspectionTool("unused", getProject());
240     UnusedDeclarationInspectionBase tool = (UnusedDeclarationInspectionBase)toolWrapper.getTool();
241     tool.ADD_NONJAVA_TO_ENTRIES = true;
242     UnusedSymbolLocalInspectionBase inspectionTool = tool.getSharedLocalInspectionTool();
243     inspectionTool.REPORT_PARAMETER_FOR_PUBLIC_METHODS = true;
244     model.commit();
245     String mergedText = "<profile version=\"1.0\">\n" +
246                         "  <option name=\"myName\" value=\"ToConvert\" />\n" +
247                         "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
248                         "    <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" +
249                         "    <option name=\"FIELD\" value=\"true\" />\n" +
250                         "    <option name=\"METHOD\" value=\"true\" />\n" +
251                         "    <option name=\"CLASS\" value=\"true\" />\n" +
252                         "    <option name=\"PARAMETER\" value=\"true\" />\n" +
253                         "    <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" +
254                         "  </inspection_tool>\n" +
255                         "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
256                         "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" +
257                         "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" +
258                         "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
259                         "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
260                         "  </inspection_tool>\n" +
261                         "  <inspection_tool class=\"unusedMerged\" />\n" +
262                         "</profile>";
263     assertEquals(mergedText, serialize(profile));
264
265     //check merged
266     Element mergedElement = JDOMUtil.loadDocument(mergedText).getRootElement();
267     profile = createProfile();
268     profile.setBaseProfile(new InspectionProfileImpl("foo"));
269     profile.readExternal(mergedElement);
270     model = profile.getModifiableModel();
271     model.commit();
272     Element copyMerged = new Element("profile");
273     profile.writeExternal(copyMerged);
274     assertElementsEqual(mergedElement, copyMerged);
275   }
276
277   public void testDisabledUnusedDeclarationWithoutChanges() throws Exception {
278     checkMergedNoChanges("<profile version=\"1.0\">\n" +
279                          "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
280                          "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
281                          "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" +
282                          "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" +
283                          "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
284                          "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"true\" />\n" +
285                          "  </inspection_tool>\n" +
286                          "</profile>");
287   }
288
289   public void testDisabledUnusedDeclarationWithChanges() throws Exception {
290     checkMergedNoChanges("<profile version=\"1.0\">\n" +
291                          "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
292                          "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
293                          "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" +
294                          "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" +
295                          "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
296                          "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
297                          "  </inspection_tool>\n" +
298                          "</profile>");
299   }
300
301   public void testEnabledUnusedDeclarationWithChanges() throws Exception {
302     checkMergedNoChanges("<profile version=\"1.0\">\n" +
303                          "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
304                          "  <inspection_tool class=\"UnusedDeclaration\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\">\n" +
305                          "    <option name=\"ADD_MAINS_TO_ENTRIES\" value=\"true\" />\n" +
306                          "    <option name=\"ADD_APPLET_TO_ENTRIES\" value=\"true\" />\n" +
307                          "    <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
308                          "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
309                          "  </inspection_tool>\n" +
310                          "</profile>");
311   }
312
313   public void testDisabledUnusedSymbolWithoutChanges() throws Exception {
314     checkMergedNoChanges("<profile version=\"1.0\">\n" +
315                          "  <option name=\"myName\" value=\"" +
316                          PROFILE +
317                          "\" />\n" +
318                          "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
319                          "    <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" +
320                          "    <option name=\"FIELD\" value=\"true\" />\n" +
321                          "    <option name=\"METHOD\" value=\"true\" />\n" +
322                          "    <option name=\"CLASS\" value=\"true\" />\n" +
323                          "    <option name=\"PARAMETER\" value=\"true\" />\n" +
324                          "    <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"true\" />\n" +
325                          "  </inspection_tool>\n" +
326                          "</profile>");
327   }
328
329   public void testEnabledUnusedSymbolWithChanges() throws Exception {
330     checkMergedNoChanges("<profile version=\"1.0\">\n" +
331                          "  <option name=\"myName\" value=\"" +
332                          PROFILE +
333                          "\" />\n" +
334                          "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\">\n" +
335                          "    <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" +
336                          "    <option name=\"FIELD\" value=\"true\" />\n" +
337                          "    <option name=\"METHOD\" value=\"true\" />\n" +
338                          "    <option name=\"CLASS\" value=\"true\" />\n" +
339                          "    <option name=\"PARAMETER\" value=\"true\" />\n" +
340                          "    <option name=\"REPORT_PARAMETER_FOR_PUBLIC_METHODS\" value=\"false\" />\n" +
341                          "  </inspection_tool>\n" +
342                          "</profile>");
343   }
344
345   protected void checkMergedNoChanges(String initialText) throws Exception {
346     final Element element = JDOMUtil.loadDocument(initialText).getRootElement();
347     InspectionProfileImpl profile = createProfile();
348     profile.setBaseProfile(new InspectionProfileImpl("foo"));
349     profile.readExternal(element);
350     ModifiableModel model = profile.getModifiableModel();
351     model.commit();
352     assertEquals(initialText, serialize(profile));
353   }
354
355   public void testLockProfile() throws Exception {
356     final List<InspectionToolWrapper> list = new ArrayList<InspectionToolWrapper>();
357     list.add(createTool("foo", true));
358
359     InspectionToolRegistrar registrar = new InspectionToolRegistrar() {
360       @NotNull
361       @Override
362       public List<InspectionToolWrapper> createTools() {
363         return list;
364       }
365     };
366
367     InspectionProfileImpl profile = createProfile(registrar);
368
369     List<ScopeToolState> tools = profile.getAllTools(getProject());
370     assertEquals(1, tools.size());
371     assertTrue(profile.isToolEnabled(HighlightDisplayKey.find("foo")));
372     assertTrue(profile.getToolDefaultState("foo", getProject()).isEnabled());
373
374     InspectionProfileImpl model = (InspectionProfileImpl)profile.getModifiableModel();
375     model.lockProfile(true);
376     model.initInspectionTools(getProject()); // todo commit should take care of initialization
377     model.commit();
378
379     assertEquals("<profile version=\"1.0\" is_locked=\"true\">\n" +
380                  "  <option name=\"myName\" value=\"Foo\" />\n" +
381                  "  <inspection_tool class=\"foo\" enabled=\"true\" level=\"ERROR\" enabled_by_default=\"true\" />\n" +
382                  "</profile>",
383                  serialize(profile));
384
385     Element element = new Element("element");
386     profile.writeExternal(element);
387
388     list.add(createTool("bar", true));
389     list.add(createTool("disabled", false));
390
391     profile = createProfile(registrar);
392     profile.readExternal(element);
393
394     tools = profile.getAllTools(getProject());
395     assertEquals(3, tools.size());
396
397     assertTrue(profile.isProfileLocked());
398     assertFalse(profile.isToolEnabled(HighlightDisplayKey.find("bar")));
399     assertFalse(profile.isToolEnabled(HighlightDisplayKey.find("disabled")));
400
401     assertTrue(profile.getToolDefaultState("bar", getProject()).isEnabled());
402     assertFalse(profile.getToolDefaultState("disabled", getProject()).isEnabled());
403
404     assertEquals("<profile version=\"1.0\" is_locked=\"true\">\n" +
405                  "  <option name=\"myName\" value=\"Foo\" />\n" +
406                  "  <inspection_tool class=\"bar\" enabled=\"false\" level=\"ERROR\" enabled_by_default=\"true\" />\n" +
407                  "  <inspection_tool class=\"disabled\" enabled=\"false\" level=\"ERROR\" enabled_by_default=\"false\" />\n" +
408                  "  <inspection_tool class=\"foo\" enabled=\"true\" level=\"ERROR\" enabled_by_default=\"true\" />\n" +
409                  "</profile>", serialize(profile));
410   }
411
412   private static String serialize(InspectionProfileImpl profile) throws WriteExternalException {
413     Element element = new Element("profile");
414     profile.writeExternal(element);
415     return JDOMUtil.writeElement(element);
416   }
417
418   private static InspectionProfileImpl createProfile(InspectionToolRegistrar registrar) {
419     InspectionProfileImpl base = new InspectionProfileImpl("Base", registrar, InspectionProfileManager.getInstance());
420     base.setBaseProfile(null);
421     InspectionProfileImpl profile = new InspectionProfileImpl("Foo", registrar, InspectionProfileManager.getInstance());
422     profile.setBaseProfile(base);
423     return profile;
424   }
425
426   public void testGlobalInspectionContext() throws Exception {
427     InspectionProfileImpl profile = new InspectionProfileImpl("Foo");
428     profile.disableAllTools(getProject());
429     profile.enableTool(new UnusedDeclarationInspectionBase(true).getShortName(), getProject());
430
431     GlobalInspectionContextImpl context = ((InspectionManagerEx)InspectionManager.getInstance(getProject())).createNewGlobalContext(false);
432     context.setExternalProfile(profile);
433     context.initializeTools(new ArrayList<Tools>(), new ArrayList<Tools>(), new ArrayList<Tools>());
434   }
435
436   public void testInspectionsInitialization() throws Exception {
437
438     InspectionProfileImpl foo = new InspectionProfileImpl("foo");
439     assertEquals(0, countInitializedTools(foo));
440     foo.initInspectionTools(getProject());
441     assertEquals(0, countInitializedTools(foo));
442
443     ModifiableModel model = foo.getModifiableModel();
444     assertEquals(0, countInitializedTools(model));
445     model.commit();
446     assertEquals(0, countInitializedTools(model));
447     assertEquals(0, countInitializedTools(foo));
448
449     model = foo.getModifiableModel();
450     assertEquals(0, countInitializedTools(model));
451     List<ScopeToolState> tools = ((InspectionProfileImpl)model).getAllTools(getProject());
452     for (ScopeToolState tool : tools) {
453       if (!tool.isEnabled()) {
454         tool.setEnabled(true);
455       }
456     }
457     model.commit();
458     assertEquals(0, countInitializedTools(model));
459   }
460
461   public void testDoNotInstantiateOnSave() throws Exception {
462     InspectionProfileImpl profile = new InspectionProfileImpl("profile");
463     profile.setBaseProfile(InspectionProfileImpl.getDefaultProfile());
464     assertEquals(0, countInitializedTools(profile));
465     InspectionToolWrapper[] toolWrappers = profile.getInspectionTools(null);
466     assertTrue(toolWrappers.length > 0);
467     InspectionToolWrapper toolWrapper = profile.getInspectionTool(new DataFlowInspection().getShortName(), getProject());
468     assertNotNull(toolWrapper);
469     String id = toolWrapper.getShortName();
470     System.out.println(id);
471     if (profile.isToolEnabled(HighlightDisplayKey.findById(id))) {
472       profile.disableTool(id, getProject());
473     }
474     else {
475       profile.enableTool(id, getProject());
476     }
477     assertEquals(0, countInitializedTools(profile));
478     profile.writeExternal(new Element("profile"));
479     List<InspectionToolWrapper> initializedTools = getInitializedTools(profile);
480     if (initializedTools.size() > 0) {
481       for (InspectionToolWrapper initializedTool : initializedTools) {
482         System.out.println(initializedTool.getShortName());
483       }
484       fail();
485     }
486   }
487
488   public void testInspectionInitializationForSerialization() throws Exception {
489     InspectionProfileImpl foo = new InspectionProfileImpl("foo");
490     foo.readExternal(JDOMUtil.loadDocument("<profile version=\"1.0\">\n" +
491                                            "    <option name=\"myName\" value=\"idea.default\" />\n" +
492                                            "    <inspection_tool class=\"AbstractMethodCallInConstructor\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\" />\n" +
493                                            "    <inspection_tool class=\"AssignmentToForLoopParameter\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\">\n" +
494                                            "      <option name=\"m_checkForeachParameters\" value=\"false\" />\n" +
495                                            "    </inspection_tool>\n" +
496                                            "</profile>").getRootElement());
497     foo.initInspectionTools(getProject());
498     assertEquals(1, countInitializedTools(foo));
499   }
500
501   public void testPreserveCompatibility() throws Exception {
502     InspectionProfileImpl foo = new InspectionProfileImpl("foo");
503     String test = "<profile version=\"1.0\" is_locked=\"false\">\n" +
504                  "  <option name=\"myName\" value=\"idea.default\" />\n" +
505                  "  <option name=\"myLocal\" value=\"false\" />\n" +
506                  "  <inspection_tool class=\"AbstractMethodCallInConstructor\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\" />\n" +
507                  "  <inspection_tool class=\"AssignmentToForLoopParameter\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\">\n" +
508                  "    <option name=\"m_checkForeachParameters\" value=\"false\" />\n" +
509                  "  </inspection_tool>\n" +
510                  "</profile>";
511     foo.readExternal(JDOMUtil.loadDocument(test).getRootElement());
512     foo.initInspectionTools(getProject());
513     Element serialized = new Element("profile");
514     foo.writeExternal(serialized);
515     assertEquals(test, JDOMUtil.writeElement(serialized));
516   }
517
518   public static int countInitializedTools(Profile foo) {
519     return getInitializedTools(foo).size();
520   }
521
522   @NotNull
523   public static List<InspectionToolWrapper> getInitializedTools(@NotNull Profile foo) {
524     List<InspectionToolWrapper> initialized = new ArrayList<InspectionToolWrapper>();
525     List<ScopeToolState> tools = ((InspectionProfileImpl)foo).getAllTools(getProject());
526     for (ScopeToolState tool : tools) {
527       InspectionToolWrapper toolWrapper = tool.getTool();
528       if (toolWrapper.isInitialized()) {
529         initialized.add(toolWrapper);
530       }
531     }
532     return initialized;
533   }
534
535   private static LocalInspectionToolWrapper createTool(String s, boolean enabled) {
536     LocalInspectionEP foo = new LocalInspectionEP();
537     foo.shortName = s;
538     foo.displayName = s;
539     foo.groupDisplayName = s;
540     foo.level = "ERROR";
541     foo.enabledByDefault = enabled;
542     foo.implementationClass = TestTool.class.getName();
543     return new LocalInspectionToolWrapper(foo);
544   }
545
546   @SuppressWarnings("InspectionDescriptionNotFoundInspection")
547   public static class TestTool extends LocalInspectionTool {
548
549   }
550 }