View Javadoc

1   package org.apache.maven.archiva.dependency.graph.tasks;
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 org.apache.commons.lang.StringUtils;
23  import org.apache.maven.archiva.dependency.graph.DependencyGraphNode;
24  import org.apache.maven.archiva.dependency.graph.tasks.DependencyManagementStack.Rules;
25  import org.apache.maven.archiva.model.ArtifactReference;
26  import org.apache.maven.archiva.model.Dependency;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * DependencyManagementStackTest 
32   *
33   * @version $Id: DependencyManagementStackTest.java 718864 2008-11-19 06:33:35Z brett $
34   */
35  public class DependencyManagementStackTest
36      extends TestCase
37  {
38      public DependencyGraphNode toNode( String key )
39      {
40          String parts[] = StringUtils.splitPreserveAllTokens( key, ":" );
41          assertEquals( "toNode(" + key + ") requires 5 parts", 5, parts.length );
42  
43          ArtifactReference ref = new ArtifactReference();
44          ref.setGroupId( parts[0] );
45          ref.setArtifactId( parts[1] );
46          ref.setVersion( parts[2] );
47          ref.setClassifier( parts[3] );
48          ref.setType( parts[4] );
49  
50          return new DependencyGraphNode( ref );
51      }
52  
53      protected Dependency toDependency( String key )
54      {
55          String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
56  
57          assertEquals( "Dependency key [" + key + "] should be 5 parts.", 5, parts.length );
58  
59          Dependency dep = new Dependency();
60  
61          dep.setGroupId( parts[0] );
62          dep.setArtifactId( parts[1] );
63          dep.setVersion( parts[2] );
64          dep.setClassifier( parts[3] );
65          dep.setType( parts[4] );
66  
67          return dep;
68      }
69  
70      public void testPushPopSimple()
71      {
72          DependencyGraphNode node = toNode( "org.apache.maven.archiva:depmanstack-testcase:1.0::jar" );
73          Dependency dep = toDependency( "junit:junit:3.8.1::jar" );
74          dep.setScope( "test" );
75          node.addDependencyManagement( dep );
76  
77          DependencyManagementStack stack = new DependencyManagementStack();
78          stack.push( node );
79          DependencyGraphNode oldnode = stack.pop();
80          assertEquals( "added node to old node", node, oldnode );
81      }
82  
83      public void testPushPopTwoDeep()
84      {
85          DependencyManagementStack stack = new DependencyManagementStack();
86          Dependency dep;
87  
88          // top node.
89          DependencyGraphNode projectNode = toNode( "org.apache.maven.archiva:depmanstack-testcase:1.0::jar" );
90          dep = toDependency( "junit:junit:3.8.1::jar" );
91          dep.setScope( "test" );
92          projectNode.addDependencyManagement( dep );
93          stack.push( projectNode );
94  
95          // direct node.
96          DependencyGraphNode directNode = toNode( "org.apache.maven.archiva:depmanstack-common:1.0::jar" );
97          dep = toDependency( "junit:junit:3.7::jar" );
98          dep.setScope( "test" );
99          directNode.addDependencyManagement( dep );
100         stack.push( directNode );
101 
102         // transitive node.
103         DependencyGraphNode transNode = toNode( "org.apache.maven.archiva:depmanstack-model:1.0::jar" );
104         dep = toDependency( "junit:junit:3.7::jar" );
105         transNode.addDependencyManagement( dep );
106         stack.push( transNode );
107 
108         // Test it
109         assertEquals( "popped node is trans node", transNode, stack.pop() );
110         assertEquals( "popped node is direct node", directNode, stack.pop() );
111         assertEquals( "popped node is project node", projectNode, stack.pop() );
112     }
113 
114     public void testApplyNodeVersionParentWins()
115     {
116         DependencyManagementStack stack = new DependencyManagementStack();
117         Dependency dep;
118 
119         // top node.
120         DependencyGraphNode projectNode = toNode( "org.apache.maven.archiva:depmanstack-testcase:1.0::jar" );
121         dep = toDependency( "junit:junit:3.8.1::jar" );
122         dep.setScope( "test" );
123         projectNode.addDependencyManagement( dep );
124         stack.push( projectNode );
125 
126         // direct node.
127         DependencyGraphNode directNode = toNode( "org.apache.maven.archiva:depmanstack-common:1.0::jar" );
128         dep = toDependency( "junit:junit:3.7::jar" );
129         dep.setScope( "test" );
130         directNode.addDependencyManagement( dep );
131         stack.push( directNode );
132 
133         // transitive node.
134         DependencyGraphNode transNode = toNode( "org.apache.maven.archiva:depmanstack-model:1.0::jar" );
135         dep = toDependency( "junit:junit:3.7.1::jar" );
136         transNode.addDependencyManagement( dep );
137         stack.push( transNode );
138 
139         // Test it
140         DependencyGraphNode junitNode = toNode( "junit:junit:1.0::jar" );
141 
142         assertRules( "junit (lvl:trans)", stack, junitNode, "3.8.1", "test", null );
143         stack.pop();
144         assertRules( "junit (lvl:direct)", stack, junitNode, "3.8.1", "test", null );
145         stack.pop();
146         assertRules( "junit (lvl:project)", stack, junitNode, "3.8.1", "test", null );
147     }
148 
149     /**
150      * This test is based off of Carlos Sanchez's depman example use case.
151      *
152      * In a simple project chain of A:1.0 -> B:1.0 -> C:1.0 -> D:1.0
153      * If B:1.0 has a dependency management section stating dep D should be version 2.0
154      * Then the dep D when viewed from A should be version 2.0 
155      */
156     public void testApplyNodeVersionCarlosABCD()
157     {
158         DependencyManagementStack stack = new DependencyManagementStack();
159         Dependency dep;
160 
161         // project node, A
162         DependencyGraphNode nodeA = toNode( "org.apache.maven.archiva:carlos-A:1.0::jar" );
163         stack.push( nodeA );
164 
165         // sub node, B
166         DependencyGraphNode nodeB = toNode( "org.apache.maven.archiva:carlos-B:1.0::jar" );
167         dep = toDependency( "org.apache.maven.archiva:carlos-D:2.0::jar" );
168         nodeB.addDependencyManagement( dep );
169         stack.push( nodeB );
170 
171         // sub node, C
172         DependencyGraphNode nodeC = toNode( "org.apache.maven.archiva:carlos-C:1.0::jar" );
173         stack.push( nodeC );
174 
175         // sub node, D
176         // Not added to the stack, as this is the node that is having the rules applied to it.
177         DependencyGraphNode nodeD = toNode( "org.apache.maven.archiva:carlos-D:1.0::jar" );
178 
179         // Test it
180         assertRules( "node D (lvl:C)", stack, nodeD, "2.0", null, null );
181         stack.pop();
182         assertRules( "node D (lvl:B)", stack, nodeD, "2.0", null, null );
183         stack.pop();
184         assertNoRules( "node D (lvl:A)", stack, nodeD, "2.0", null, null );
185     }
186 
187     /**
188      * Test for expected rules, that should be enforced for the provided node.
189      * NOTE: This test will update the node.artifact.version to whatever is stated in the rules.
190      */
191     private void assertRules( String msg, DependencyManagementStack stack, DependencyGraphNode node,
192                               String expectedVersion, String expectedScope, String expectedExclusions[] )
193     {
194         Rules rules = stack.getRules( node );
195         assertNotNull( msg + " rules should not be null.", rules );
196 
197         node.getArtifact().setVersion( rules.artifact.getVersion() );
198 
199         assertEquals( msg + ": version", expectedVersion, rules.artifact.getVersion() );
200         assertEquals( msg + ": scope", expectedScope, rules.scope );
201 
202         if ( expectedExclusions != null )
203         {
204             // TODO: test for exclusion settings.
205         }
206     }
207 
208     /**
209      * Test for when there are no rules being enforced for the provided node.
210      * Similar to assertRules() above.
211      */
212     private void assertNoRules( String msg, DependencyManagementStack stack, DependencyGraphNode node,
213                                 String expectedVersion, String expectedScope, String expectedExclusions[] )
214     {
215         Rules rules = stack.getRules( node );
216         assertNull( msg + " rules should be null.", rules );
217 
218         assertEquals( msg + ": version", expectedVersion, node.getArtifact().getVersion() );
219 
220         if ( expectedExclusions != null )
221         {
222             // TODO: test for exclusion settings.
223         }
224     }
225 }