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 java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.continuum.model.project.Project;
27  import org.apache.maven.continuum.model.project.ProjectDependency;
28  import org.apache.maven.continuum.model.project.ProjectGroup;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * @author <a href="mailto:jmcconnell@apache.org">Jesse McConnell</a>
34   * @version $Id:$
35   */
36  public class ProjectSorterTest
37      extends TestCase
38  {
39  
40      /**
41       * test basic three project tree (really a line in this case)
42       */
43      public void testBasicNestedProjectStructure()
44          throws Exception
45      {
46          List<Project> list = new ArrayList<Project>();
47  
48          Project top = getNewProject( "top" );
49          list.add( top );
50  
51          Project c1 = getNewProject( "c1" );
52          c1.setParent( generateProjectDependency( top ) );
53          list.add( c1 );
54  
55          Project c2 = getNewProject( "c2" );
56          c2.setParent( generateProjectDependency( top ) );
57          c2.setDependencies( Collections.singletonList( generateProjectDependency( c1 ) ) );
58          list.add( c2 );
59  
60          List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
61  
62          assertNotNull( sortedList );
63  
64          Project p1 = sortedList.get( 0 );
65          assertEquals( top.getArtifactId(), p1.getArtifactId() );
66          Project p2 = sortedList.get( 1 );
67          assertEquals( c1.getArtifactId(), p2.getArtifactId() );
68          Project p3 = sortedList.get( 2 );
69          assertEquals( c2.getArtifactId(), p3.getArtifactId() );
70      }
71  
72      public void testNestedProjectStructureWithoutModulesDefinedInParentPom()
73          throws Exception
74      {
75          Project top = getNewProject( "top" );
76  
77          Project war1 = getNewProject( "war1" );
78          war1.setParent( generateProjectDependency( top ) );
79  
80          Project war2 = getNewProject( "war2" );
81          war2.setParent( generateProjectDependency( top ) );
82  
83          Project ear1 = getNewProject( "ear1" );
84          ear1.setParent( generateProjectDependency( top ) );
85          List<ProjectDependency> deps = new ArrayList<ProjectDependency>();
86          deps.add( generateProjectDependency( war1 ) );
87          deps.add( generateProjectDependency( war2 ) );
88          ear1.setDependencies( deps );
89  
90          List<Project> list = new ArrayList<Project>();
91  
92          // We add projects in a random order to really check the project orter
93          list.add( top );
94          list.add( ear1 );
95          list.add( war1 );
96          list.add( war2 );
97  
98          List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
99  
100         assertNotNull( sortedList );
101 
102         Project p1 = sortedList.get( 0 ); //top project must be the first
103         assertEquals( top.getArtifactId(), p1.getArtifactId() );
104         Project p4 = sortedList.get( 3 ); //ear1 project must be the latest
105         assertEquals( ear1.getArtifactId(), p4.getArtifactId() );
106     }
107     
108     /**
109      * test project build order
110      * build order: B -> A -> D -> C -> E
111      *
112      * @throws Exception
113      */
114     public void testProjectBuildOrder()
115         throws Exception
116     {
117         List<Project> list = new ArrayList<Project>();
118 
119         Project projectA = getNewProject( "A" );
120         Project projectB = getNewProject( "B" );
121         Project projectC = getNewProject( "C" );
122         Project projectD = getNewProject( "D" );
123         Project projectE = getNewProject( "E" );
124         
125         projectA.setParent( generateProjectDependency( projectB ) );
126         projectE.setParent( generateProjectDependency( projectB ) );
127         projectC.setParent( generateProjectDependency( projectA ) );
128         projectC.setDependencies( Collections.singletonList( generateProjectDependency( projectD ) ) );
129         projectD.setParent( generateProjectDependency( projectA ) );
130                 
131         list.add( projectA );
132         list.add( projectB );
133         list.add( projectC );
134         list.add( projectD );
135         list.add( projectE );
136 
137         List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
138         assertNotNull( sortedList );
139         
140         List<Project> expectedList = new ArrayList<Project>();
141         
142         expectedList.add( projectB );
143         expectedList.add( projectA );
144         expectedList.add( projectD );
145         expectedList.add( projectC );
146         expectedList.add( projectE );
147         
148         for ( int i = 0; i < sortedList.size(); i++ )
149         {
150             Project sorted = sortedList.get( i );
151             Project expected = expectedList.get( i );
152             assertEquals( sorted.getArtifactId(), expected.getArtifactId() );
153         }
154     }
155 
156     /**
157      * test one of the child projects not having the artifactId or groupId empty and working off the
158      * name instead
159      */
160     public void testIncompleteNestedProjectStructure()
161         throws Exception
162     {
163         List<Project> list = new ArrayList<Project>();
164 
165         Project top = getNewProject( "top" );
166         list.add( top );
167 
168         Project c1 = getIncompleteProject( "c1" );
169         c1.setParent( generateProjectDependency( top ) );
170         list.add( c1 );
171 
172         Project c2 = getNewProject( "c2" );
173         c2.setParent( generateProjectDependency( top ) );
174         c2.setDependencies( Collections.singletonList( generateProjectDependency( c1 ) ) );
175         list.add( c2 );
176 
177         List<Project> sortedList = ProjectSorter.getSortedProjects( list, null );
178 
179         assertNotNull( sortedList );
180 
181         Project p1 = sortedList.get( 0 );
182         assertEquals( top.getArtifactId(), p1.getArtifactId() );
183         Project p2 = sortedList.get( 1 );
184         assertEquals( c1.getArtifactId(), p2.getArtifactId() );
185         Project p3 = sortedList.get( 2 );
186         assertEquals( c2.getArtifactId(), p3.getArtifactId() );
187     }
188 
189     /**
190      * project sorter can work with name replacing the artifactid and groupId
191      *
192      * @param projectId The project id
193      * @return The generated Project
194      */
195     private Project getIncompleteProject( String projectId )
196     {
197         Project project = new Project();
198         project.setName( "foo" + projectId );
199         project.setVersion( "v" + projectId );
200         project.setProjectGroup( new ProjectGroup() );
201 
202         return project;
203     }
204 
205     private Project getNewProject( String projectId )
206     {
207         Project project = new Project();
208         project.setArtifactId( "a" + projectId );
209         project.setGroupId( "g" + projectId );
210         project.setVersion( "v" + projectId );
211         project.setName( "n" + projectId );
212         project.setProjectGroup( new ProjectGroup() );
213 
214         return project;
215     }
216 
217     private ProjectDependency generateProjectDependency( Project project )
218     {
219         ProjectDependency dep = new ProjectDependency();
220         dep.setArtifactId( project.getArtifactId() );
221         dep.setGroupId( project.getGroupId() );
222         dep.setVersion( project.getVersion() );
223 
224         return dep;
225     }
226 
227 }