1 package org.apache.archiva.dependency.tree.maven2;
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 junit.framework.TestCase;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.easymock.MockControl;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.sonatype.aether.RepositorySystem;
33 import org.sonatype.aether.artifact.Artifact;
34 import org.sonatype.aether.graph.Dependency;
35 import org.sonatype.aether.graph.DependencyNode;
36 import org.sonatype.aether.graph.DependencyVisitor;
37 import org.sonatype.aether.impl.internal.DefaultRepositorySystem;
38 import org.sonatype.aether.util.artifact.DefaultArtifact;
39 import org.sonatype.aether.util.graph.DefaultDependencyNode;
40 import org.springframework.test.context.ContextConfiguration;
42 import javax.inject.Inject;
43 import javax.inject.Named;
45 import java.util.Collections;
46 import java.util.HashMap;
49 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
50 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
51 public class DependencyTreeBuilderTestMaven3
55 @Named( value = "dependencyTreeBuilder#maven3" )
56 private Maven3DependencyTreeBuilder builder;
59 private PlexusSisuBridge plexusSisuBridge;
61 private static final String TEST_REPO_ID = "test";
63 private static final String TEST_VERSION = "1.2.1";
65 private static final String TEST_ARTIFACT_ID = "archiva-common";
67 private static final String TEST_GROUP_ID = "org.apache.archiva";
69 private DefaultRepositorySystem defaultRepositorySystem;
72 final Map<String, DependencyNode> nodes = new HashMap<String, DependencyNode>();
75 @Named( value = "archivaConfiguration#test" )
76 ArchivaConfiguration config;
84 defaultRepositorySystem = (DefaultRepositorySystem) plexusSisuBridge.lookup( RepositorySystem.class );
86 Configuration configuration = new Configuration();
87 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
88 repoConfig.setId( TEST_REPO_ID );
89 repoConfig.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
90 configuration.addManagedRepository( repoConfig );
91 config.save( configuration );
93 //artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory();
97 private Artifact createArtifact( String groupId, String artifactId, String version )
99 return new DefaultArtifact( groupId, artifactId, "jar", version );
102 private String getId( Artifact artifact )
104 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
108 public void testBuilderDependencies()
112 DependencyNode springContext = new DefaultDependencyNode(
113 new Dependency( createArtifact( "org.springframework", "spring-context", "2.5.6" ), "compile" ) );
115 //springContext.setPremanagedVersion( "2.5.5" );
117 nodes.put( getId( springContext.getDependency().getArtifact() ), springContext );
119 DependencyNode springTest = new DefaultDependencyNode(
120 new Dependency( createArtifact( "org.springframework", "spring-test", "2.5.5" ), "test" ) );
122 nodes.put( getId( springTest.getDependency().getArtifact() ), springTest );
124 DependencyNode plexusUtils = new DefaultDependencyNode(
125 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-utils", "1.4.5" ), "compile" ) );
127 //plexusUtils.setPremanagedVersion( "1.5.1" );
129 nodes.put( getId( plexusUtils.getDependency().getArtifact() ), plexusUtils );
131 DependencyNode slf4jLog4j12 = new DefaultDependencyNode(
132 new Dependency( createArtifact( "org.slf4j", "slf4j-log4j12", "1.5.0" ), "runtime" ) );
134 //slf4jLog4j12.setPremanagedScope( "test" );
136 nodes.put( getId( slf4jLog4j12.getDependency().getArtifact() ), slf4jLog4j12 );
138 DependencyNode plexusLog4j = new DefaultDependencyNode(
139 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-log4j-logging", "1.1-alpha-3" ), "test" ) );
141 nodes.put( getId( plexusLog4j.getDependency().getArtifact() ), plexusLog4j );
143 DependencyNode log4j =
144 new DefaultDependencyNode( new Dependency( createArtifact( "log4j", "log4j", "1.2.14" ), "test" ) );
146 nodes.put( getId( log4j.getDependency().getArtifact() ), log4j );
148 DependencyNode mavenArtifact = new DefaultDependencyNode(
149 new Dependency( createArtifact( "org.apache.maven", "maven-artifact", "2.0.8" ), "test" ) );
151 nodes.put( getId( mavenArtifact.getDependency().getArtifact() ), mavenArtifact );
153 DependencyNode mavenProject = new DefaultDependencyNode(
154 new Dependency( createArtifact( "org.apache.maven", "maven-project", "2.0.8" ), "test" ) );
156 nodes.put( getId( mavenProject.getDependency().getArtifact() ), mavenProject );
158 DependencyNode mavenCore = new DefaultDependencyNode(
159 new Dependency( createArtifact( "org.apache.maven", "maven-core", "2.0.8" ), "test" ) );
161 nodes.put( getId( mavenCore.getDependency().getArtifact() ), mavenCore );
163 DependencyNode mavenSettings = new DefaultDependencyNode(
164 new Dependency( createArtifact( "org.apache.maven", "maven-settings", "2.0.8" ), "test" ) );
166 nodes.put( getId( mavenSettings.getDependency().getArtifact() ), mavenSettings );
168 DependencyNode mavenModel = new DefaultDependencyNode(
169 new Dependency( createArtifact( "org.apache.maven", "maven-model", "2.0.8" ), "test" ) );
171 nodes.put( getId( mavenModel.getDependency().getArtifact() ), mavenModel );
173 DependencyNode plexusCommandLine = new DefaultDependencyNode(
174 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-command-line", "1.0-alpha-2" ), "test" ) );
176 nodes.put( getId( plexusCommandLine.getDependency().getArtifact() ), plexusCommandLine );
178 DependencyNode plexusRegistryCommons = new DefaultDependencyNode(
179 new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-commons", "1.0-alpha-2" ),
182 nodes.put( getId( plexusRegistryCommons.getDependency().getArtifact() ), plexusRegistryCommons );
184 //plexusRegistryCommons.setPremanagedVersion( "1.0-alpha-3" );
186 DependencyNode plexusRegistryApi = new DefaultDependencyNode(
187 new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-api", "1.0-alpha-2" ),
190 nodes.put( getId( plexusRegistryApi.getDependency().getArtifact() ), plexusRegistryApi );
192 //plexusRegistryApi.setPremanagedVersion( "1.0-alpha-3" );
194 DependencyNode plexusSpring = new DefaultDependencyNode(
195 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-spring", "1.2" ), "test" ) );
197 nodes.put( getId( plexusSpring.getDependency().getArtifact() ), plexusSpring );
199 plexusSpring.getChildren().add( springContext );
200 plexusSpring.getChildren().add( springTest );
201 plexusSpring.getChildren().add( plexusUtils );
202 plexusSpring.getChildren().add( slf4jLog4j12 );
203 plexusSpring.getChildren().add( plexusLog4j );
204 plexusSpring.getChildren().add( log4j );
205 plexusSpring.getChildren().add( mavenArtifact );
206 plexusSpring.getChildren().add( mavenProject );
207 plexusSpring.getChildren().add( mavenCore );
208 plexusSpring.getChildren().add( mavenSettings );
209 plexusSpring.getChildren().add( mavenModel );
210 plexusSpring.getChildren().add( plexusCommandLine );
211 plexusSpring.getChildren().add( plexusRegistryCommons );
212 plexusSpring.getChildren().add( plexusRegistryApi );
214 DependencyNode commonsLang = new DefaultDependencyNode(
215 new Dependency( createArtifact( "commons-lang", "commons-lang", "2.2" ), "compile" ) );
217 nodes.put( getId( commonsLang.getDependency().getArtifact() ), commonsLang );
219 DependencyNode commonsIO = new DefaultDependencyNode(
220 new Dependency( createArtifact( "commons-io", "commons-io", "1.4" ), "compile" ) );
222 nodes.put( getId( commonsIO.getDependency().getArtifact() ), commonsIO );
224 DependencyNode slf4j = new DefaultDependencyNode(
225 new Dependency( createArtifact( "org.slf4j", "slf4j-api", "1.5.0" ), "compile" ) );
227 nodes.put( getId( slf4j.getDependency().getArtifact() ), slf4j );
229 DependencyNode plexusAPI = new DefaultDependencyNode(
230 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" ),
233 nodes.put( getId( plexusAPI.getDependency().getArtifact() ), plexusAPI );
235 DependencyNode xalan =
236 new DefaultDependencyNode( new Dependency( createArtifact( "xalan", "xalan", "2.7.0" ), "compile" ) );
238 nodes.put( getId( xalan.getDependency().getArtifact() ), xalan );
240 DependencyNode dom4j =
241 new TestDefaultDependencyNode( new Dependency( createArtifact( "dom4j", "dom4j", "1.6.1" ), "test" ) );
243 nodes.put( getId( dom4j.getDependency().getArtifact() ), dom4j );
245 //dom4j.setFailedUpdateScope("compile");
247 DependencyNode junit =
248 new TestDefaultDependencyNode( new Dependency( createArtifact( "junit", "junit", "3.8.1" ), "test" ) );
250 nodes.put( getId( junit.getDependency().getArtifact() ), junit );
252 DependencyNode easymock = new TestDefaultDependencyNode(
253 new Dependency( createArtifact( "easymock", "easymock", "1.2_Java1.3" ), "test" ) );
255 nodes.put( getId( easymock.getDependency().getArtifact() ), easymock );
257 DependencyNode easymockExt = new TestDefaultDependencyNode(
258 new Dependency( createArtifact( "easymock", "easymockclassextension", "1.2" ), "test" ) );
260 nodes.put( getId( easymockExt.getDependency().getArtifact() ), easymockExt );
262 DependencyNode mainNode = new TestDefaultDependencyNode(
263 new Dependency( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ), "" ) );
265 nodes.put( getId( mainNode.getDependency().getArtifact() ), mainNode );
267 mainNode.getChildren().add( commonsLang );
268 mainNode.getChildren().add( commonsIO );
269 mainNode.getChildren().add( slf4j );
270 mainNode.getChildren().add( plexusAPI );
271 mainNode.getChildren().add( plexusSpring );
272 mainNode.getChildren().add( xalan );
273 mainNode.getChildren().add( dom4j );
274 mainNode.getChildren().add( junit );
275 mainNode.getChildren().add( easymock );
276 mainNode.getChildren().add( easymockExt );
278 /*defaultRepositorySystem.setDependencyCollector( new DependencyCollector()
281 public CollectResult collectDependencies( RepositorySystemSession session, CollectRequest request )
282 throws DependencyCollectionException
284 CollectResult collectResult = new CollectResult( request );
285 collectResult.setRoot( new DefaultDependencyNode() );
286 for ( Dependency dependency : request.getDependencies() )
288 DependencyNode node = nodes.get( getId( dependency.getArtifact() ) );
291 collectResult.getRoot().getChildren().add( node );
294 return collectResult;
299 MockControl control = MockControl.createStrictControl( DependencyVisitor.class );
300 DependencyVisitor visitor = (DependencyVisitor) control.getMock();
302 control.expectAndReturn( visitor.visitEnter( mainNode ), true );
304 control.expectAndReturn( visitor.visitEnter( commonsLang ), true );
305 control.expectAndReturn( visitor.visitLeave( commonsLang ), true );
307 control.expectAndReturn( visitor.visitEnter( commonsIO ), true );
308 control.expectAndReturn( visitor.visitLeave( commonsIO ), true );
310 control.expectAndReturn( visitor.visitEnter( slf4j ), true );
311 control.expectAndReturn( visitor.visitLeave( slf4j ), true );
313 control.expectAndReturn( visitor.visitEnter( plexusAPI ), true );
314 control.expectAndReturn( visitor.visitLeave( plexusAPI ), true );
316 control.expectAndReturn( visitor.visitEnter( plexusSpring ), true );
318 control.expectAndReturn( visitor.visitEnter( springContext ), true );
319 control.expectAndReturn( visitor.visitLeave( springContext ), true );
321 control.expectAndReturn( visitor.visitEnter( springTest ), true );
322 control.expectAndReturn( visitor.visitLeave( springTest ), true );
324 control.expectAndReturn( visitor.visitEnter( plexusUtils ), true );
325 control.expectAndReturn( visitor.visitLeave( plexusUtils ), true );
327 control.expectAndReturn( visitor.visitEnter( slf4jLog4j12 ), true );
328 control.expectAndReturn( visitor.visitLeave( slf4jLog4j12 ), true );
330 control.expectAndReturn( visitor.visitEnter( plexusLog4j ), true );
331 control.expectAndReturn( visitor.visitLeave( plexusLog4j ), true );
333 control.expectAndReturn( visitor.visitEnter( log4j ), true );
334 control.expectAndReturn( visitor.visitLeave( log4j ), true );
336 control.expectAndReturn( visitor.visitEnter( mavenArtifact ), true );
337 control.expectAndReturn( visitor.visitLeave( mavenArtifact ), true );
339 control.expectAndReturn( visitor.visitEnter( mavenProject ), true );
340 control.expectAndReturn( visitor.visitLeave( mavenProject ), true );
342 control.expectAndReturn( visitor.visitEnter( mavenCore ), true );
343 control.expectAndReturn( visitor.visitLeave( mavenCore ), true );
345 control.expectAndReturn( visitor.visitEnter( mavenSettings ), true );
346 control.expectAndReturn( visitor.visitLeave( mavenSettings ), true );
348 control.expectAndReturn( visitor.visitEnter( mavenModel ), true );
349 control.expectAndReturn( visitor.visitLeave( mavenModel ), true );
351 control.expectAndReturn( visitor.visitEnter( plexusCommandLine ), true );
352 control.expectAndReturn( visitor.visitLeave( plexusCommandLine ), true );
354 control.expectAndReturn( visitor.visitEnter( plexusRegistryCommons ), true );
355 control.expectAndReturn( visitor.visitLeave( plexusRegistryCommons ), true );
357 control.expectAndReturn( visitor.visitEnter( plexusRegistryApi ), true );
358 control.expectAndReturn( visitor.visitLeave( plexusRegistryApi ), true );
360 control.expectAndReturn( visitor.visitLeave( plexusSpring ), true );
362 control.expectAndReturn( visitor.visitEnter( xalan ), true );
363 control.expectAndReturn( visitor.visitLeave( xalan ), true );
365 control.expectAndReturn( visitor.visitEnter( dom4j ), true );
366 control.expectAndReturn( visitor.visitLeave( dom4j ), true );
368 control.expectAndReturn( visitor.visitEnter( junit ), true );
369 control.expectAndReturn( visitor.visitLeave( junit ), true );
371 control.expectAndReturn( visitor.visitEnter( easymock ), true );
372 control.expectAndReturn( visitor.visitLeave( easymock ), true );
374 control.expectAndReturn( visitor.visitEnter( easymockExt ), true );
375 control.expectAndReturn( visitor.visitLeave( easymockExt ), true );
377 control.expectAndReturn( visitor.visitLeave( mainNode ), true );
381 visitor = new DependencyVisitor()
383 public boolean visitEnter( DependencyNode dependencyNode )
388 public boolean visitLeave( DependencyNode dependencyNode )
394 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
395 TEST_VERSION, visitor );
401 public static class TestDefaultDependencyNode
402 extends DefaultDependencyNode
405 private TestDefaultDependencyNode( Dependency dependency )
411 public int hashCode()
413 return super.hashCode();
417 public boolean equals( Object o )
419 DependencyNode node = (DependencyNode) o;
420 boolean equals = this.getDependency().getArtifact().getGroupId().equals(
421 node.getDependency().getArtifact().getGroupId() ) &&
422 this.getDependency().getArtifact().getArtifactId().equals(
423 node.getDependency().getArtifact().getArtifactId() ) &&
424 this.getDependency().getArtifact().getVersion().equals(
425 node.getDependency().getArtifact().getVersion() );