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