View Javadoc
1   package org.eclipse.aether.util.graph.transformer;
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 static org.junit.Assert.*;
23  
24  import java.util.Locale;
25  
26  import org.eclipse.aether.collection.DependencyGraphTransformer;
27  import org.eclipse.aether.graph.DependencyNode;
28  import org.eclipse.aether.internal.test.util.DependencyGraphParser;
29  import org.junit.Test;
30  
31  public class JavaScopeSelectorTest
32      extends AbstractDependencyGraphTransformerTest
33  {
34  
35      private enum Scope
36      {
37          TEST, PROVIDED, RUNTIME, COMPILE;
38  
39          @Override
40          public String toString()
41          {
42              return super.name().toLowerCase( Locale.ENGLISH );
43          }
44      }
45  
46      @Override
47      protected DependencyGraphTransformer newTransformer()
48      {
49          return new ConflictResolver( new NearestVersionSelector(), new JavaScopeSelector(),
50                                       new SimpleOptionalitySelector(), new JavaScopeDeriver() );
51      }
52  
53      @Override
54      protected DependencyGraphParser newParser()
55      {
56          return new DependencyGraphParser( "transformer/scope-calculator/" );
57      }
58  
59      private void expectScope( String expected, DependencyNode root, int... coords )
60      {
61          expectScope( null, expected, root, coords );
62      }
63  
64      private void expectScope( String msg, String expected, DependencyNode root, int... coords )
65      {
66          if ( msg == null )
67          {
68              msg = "";
69          }
70          try
71          {
72              DependencyNode node = root;
73              node = path( node, coords );
74  
75              assertEquals( msg + "\nculprit: " + node.toString() + "\n", expected, node.getDependency().getScope() );
76          }
77          catch ( IndexOutOfBoundsException e )
78          {
79              throw new IllegalArgumentException( "illegal coordinates for child", e );
80          }
81          catch ( NullPointerException e )
82          {
83              throw new IllegalArgumentException( "illegal coordinates for child", e );
84          }
85      }
86  
87      private DependencyNode path( DependencyNode node, int... coords )
88      {
89          for ( int coord : coords )
90          {
91              node = node.getChildren().get( coord );
92          }
93          return node;
94      }
95  
96      @Test
97      public void testScopeInheritanceProvided()
98          throws Exception
99      {
100         String resource = "inheritance.txt";
101 
102         String expected = "test";
103         DependencyNode root = transform( parseResource( resource, "provided", "test" ) );
104         expectScope( parser.dump( root ), expected, root, 0, 0 );
105     }
106 
107     @Test
108     public void testConflictWinningScopeGetsUsedForInheritance()
109         throws Exception
110     {
111         DependencyNode root = parseResource( "conflict-and-inheritance.txt" );
112         assertSame( root, transform( root ) );
113 
114         expectScope( "compile", root, 0, 0 );
115         expectScope( "compile", root, 0, 0, 0 );
116     }
117 
118     @Test
119     public void testScopeOfDirectDependencyWinsConflictAndGetsUsedForInheritanceToChildrenEverywhereInGraph()
120         throws Exception
121     {
122         DependencyNode root = parseResource( "direct-with-conflict-and-inheritance.txt" );
123         assertSame( root, transform( root ) );
124 
125         expectScope( "test", root, 0, 0 );
126     }
127 
128     @Test
129     public void testCycleA()
130         throws Exception
131     {
132         DependencyNode root = parseResource( "cycle-a.txt" );
133         assertSame( root, transform( root ) );
134 
135         expectScope( "compile", root, 0 );
136         expectScope( "runtime", root, 1 );
137     }
138 
139     @Test
140     public void testCycleB()
141         throws Exception
142     {
143         DependencyNode root = parseResource( "cycle-b.txt" );
144         assertSame( root, transform( root ) );
145 
146         expectScope( "runtime", root, 0 );
147         expectScope( "compile", root, 1 );
148     }
149 
150     @Test
151     public void testCycleC()
152         throws Exception
153     {
154         DependencyNode root = parseResource( "cycle-c.txt" );
155         assertSame( root, transform( root ) );
156 
157         expectScope( "runtime", root, 0 );
158         expectScope( "runtime", root, 0, 0 );
159         expectScope( "runtime", root, 1 );
160         expectScope( "runtime", root, 1, 0 );
161     }
162 
163     @Test
164     public void testCycleD()
165         throws Exception
166     {
167         DependencyNode root = parseResource( "cycle-d.txt" );
168         assertSame( root, transform( root ) );
169 
170         expectScope( "compile", root, 0 );
171         expectScope( "compile", root, 0, 0 );
172     }
173 
174     @Test
175     public void testDirectNodesAlwaysWin()
176         throws Exception
177     {
178 
179         for ( Scope directScope : Scope.values() )
180         {
181             String direct = directScope.toString();
182 
183             DependencyNode root = parseResource( "direct-nodes-winning.txt", direct );
184 
185             String msg =
186                 String.format( "direct node should be setting scope ('%s') for all nodes.\n" + parser.dump( root ),
187                                direct );
188             assertSame( root, transform( root ) );
189             msg += "\ntransformed:\n" + parser.dump( root );
190 
191             expectScope( msg, direct, root, 0 );
192         }
193     }
194 
195     @Test
196     public void testNonDirectMultipleInheritance()
197         throws Exception
198     {
199         for ( Scope scope1 : Scope.values() )
200         {
201             for ( Scope scope2 : Scope.values() )
202             {
203                 DependencyNode root = parseResource( "multiple-inheritance.txt", scope1.toString(), scope2.toString() );
204 
205                 String expected = scope1.compareTo( scope2 ) >= 0 ? scope1.toString() : scope2.toString();
206                 String msg = String.format( "expected '%s' to win\n" + parser.dump( root ), expected );
207 
208                 assertSame( root, transform( root ) );
209                 msg += "\ntransformed:\n" + parser.dump( root );
210 
211                 expectScope( msg, expected, root, 0, 0 );
212             }
213         }
214     }
215 
216     @Test
217     public void testConflictScopeOrdering()
218         throws Exception
219     {
220         for ( Scope scope1 : Scope.values() )
221         {
222             for ( Scope scope2 : Scope.values() )
223             {
224                 DependencyNode root = parseResource( "dueling-scopes.txt", scope1.toString(), scope2.toString() );
225 
226                 String expected = scope1.compareTo( scope2 ) >= 0 ? scope1.toString() : scope2.toString();
227                 String msg = String.format( "expected '%s' to win\n" + parser.dump( root ), expected );
228 
229                 assertSame( root, transform( root ) );
230                 msg += "\ntransformed:\n" + parser.dump( root );
231 
232                 expectScope( msg, expected, root, 0, 0 );
233             }
234         }
235     }
236 
237     /**
238      * obscure case (illegal maven POM).
239      */
240     @Test
241     public void testConflictingDirectNodes()
242         throws Exception
243     {
244         for ( Scope scope1 : Scope.values() )
245         {
246             for ( Scope scope2 : Scope.values() )
247             {
248                 DependencyNode root = parseResource( "conflicting-direct-nodes.txt", scope1.toString(), scope2.toString() );
249 
250                 String expected = scope1.toString();
251                 String msg = String.format( "expected '%s' to win\n" + parser.dump( root ), expected );
252 
253                 assertSame( root, transform( root ) );
254                 msg += "\ntransformed:\n" + parser.dump( root );
255 
256                 expectScope( msg, expected, root, 0 );
257             }
258         }
259     }
260 
261 }