/* * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.intellij.codeInspection; import com.intellij.codeInsight.daemon.HighlightDisplayKey; import com.intellij.codeInspection.dataFlow.DataFlowInspection; import com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase; import com.intellij.codeInspection.ex.*; import com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase; import com.intellij.openapi.util.JDOMUtil; import com.intellij.openapi.util.WriteExternalException; import com.intellij.profile.Profile; import com.intellij.profile.codeInspection.InspectionProfileManager; import com.intellij.testFramework.LightIdeaTestCase; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.util.ArrayList; import java.util.List; import static com.intellij.testFramework.PlatformTestUtil.assertElementsEqual; /** * @author Anna.Kozlova * Date: 18-Aug-2006 */ public class InspectionProfileTest extends LightIdeaTestCase { private static final String PROFILE = "ToConvert"; @Override protected void setUp() throws Exception { //noinspection AssignmentToStaticFieldFromInstanceMethod InspectionProfileImpl.INIT_INSPECTIONS = true; super.setUp(); InspectionToolRegistrar.getInstance().ensureInitialized(); } @Override protected void tearDown() throws Exception { super.tearDown(); //noinspection AssignmentToStaticFieldFromInstanceMethod InspectionProfileImpl.INIT_INSPECTIONS = false; InspectionProfileManager.getInstance().deleteProfile(PROFILE); } public void testCopyProjectProfile() throws Exception { final Element element = loadProfile(); 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); } private static InspectionProfileImpl createProfile() { InspectionProfileImpl profile = new InspectionProfileImpl(PROFILE); profile.setBaseProfile(InspectionProfileImpl.getDefaultProfile()); return profile; } public void testConvertOldProfile() throws Exception { final Element element = loadOldStyleProfile(); final InspectionProfileImpl profile = createProfile(); profile.readExternal(element); final ModifiableModel model = profile.getModifiableModel(); model.commit(); final Element copy = new Element("inspections"); profile.writeExternal(copy); assertElementsEqual(loadProfile(), copy); } private static Element loadOldStyleProfile() throws IOException, JDOMException { final Document document = JDOMUtil.loadDocument("\n" + " "); return document.getRootElement(); } private static Element loadProfile() throws IOException, JDOMException { final Document document = JDOMUtil.loadDocument("\n" + " "); return document.getRootElement(); } public void testReloadProfileWithUnknownScopes() throws Exception { final Element element = JDOMUtil.loadDocument("\n" + " ").getRootElement(); 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); } public void testMergeUnusedDeclarationAndUnusedSymbol() throws Exception { //no specific settings final Element element = JDOMUtil.loadDocument("\n" + " ").getRootElement(); InspectionProfileImpl profile = createProfile(); profile.setBaseProfile(new InspectionProfileImpl("foo")); profile.readExternal(element); ModifiableModel model = profile.getModifiableModel(); model.commit(); final Element copy = new Element("inspections"); profile.writeExternal(copy); assertElementsEqual(element, copy); //settings to merge final Element unusedProfile = JDOMUtil.loadDocument("\n" + " ").getRootElement(); profile.readExternal(unusedProfile); model = profile.getModifiableModel(); model.commit(); assertEquals("\n" + " ", serialize(profile)); //make them default profile = createProfile(); profile.setBaseProfile(new InspectionProfileImpl("foo")); profile.readExternal(unusedProfile); model = profile.getModifiableModel(); InspectionToolWrapper toolWrapper = ((InspectionProfileImpl)model).getInspectionTool("unused", getProject()); UnusedDeclarationInspectionBase tool = (UnusedDeclarationInspectionBase)toolWrapper.getTool(); tool.ADD_NONJAVA_TO_ENTRIES = true; UnusedSymbolLocalInspectionBase inspectionTool = tool.getSharedLocalInspectionTool(); inspectionTool.REPORT_PARAMETER_FOR_PUBLIC_METHODS = true; model.commit(); String mergedText = "\n" + " "; assertEquals(mergedText, serialize(profile)); //check merged Element mergedElement = JDOMUtil.loadDocument(mergedText).getRootElement(); profile = createProfile(); profile.setBaseProfile(new InspectionProfileImpl("foo")); profile.readExternal(mergedElement); model = profile.getModifiableModel(); model.commit(); Element copyMerged = new Element("profile"); profile.writeExternal(copyMerged); assertElementsEqual(mergedElement, copyMerged); } public void testDisabledUnusedDeclarationWithoutChanges() throws Exception { checkMergedNoChanges("\n" + " "); } public void testDisabledUnusedDeclarationWithChanges() throws Exception { checkMergedNoChanges("\n" + " "); } public void testEnabledUnusedDeclarationWithChanges() throws Exception { checkMergedNoChanges("\n" + " "); } public void testDisabledUnusedSymbolWithoutChanges() throws Exception { checkMergedNoChanges("\n" + " "); } public void testEnabledUnusedSymbolWithChanges() throws Exception { checkMergedNoChanges("\n" + " "); } protected void checkMergedNoChanges(String initialText) throws Exception { final Element element = JDOMUtil.loadDocument(initialText).getRootElement(); InspectionProfileImpl profile = createProfile(); profile.setBaseProfile(new InspectionProfileImpl("foo")); profile.readExternal(element); ModifiableModel model = profile.getModifiableModel(); model.commit(); assertEquals(initialText, serialize(profile)); } public void testLockProfile() throws Exception { final List list = new ArrayList(); list.add(createTool("foo", true)); InspectionToolRegistrar registrar = new InspectionToolRegistrar() { @NotNull @Override public List createTools() { return list; } }; InspectionProfileImpl profile = createProfile(registrar); List tools = profile.getAllTools(getProject()); assertEquals(1, tools.size()); assertTrue(profile.isToolEnabled(HighlightDisplayKey.find("foo"))); assertTrue(profile.getToolDefaultState("foo", getProject()).isEnabled()); InspectionProfileImpl model = (InspectionProfileImpl)profile.getModifiableModel(); model.lockProfile(true); model.initInspectionTools(getProject()); // todo commit should take care of initialization model.commit(); assertEquals("\n" + " ", serialize(profile)); Element element = new Element("element"); profile.writeExternal(element); list.add(createTool("bar", true)); list.add(createTool("disabled", false)); profile = createProfile(registrar); profile.readExternal(element); tools = profile.getAllTools(getProject()); assertEquals(3, tools.size()); assertTrue(profile.isProfileLocked()); assertFalse(profile.isToolEnabled(HighlightDisplayKey.find("bar"))); assertFalse(profile.isToolEnabled(HighlightDisplayKey.find("disabled"))); assertTrue(profile.getToolDefaultState("bar", getProject()).isEnabled()); assertFalse(profile.getToolDefaultState("disabled", getProject()).isEnabled()); assertEquals("\n" + " ", serialize(profile)); } private static String serialize(InspectionProfileImpl profile) throws WriteExternalException { Element element = new Element("profile"); profile.writeExternal(element); return JDOMUtil.writeElement(element); } private static InspectionProfileImpl createProfile(InspectionToolRegistrar registrar) { InspectionProfileImpl base = new InspectionProfileImpl("Base", registrar, InspectionProfileManager.getInstance()); base.setBaseProfile(null); InspectionProfileImpl profile = new InspectionProfileImpl("Foo", registrar, InspectionProfileManager.getInstance()); profile.setBaseProfile(base); return profile; } public void testGlobalInspectionContext() throws Exception { InspectionProfileImpl profile = new InspectionProfileImpl("Foo"); profile.disableAllTools(getProject()); profile.enableTool(new UnusedDeclarationInspectionBase(true).getShortName(), getProject()); GlobalInspectionContextImpl context = ((InspectionManagerEx)InspectionManager.getInstance(getProject())).createNewGlobalContext(false); context.setExternalProfile(profile); context.initializeTools(new ArrayList(), new ArrayList(), new ArrayList()); } public void testInspectionsInitialization() throws Exception { InspectionProfileImpl foo = new InspectionProfileImpl("foo"); assertEquals(0, countInitializedTools(foo)); foo.initInspectionTools(getProject()); assertEquals(0, countInitializedTools(foo)); ModifiableModel model = foo.getModifiableModel(); assertEquals(0, countInitializedTools(model)); model.commit(); assertEquals(0, countInitializedTools(model)); assertEquals(0, countInitializedTools(foo)); model = foo.getModifiableModel(); assertEquals(0, countInitializedTools(model)); List tools = ((InspectionProfileImpl)model).getAllTools(getProject()); for (ScopeToolState tool : tools) { if (!tool.isEnabled()) { tool.setEnabled(true); } } model.commit(); assertEquals(0, countInitializedTools(model)); } public void testDoNotInstantiateOnSave() throws Exception { InspectionProfileImpl profile = new InspectionProfileImpl("profile"); profile.setBaseProfile(InspectionProfileImpl.getDefaultProfile()); assertEquals(0, countInitializedTools(profile)); InspectionToolWrapper[] toolWrappers = profile.getInspectionTools(null); assertTrue(toolWrappers.length > 0); InspectionToolWrapper toolWrapper = profile.getInspectionTool(new DataFlowInspection().getShortName(), getProject()); assertNotNull(toolWrapper); String id = toolWrapper.getShortName(); System.out.println(id); if (profile.isToolEnabled(HighlightDisplayKey.findById(id))) { profile.disableTool(id, getProject()); } else { profile.enableTool(id, getProject()); } assertEquals(0, countInitializedTools(profile)); profile.writeExternal(new Element("profile")); List initializedTools = getInitializedTools(profile); if (initializedTools.size() > 0) { for (InspectionToolWrapper initializedTool : initializedTools) { System.out.println(initializedTool.getShortName()); } fail(); } } public void testInspectionInitializationForSerialization() throws Exception { InspectionProfileImpl foo = new InspectionProfileImpl("foo"); foo.readExternal(JDOMUtil.loadDocument("\n" + " ").getRootElement()); foo.initInspectionTools(getProject()); assertEquals(1, countInitializedTools(foo)); } public void testPreserveCompatibility() throws Exception { InspectionProfileImpl foo = new InspectionProfileImpl("foo"); String test = "\n" + " "; foo.readExternal(JDOMUtil.loadDocument(test).getRootElement()); foo.initInspectionTools(getProject()); Element serialized = new Element("profile"); foo.writeExternal(serialized); assertEquals(test, JDOMUtil.writeElement(serialized)); } public static int countInitializedTools(Profile foo) { return getInitializedTools(foo).size(); } @NotNull public static List getInitializedTools(@NotNull Profile foo) { List initialized = new ArrayList(); List tools = ((InspectionProfileImpl)foo).getAllTools(getProject()); for (ScopeToolState tool : tools) { InspectionToolWrapper toolWrapper = tool.getTool(); if (toolWrapper.isInitialized()) { initialized.add(toolWrapper); } } return initialized; } private static LocalInspectionToolWrapper createTool(String s, boolean enabled) { LocalInspectionEP foo = new LocalInspectionEP(); foo.shortName = s; foo.displayName = s; foo.groupDisplayName = s; foo.level = "ERROR"; foo.enabledByDefault = enabled; foo.implementationClass = TestTool.class.getName(); return new LocalInspectionToolWrapper(foo); } @SuppressWarnings("InspectionDescriptionNotFoundInspection") public static class TestTool extends LocalInspectionTool { } }