IDEA-76142: Gradle support - cannot update IDEA projects once one of build.gradle...
[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.project.Project
6 import com.intellij.openapi.util.Disposer
7 import com.intellij.testFramework.SkipInHeadlessEnvironment
8 import com.intellij.util.containers.ContainerUtil
9 import org.jetbrains.plugins.gradle.testutil.ChangeBuilder
10 import org.jetbrains.plugins.gradle.testutil.GradleProjectBuilder
11 import org.jetbrains.plugins.gradle.testutil.IntellijProjectBuilder
12 import org.jetbrains.plugins.gradle.testutil.ProjectStructureChecker
13 import org.junit.After
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
20 /**
21  * @author Denis Zhdanov
22  * @since 01/25/2012
23  */
24 @SkipInHeadlessEnvironment
25 public class GradleProjectStructureChangesModelTest {
26
27   GradleProjectStructureChangesModel changesModel
28   GradleProjectStructureTreeModel treeModel
29   def gradle
30   def intellij
31   def changes
32   def treeChecker
33   def container
34   Disposable disposable = [dispose: { }] as Disposable
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(PlatformFacade, intellij.platformFacade as PlatformFacade)
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   }
53
54   @After
55   public void tearDown() {
56     Disposer.dispose(disposable)
57   }
58   
59   @Test
60   public void mergeGradleLocalToIntellij() {
61     // Configure initial projects state.
62     init(
63       gradle {
64         module {
65           dependencies {
66             lib(name: "lib1")
67             lib(name: "lib2")
68       } } },
69
70       intellij {
71         module {
72           dependencies {
73             lib(name: "lib1")
74       } } }
75     )
76     
77     // Check that the initial projects state is correctly parsed.
78     checkChanges {
79       presence {
80         lib(gradle: gradle.modules.dependencies.flatten().findAll { it.name == "lib2" })
81     } }
82     checkTree {
83       project {
84         module("xxx") {
85           dependencies {
86             lib1()
87             lib2('gradle')
88     } } } }
89
90     // Define changed project state.
91     gradle {
92       module {
93         dependencies {
94           lib(name: "lib1")
95     } } }
96     
97     // Apply the changed project state and check if it's correctly parsed.
98     changesModel.update(gradle.project)
99     assertEquals([].toSet(), changesModel.changes)
100     checkTree {
101       project {
102         module {
103           dependencies {
104             lib1()
105     } } } }
106   }
107
108   @SuppressWarnings("GroovyAssignabilityCheck")
109   private def init(gradleProjectInit, intellijProjectInit) {
110     treeModel = container.getComponentInstance(GradleProjectStructureTreeModel) as GradleProjectStructureTreeModel
111     changesModel.addListener({ old, current ->
112       treeModel.update(current)
113       treeModel.pruneObsoleteNodes(ContainerUtil.subtract(old, current));
114     } as GradleProjectStructureChangeListener)
115     changesModel.update(gradle.project)
116   }
117
118   private def checkChanges(c) {
119     c.delegate = changes
120     assertEquals(c(), changesModel.changes)
121   }
122
123   private def checkTree(c) {
124     def nodeBuilder = new NodeBuilder()
125     c.delegate = nodeBuilder
126     def expected = c()
127     treeChecker.check(expected, treeModel.root)
128   }
129 }