1 package org.apache.maven.archiva.dependency.graph.walk;
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.lang.StringUtils;
23 import org.apache.maven.archiva.dependency.graph.DependencyGraph;
24 import org.apache.maven.archiva.dependency.graph.DependencyGraphEdge;
25 import org.apache.maven.archiva.dependency.graph.DependencyGraphKeys;
26 import org.apache.maven.archiva.dependency.graph.DependencyGraphNode;
27 import org.apache.maven.archiva.dependency.graph.tasks.FlagCyclicEdgesTask;
28 import org.apache.maven.archiva.model.ArtifactReference;
30 import java.util.List;
32 import junit.framework.TestCase;
35 * DependencyGraphWalkerTest
39 public class DependencyGraphWalkerTest
44 * [foo-util] ---> [foo-common]
46 * ---------> [foo-xml] ---> [xercesImpl] ---> [xmlParserAPIs]
48 * \ ---> [jdom] ----+
50 * ----> [jaxen] <--+
53 public void testModerateWalk()
55 DependencyGraph graph = new DependencyGraph( "org.foo", "foo-util", "1.0" );
56 String rootKey = DependencyGraphKeys.toKey( graph.getRootNode().getArtifact() );
57 addEdgeAndNodes( graph, toEdge( rootKey, "org.foo:foo-common:1.0::jar" ) );
58 addEdgeAndNodes( graph, toEdge( rootKey, "org.foo:foo-xml:1.0::jar" ) );
60 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "xerces:xercesImpl:2.2.1::jar" ) );
61 addEdgeAndNodes( graph, toEdge( "xerces:xercesImpl:2.2.1::jar", "xerces:xmlParserAPIs:2.2.1::jar" ) );
62 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "jdom:jdom:1.0::jar" ) );
63 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "jaxen:jaxen:1.0::jar" ) );
64 addEdgeAndNodes( graph, toEdge( "jdom:jdom:1.0::jar", "jaxen:jaxen:1.0::jar" ) );
66 DependencyGraphWalker walker = new WalkDepthFirstSearch();
67 WalkCollector walkCollector = new WalkCollector();
68 walker.visit( graph, walkCollector );
70 String expectedPath[] = new String[] {
72 "org.foo:foo-common:1.0::jar",
73 "org.foo:foo-xml:1.0::jar",
74 "jaxen:jaxen:1.0::jar",
75 "xerces:xercesImpl:2.2.1::jar",
76 "xerces:xmlParserAPIs:2.2.1::jar",
77 "jdom:jdom:1.0::jar" };
79 assertVisitor( walkCollector, 1, 7, 7 );
80 assertPath( expectedPath, walkCollector.getCollectedPath() );
85 * [foo-util] ---> [foo-common]
87 * ---------> [foo-xml] ---> [xercesImpl] ---> [xmlParserAPIs]
90 public void testSimpleWalk()
92 DependencyGraph graph = new DependencyGraph( "org.foo", "foo-util", "1.0" );
93 String rootKey = DependencyGraphKeys.toKey( graph.getRootNode().getArtifact() );
94 addEdgeAndNodes( graph, toEdge( rootKey, "org.foo:foo-common:1.0::jar" ) );
95 addEdgeAndNodes( graph, toEdge( rootKey, "org.foo:foo-xml:1.0::jar" ) );
96 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "xerces:xercesImpl:2.2.1::jar" ) );
97 addEdgeAndNodes( graph, toEdge( "xerces:xercesImpl:2.2.1::jar", "xerces:xmlParserAPIs:2.2.1::jar" ) );
99 DependencyGraphWalker walker = new WalkDepthFirstSearch();
100 WalkCollector walkCollector = new WalkCollector();
101 walker.visit( graph, walkCollector );
103 String expectedPath[] = new String[] {
105 "org.foo:foo-common:1.0::jar",
106 "org.foo:foo-xml:1.0::jar",
107 "xerces:xercesImpl:2.2.1::jar",
108 "xerces:xmlParserAPIs:2.2.1::jar" };
110 assertVisitor( walkCollector, 1, 5, 4 );
111 assertPath( expectedPath, walkCollector.getCollectedPath() );
116 * [foo-util] ---> [foo-common]
118 * \ +----------------------------------------+
120 * -------> [foo-xml] ---> [xercesImpl] ---> [xmlParserAPIs]
122 * \ ---> [jdom] ----+
124 * ----> [jaxen] <--+
127 public void testDeepNodeWalk()
129 DependencyGraph graph = new DependencyGraph( "org.foo", "foo-util", "1.0" );
130 String rootKey = DependencyGraphKeys.toKey( graph.getRootNode().getArtifact() );
131 addEdgeAndNodes( graph, toEdge( rootKey, "org.foo:foo-common:1.0::jar" ) );
132 addEdgeAndNodes( graph, toEdge( rootKey, "org.foo:foo-xml:1.0::jar" ) );
134 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "xerces:xercesImpl:2.2.1::jar" ) );
135 addEdgeAndNodes( graph, toEdge( "xerces:xercesImpl:2.2.1::jar", "xerces:xmlParserAPIs:2.2.1::jar" ) );
136 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "jdom:jdom:1.0::jar" ) );
137 addEdgeAndNodes( graph, toEdge( "org.foo:foo-xml:1.0::jar", "jaxen:jaxen:1.0::jar" ) );
138 addEdgeAndNodes( graph, toEdge( "jdom:jdom:1.0::jar", "jaxen:jaxen:1.0::jar" ) );
139 // introduce cyclic dep. intentional. should only result in walking to foo-xml once.
140 addEdgeAndNodes( graph, toEdge( "xerces:xmlParserAPIs:2.2.1::jar", "org.foo:foo-xml:1.0::jar" ) );
142 new FlagCyclicEdgesTask().executeTask( graph );
144 DependencyGraphWalker walker = new WalkDepthFirstSearch();
145 WalkCollector walkCollector = new WalkCollector();
146 ArtifactReference startRef = toArtifactReference( "org.foo:foo-xml:1.0::jar" );
147 DependencyGraphNode startNode = new DependencyGraphNode( startRef );
148 walker.visit( graph, startNode, walkCollector );
150 String expectedPath[] = new String[] {
151 "org.foo:foo-xml:1.0::jar",
152 "jaxen:jaxen:1.0::jar",
153 "xerces:xercesImpl:2.2.1::jar",
154 "xerces:xmlParserAPIs:2.2.1::jar",
155 "jdom:jdom:1.0::jar" };
157 assertVisitor( walkCollector, 1, 5, 6 );
158 assertPath( expectedPath, walkCollector.getCollectedPath() );
161 private void addEdgeAndNodes( DependencyGraph graph, DependencyGraphEdge edge )
163 ensureNodeExists( graph, edge.getNodeFrom() );
164 ensureNodeExists( graph, edge.getNodeTo() );
165 graph.addEdge( edge );
168 private void ensureNodeExists( DependencyGraph graph, ArtifactReference artifact )
170 DependencyGraphNode node = graph.getNode( artifact );
173 node = new DependencyGraphNode( artifact );
174 graph.addNode( node );
178 private void assertPath( String[] expectedPath, List collectedPath )
180 assertEquals( "Path.length", expectedPath.length, collectedPath.size() );
182 for ( int i = 0; i < expectedPath.length; i++ )
184 assertEquals( "Walk path[" + i + "]", expectedPath[i], (String) collectedPath.get( i ) );
188 private void assertVisitor( WalkCollector walkCollector, int countGraphs, int countNodes, int countEdges )
190 assertEquals( "Count of graph discovery.", countGraphs, walkCollector.getCountDiscoverGraph() );
191 assertEquals( "Count of graph finished.", countGraphs, walkCollector.getCountFinishGraph() );
192 assertEquals( "Discover - Finish = 0 (on graph counts)", 0,
193 ( walkCollector.getCountDiscoverGraph() - walkCollector.getCountFinishGraph() ) );
195 assertEquals( "Count of node discovery.", countNodes, walkCollector.getCountDiscoverNode() );
196 assertEquals( "Count of node finished.", countNodes, walkCollector.getCountFinishNode() );
197 assertEquals( "Discover - Finish = 0 (on node counts)", 0,
198 ( walkCollector.getCountDiscoverNode() - walkCollector.getCountFinishNode() ) );
200 assertEquals( "Count of edge discovery.", countEdges, walkCollector.getCountDiscoverEdge() );
201 assertEquals( "Count of edge finished.", countEdges, walkCollector.getCountFinishEdge() );
202 assertEquals( "Discover - Finish = 0 (on edge counts)", 0,
203 ( walkCollector.getCountDiscoverEdge() - walkCollector.getCountFinishEdge() ) );
206 private DependencyGraphEdge toEdge( String fromKey, String toKey )
208 ArtifactReference nodeFrom = toArtifactReference( fromKey );
209 ArtifactReference nodeTo = toArtifactReference( toKey );
211 return new DependencyGraphEdge( nodeFrom, nodeTo );
214 private ArtifactReference toArtifactReference( String key )
216 String parts[] = StringUtils.splitPreserveAllTokens( key, ':' );
217 assertEquals( "ArtifactReference [" + key + "] parts should equal 5", 5, parts.length );
219 ArtifactReference artifact = new ArtifactReference();
220 artifact.setGroupId( parts[0] );
221 artifact.setArtifactId( parts[1] );
222 artifact.setVersion( parts[2] );
223 artifact.setClassifier( parts[3] );
224 artifact.setType( parts[4] );