[workspace model] provide implementation of FacetManager which stores data in workspa...
[idea/community.git] / java / idea-ui / testSrc / com / intellij / facet / ImportedFacetsSerializationTest.kt
1 // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.facet
3
4 import com.intellij.facet.mock.MockFacetConfiguration
5 import com.intellij.openapi.roots.ProjectModelExternalSource
6 import org.jetbrains.jps.model.serialization.facet.FacetState
7 import org.junit.Assume
8
9 /**
10  * @author nik
11  */
12 class ImportedFacetsSerializationTest : FacetTestCase() {
13   override fun getProjectDirOrFile() = getProjectDirOrFile(true)
14
15   override fun isCreateProjectFileExplicitly() = false
16
17   fun `test regular facet`() {
18     addFacet()
19     assertEmpty(getExternalFacetStates())
20     val state = assertOneElement(facetManager.state.facets)
21     assertEquals("name", state.name)
22     assertNull(state.externalSystemId)
23     assertNull(FacetFromExternalSourcesStorage.getInstance(module).externalSource)
24   }
25
26   fun `test imported facet`() {
27     addFacet(createFacet("name"), MOCK_EXTERNAL_SOURCE)
28     assertEmpty(facetManager.state.facets)
29     val state = assertOneElement(getExternalFacetStates())
30     assertEquals("name", state.name)
31     assertNotNull(state.configuration)
32     assertEquals("mock", state.externalSystemId)
33     assertEquals("mock", FacetFromExternalSourcesStorage.getInstance(module).externalSource?.id)
34   }
35
36   fun `test regular facet and sub-facet`() {
37     createFacetAndSubFacet(null, null)
38     assertEmpty(getExternalFacetStates())
39     val state = assertOneElement(facetManager.state.facets)
40     checkFacetAndSubFacetState(state, true, true)
41     reloadStateAndCheckFacetAndSubFacet(null, null)
42   }
43
44   fun `test imported facet and sub-facet`() {
45     createFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, MOCK_EXTERNAL_SOURCE)
46     assertEmpty(facetManager.state.facets)
47     val state = assertOneElement(getExternalFacetStates())
48     checkFacetAndSubFacetState(state, true, true)
49     reloadStateAndCheckFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, MOCK_EXTERNAL_SOURCE)
50   }
51
52   fun `test imported facet and regular sub-facet`() {
53     createFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, null)
54     val state = assertOneElement(facetManager.state.facets)
55     checkFacetAndSubFacetState(state, false, true)
56
57     val externalState = assertOneElement(getExternalFacetStates())
58     checkFacetAndSubFacetState(externalState, true, false)
59     reloadStateAndCheckFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, null)
60   }
61
62   fun `test regular facet and imported sub-facet`() {
63     createFacetAndSubFacet(null, MOCK_EXTERNAL_SOURCE)
64     val state = assertOneElement(facetManager.state.facets)
65     checkFacetAndSubFacetState(state, true, false)
66
67     val externalState = assertOneElement(getExternalFacetStates())
68     checkFacetAndSubFacetState(externalState, false, true)
69     reloadStateAndCheckFacetAndSubFacet(null, MOCK_EXTERNAL_SOURCE)
70   }
71
72   private fun createFacetAndSubFacet(facetSource: ProjectModelExternalSource?, subFacetSource: ProjectModelExternalSource?) {
73     val facet = addFacet(createFacet("name"), facetSource)
74     facet.configuration.data = "1"
75     val subFacet = addSubFacet(facet, "sub", subFacetSource)
76     subFacet.configuration.data = "2"
77   }
78
79   private fun reloadStateAndCheckFacetAndSubFacet(facetSource: ProjectModelExternalSource?, subFacetSource: ProjectModelExternalSource?) {
80     facetManager.loadState(facetManager.state)
81     val facets = facetManager.sortedFacets
82     assertEquals(2, facets.size)
83     val (facet, subFacet) = facets
84     assertEquals("name", facet.name)
85     assertEquals(facetSource?.id, facet.externalSource?.id)
86     assertEquals("1", (facet.configuration as MockFacetConfiguration).data)
87
88     assertEquals(facet, subFacet.underlyingFacet)
89     assertEquals("sub", subFacet.name)
90     assertEquals(subFacetSource?.id, subFacet.externalSource?.id)
91     assertEquals("2", (subFacet.configuration as MockFacetConfiguration).data)
92   }
93
94   private fun checkFacetAndSubFacetState(state: FacetState, hasConfiguration: Boolean, hasConfigurationOfSubFacet: Boolean) {
95     assertEquals("name", state.name)
96     if (hasConfiguration) {
97       assertNotNull(state.configuration)
98     }
99     else {
100       assertNull(state.configuration)
101     }
102
103     if (hasConfigurationOfSubFacet) {
104       val subState = assertOneElement(state.subFacets)
105       assertEquals("sub", subState.name)
106       assertNotNull(subState.configuration)
107     }
108     else {
109       assertEmpty(state.subFacets)
110     }
111   }
112
113   override fun getFacetManager(): FacetManagerImpl {
114     val facetManager = super.getFacetManager()
115     Assume.assumeTrue("Test is ignored in workspace model", facetManager is FacetManagerImpl)
116     return facetManager as FacetManagerImpl
117   }
118
119   private fun getExternalFacetStates() = FacetFromExternalSourcesStorage.getInstance(module).state.facets
120 }
121
122 private val MOCK_EXTERNAL_SOURCE = object: ProjectModelExternalSource {
123   override fun getDisplayName() = "mock"
124
125   override fun getId() = "mock"
126 }