1 package org.apache.continuum.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.maven.continuum.model.project.Project;
24 import org.apache.maven.continuum.model.project.ProjectDependency;
25 import org.apache.maven.continuum.model.project.ProjectGroup;
26
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30
31
32
33
34
35 public class ProjectSorterTest
36 extends TestCase
37 {
38
39
40
41
42 public void testBasicNestedProjectStructure()
43 throws Exception
44 {
45 List<Project> list = new ArrayList<Project>();
46
47 Project top = getNewProject( "top" );
48 list.add( top );
49
50 Project c1 = getNewProject( "c1" );
51 c1.setParent( generateProjectDependency( top ) );
52 list.add( c1 );
53
54 Project c2 = getNewProject( "c2" );
55 c2.setParent( generateProjectDependency( top ) );
56 c2.setDependencies( Collections.singletonList( generateProjectDependency( c1 ) ) );
57 list.add( c2 );
58
59 List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
60
61 assertNotNull( sortedList );
62
63 Project p1 = sortedList.get( 0 );
64 assertEquals( top.getArtifactId(), p1.getArtifactId() );
65 Project p2 = sortedList.get( 1 );
66 assertEquals( c1.getArtifactId(), p2.getArtifactId() );
67 Project p3 = sortedList.get( 2 );
68 assertEquals( c2.getArtifactId(), p3.getArtifactId() );
69 }
70
71 public void testNestedProjectStructureWithoutModulesDefinedInParentPom()
72 throws Exception
73 {
74 Project top = getNewProject( "top" );
75
76 Project war1 = getNewProject( "war1" );
77 war1.setParent( generateProjectDependency( top ) );
78
79 Project war2 = getNewProject( "war2" );
80 war2.setParent( generateProjectDependency( top ) );
81
82 Project ear1 = getNewProject( "ear1" );
83 ear1.setParent( generateProjectDependency( top ) );
84 List<ProjectDependency> deps = new ArrayList<ProjectDependency>();
85 deps.add( generateProjectDependency( war1 ) );
86 deps.add( generateProjectDependency( war2 ) );
87 ear1.setDependencies( deps );
88
89 List<Project> list = new ArrayList<Project>();
90
91
92 list.add( top );
93 list.add( ear1 );
94 list.add( war1 );
95 list.add( war2 );
96
97 List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
98
99 assertNotNull( sortedList );
100
101 Project p1 = sortedList.get( 0 );
102 assertEquals( top.getArtifactId(), p1.getArtifactId() );
103 Project p4 = sortedList.get( 3 );
104 assertEquals( ear1.getArtifactId(), p4.getArtifactId() );
105 }
106
107
108
109
110
111
112
113 public void testProjectBuildOrder()
114 throws Exception
115 {
116 List<Project> list = new ArrayList<Project>();
117
118 Project projectA = getNewProject( "A" );
119 Project projectB = getNewProject( "B" );
120 Project projectC = getNewProject( "C" );
121 Project projectD = getNewProject( "D" );
122 Project projectE = getNewProject( "E" );
123
124 projectA.setParent( generateProjectDependency( projectB ) );
125 projectE.setParent( generateProjectDependency( projectB ) );
126 projectC.setParent( generateProjectDependency( projectA ) );
127 projectC.setDependencies( Collections.singletonList( generateProjectDependency( projectD ) ) );
128 projectD.setParent( generateProjectDependency( projectA ) );
129
130 list.add( projectA );
131 list.add( projectB );
132 list.add( projectC );
133 list.add( projectD );
134 list.add( projectE );
135
136 List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
137 assertNotNull( sortedList );
138
139 List<Project> expectedList = new ArrayList<Project>();
140
141 expectedList.add( projectB );
142 expectedList.add( projectA );
143 expectedList.add( projectD );
144 expectedList.add( projectC );
145 expectedList.add( projectE );
146
147 for ( int i = 0; i < sortedList.size(); i++ )
148 {
149 Project sorted = sortedList.get( i );
150 Project expected = expectedList.get( i );
151 assertEquals( sorted.getArtifactId(), expected.getArtifactId() );
152 }
153 }
154
155
156
157
158
159 public void testIncompleteNestedProjectStructure()
160 throws Exception
161 {
162 List<Project> list = new ArrayList<Project>();
163
164 Project top = getNewProject( "top" );
165 list.add( top );
166
167 Project c1 = getIncompleteProject( "c1" );
168 c1.setParent( generateProjectDependency( top ) );
169 list.add( c1 );
170
171 Project c2 = getNewProject( "c2" );
172 c2.setParent( generateProjectDependency( top ) );
173 c2.setDependencies( Collections.singletonList( generateProjectDependency( c1 ) ) );
174 list.add( c2 );
175
176 List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
177
178 assertNotNull( sortedList );
179
180 Project p1 = sortedList.get( 0 );
181 assertEquals( top.getArtifactId(), p1.getArtifactId() );
182 Project p2 = sortedList.get( 1 );
183 assertEquals( c1.getArtifactId(), p2.getArtifactId() );
184 Project p3 = sortedList.get( 2 );
185 assertEquals( c2.getArtifactId(), p3.getArtifactId() );
186 }
187
188
189
190
191
192
193
194 private Project getIncompleteProject( String projectId )
195 {
196 Project project = new Project();
197 project.setName( "foo" + projectId );
198 project.setVersion( "v" + projectId );
199 project.setProjectGroup( new ProjectGroup() );
200
201 return project;
202 }
203
204 private Project getNewProject( String projectId )
205 {
206 Project project = new Project();
207 project.setArtifactId( "a" + projectId );
208 project.setGroupId( "g" + projectId );
209 project.setVersion( "v" + projectId );
210 project.setName( "n" + projectId );
211 project.setProjectGroup( new ProjectGroup() );
212
213 return project;
214 }
215
216 private ProjectDependency generateProjectDependency( Project project )
217 {
218 ProjectDependency dep = new ProjectDependency();
219 dep.setArtifactId( project.getArtifactId() );
220 dep.setGroupId( project.getGroupId() );
221 dep.setVersion( project.getVersion() );
222
223 return dep;
224 }
225
226 }