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 java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections.Predicate;
28 import org.apache.commons.collections.functors.AndPredicate;
29 import org.apache.commons.collections.functors.NotPredicate;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.maven.archiva.dependency.graph.functors.EdgeExactScopePredicate;
32 import org.apache.maven.archiva.dependency.graph.functors.EdgeFromPredicate;
33 import org.apache.maven.archiva.dependency.graph.functors.NodeFromParentPredicate;
34 import org.apache.maven.archiva.dependency.graph.functors.NodePredicate;
35 import org.apache.maven.archiva.dependency.graph.functors.ToKeyTransformer;
36 import org.apache.maven.archiva.model.ArchivaProjectModel;
37 import org.apache.maven.archiva.model.ArtifactReference;
38 import org.apache.maven.archiva.model.Dependency;
39 import org.apache.maven.archiva.model.VersionedReference;
40 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 * AbstractDependencyGraphFactoryTestCase
47 public abstract class AbstractDependencyGraphFactoryTestCase
48 extends PlexusInSpringTestCase
50 public class ExpectedEdge
56 public ExpectedEdge( String from, String to )
63 public class GraphEdgePredicate
66 private String edgeFrom;
68 private String edgeTo;
70 public GraphEdgePredicate( String edgeFrom, String edgeTo )
72 this.edgeFrom = edgeFrom;
76 public boolean evaluate( Object object )
78 boolean satisfies = false;
80 if ( object instanceof DependencyGraphEdge )
82 DependencyGraphEdge edge = (DependencyGraphEdge) object;
83 String actualFrom = ArtifactReference.toKey( edge.getNodeFrom() );
84 String actualTo = ArtifactReference.toKey( edge.getNodeTo() );
86 satisfies = ( StringUtils.equals( edgeFrom, actualFrom ) && StringUtils.equals( edgeTo, actualTo ) );
93 @SuppressWarnings("unchecked")
94 protected void assertDirectNodes( DependencyGraph graph, List<DependencyGraphNode> expectedNodes, String scope )
96 DependencyGraphNode rootNode = graph.getRootNode();
97 List<DependencyGraphEdge> rootEdges = graph.getEdgesFrom( rootNode );
98 List<DependencyGraphEdge> actualEdges = new ArrayList<DependencyGraphEdge>();
100 Predicate directDep = NotPredicate.getInstance( new NodeFromParentPredicate() );
101 Predicate scopedDirectDeps = AndPredicate.getInstance( new EdgeExactScopePredicate( scope ), directDep );
102 CollectionUtils.select( rootEdges, scopedDirectDeps, actualEdges );
103 // CollectionUtils.select( rootEdges, new EdgeExactScopePredicate( scope ), actualEdges );
105 if ( expectedNodes.size() != actualEdges.size() )
107 StringBuffer sb = new StringBuffer();
109 sb.append( "Direct node.count with <" ).append( scope ).append( "> edges from [" );
110 sb.append( DependencyGraphKeys.toKey( rootNode.getArtifact() ) ).append( "]" ).append( " expected:<" );
111 sb.append( expectedNodes.size() ).append( "> but was:<" );
112 sb.append( actualEdges.size() ).append( ">" );
114 CollectionUtils.transform( actualEdges, new ToKeyTransformer() );
116 Collection<String> missingActualKeys = CollectionUtils.subtract( actualEdges, expectedNodes );
117 for ( String key : missingActualKeys )
119 sb.append( "\n (Extra Actual) " ).append( key );
122 Collection<String> missingExpectedKeys = CollectionUtils.subtract( expectedNodes, actualEdges );
123 for ( String key : missingExpectedKeys )
125 sb.append( "\n (Extra Expected) " ).append( key );
128 fail( sb.toString() );
131 for ( DependencyGraphEdge edge : actualEdges )
133 String actualKey = DependencyGraphKeys.toKey( edge.getNodeTo() );
134 assertTrue( "Direct <" + scope + "> node To [" + actualKey + "] exists in expectedNodes.", expectedNodes
135 .contains( actualKey ) );
139 protected void assertEdges( DependencyGraph graph, List<ExpectedEdge> expectedEdges )
141 assertNotNull( "Graph.edges should never be null.", graph.getEdges() );
142 assertEquals( "Graph.edges.size()", expectedEdges.size(), graph.getEdges().size() );
144 for ( ExpectedEdge expectedEdge : expectedEdges )
146 Predicate edgePredicate = new GraphEdgePredicate( expectedEdge.from, expectedEdge.to );
148 DependencyGraphEdge edge = (DependencyGraphEdge) CollectionUtils.find( graph.getEdges(), edgePredicate );
151 fail( "Unable to find expected edge from:<" + expectedEdge.from + "> to:<" + expectedEdge.to + ">" );
156 @SuppressWarnings("unchecked")
157 protected void assertGraph( DependencyGraph graph, String rootRefKey, List expectedNodeKeys )
159 assertNotNull( "Graph.nodes should never be null.", graph.getNodes() );
160 assertTrue( "Graph.nodes.size() should always be 1 or better.", graph.getNodes().size() >= 1 );
162 ArtifactReference rootRef = graph.getRootNode().getArtifact();
163 StringBuffer actualRootRef = new StringBuffer();
164 actualRootRef.append( rootRef.getGroupId() ).append( ":" );
165 actualRootRef.append( rootRef.getArtifactId() ).append( ":" );
166 actualRootRef.append( rootRef.getVersion() );
168 assertEquals( "Graph.root", rootRefKey, actualRootRef.toString() );
170 List<DependencyGraphNode> actualNodes = new ArrayList<DependencyGraphNode>();
172 Predicate notRootNode = NotPredicate.getInstance( new NodePredicate( graph.getRootNode() ) );
173 CollectionUtils.select( graph.getNodes(), notRootNode, actualNodes );
175 boolean fail = false;
176 StringBuffer sb = new StringBuffer();
178 if ( expectedNodeKeys.size() != actualNodes.size() )
180 sb.append( "node.count expected:<" );
181 sb.append( expectedNodeKeys.size() ).append( "> but was:<" );
182 sb.append( actualNodes.size() ).append( ">" );
186 CollectionUtils.transform( actualNodes, new ToKeyTransformer() );
188 Collection<DependencyGraphNode> missingActualKeys = CollectionUtils.subtract( actualNodes, expectedNodeKeys );
189 for ( DependencyGraphNode key : missingActualKeys )
191 sb.append( "\n (Extra Actual) " ).append( key );
195 Collection<DependencyGraphNode> missingExpectedKeys = CollectionUtils.subtract( expectedNodeKeys, actualNodes );
196 for ( DependencyGraphNode key : missingExpectedKeys )
198 sb.append( "\n (Extra Expected) " ).append( key );
204 fail( sb.toString() );
208 it = actualNodes.iterator();
209 while ( it.hasNext() )
211 DependencyGraphNode node = (DependencyGraphNode) it.next();
212 assertNotNull( "Artifact reference in node should not be null.", node.getArtifact() );
213 String key = ArtifactReference.toKey( node.getArtifact() );
214 assertTrue( "Artifact reference [" + key + "] should be in expectedNodeKeys.", expectedNodeKeys
220 @SuppressWarnings("unchecked")
221 protected void assertNodes( DependencyGraph graph, List<String> expectedNodeKeys )
223 assertNotNull( "Graph.nodes should never be null.", graph.getNodes() );
224 assertTrue( "Graph.nodes.size() should always be 1 or better.", graph.getNodes().size() >= 1 );
225 // assertEquals( "Graph.nodes.size()", expectedNodeKeys.size(), graph.getNodes().size() );
227 List<DependencyGraphNode> actualNodes = new ArrayList<DependencyGraphNode>();
228 actualNodes.addAll( graph.getNodes() );
230 if ( expectedNodeKeys.size() != actualNodes.size() )
232 StringBuffer sb = new StringBuffer();
234 sb.append( "node.count expected:<" );
235 sb.append( expectedNodeKeys.size() ).append( "> but was:<" );
236 sb.append( actualNodes.size() ).append( ">" );
238 CollectionUtils.transform( actualNodes, new ToKeyTransformer() );
240 Collection<DependencyGraphNode> missingActualKeys = CollectionUtils.subtract( actualNodes, expectedNodeKeys );
241 for ( DependencyGraphNode key : missingActualKeys )
243 sb.append( "\n (Extra Actual) " ).append( key );
246 Collection<String> missingExpectedKeys = CollectionUtils.subtract( expectedNodeKeys, actualNodes );
247 for ( String key : missingExpectedKeys )
249 sb.append( "\n (Extra Expected) " ).append( key );
252 fail( sb.toString() );
255 for ( DependencyGraphNode node : graph.getNodes() )
257 assertNotNull( "Artifact reference in node should not be null.", node.getArtifact() );
258 String key = ArtifactReference.toKey( node.getArtifact() );
259 assertTrue( "Artifact reference [" + key + "] should be in expectedNodeKeys.", expectedNodeKeys
264 protected void assertRootNode( DependencyGraph graph, String expectedKey )
266 DependencyGraphNode node = graph.getRootNode();
268 String actualKey = DependencyGraphKeys.toKey( node.getArtifact() );
269 assertEquals( "Root Node", expectedKey, actualKey );
272 @SuppressWarnings("unchecked")
273 protected void assertTransientNodes( DependencyGraph graph, List<DependencyGraphNode> expectedNodes, String scope )
275 // Gather up the transient nodes from the DependencyGraph.
276 ArrayList<DependencyGraphEdge> actualEdges = new ArrayList<DependencyGraphEdge>();
278 DependencyGraphNode rootNode = graph.getRootNode();
280 Predicate transientDep = NotPredicate.getInstance( new EdgeFromPredicate( rootNode.getArtifact() ) );
281 Predicate edgeByExactScope = new EdgeExactScopePredicate( scope );
282 Predicate transitiveEdgesByScopePredicate = AndPredicate.getInstance( transientDep, edgeByExactScope );
284 CollectionUtils.select( graph.getEdges(), transitiveEdgesByScopePredicate, actualEdges );
286 if ( expectedNodes.size() != actualEdges.size() )
288 StringBuffer sb = new StringBuffer();
290 sb.append( "Transient node.count with <" ).append( scope ).append( "> edges from [" );
291 sb.append( DependencyGraphKeys.toKey( rootNode.getArtifact() ) ).append( "]" ).append( " expected:<" );
292 sb.append( expectedNodes.size() ).append( "> but was:<" );
293 sb.append( actualEdges.size() ).append( ">" );
295 CollectionUtils.transform( actualEdges, new ToKeyTransformer() );
297 Collection<DependencyGraphNode> missingActualKeys = CollectionUtils.subtract( actualEdges, expectedNodes );
298 for ( DependencyGraphNode key : missingActualKeys )
300 sb.append( "\n (Extra Actual) " ).append( key );
303 Collection<DependencyGraphNode> missingExpectedKeys = CollectionUtils.subtract( expectedNodes, actualEdges );
304 for ( DependencyGraphNode key : missingExpectedKeys )
306 sb.append( "\n (Extra Expected) " ).append( key );
309 fail( sb.toString() );
312 for ( DependencyGraphEdge edge : actualEdges )
314 String actualKey = DependencyGraphKeys.toKey( edge.getNodeTo() );
315 assertTrue( "Transient Node To [" + actualKey + "] exists in expectedNodes.", expectedNodes
316 .contains( actualKey ) );
320 protected Dependency toDependency( String key )
322 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
324 assertEquals( "Dependency key [" + key + "] should be 5 parts.", 5, parts.length );
326 Dependency dep = new Dependency();
328 dep.setGroupId( parts[0] );
329 dep.setArtifactId( parts[1] );
330 dep.setVersion( parts[2] );
331 dep.setClassifier( parts[3] );
332 dep.setType( parts[4] );
337 protected ArchivaProjectModel toModel( String key, Dependency deps[] )
339 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
341 assertEquals( "Dependency key [" + key + "] should be 3 parts.", 3, parts.length );
343 ArchivaProjectModel model = new ArchivaProjectModel();
344 model.setGroupId( parts[0] );
345 model.setArtifactId( parts[1] );
346 model.setVersion( parts[2] );
347 model.setOrigin( "testcase" );
348 model.setPackaging( "jar" );
352 for ( int i = 0; i < deps.length; i++ )
354 Dependency dep = deps[i];
355 model.addDependency( dep );
362 protected VersionedReference toVersionedReference( String key )
364 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
365 assertEquals( "Versioned Reference [" + key + "] part count.", 3, parts.length );
367 VersionedReference ref = new VersionedReference();
368 ref.setGroupId( parts[0] );
369 ref.setArtifactId( parts[1] );
370 ref.setVersion( parts[2] );