Merge remote-tracking branch 'origin/master' into develar/is
[idea/community.git] / java / java-tests / testSrc / com / intellij / codeInspection / ex / InspectionProfileTest.java
index 85b9202d71fd28a2fe400248c28f24eafa900493..6b049b7d6ee5656e0b834d23070117c2aacfa7af 100644 (file)
@@ -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.
@@ -28,17 +28,22 @@ import com.intellij.openapi.util.WriteExternalException;
 import com.intellij.profile.Profile;
 import com.intellij.profile.codeInspection.InspectionProfileManager;
 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
+import com.intellij.profile.codeInspection.ProjectInspectionProfileManager;
 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;
 import static com.intellij.testFramework.PlatformTestUtil.assertElementsEqual;
 
 /**
@@ -58,10 +63,15 @@ public class InspectionProfileTest extends LightIdeaTestCase {
 
   @Override
   protected void tearDown() throws Exception {
-    super.tearDown();
-    //noinspection AssignmentToStaticFieldFromInstanceMethod
-    InspectionProfileImpl.INIT_INSPECTIONS = false;
-    InspectionProfileManager.getInstance().deleteProfile(PROFILE);
+    try {
+      //noinspection SuperTearDownInFinally
+      super.tearDown();
+    }
+    finally {
+      //noinspection AssignmentToStaticFieldFromInstanceMethod
+      InspectionProfileImpl.INIT_INSPECTIONS = false;
+      InspectionProfileManager.getInstance().deleteProfile(PROFILE);
+    }
   }
 
   public void testCopyProjectProfile() throws Exception {
@@ -70,16 +80,14 @@ public class InspectionProfileTest extends LightIdeaTestCase {
     profile.readExternal(element);
     final ModifiableModel model = profile.getModifiableModel();
     model.commit();
-    final Element copy = new Element("inspections");
-    profile.writeExternal(copy);
-    assertElementsEqual(element, copy);
+    assertElementsEqual(element, serializeProfile(profile));
   }
 
   private static InspectionProfileImpl createProfile() {
-    return new InspectionProfileImpl(PROFILE, InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), InspectionProfileImpl.getDefaultProfile());
+    return new InspectionProfileImpl(PROFILE, InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), InspectionProfileImpl.getDefaultProfile(), null);
   }
   private static InspectionProfileImpl createProfile(@NotNull InspectionProfileImpl base) {
-    return new InspectionProfileImpl(PROFILE, InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), base);
+    return new InspectionProfileImpl(PROFILE, InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), base, null);
   }
 
   public void testSameNameSharedProfile() throws Exception {
@@ -87,16 +95,16 @@ public class InspectionProfileTest extends LightIdeaTestCase {
     InspectionProfileImpl localProfile = createProfile();
     profileManager.updateProfile(localProfile);
 
-    InspectionProjectProfileManager projectProfileManager = InspectionProjectProfileManager.getInstance(getProject());
+    ProjectInspectionProfileManager projectProfileManager = ProjectInspectionProfileManager.getInstanceImpl(getProject());
     try {
       //normally on open project profile wrappers are init for both managers
       profileManager.updateProfile(localProfile);
       InspectionProfileImpl profile = new InspectionProfileImpl(PROFILE, InspectionToolRegistrar.getInstance(), projectProfileManager,
-                                                                InspectionProfileImpl.getDefaultProfile());
+                                                                InspectionProfileImpl.getDefaultProfile(), null);
       projectProfileManager.updateProfile(profile);
-      projectProfileManager.setProjectProfile(profile.getName());
+      projectProfileManager.setRootProfile(profile.getName());
 
-      assertTrue(projectProfileManager.getInspectionProfile() == profile);
+      assertTrue(projectProfileManager.getCurrentProfile() == profile);
     }
     finally {
       projectProfileManager.deleteProfile(PROFILE);
@@ -142,49 +150,47 @@ public class InspectionProfileTest extends LightIdeaTestCase {
     ModifiableModel model = profile.getModifiableModel();
     model.commit();
 
-    Element copy = new Element("inspections");
-    profile.writeExternal(copy);
-    assertElementsEqual(loadProfile(), copy);
+    assertElementsEqual(loadProfile(), serializeProfile(profile));
   }
 
   private static Element loadProfile() throws IOException, JDOMException {
-    return JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
-                                 "  <option name=\"myName\" value=\"ToConvert\" />\n" +
-                                 "  <inspection_tool class=\"JavaDoc\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
-                                 "    <option name=\"TOP_LEVEL_CLASS_OPTIONS\">\n" +
-                                 "      <value>\n" +
-                                 "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
-                                 "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
-                                 "      </value>\n" +
-                                 "    </option>\n" +
-                                 "    <option name=\"INNER_CLASS_OPTIONS\">\n" +
-                                 "      <value>\n" +
-                                 "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
-                                 "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
-                                 "      </value>\n" +
-                                 "    </option>\n" +
-                                 "    <option name=\"METHOD_OPTIONS\">\n" +
-                                 "      <value>\n" +
-                                 "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
-                                 "        <option name=\"REQUIRED_TAGS\" value=\"@return@param@throws or @exception\" />\n" +
-                                 "      </value>\n" + "    </option>\n" +
-                                 "    <option name=\"FIELD_OPTIONS\">\n" +
-                                 "      <value>\n" +
-                                 "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
-                                 "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
-                                 "      </value>\n" +
-                                 "    </option>\n" +
-                                 "    <option name=\"IGNORE_DEPRECATED\" value=\"false\" />\n" +
-                                 "    <option name=\"IGNORE_JAVADOC_PERIOD\" value=\"false\" />\n" +
-                                 "    <option name=\"IGNORE_DUPLICATED_THROWS\" value=\"false\" />\n" +
-                                 "    <option name=\"IGNORE_POINT_TO_ITSELF\" value=\"false\" />\n" +
-                                 "    <option name=\"myAdditionalJavadocTags\" value=\"tag1,tag2 \" />\n" +
-                                 "  </inspection_tool>\n" +
-                                 "</inspections>").getRootElement();
+    return JdomKt.loadElement("<profile version=\"1.0\">\n" +
+                       "  <option name=\"myName\" value=\"ToConvert\" />\n" +
+                       "  <inspection_tool class=\"JavaDoc\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
+                       "    <option name=\"TOP_LEVEL_CLASS_OPTIONS\">\n" +
+                       "      <value>\n" +
+                       "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
+                       "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
+                       "      </value>\n" +
+                       "    </option>\n" +
+                       "    <option name=\"INNER_CLASS_OPTIONS\">\n" +
+                       "      <value>\n" +
+                       "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
+                       "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
+                       "      </value>\n" +
+                       "    </option>\n" +
+                       "    <option name=\"METHOD_OPTIONS\">\n" +
+                       "      <value>\n" +
+                       "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
+                       "        <option name=\"REQUIRED_TAGS\" value=\"@return@param@throws or @exception\" />\n" +
+                       "      </value>\n" + "    </option>\n" +
+                       "    <option name=\"FIELD_OPTIONS\">\n" +
+                       "      <value>\n" +
+                       "        <option name=\"ACCESS_JAVADOC_REQUIRED_FOR\" value=\"none\" />\n" +
+                       "        <option name=\"REQUIRED_TAGS\" value=\"\" />\n" +
+                       "      </value>\n" +
+                       "    </option>\n" +
+                       "    <option name=\"IGNORE_DEPRECATED\" value=\"false\" />\n" +
+                       "    <option name=\"IGNORE_JAVADOC_PERIOD\" value=\"false\" />\n" +
+                       "    <option name=\"IGNORE_DUPLICATED_THROWS\" value=\"false\" />\n" +
+                       "    <option name=\"IGNORE_POINT_TO_ITSELF\" value=\"false\" />\n" +
+                       "    <option name=\"myAdditionalJavadocTags\" value=\"tag1,tag2 \" />\n" +
+                       "  </inspection_tool>\n" +
+                       "</profile>");
   }
 
   public void testReloadProfileWithUnknownScopes() throws Exception {
-    final Element element = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
+    final Element element = JdomKt.loadElement("<profile version=\"1.0\">\n" +
                                                   "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
                                                   "  <inspection_tool class=\"ArgNamesErrorsInspection\" enabled=\"true\" level=\"ERROR\" enabled_by_default=\"false\" />\n" +
                                                   "  <inspection_tool class=\"ArgNamesWarningsInspection\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
@@ -195,32 +201,28 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                                                   "  <inspection_tool class=\"UNUSED_IMPORT\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"true\">\n" +
                                                   "    <scope name=\"Unknown scope name\" level=\"WARNING\" enabled=\"true\" />\n" +
                                                   "  </inspection_tool>\n" +
-                                                  "</inspections>").getRootElement();
+                                                  "</profile>");
     final InspectionProfileImpl profile = createProfile();
     profile.readExternal(element);
     final ModifiableModel model = profile.getModifiableModel();
     model.commit();
-    final Element copy = new Element("inspections");
-    profile.writeExternal(copy);
-    assertElementsEqual(element, copy);
+    assertElementsEqual(element, serializeProfile(profile));
   }
 
   public void testMergeUnusedDeclarationAndUnusedSymbol() throws Exception {
     //no specific settings
-    final Element element = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
+    final Element element = JdomKt.loadElement("<profile version=\"1.0\">\n" +
                                                   "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
-                                                  "</inspections>").getRootElement();
+                                                  "</profile>");
     InspectionProfileImpl profile = createProfile(new InspectionProfileImpl("foo"));
     profile.readExternal(element);
     ModifiableModel model = profile.getModifiableModel();
     model.commit();
-    final Element copy = new Element("inspections");
-    profile.writeExternal(copy);
-    assertElementsEqual(element, copy);
+    assertElementsEqual(element, serializeProfile(profile));
 
 
     //settings to merge
-    final Element unusedProfile = JDOMUtil.loadDocument("<inspections version=\"1.0\">\n" +
+    final Element unusedProfile = JdomKt.loadElement("<profile version=\"1.0\">\n" +
                                                         "  <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
                                                         "  <inspection_tool class=\"UNUSED_SYMBOL\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
                                                         "      <option name=\"LOCAL_VARIABLE\" value=\"true\" />\n" +
@@ -236,7 +238,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                                                         "      <option name=\"ADD_SERVLET_TO_ENTRIES\" value=\"true\" />\n" +
                                                         "      <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
                                                         "   </inspection_tool>\n" +
-                                                        "</inspections>").getRootElement();
+                                                        "</profile>");
     profile.readExternal(unusedProfile);
     model = profile.getModifiableModel();
     model.commit();
@@ -288,8 +290,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                         "</profile>";
     assertEquals(mergedText, serialize(profile));
 
-    Element toImportElement = new Element("profile");
-    profile.writeExternal(toImportElement);
+    Element toImportElement = serializeProfile(profile);
     final InspectionProfileImpl importedProfile =
       InspectionToolsConfigurable.importInspectionProfile(toImportElement, InspectionProfileManager.getInstance(), getProject(), null);
 
@@ -299,13 +300,9 @@ public class InspectionProfileTest extends LightIdeaTestCase {
     profile.readExternal(mergedElement);
     model = profile.getModifiableModel();
     model.commit();
-    Element copyMerged = new Element("profile");
-    profile.writeExternal(copyMerged);
-    assertElementsEqual(mergedElement, copyMerged);
+    assertElementsEqual(mergedElement, serializeProfile(profile));
 
-    Element imported = new Element("profile");
-    importedProfile.writeExternal(imported);
-    assertElementsEqual(mergedElement, imported);
+    assertElementsEqual(mergedElement, serializeProfile(importedProfile));
   }
 
   public void testStoredMemberVisibility() throws Exception {
@@ -324,7 +321,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                                                "    <option name=\"ADD_NONJAVA_TO_ENTRIES\" value=\"false\" />\n" +
                                                "  </inspection_tool>\n" +
                                                "</profile>").getRootElement());
-    InspectionProfileImpl model = (InspectionProfileImpl)profile.getModifiableModel();
+    InspectionProfileImpl model = profile.getModifiableModel();
     InspectionToolWrapper toolWrapper = model.getInspectionTool("unused", getProject());
     UnusedDeclarationInspectionBase tool = (UnusedDeclarationInspectionBase)toolWrapper.getTool();
     UnusedSymbolLocalInspectionBase inspectionTool = tool.getSharedLocalInspectionTool();
@@ -432,7 +429,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                          "</profile>");
   }
 
-  private void checkMergedNoChanges(String initialText) throws Exception {
+  private static void checkMergedNoChanges(String initialText) throws Exception {
     final Element element = JDOMUtil.loadDocument(initialText).getRootElement();
     InspectionProfileImpl profile = createProfile(new InspectionProfileImpl("foo"));
     profile.readExternal(element);
@@ -442,7 +439,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
   }
 
   public void testLockProfile() throws Exception {
-    final List<InspectionToolWrapper> list = new ArrayList<InspectionToolWrapper>();
+    final List<InspectionToolWrapper> list = new ArrayList<>();
     list.add(createTool("foo", true));
 
     InspectionToolRegistrar registrar = new InspectionToolRegistrar() {
@@ -460,7 +457,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
     assertTrue(profile.isToolEnabled(HighlightDisplayKey.find("foo")));
     assertTrue(profile.getToolDefaultState("foo", getProject()).isEnabled());
 
-    InspectionProfileImpl model = (InspectionProfileImpl)profile.getModifiableModel();
+    InspectionProfileImpl model = profile.getModifiableModel();
     model.lockProfile(true);
     model.initInspectionTools(getProject()); // todo commit should take care of initialization
     model.commit();
@@ -471,8 +468,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                  "</profile>",
                  serialize(profile));
 
-    Element element = new Element("element");
-    profile.writeExternal(element);
+    Element element = serializeProfile(profile);
 
     list.add(createTool("bar", true));
     list.add(createTool("disabled", false));
@@ -499,14 +495,12 @@ public class InspectionProfileTest extends LightIdeaTestCase {
   }
 
   private static String serialize(InspectionProfileImpl profile) throws WriteExternalException {
-    Element element = new Element("profile");
-    profile.writeExternal(element);
-    return JDOMUtil.writeElement(element);
+    return JDOMUtil.writeElement(serializeProfile(profile));
   }
 
-  private static InspectionProfileImpl createProfile(InspectionToolRegistrar registrar) {
-    InspectionProfileImpl base = new InspectionProfileImpl("Base", registrar, InspectionProfileManager.getInstance(), null);
-    return new InspectionProfileImpl("Foo", registrar, InspectionProfileManager.getInstance(), base);
+  private static InspectionProfileImpl createProfile(@NotNull InspectionToolRegistrar registrar) {
+    InspectionProfileImpl base = new InspectionProfileImpl("Base", registrar, InspectionProfileManager.getInstance(), null, null);
+    return new InspectionProfileImpl("Foo", registrar, InspectionProfileManager.getInstance(), base, null);
   }
 
   public void testGlobalInspectionContext() throws Exception {
@@ -516,7 +510,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
 
     GlobalInspectionContextImpl context = ((InspectionManagerEx)InspectionManager.getInstance(getProject())).createNewGlobalContext(false);
     context.setExternalProfile(profile);
-    context.initializeTools(new ArrayList<Tools>(), new ArrayList<Tools>(), new ArrayList<Tools>());
+    context.initializeTools(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
   }
 
   public void testInspectionsInitialization() throws Exception {
@@ -545,7 +539,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
   }
 
   public void testDoNotInstantiateOnSave() throws Exception {
-    InspectionProfileImpl profile = new InspectionProfileImpl("profile", InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), InspectionProfileImpl.getDefaultProfile());
+    InspectionProfileImpl profile = new InspectionProfileImpl("profile", InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), InspectionProfileImpl.getDefaultProfile(), null);
     assertEquals(0, countInitializedTools(profile));
     InspectionToolWrapper[] toolWrappers = profile.getInspectionTools(null);
     assertTrue(toolWrappers.length > 0);
@@ -560,7 +554,7 @@ public class InspectionProfileTest extends LightIdeaTestCase {
       profile.enableTool(id, getProject());
     }
     assertEquals(0, countInitializedTools(profile));
-    profile.writeExternal(new Element("profile"));
+    serializeProfile(profile);
     List<InspectionToolWrapper> initializedTools = getInitializedTools(profile);
     if (initializedTools.size() > 0) {
       for (InspectionToolWrapper initializedTool : initializedTools) {
@@ -595,26 +589,27 @@ public class InspectionProfileTest extends LightIdeaTestCase {
                  "</profile>";
     foo.readExternal(JDOMUtil.loadDocument(test).getRootElement());
     foo.initInspectionTools(getProject());
-    Element serialized = new Element("profile");
-    foo.writeExternal(serialized);
-    assertEquals(test, JDOMUtil.writeElement(serialized));
+    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<InspectionToolWrapper> getInitializedTools(@NotNull Profile foo) {
-    List<InspectionToolWrapper> initialized = new ArrayList<InspectionToolWrapper>();
-    List<ScopeToolState> tools = ((InspectionProfileImpl)foo).getAllTools(getProject());
+  public static List<InspectionToolWrapper> getInitializedTools(@NotNull InspectionProfileImpl foo) {
+    List<InspectionToolWrapper> initialized = null;
+    List<ScopeToolState> 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) {