1   package org.apache.maven.project;
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  
24  import org.apache.maven.model.Build;
25  import org.apache.maven.model.Dependency;
26  import org.apache.maven.model.Extension;
27  import org.apache.maven.model.Model;
28  import org.codehaus.plexus.util.dag.CycleDetectedException;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  
34  /**
35   * Test sorting projects by dependencies.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38   * @version $Id: ProjectSorterTest.java 495147 2007-01-11 07:47:53Z jvanzyl $
39   */
40  public class ProjectSorterTest
41      extends TestCase
42  {
43      
44      public void testShouldNotFailWhenProjectReferencesNonExistentProject()
45          throws CycleDetectedException, DuplicateProjectException
46      {
47          MavenProject project = createProject( "group", "artifact", "1.0" );
48          Model model = project.getModel();
49          
50          Build build = model.getBuild();
51          
52          if ( build == null )
53          {
54              build = new Build();
55              model.setBuild( build );
56          }
57          
58          Extension extension = new Extension();
59          
60          extension.setArtifactId( "other-artifact" );
61          extension.setGroupId( "other.group" );
62          extension.setVersion( "1.0" );
63          
64          build.addExtension( extension );
65          
66          new ProjectSorter( Collections.singletonList( project ) );
67      }
68      
69      public void testMatchingArtifactIdsDifferentGroupIds()
70          throws CycleDetectedException, DuplicateProjectException
71      {
72          List projects = new ArrayList();
73          MavenProject project1 = createProject( "groupId1", "artifactId", "1.0" );
74          projects.add( project1 );
75          MavenProject project2 = createProject( "groupId2", "artifactId", "1.0" );
76          projects.add( project2 );
77          project1.getDependencies().add( createDependency( project2 ) );
78  
79          projects = new ProjectSorter( projects ).getSortedProjects();
80  
81          assertEquals( project2, projects.get( 0 ) );
82          assertEquals( project1, projects.get( 1 ) );
83      }
84  
85      public void testMatchingGroupIdsDifferentArtifactIds()
86          throws CycleDetectedException, DuplicateProjectException
87      {
88          List projects = new ArrayList();
89          MavenProject project1 = createProject( "groupId", "artifactId1", "1.0" );
90          projects.add( project1 );
91          MavenProject project2 = createProject( "groupId", "artifactId2", "1.0" );
92          projects.add( project2 );
93          project1.getDependencies().add( createDependency( project2 ) );
94  
95          projects = new ProjectSorter( projects ).getSortedProjects();
96  
97          assertEquals( project2, projects.get( 0 ) );
98          assertEquals( project1, projects.get( 1 ) );
99      }
100 
101     public void testMatchingIdsAndVersions()
102         throws CycleDetectedException
103     {
104         List projects = new ArrayList();
105         MavenProject project1 = createProject( "groupId", "artifactId", "1.0" );
106         projects.add( project1 );
107         MavenProject project2 = createProject( "groupId", "artifactId", "1.0" );
108         projects.add( project2 );
109 
110         try 
111         {
112             projects = new ProjectSorter( projects ).getSortedProjects();
113             fail( "Duplicate projects should fail" );
114         }
115         catch ( DuplicateProjectException e )
116         {
117             // expected
118             assertTrue( true );
119         }
120     }
121 
122     public void testMatchingIdsAndDifferentVersions()
123         throws CycleDetectedException
124     {
125         List projects = new ArrayList();
126         MavenProject project1 = createProject( "groupId", "artifactId", "1.0" );
127         projects.add( project1 );
128         MavenProject project2 = createProject( "groupId", "artifactId", "2.0" );
129         projects.add( project2 );
130 
131         try 
132         {
133             projects = new ProjectSorter( projects ).getSortedProjects();
134             fail( "Duplicate projects should fail" );
135         }
136         catch ( DuplicateProjectException e )
137         {
138             // expected
139             assertTrue( true );
140         }
141     }
142 
143     private Dependency createDependency( MavenProject project )
144     {
145         Dependency depdendency = new Dependency();
146         depdendency.setArtifactId( project.getArtifactId() );
147         depdendency.setGroupId( project.getGroupId() );
148         depdendency.setVersion( project.getVersion() );
149         return depdendency;
150     }
151 
152     private static MavenProject createProject( String groupId, String artifactId, String version )
153     {
154         Model model = new Model();
155         model.setGroupId( groupId );
156         model.setArtifactId( artifactId );
157         model.setVersion( version );
158         return new MavenProject( model );
159     }
160 }