KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > ProjectSorterTest


1 package org.apache.maven.project;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20
21 import org.apache.maven.model.Dependency;
22 import org.apache.maven.model.Model;
23 import org.codehaus.plexus.util.dag.CycleDetectedException;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Test sorting projects by dependencies.
30  *
31  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
32  * @version $Id: ProjectSorterTest.java 327885 2005-10-23 23:50:38Z brett $
33  */

34 public class ProjectSorterTest
35     extends TestCase
36 {
37     public void testMatchingArtifactIdsDifferentGroupIds()
38         throws CycleDetectedException, DuplicateProjectException
39     {
40         List JavaDoc projects = new ArrayList JavaDoc();
41         MavenProject project1 = createProject( "groupId1", "artifactId", "1.0" );
42         projects.add( project1 );
43         MavenProject project2 = createProject( "groupId2", "artifactId", "1.0" );
44         projects.add( project2 );
45         project1.getDependencies().add( createDependency( project2 ) );
46
47         projects = new ProjectSorter( projects ).getSortedProjects();
48
49         assertEquals( project2, projects.get( 0 ) );
50         assertEquals( project1, projects.get( 1 ) );
51     }
52
53     public void testMatchingGroupIdsDifferentArtifactIds()
54         throws CycleDetectedException, DuplicateProjectException
55     {
56         List JavaDoc projects = new ArrayList JavaDoc();
57         MavenProject project1 = createProject( "groupId", "artifactId1", "1.0" );
58         projects.add( project1 );
59         MavenProject project2 = createProject( "groupId", "artifactId2", "1.0" );
60         projects.add( project2 );
61         project1.getDependencies().add( createDependency( project2 ) );
62
63         projects = new ProjectSorter( projects ).getSortedProjects();
64
65         assertEquals( project2, projects.get( 0 ) );
66         assertEquals( project1, projects.get( 1 ) );
67     }
68
69     public void testMatchingIdsAndVersions()
70         throws CycleDetectedException
71     {
72         List JavaDoc projects = new ArrayList JavaDoc();
73         MavenProject project1 = createProject( "groupId", "artifactId", "1.0" );
74         projects.add( project1 );
75         MavenProject project2 = createProject( "groupId", "artifactId", "1.0" );
76         projects.add( project2 );
77
78         try
79         {
80             projects = new ProjectSorter( projects ).getSortedProjects();
81             fail( "Duplicate projects should fail" );
82         }
83         catch ( DuplicateProjectException e )
84         {
85             // expected
86
assertTrue( true );
87         }
88     }
89
90     public void testMatchingIdsAndDifferentVersions()
91         throws CycleDetectedException
92     {
93         List JavaDoc projects = new ArrayList JavaDoc();
94         MavenProject project1 = createProject( "groupId", "artifactId", "1.0" );
95         projects.add( project1 );
96         MavenProject project2 = createProject( "groupId", "artifactId", "2.0" );
97         projects.add( project2 );
98
99         try
100         {
101             projects = new ProjectSorter( projects ).getSortedProjects();
102             fail( "Duplicate projects should fail" );
103         }
104         catch ( DuplicateProjectException e )
105         {
106             // expected
107
assertTrue( true );
108         }
109     }
110
111     private Dependency createDependency( MavenProject project )
112     {
113         Dependency depdendency = new Dependency();
114         depdendency.setArtifactId( project.getArtifactId() );
115         depdendency.setGroupId( project.getGroupId() );
116         depdendency.setVersion( project.getVersion() );
117         return depdendency;
118     }
119
120     private static MavenProject createProject( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version )
121     {
122         Model model = new Model();
123         model.setGroupId( groupId );
124         model.setArtifactId( artifactId );
125         model.setVersion( version );
126         return new MavenProject( model );
127     }
128 }
129
Popular Tags