c8ec3829baed1714214e849c936e85c6b09ce716
[idea/community.git] / plugins / gradle / testSources / org / jetbrains / plugins / gradle / sync / GradleProjectStructureChangesModelTest.groovy
1 package org.jetbrains.plugins.gradle.sync;
2
3
4 import com.intellij.openapi.Disposable
5 import com.intellij.openapi.application.Application
6 import com.intellij.openapi.application.ApplicationManager
7 import com.intellij.openapi.application.ex.ApplicationInfoEx
8 import com.intellij.openapi.project.Project
9 import com.intellij.testFramework.SkipInHeadlessEnvironment
10 import com.intellij.util.containers.ContainerUtil
11 import org.jetbrains.plugins.gradle.testutil.ChangeBuilder
12 import org.jetbrains.plugins.gradle.testutil.GradleProjectBuilder
13 import org.jetbrains.plugins.gradle.testutil.IntellijProjectBuilder
14 import org.jetbrains.plugins.gradle.testutil.ProjectStructureChecker
15 import org.junit.After
16 import org.junit.Before
17 import org.junit.Test
18 import org.picocontainer.defaults.DefaultPicoContainer
19 import org.jetbrains.plugins.gradle.diff.*
20 import static org.junit.Assert.assertEquals
21 import com.intellij.openapi.util.Disposer
22
23 /**
24  * @author Denis Zhdanov
25  * @since 01/25/2012
26  */
27 @SkipInHeadlessEnvironment
28 public class GradleProjectStructureChangesModelTest {
29
30   GradleProjectStructureChangesModel changesModel
31   GradleProjectStructureTreeModel treeModel
32   def gradle
33   def intellij
34   def changes
35   def treeChecker
36   def container
37   Disposable disposable = [dispose: { }] as Disposable
38   
39   @Before
40   public void setUp() {
41     gradle = new GradleProjectBuilder()
42     intellij = new IntellijProjectBuilder()
43     changes = new ChangeBuilder()
44     treeChecker = new ProjectStructureChecker()
45     container = new DefaultPicoContainer()
46     container.registerComponentInstance(Project, intellij.project)
47     container.registerComponentInstance(GradleProjectStructureHelper, intellij.projectStructureHelper as GradleProjectStructureHelper)
48     container.registerComponentImplementation(GradleProjectStructureChangesModel)
49     container.registerComponentImplementation(GradleStructureChangesCalculator, GradleProjectStructureChangesCalculator)
50     container.registerComponentImplementation(GradleModuleStructureChangesCalculator)
51     container.registerComponentImplementation(GradleLibraryDependencyStructureChangesCalculator)
52     container.registerComponentImplementation(GradleProjectStructureTreeModel)
53     
54     changesModel = container.getComponentInstance(GradleProjectStructureChangesModel) as GradleProjectStructureChangesModel
55     def applicationInfo = [getSmallIconUrl: {"/nodes/ideaProject.png"}] as ApplicationInfoEx
56     ApplicationManager.setApplication([getComponent: { applicationInfo } ] as Application, disposable)
57   }
58
59   @After
60   public void tearDown() {
61     Disposer.dispose(disposable)
62   }
63   
64   @Test
65   public void mergeGradleLocalToIntellij() {
66     // Configure initial projects state.
67     init(
68       gradle {
69         module {
70           dependencies {
71             lib(name: "lib1")
72             lib(name: "lib2")
73       } } },
74
75       intellij {
76         module {
77           dependencies {
78             lib(name: "lib1")
79       } } }
80     )
81     
82     // Check that the initial projects state is correctly parsed.
83     checkChanges {
84       presence {
85         lib(gradle: gradle.modules.dependencies.flatten().findAll { it.name == "lib2" })
86     } }
87     checkTree {
88       project {
89         module("xxx") {
90           dependencies {
91             lib1()
92             lib2('gradle')
93     } } } }
94
95     // Define changed project state.
96     gradle {
97       module {
98         dependencies {
99           lib(name: "lib1")
100     } } }
101     
102     // Apply the changed project state and check if it's correctly parsed.
103     changesModel.update(gradle.project)
104     assertEquals([].toSet(), changesModel.changes)
105     checkTree {
106       project {
107         module {
108           dependencies {
109             lib1()
110     } } } }
111   }
112
113   private def init(gradleProjectInit, intellijProjectInit) {
114     treeModel = container.getComponentInstance(GradleProjectStructureTreeModel) as GradleProjectStructureTreeModel
115     changesModel.addListener({ old, current ->
116       treeModel.update(current)
117       treeModel.pruneObsoleteNodes(ContainerUtil.subtract(old, current));
118     } as GradleProjectStructureChangeListener)
119     changesModel.update(gradle.project)
120   }
121
122   private def checkChanges(c) {
123     c.delegate = changes
124     assertEquals(c(), changesModel.changes)
125   }
126
127   private def checkTree(c) {
128     def nodeBuilder = new NodeBuilder()
129     c.delegate = nodeBuilder
130     def expected = c()
131     treeChecker.check(expected, treeModel.root)
132   }
133 }