1 package org.apache.maven.archiva.dependency.graph;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.functors.AndPredicate;
25 import org.apache.commons.collections.functors.NotPredicate;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.dependency.graph.functors.EdgeExactScopePredicate;
28 import org.apache.maven.archiva.dependency.graph.functors.EdgeFromPredicate;
29 import org.apache.maven.archiva.dependency.graph.functors.NodeFromParentPredicate;
30 import org.apache.maven.archiva.dependency.graph.functors.NodePredicate;
31 import org.apache.maven.archiva.dependency.graph.functors.ToKeyTransformer;
32 import org.apache.maven.archiva.model.ArchivaProjectModel;
33 import org.apache.maven.archiva.model.ArtifactReference;
34 import org.apache.maven.archiva.model.Dependency;
35 import org.apache.maven.archiva.model.VersionedReference;
36 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Iterator;
41 import java.util.List;
44 * AbstractDependencyGraphFactoryTestCase
48 public abstract class AbstractDependencyGraphFactoryTestCase
49 extends PlexusInSpringTestCase
51 public class ExpectedEdge
57 public ExpectedEdge( String from, String to )
64 public class GraphEdgePredicate
67 private String edgeFrom;
69 private String edgeTo;
71 public GraphEdgePredicate( String edgeFrom, String edgeTo )
73 this.edgeFrom = edgeFrom;
77 public boolean evaluate( Object object )
79 boolean satisfies = false;
81 if ( object instanceof DependencyGraphEdge )
83 DependencyGraphEdge edge = (DependencyGraphEdge) object;
84 String actualFrom = ArtifactReference.toKey( edge.getNodeFrom() );
85 String actualTo = ArtifactReference.toKey( edge.getNodeTo() );
87 satisfies = ( StringUtils.equals( edgeFrom, actualFrom ) && StringUtils.equals( edgeTo, actualTo ) );
94 protected void assertDirectNodes( DependencyGraph graph, List expectedNodes, String scope )
97 DependencyGraphNode rootNode = graph.getRootNode();
98 List rootEdges = graph.getEdgesFrom( rootNode );
99 List actualEdges = new ArrayList();
101 Predicate directDep = NotPredicate.getInstance( new NodeFromParentPredicate() );
102 Predicate scopedDirectDeps = AndPredicate.getInstance( new EdgeExactScopePredicate( scope ), directDep );
103 CollectionUtils.select( rootEdges, scopedDirectDeps, actualEdges );
104 // CollectionUtils.select( rootEdges, new EdgeExactScopePredicate( scope ), actualEdges );
106 if ( expectedNodes.size() != actualEdges.size() )
108 StringBuffer sb = new StringBuffer();
110 sb.append( "Direct node.count with <" ).append( scope ).append( "> edges from [" );
111 sb.append( DependencyGraphKeys.toKey( rootNode.getArtifact() ) ).append( "]" ).append( " expected:<" );
112 sb.append( expectedNodes.size() ).append( "> but was:<" );
113 sb.append( actualEdges.size() ).append( ">" );
115 CollectionUtils.transform( actualEdges, new ToKeyTransformer() );
117 Collection missingActualKeys = CollectionUtils.subtract( actualEdges, expectedNodes );
118 it = missingActualKeys.iterator();
119 while ( it.hasNext() )
121 sb.append( "\n (Extra Actual) " ).append( (String) it.next() );
124 Collection missingExpectedKeys = CollectionUtils.subtract( expectedNodes, actualEdges );
125 it = missingExpectedKeys.iterator();
126 while ( it.hasNext() )
128 sb.append( "\n (Extra Expected) " ).append( (String) it.next() );
131 fail( sb.toString() );
134 it = actualEdges.iterator();
135 while ( it.hasNext() )
137 DependencyGraphEdge edge = (DependencyGraphEdge) it.next();
138 String actualKey = DependencyGraphKeys.toKey( edge.getNodeTo() );
139 assertTrue( "Direct <" + scope + "> node To [" + actualKey + "] exists in expectedNodes.", expectedNodes
140 .contains( actualKey ) );
144 protected void assertEdges( DependencyGraph graph, List expectedEdges )
146 assertNotNull( "Graph.edges should never be null.", graph.getEdges() );
147 assertEquals( "Graph.edges.size()", expectedEdges.size(), graph.getEdges().size() );
149 Iterator it = expectedEdges.iterator();
150 while ( it.hasNext() )
152 ExpectedEdge expectedEdge = (ExpectedEdge) it.next();
153 Predicate edgePredicate = new GraphEdgePredicate( expectedEdge.from, expectedEdge.to );
155 DependencyGraphEdge edge = (DependencyGraphEdge) CollectionUtils.find( graph.getEdges(), edgePredicate );
158 fail( "Unable to find expected edge from:<" + expectedEdge.from + "> to:<" + expectedEdge.to + ">" );
163 protected void assertGraph( DependencyGraph graph, String rootRefKey, List expectedNodeKeys )
165 assertNotNull( "Graph.nodes should never be null.", graph.getNodes() );
166 assertTrue( "Graph.nodes.size() should always be 1 or better.", graph.getNodes().size() >= 1 );
168 ArtifactReference rootRef = graph.getRootNode().getArtifact();
169 StringBuffer actualRootRef = new StringBuffer();
170 actualRootRef.append( rootRef.getGroupId() ).append( ":" );
171 actualRootRef.append( rootRef.getArtifactId() ).append( ":" );
172 actualRootRef.append( rootRef.getVersion() );
174 assertEquals( "Graph.root", rootRefKey, actualRootRef.toString() );
177 List actualNodes = new ArrayList();
179 Predicate notRootNode = NotPredicate.getInstance( new NodePredicate( graph.getRootNode() ) );
180 CollectionUtils.select( graph.getNodes(), notRootNode, actualNodes );
182 boolean fail = false;
183 StringBuffer sb = new StringBuffer();
185 if ( expectedNodeKeys.size() != actualNodes.size() )
187 sb.append( "node.count expected:<" );
188 sb.append( expectedNodeKeys.size() ).append( "> but was:<" );
189 sb.append( actualNodes.size() ).append( ">" );
193 CollectionUtils.transform( actualNodes, new ToKeyTransformer() );
195 Collection missingActualKeys = CollectionUtils.subtract( actualNodes, expectedNodeKeys );
196 it = missingActualKeys.iterator();
197 while ( it.hasNext() )
199 sb.append( "\n (Extra Actual) " ).append( (String) it.next() );
203 Collection missingExpectedKeys = CollectionUtils.subtract( expectedNodeKeys, actualNodes );
204 it = missingExpectedKeys.iterator();
205 while ( it.hasNext() )
207 sb.append( "\n (Extra Expected) " ).append( (String) it.next() );
213 fail( sb.toString() );
217 it = actualNodes.iterator();
218 while ( it.hasNext() )
220 DependencyGraphNode node = (DependencyGraphNode) it.next();
221 assertNotNull( "Artifact reference in node should not be null.", node.getArtifact() );
222 String key = ArtifactReference.toKey( node.getArtifact() );
223 assertTrue( "Artifact reference [" + key + "] should be in expectedNodeKeys.", expectedNodeKeys
229 protected void assertNodes( DependencyGraph graph, List expectedNodeKeys )
231 assertNotNull( "Graph.nodes should never be null.", graph.getNodes() );
232 assertTrue( "Graph.nodes.size() should always be 1 or better.", graph.getNodes().size() >= 1 );
233 // assertEquals( "Graph.nodes.size()", expectedNodeKeys.size(), graph.getNodes().size() );
236 List actualNodes = new ArrayList();
237 actualNodes.addAll( graph.getNodes() );
239 if ( expectedNodeKeys.size() != actualNodes.size() )
241 StringBuffer sb = new StringBuffer();
243 sb.append( "node.count expected:<" );
244 sb.append( expectedNodeKeys.size() ).append( "> but was:<" );
245 sb.append( actualNodes.size() ).append( ">" );
247 CollectionUtils.transform( actualNodes, new ToKeyTransformer() );
249 Collection missingActualKeys = CollectionUtils.subtract( actualNodes, expectedNodeKeys );
250 it = missingActualKeys.iterator();
251 while ( it.hasNext() )
253 sb.append( "\n (Extra Actual) " ).append( (String) it.next() );
256 Collection missingExpectedKeys = CollectionUtils.subtract( expectedNodeKeys, actualNodes );
257 it = missingExpectedKeys.iterator();
258 while ( it.hasNext() )
260 sb.append( "\n (Extra Expected) " ).append( (String) it.next() );
263 fail( sb.toString() );
266 it = graph.getNodes().iterator();
267 while ( it.hasNext() )
269 DependencyGraphNode node = (DependencyGraphNode) it.next();
270 assertNotNull( "Artifact reference in node should not be null.", node.getArtifact() );
271 String key = ArtifactReference.toKey( node.getArtifact() );
272 assertTrue( "Artifact reference [" + key + "] should be in expectedNodeKeys.", expectedNodeKeys
277 protected void assertRootNode( DependencyGraph graph, String expectedKey )
279 DependencyGraphNode node = graph.getRootNode();
281 String actualKey = DependencyGraphKeys.toKey( node.getArtifact() );
282 assertEquals( "Root Node", expectedKey, actualKey );
285 protected void assertTransientNodes( DependencyGraph graph, List expectedNodes, String scope )
289 // Gather up the transient nodes from the DependencyGraph.
290 List actualEdges = new ArrayList();
292 DependencyGraphNode rootNode = graph.getRootNode();
294 Predicate transientDep = NotPredicate.getInstance( new EdgeFromPredicate( rootNode.getArtifact() ) );
295 Predicate edgeByExactScope = new EdgeExactScopePredicate( scope );
296 Predicate transitiveEdgesByScopePredicate = AndPredicate.getInstance( transientDep, edgeByExactScope );
298 CollectionUtils.select( graph.getEdges(), transitiveEdgesByScopePredicate, actualEdges );
300 if ( expectedNodes.size() != actualEdges.size() )
302 StringBuffer sb = new StringBuffer();
304 sb.append( "Transient node.count with <" ).append( scope ).append( "> edges from [" );
305 sb.append( DependencyGraphKeys.toKey( rootNode.getArtifact() ) ).append( "]" ).append( " expected:<" );
306 sb.append( expectedNodes.size() ).append( "> but was:<" );
307 sb.append( actualEdges.size() ).append( ">" );
309 CollectionUtils.transform( actualEdges, new ToKeyTransformer() );
311 Collection missingActualKeys = CollectionUtils.subtract( actualEdges, expectedNodes );
312 it = missingActualKeys.iterator();
313 while ( it.hasNext() )
315 sb.append( "\n (Extra Actual) " ).append( (String) it.next() );
318 Collection missingExpectedKeys = CollectionUtils.subtract( expectedNodes, actualEdges );
319 it = missingExpectedKeys.iterator();
320 while ( it.hasNext() )
322 sb.append( "\n (Extra Expected) " ).append( (String) it.next() );
325 fail( sb.toString() );
328 it = actualEdges.iterator();
329 while ( it.hasNext() )
331 DependencyGraphEdge edge = (DependencyGraphEdge) it.next();
332 String actualKey = DependencyGraphKeys.toKey( edge.getNodeTo() );
333 assertTrue( "Transient Node To [" + actualKey + "] exists in expectedNodes.", expectedNodes
334 .contains( actualKey ) );
338 protected Dependency toDependency( String key )
340 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
342 assertEquals( "Dependency key [" + key + "] should be 5 parts.", 5, parts.length );
344 Dependency dep = new Dependency();
346 dep.setGroupId( parts[0] );
347 dep.setArtifactId( parts[1] );
348 dep.setVersion( parts[2] );
349 dep.setClassifier( parts[3] );
350 dep.setType( parts[4] );
355 protected ArchivaProjectModel toModel( String key, Dependency deps[] )
357 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
359 assertEquals( "Dependency key [" + key + "] should be 3 parts.", 3, parts.length );
361 ArchivaProjectModel model = new ArchivaProjectModel();
362 model.setGroupId( parts[0] );
363 model.setArtifactId( parts[1] );
364 model.setVersion( parts[2] );
365 model.setOrigin( "testcase" );
366 model.setPackaging( "jar" );
370 for ( int i = 0; i < deps.length; i++ )
372 Dependency dep = deps[i];
373 model.addDependency( dep );
380 protected VersionedReference toVersionedReference( String key )
382 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
383 assertEquals( "Versioned Reference [" + key + "] part count.", 3, parts.length );
385 VersionedReference ref = new VersionedReference();
386 ref.setGroupId( parts[0] );
387 ref.setArtifactId( parts[1] );
388 ref.setVersion( parts[2] );