2 * Copyright 2000-2016 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.codeInspection.ex
18 import com.intellij.codeHighlighting.HighlightDisplayLevel
19 import com.intellij.configurationStore.PROJECT_CONFIG_DIR
20 import com.intellij.configurationStore.StoreAwareProjectManager
21 import com.intellij.configurationStore.loadAndUseProject
22 import com.intellij.configurationStore.saveStore
23 import com.intellij.openapi.components.stateStore
24 import com.intellij.openapi.project.ProjectManager
25 import com.intellij.profile.codeInspection.ProjectInspectionProfileManager
26 import com.intellij.testFramework.Assertions.assertThat
27 import com.intellij.testFramework.ProjectRule
28 import com.intellij.testFramework.RuleChain
29 import com.intellij.testFramework.TemporaryDirectory
30 import com.intellij.testFramework.runInInitMode
31 import com.intellij.util.delete
32 import com.intellij.util.readText
33 import com.intellij.util.write
34 import org.junit.ClassRule
37 import java.nio.file.Paths
39 internal class ProjectInspectionManagerTest {
43 val projectRule = ProjectRule()
46 val tempDirManager = TemporaryDirectory()
48 private val ruleChain = RuleChain(tempDirManager)
49 @Rule fun getChain() = ruleChain
51 @Test fun `component`() {
52 loadAndUseProject(tempDirManager, {
55 val projectInspectionProfileManager = ProjectInspectionProfileManager.getInstanceImpl(project)
57 assertThat(projectInspectionProfileManager.state).isEmpty()
59 projectInspectionProfileManager.currentProfile
61 assertThat(projectInspectionProfileManager.state).isEmpty()
63 // cause to use app profile
64 projectInspectionProfileManager.setRootProfile(null)
65 val doNotUseProjectProfileState = """
68 <option name="USE_PROJECT_PROFILE" value="false" />
69 <version value="1.0" />
71 </state>""".trimIndent()
72 assertThat(projectInspectionProfileManager.state).isEqualTo(doNotUseProjectProfileState)
74 val inspectionDir = Paths.get(project.stateStore.stateStorageManager.expandMacros(PROJECT_CONFIG_DIR), "inspectionProfiles")
75 val file = inspectionDir.resolve("profiles_settings.xml")
77 assertThat(file).exists()
78 val doNotUseProjectProfileData = """
79 <component name="InspectionProjectProfileManager">
81 <option name="USE_PROJECT_PROFILE" value="false" />
82 <version value="1.0" />
84 </component>""".trimIndent()
85 assertThat(file.readText()).isEqualTo(doNotUseProjectProfileData)
90 project.baseDir.refresh(false, true)
91 (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm()
92 assertThat(projectInspectionProfileManager.state).isEmpty()
94 file.write(doNotUseProjectProfileData)
95 project.baseDir.refresh(false, true)
96 (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm()
97 assertThat(projectInspectionProfileManager.state).isEqualTo(doNotUseProjectProfileState)
101 @Test fun `profiles`() {
102 loadAndUseProject(tempDirManager, {
105 val projectInspectionProfileManager = ProjectInspectionProfileManager.getInstanceImpl(project)
107 assertThat(projectInspectionProfileManager.state).isEmpty()
109 // cause to use app profile
111 val currentProfile = projectInspectionProfileManager.currentProfile
112 assertThat(currentProfile.isProjectLevel).isTrue()
113 currentProfile.disableTool("Convert2Diamond", project)
118 val inspectionDir = Paths.get(project.stateStore.stateStorageManager.expandMacros(PROJECT_CONFIG_DIR), "inspectionProfiles")
119 val file = inspectionDir.resolve("profiles_settings.xml")
121 assertThat(file).doesNotExist()
122 val profileFile = inspectionDir.resolve("Project_Default.xml")
123 assertThat(profileFile.readText()).isEqualTo("""
124 <component name="InspectionProjectProfileManager">
125 <profile version="1.0">
126 <option name="myName" value="Project Default" />
127 <inspection_tool class="Convert2Diamond" enabled="false" level="WARNING" enabled_by_default="false" />
129 </component>""".trimIndent())
131 profileFile.write("""
132 <component name="InspectionProjectProfileManager">
133 <profile version="1.0">
134 <option name="myName" value="Project Default" />
135 <inspection_tool class="Convert2Diamond" enabled="false" level="ERROR" enabled_by_default="false" />
137 </component>""".trimIndent())
139 project.baseDir.refresh(false, true)
140 (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm()
142 assertThat(projectInspectionProfileManager.currentProfile.getToolDefaultState("Convert2Diamond", project).level).isEqualTo(HighlightDisplayLevel.ERROR)