0fee96760563660fe311a4368a764e4afe16cb16
[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
8 /**
9  * @author nik
10  */
11 class ImportedFacetsSerializationTest : FacetTestCase() {
12   override fun getProjectDirOrFile() = getProjectDirOrFile(true)
13
14   override fun isCreateProjectFileExplicitly() = false
15
16   fun `test regular facet`() {
17     addFacet()
18     assertEmpty(getExternalFacetStates())
19     val state = assertOneElement(facetManager.state.facets)
20     assertEquals("name", state.name)
21     assertNull(state.externalSystemId)
22     assertNull(FacetFromExternalSourcesStorage.getInstance(module).externalSource)
23   }
24
25   fun `test imported facet`() {
26     addFacet(createFacet("name"), MOCK_EXTERNAL_SOURCE)
27     assertEmpty(facetManager.state.facets)
28     val state = assertOneElement(getExternalFacetStates())
29     assertEquals("name", state.name)
30     assertNotNull(state.configuration)
31     assertEquals("mock", state.externalSystemId)
32     assertEquals("mock", FacetFromExternalSourcesStorage.getInstance(module).externalSource?.id)
33   }
34
35   fun `test regular facet and sub-facet`() {
36     createFacetAndSubFacet(null, null)
37     assertEmpty(getExternalFacetStates())
38     val state = assertOneElement(facetManager.state.facets)
39     checkFacetAndSubFacetState(state, true, true)
40     reloadStateAndCheckFacetAndSubFacet(null, null)
41   }
42
43   fun `test imported facet and sub-facet`() {
44     createFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, MOCK_EXTERNAL_SOURCE)
45     assertEmpty(facetManager.state.facets)
46     val state = assertOneElement(getExternalFacetStates())
47     checkFacetAndSubFacetState(state, true, true)
48     reloadStateAndCheckFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, MOCK_EXTERNAL_SOURCE)
49   }
50
51   fun `test imported facet and regular sub-facet`() {
52     createFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, null)
53     val state = assertOneElement(facetManager.state.facets)
54     checkFacetAndSubFacetState(state, false, true)
55
56     val externalState = assertOneElement(getExternalFacetStates())
57     checkFacetAndSubFacetState(externalState, true, false)
58     reloadStateAndCheckFacetAndSubFacet(MOCK_EXTERNAL_SOURCE, null)
59   }
60
61   fun `test regular facet and imported sub-facet`() {
62     createFacetAndSubFacet(null, MOCK_EXTERNAL_SOURCE)
63     val state = assertOneElement(facetManager.state.facets)
64     checkFacetAndSubFacetState(state, true, false)
65
66     val externalState = assertOneElement(getExternalFacetStates())
67     checkFacetAndSubFacetState(externalState, false, true)
68     reloadStateAndCheckFacetAndSubFacet(null, MOCK_EXTERNAL_SOURCE)
69   }
70
71   private fun createFacetAndSubFacet(facetSource: ProjectModelExternalSource?, subFacetSource: ProjectModelExternalSource?) {
72     val facet = addFacet(createFacet("name"), facetSource)
73     facet.configuration.data = "1"
74     val subFacet = addSubFacet(facet, "sub", subFacetSource)
75     subFacet.configuration.data = "2"
76   }
77
78   private fun reloadStateAndCheckFacetAndSubFacet(facetSource: ProjectModelExternalSource?, subFacetSource: ProjectModelExternalSource?) {
79     facetManager.loadState(facetManager.state)
80     val facets = facetManager.sortedFacets
81     assertEquals(2, facets.size)
82     val (facet, subFacet) = facets
83     assertEquals("name", facet.name)
84     assertEquals(facetSource?.id, facet.externalSource?.id)
85     assertEquals("1", (facet.configuration as MockFacetConfiguration).data)
86
87     assertEquals(facet, subFacet.underlyingFacet)
88     assertEquals("sub", subFacet.name)
89     assertEquals(subFacetSource?.id, subFacet.externalSource?.id)
90     assertEquals("2", (subFacet.configuration as MockFacetConfiguration).data)
91   }
92
93   private fun checkFacetAndSubFacetState(state: FacetState, hasConfiguration: Boolean, hasConfigurationOfSubFacet: Boolean) {
94     assertEquals("name", state.name)
95     if (hasConfiguration) {
96       assertNotNull(state.configuration)
97     }
98     else {
99       assertNull(state.configuration)
100     }
101
102     if (hasConfigurationOfSubFacet) {
103       val subState = assertOneElement(state.subFacets)
104       assertEquals("sub", subState.name)
105       assertNotNull(subState.configuration)
106     }
107     else {
108       assertEmpty(state.subFacets)
109     }
110   }
111
112   private fun getExternalFacetStates() = FacetFromExternalSourcesStorage.getInstance(module).state.facets
113 }
114
115 private val MOCK_EXTERNAL_SOURCE = object: ProjectModelExternalSource {
116   override fun getDisplayName() = "mock"
117
118   override fun getId() = "mock"
119 }