/*
* 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.
* 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.ex;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.LocalInspectionEP;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ModifiableModel;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase;
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.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;
/**
* @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().createTools();
}
@Override
protected void tearDown() throws Exception {
try {
//noinspection SuperTearDownInFinally
super.tearDown();
}
finally {
//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();
assertElementsEqual(element, serializeProfile(profile));
}
private static InspectionProfileImpl createProfile() {
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, null);
}
public void testSameNameSharedProfile() throws Exception {
InspectionProfileManager profileManager = InspectionProfileManager.getInstance();
InspectionProfileImpl localProfile = createProfile();
profileManager.updateProfile(localProfile);
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(), null);
projectProfileManager.updateProfile(profile);
projectProfileManager.setRootProfile(profile.getName());
assertTrue(projectProfileManager.getCurrentProfile() == profile);
}
finally {
projectProfileManager.deleteProfile(PROFILE);
}
}
public void testConvertOldProfile() throws Exception {
Element element = JDOMUtil.loadDocument("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" + " \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ").getRootElement();
InspectionProfileImpl profile = createProfile();
profile.readExternal(element);
ModifiableModel model = profile.getModifiableModel();
model.commit();
assertElementsEqual(loadProfile(), serializeProfile(profile));
}
private static Element loadProfile() throws IOException, JDOMException {
return JdomKt.loadElement("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" + " \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
public void testReloadProfileWithUnknownScopes() throws Exception {
final Element element = JdomKt.loadElement("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
/*" \n" +*/
" \n" +
" \n" +
" \n" +
" \n" +
" ");
final InspectionProfileImpl profile = createProfile();
profile.readExternal(element);
final ModifiableModel model = profile.getModifiableModel();
model.commit();
assertElementsEqual(element, serializeProfile(profile));
}
public void testMergeUnusedDeclarationAndUnusedSymbol() throws Exception {
//no specific settings
final Element element = JdomKt.loadElement("\n" +
" \n" +
" ");
InspectionProfileImpl profile = createProfile(new InspectionProfileImpl("foo"));
profile.readExternal(element);
ModifiableModel model = profile.getModifiableModel();
model.commit();
assertElementsEqual(element, serializeProfile(profile));
//settings to merge
final Element unusedProfile = JdomKt.loadElement("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
profile.readExternal(unusedProfile);
model = profile.getModifiableModel();
model.commit();
assertEquals("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ", serialize(profile));
//make them default
profile = createProfile(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.setParameterVisibility(PsiModifier.PUBLIC);
model.commit();
String mergedText = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ";
assertEquals(mergedText, serialize(profile));
Element toImportElement = serializeProfile(profile);
final InspectionProfileImpl importedProfile =
InspectionToolsConfigurable.importInspectionProfile(toImportElement, InspectionProfileManager.getInstance(), getProject(), null);
//check merged
Element mergedElement = JDOMUtil.loadDocument(mergedText).getRootElement();
profile = createProfile(new InspectionProfileImpl("foo"));
profile.readExternal(mergedElement);
model = profile.getModifiableModel();
model.commit();
assertElementsEqual(mergedElement, serializeProfile(profile));
assertElementsEqual(mergedElement, serializeProfile(importedProfile));
}
public void testStoredMemberVisibility() throws Exception {
InspectionProfileImpl profile = createProfile(new InspectionProfileImpl("foo"));
profile.readExternal(JDOMUtil.loadDocument("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ").getRootElement());
InspectionProfileImpl model = profile.getModifiableModel();
InspectionToolWrapper toolWrapper = model.getInspectionTool("unused", getProject());
UnusedDeclarationInspectionBase tool = (UnusedDeclarationInspectionBase)toolWrapper.getTool();
UnusedSymbolLocalInspectionBase inspectionTool = tool.getSharedLocalInspectionTool();
inspectionTool.setClassVisibility("none");
model.commit();
String mergedText = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ";
assertEquals(mergedText, serialize(profile));
}
public void testDisabledUnusedDeclarationWithoutChanges() throws Exception {
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
public void testMergedMisspelledInspections() throws Exception {
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
public void testDisabledUnusedDeclarationWithChanges() throws Exception {
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
public void testEnabledUnusedDeclarationWithChanges() throws Exception {
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
public void testDisabledUnusedSymbolWithoutChanges() throws Exception {
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
public void testEnabledUnusedSymbolWithChanges() throws Exception {
checkMergedNoChanges("\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ");
}
private static void checkMergedNoChanges(String initialText) throws Exception {
final Element element = JDOMUtil.loadDocument(initialText).getRootElement();
InspectionProfileImpl profile = createProfile(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 = profile.getModifiableModel();
model.lockProfile(true);
model.initInspectionTools(getProject()); // todo commit should take care of initialization
model.commit();
assertEquals("\n" +
" \n" +
" \n" +
" ",
serialize(profile));
Element element = serializeProfile(profile);
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" +
" \n" +
" \n" +
" \n" +
" \n" +
" ", serialize(profile));
}
private static String serialize(InspectionProfileImpl profile) throws WriteExternalException {
return JDOMUtil.writeElement(serializeProfile(profile));
}
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 {
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", InspectionToolRegistrar.getInstance(), InspectionProfileManager.getInstance(), InspectionProfileImpl.getDefaultProfile(), null);
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));
serializeProfile(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" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ").getRootElement());
foo.initInspectionTools(getProject());
assertEquals(1, countInitializedTools(foo));
}
public void testPreserveCompatibility() throws Exception {
InspectionProfileImpl foo = new InspectionProfileImpl("foo", InspectionToolRegistrar.getInstance(), InspectionProjectProfileManager.getInstance(getProject()));
String test = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" ";
foo.readExternal(JDOMUtil.loadDocument(test).getRootElement());
foo.initInspectionTools(getProject());
assertEquals(test, JDOMUtil.writeElement(serializeProfile(foo)));
}
public static int countInitializedTools(@NotNull Profile foo) {
return getInitializedTools((InspectionProfileImpl)foo).size();
}
@NotNull
public static List getInitializedTools(@NotNull InspectionProfileImpl foo) {
List initialized = null;
List 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 == null ? Collections.emptyList() : 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 {
}
}