View Javadoc

1   package org.apache.continuum.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:jmcconnell@apache.org">Jesse McConnell</a>
33   * @version $Id:$
34   */
35  public class ProjectSorterTest
36      extends TestCase
37  {
38  
39      /**
40       * test basic three project tree (really a line in this case)
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          // We add projects in a random order to really check the project orter
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 ); //top project must be the first
102         assertEquals( top.getArtifactId(), p1.getArtifactId() );
103         Project p4 = sortedList.get( 3 ); //ear1 project must be the latest
104         assertEquals( ear1.getArtifactId(), p4.getArtifactId() );
105     }
106 
107     /**
108      * test project build order
109      * build order: B -> A -> D -> C -> E
110      *
111      * @throws Exception
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      * test one of the child projects not having the artifactId or groupId empty and working off the
157      * name instead
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      * project sorter can work with name replacing the artifactid and groupId
190      *
191      * @param projectId The project id
192      * @return The generated Project
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 }