/*
* 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" +
" \n" +
" \n" +
" \n" +
"
\n" +
" \n" +
"");
return document.getRootElement();
}
public void testReloadProfileWithUnknownScopes() throws Exception {
final Element element = JDOMUtil.loadDocument("\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
/*" \n" +*/
" \n" +
" \n" +
" \n" +
" \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" +
"
\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" +
"
\n" +
" \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" +
"