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