fix SingleInspectionProfilePanelTest
[idea/community.git] / java / java-tests / testSrc / com / intellij / codeInspection / ex / ProjectInspectionManagerTest.kt
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.codeInspection.ex
17
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
35 import org.junit.Rule
36 import org.junit.Test
37 import java.nio.file.Paths
38
39 internal class ProjectInspectionManagerTest {
40   companion object {
41     @JvmField
42     @ClassRule
43     val projectRule = ProjectRule()
44   }
45
46   val tempDirManager = TemporaryDirectory()
47
48   private val ruleChain = RuleChain(tempDirManager)
49   @Rule fun getChain() = ruleChain
50
51   @Test fun `component`() {
52     loadAndUseProject(tempDirManager, {
53       it.path
54     }) { project ->
55       val projectInspectionProfileManager = ProjectInspectionProfileManager.getInstanceImpl(project)
56
57       assertThat(projectInspectionProfileManager.state).isEmpty()
58
59       projectInspectionProfileManager.currentProfile
60
61       assertThat(projectInspectionProfileManager.state).isEmpty()
62
63       // cause to use app profile
64       projectInspectionProfileManager.setRootProfile(null)
65       val doNotUseProjectProfileState = """
66       <state>
67         <settings>
68           <option name="USE_PROJECT_PROFILE" value="false" />
69           <version value="1.0" />
70         </settings>
71       </state>""".trimIndent()
72       assertThat(projectInspectionProfileManager.state).isEqualTo(doNotUseProjectProfileState)
73
74       val inspectionDir = Paths.get(project.stateStore.stateStorageManager.expandMacros(PROJECT_CONFIG_DIR), "inspectionProfiles")
75       val file = inspectionDir.resolve("profiles_settings.xml")
76       project.saveStore()
77       assertThat(file).exists()
78       val doNotUseProjectProfileData = """
79       <component name="InspectionProjectProfileManager">
80         <settings>
81           <option name="USE_PROJECT_PROFILE" value="false" />
82           <version value="1.0" />
83         </settings>
84       </component>""".trimIndent()
85       assertThat(file.readText()).isEqualTo(doNotUseProjectProfileData)
86
87       // test load
88       file.delete()
89
90       project.baseDir.refresh(false, true)
91       (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm()
92       assertThat(projectInspectionProfileManager.state).isEmpty()
93
94       file.write(doNotUseProjectProfileData)
95       project.baseDir.refresh(false, true)
96       (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm()
97       assertThat(projectInspectionProfileManager.state).isEqualTo(doNotUseProjectProfileState)
98     }
99   }
100
101   @Test fun `profiles`() {
102     loadAndUseProject(tempDirManager, {
103       it.path
104     }) { project ->
105       val projectInspectionProfileManager = ProjectInspectionProfileManager.getInstanceImpl(project)
106
107       assertThat(projectInspectionProfileManager.state).isEmpty()
108
109       // cause to use app profile
110       runInInitMode {
111         val currentProfile = projectInspectionProfileManager.currentProfile
112         assertThat(currentProfile.isProjectLevel).isTrue()
113         currentProfile.disableTool("Convert2Diamond", project)
114       }
115
116       project.saveStore()
117
118       val inspectionDir = Paths.get(project.stateStore.stateStorageManager.expandMacros(PROJECT_CONFIG_DIR), "inspectionProfiles")
119       val file = inspectionDir.resolve("profiles_settings.xml")
120
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" />
128         </profile>
129       </component>""".trimIndent())
130
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" />
136         </profile>
137       </component>""".trimIndent())
138
139       project.baseDir.refresh(false, true)
140       (ProjectManager.getInstance() as StoreAwareProjectManager).flushChangedAlarm()
141       runInInitMode {
142         assertThat(projectInspectionProfileManager.currentProfile.getToolDefaultState("Convert2Diamond", project).level).isEqualTo(HighlightDisplayLevel.ERROR)
143       }
144     }
145   }
146 }