1   package org.apache.maven.jelly.tags.maven;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.maven.project.Project;
27  
28  /**
29   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
30   * @version $Id: DependencyResolverTest.java 517014 2007-03-11 21:15:50Z ltheussl $
31   */
32  public class DependencyResolverTest
33      extends TestCase
34  {
35      private WerkzDependencyResolver dri;
36  
37      public void testSimple1()
38          throws Exception
39      {
40          List projects = DependencyResolverTestData.getTestSimple1();
41          testSimple1Source( dri, projects );
42          testSimple1Binary( dri, projects );
43      }
44  
45      public void testSimple2()
46          throws Exception
47      {
48          List projects = DependencyResolverTestData.getTestSimple2();
49          testSimple2( dri, projects );
50      }
51  
52      public void testSimple3()
53          throws Exception
54      {
55          List projects = DependencyResolverTestData.getTestSimple3();
56          testSimple3( dri, projects );
57      }
58  
59      public void testComplex()
60          throws Exception
61      {
62          List projects = DependencyResolverTestData.getTestComplex();
63          testComplex( dri, projects );
64      }
65  
66      public void testCycle()
67          throws Exception
68      {
69          List projects = DependencyResolverTestData.getTestCycle();
70          testCycle( dri, projects );
71      }
72  
73      public void testCycle( DependencyResolverInterface dri, List projects )
74          throws Exception
75      {
76          DependencyResolver dr = new DependencyResolver( dri );
77          dr.setProjects( projects );
78          try
79          {
80              dr.getSortedDependencies( true );
81              fail( "Shouldn't be able to resolve cycles" );
82          }
83          catch ( Exception e )
84          {
85              //Success
86          }
87  
88          try
89          {
90              dr.getSortedDependencies( false );
91              fail( "Shouldn't be able to resolve cycles" );
92          }
93          catch ( Exception e )
94          {
95              //Success
96          }
97      }
98  
99      public void testSimple1Source( DependencyResolverInterface dri, List projects )
100         throws Exception
101     {
102         DependencyResolver dr = new DependencyResolver( dri );
103         dr.setProjects( projects );
104 
105         assertEquals( "test1.sorted", "b:b,e:e,n:n,j:j", listify( dri.getSortedDependencies( true ) ) );
106 
107         projects = dr.getSortedDependencies( true );
108         assertBefore( projects, "b:b", "e:e" );
109         assertBefore( projects, "e:e", "n:n" );
110         assertBefore( projects, "n:n", "j:j" );
111 
112         Project e = DependencyResolver.getProject( projects, "e:e" );
113         assertEquals( "test1.sorted", "b:b,e:e", listify( dri.getSortedDependencies( e, true ) ) );
114         Project n = DependencyResolver.getProject( projects, "n:n" );
115         assertEquals( "test1.sorted", "b:b,e:e,n:n", listify( dri.getSortedDependencies( n, true ) ) );
116         Project j = DependencyResolver.getProject( projects, "j:j" );
117         assertEquals( "test1.sorted", "b:b,e:e,n:n,j:j", listify( dri.getSortedDependencies( j, true ) ) );
118     }
119 
120     /**
121      * @todo THIS IS A DUPLICATE of testSimple1Source
122      * @param dri dependency resolver
123      * @param projects list of projects to resolve
124      * @throws Exception when anything happens
125      */
126     public void testSimple1Binary( DependencyResolverInterface dri, List projects )
127         throws Exception
128     {
129         testSimple1Source( dri, projects );
130     }
131 
132     public void testSimple2( DependencyResolverInterface dri, List projects )
133         throws Exception
134     {
135         dri.setProjects( projects );
136         projects = dri.getSortedDependencies( true );
137 
138         assertEquals( "test3.sorted", "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow,nf:nf,ox:ox", listify( dri
139             .getSortedDependencies( true ) ) );
140         Project eq = DependencyResolver.getProject( projects, "eq:eq" );
141         assertEquals( "test3.sorted", "th:th,eq:eq", listify( dri.getSortedDependencies( eq, true ) ) );
142         Project ck = DependencyResolver.getProject( projects, "ck:ck" );
143         assertEquals( "test3.sorted", "th:th,eq:eq,ui:ui,ck:ck", listify( dri.getSortedDependencies( ck, true ) ) );
144         Project ow = DependencyResolver.getProject( projects, "ow:ow" );
145         assertEquals( "test3.sorted", "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow",
146                       listify( dri.getSortedDependencies( ow, true ) ) );
147     }
148 
149     public void testSimple3( DependencyResolverInterface dri, List projects )
150         throws Exception
151     {
152         dri.setProjects( projects );
153         projects = dri.getSortedDependencies( true );
154 
155         assertEquals( "test2.sorted", "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow,nf:nf,ox:ox", listify( dri
156             .getSortedDependencies( true ) ) );
157         Project eq = DependencyResolver.getProject( projects, "eq:eq" );
158         assertEquals( "test3.sorted", "th:th,eq:eq", listify( dri.getSortedDependencies( eq, true ) ) );
159         Project ck = DependencyResolver.getProject( projects, "ck:ck" );
160         assertEquals( "test3.sorted", "th:th,eq:eq,ui:ui,ck:ck", listify( dri.getSortedDependencies( ck, true ) ) );
161         Project ow = DependencyResolver.getProject( projects, "ow:ow" );
162         assertEquals( "test3.sorted", "th:th,eq:eq,ui:ui,ck:ck,br:br,ow:ow",
163                       listify( dri.getSortedDependencies( ow, true ) ) );
164     }
165 
166     public void testComplex( DependencyResolverInterface dri, List projects )
167         throws Exception
168     {
169         dri.setProjects( projects );
170         //System.out.println("Source DepResolver:" + dri.getClass().getName());
171         projects = dri.getSortedDependencies( true );
172         //dumpList(projects);
173         assertEquals( "SourceDep.size()", projects.size(), 12 );
174         assertBefore( projects, "tyre:tyre", "car:car" );
175         assertBefore( projects, "rubber:rubber", "tyre:tyre" );
176         assertBefore( projects, "beer:beer", "driver:driver" );
177 
178         //System.out.println("Binary DepResolver:" + dri.getClass().getName());
179         projects = dri.getSortedDependencies( false );
180         //dumpList(projects);
181         assertEquals( "SourceDep.size()", projects.size(), 5 );
182         assertBefore( projects, "hops:hops", "beer:beer" );
183         assertBefore( projects, "tyre:tyre", "car:car" );
184         assertBefore( projects, "beer:beer", "driver:driver" );
185 
186     }
187 
188     public void dumpList( List projects )
189     {
190         Iterator iter = projects.iterator();
191         while ( iter.hasNext() )
192         {
193             Project project = (Project) iter.next();
194             System.out.println( project.getId() );
195         }
196     }
197 
198     public void assertBefore( List projects, String first, String second )
199     {
200         assertTrue( first + " earlier than " + second, getIndex( projects, first ) < getIndex( projects, second ) );
201     }
202 
203     public int getIndex( List projects, String id )
204     {
205         for ( int i = 0; i < projects.size(); i++ )
206         {
207             Project p = (Project) projects.get( i );
208             if ( p.getId().equals( id ) )
209             {
210                 return i;
211             }
212         }
213         throw new IllegalArgumentException( "No such project: " + id );
214     }
215 
216     public String listify( List projects )
217     {
218         StringBuffer buf = new StringBuffer();
219         Iterator iter = projects.iterator();
220         while ( iter.hasNext() )
221         {
222             Project project = (Project) iter.next();
223             buf.append( project.getId() );
224             if ( iter.hasNext() )
225             {
226                 buf.append( "," );
227             }
228         }
229         return buf.toString();
230     }
231 
232     protected void setUp()
233         throws Exception
234     {
235         super.setUp();
236         dri = new WerkzDependencyResolver();
237     }
238 }