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.apache.maven.project.DependencyResolutionResult;
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.RepositorySystemSession;
34 import org.sonatype.aether.artifact.Artifact;
35 import org.sonatype.aether.collection.CollectRequest;
36 import org.sonatype.aether.collection.CollectResult;
37 import org.sonatype.aether.collection.DependencyCollectionException;
38 import org.sonatype.aether.graph.Dependency;
39 import org.sonatype.aether.graph.DependencyNode;
40 import org.sonatype.aether.graph.DependencyVisitor;
41 import org.sonatype.aether.impl.DependencyCollector;
42 import org.sonatype.aether.impl.internal.DefaultRepositorySystem;
43 import org.sonatype.aether.util.artifact.DefaultArtifact;
44 import org.sonatype.aether.util.graph.DefaultDependencyNode;
45 import org.springframework.test.context.ContextConfiguration;
47 import javax.inject.Inject;
48 import javax.inject.Named;
50 import java.util.Collections;
51 import java.util.HashMap;
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
56 public class DependencyTreeBuilderTestMaven3
60 @Named( value = "dependencyTreeBuilder#maven3" )
61 private Maven3DependencyTreeBuilder builder;
64 private PlexusSisuBridge plexusSisuBridge;
66 private static final String TEST_REPO_ID = "test";
68 private static final String TEST_VERSION = "1.2.1";
70 private static final String TEST_ARTIFACT_ID = "archiva-common";
72 private static final String TEST_GROUP_ID = "org.apache.archiva";
74 private DefaultRepositorySystem defaultRepositorySystem;
78 @Named( value = "archivaConfiguration#test" )
79 ArchivaConfiguration config;
87 defaultRepositorySystem = (DefaultRepositorySystem) plexusSisuBridge.lookup( RepositorySystem.class );
89 final Map<String, DependencyNode> nodes = new HashMap<String, DependencyNode>();
91 DefaultDependencyNode springContext = new DefaultDependencyNode(
92 new Dependency( createArtifact( "org.springframework", "spring-context", "2.5.6" ), "compile" ) );
94 springContext.setPremanagedVersion( "2.5.5" );
96 nodes.put( getId( springContext.getDependency().getArtifact() ), springContext );
98 DefaultDependencyNode springTest = new DefaultDependencyNode(
99 new Dependency( createArtifact( "org.springframework", "spring-test", "2.5.5" ), "test" ) );
101 nodes.put( getId( springTest.getDependency().getArtifact() ), springTest );
103 DefaultDependencyNode plexusUtils = new DefaultDependencyNode(
104 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-utils", "1.4.5" ), "compile" ) );
106 plexusUtils.setPremanagedVersion( "1.5.1" );
108 nodes.put( getId( plexusUtils.getDependency().getArtifact() ), plexusUtils );
110 DefaultDependencyNode slf4jLog4j12 = new DefaultDependencyNode(
111 new Dependency( createArtifact( "org.slf4j", "slf4j-log4j12", "1.5.0" ), "runtime" ) );
113 slf4jLog4j12.setPremanagedScope( "test" );
115 nodes.put( getId( slf4jLog4j12.getDependency().getArtifact() ), slf4jLog4j12 );
117 DefaultDependencyNode plexusLog4j = new DefaultDependencyNode(
118 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-log4j-logging", "1.1-alpha-3" ), "test" ) );
120 nodes.put( getId( plexusLog4j.getDependency().getArtifact() ), plexusLog4j );
122 DefaultDependencyNode log4j =
123 new DefaultDependencyNode( new Dependency( createArtifact( "log4j", "log4j", "1.2.14" ), "test" ) );
125 nodes.put( getId( log4j.getDependency().getArtifact() ), log4j );
127 DefaultDependencyNode mavenArtifact = new DefaultDependencyNode(
128 new Dependency( createArtifact( "org.apache.maven", "maven-artifact", "2.0.8" ), "test" ) );
130 nodes.put( getId( mavenArtifact.getDependency().getArtifact() ), mavenArtifact );
132 DefaultDependencyNode mavenProject = new DefaultDependencyNode(
133 new Dependency( createArtifact( "org.apache.maven", "maven-project", "2.0.8" ), "test" ) );
135 nodes.put( getId( mavenProject.getDependency().getArtifact() ), mavenProject );
137 DefaultDependencyNode mavenCore = new DefaultDependencyNode(
138 new Dependency( createArtifact( "org.apache.maven", "maven-core", "2.0.8" ), "test" ) );
140 nodes.put( getId( mavenCore.getDependency().getArtifact() ), mavenCore );
142 DefaultDependencyNode mavenSettings = new DefaultDependencyNode(
143 new Dependency( createArtifact( "org.apache.maven", "maven-settings", "2.0.8" ), "test" ) );
145 nodes.put( getId( mavenSettings.getDependency().getArtifact() ), mavenSettings );
147 DefaultDependencyNode mavenModel = new DefaultDependencyNode(
148 new Dependency( createArtifact( "org.apache.maven", "maven-model", "2.0.8" ), "test" ) );
150 nodes.put( getId( mavenModel.getDependency().getArtifact() ), mavenModel );
152 DefaultDependencyNode plexusCommandLine = new DefaultDependencyNode(
153 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-command-line", "1.0-alpha-2" ), "test" ) );
155 nodes.put( getId( plexusCommandLine.getDependency().getArtifact() ), plexusCommandLine );
157 DefaultDependencyNode plexusRegistryCommons = new DefaultDependencyNode(
158 new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-commons", "1.0-alpha-2" ),
161 nodes.put( getId( plexusRegistryCommons.getDependency().getArtifact() ), plexusRegistryCommons );
163 plexusRegistryCommons.setPremanagedVersion( "1.0-alpha-3" );
165 DefaultDependencyNode plexusRegistryApi = new DefaultDependencyNode(
166 new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-api", "1.0-alpha-2" ),
169 nodes.put( getId( plexusRegistryApi.getDependency().getArtifact() ), plexusRegistryApi );
171 plexusRegistryApi.setPremanagedVersion( "1.0-alpha-3" );
173 DefaultDependencyNode plexusSpring = new DefaultDependencyNode(
174 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-spring", "1.2" ), "test" ) );
176 nodes.put( getId( plexusSpring.getDependency().getArtifact() ), plexusSpring );
178 plexusSpring.getChildren().add( springContext );
179 plexusSpring.getChildren().add( springTest );
180 plexusSpring.getChildren().add( plexusUtils );
181 plexusSpring.getChildren().add( slf4jLog4j12 );
182 plexusSpring.getChildren().add( plexusLog4j );
183 plexusSpring.getChildren().add( log4j );
184 plexusSpring.getChildren().add( mavenArtifact );
185 plexusSpring.getChildren().add( mavenProject );
186 plexusSpring.getChildren().add( mavenCore );
187 plexusSpring.getChildren().add( mavenSettings );
188 plexusSpring.getChildren().add( mavenModel );
189 plexusSpring.getChildren().add( plexusCommandLine );
190 plexusSpring.getChildren().add( plexusRegistryCommons );
191 plexusSpring.getChildren().add( plexusRegistryApi );
193 DefaultDependencyNode commonsLang = new DefaultDependencyNode(
194 new Dependency( createArtifact( "commons-lang", "commons-lang", "2.2" ), "compile" ) );
196 nodes.put( getId( commonsLang.getDependency().getArtifact() ), commonsLang );
198 DefaultDependencyNode commonsIO = new DefaultDependencyNode(
199 new Dependency( createArtifact( "commons-io", "commons-io", "1.4" ), "compile" ) );
201 nodes.put( getId( commonsIO.getDependency().getArtifact() ), commonsIO );
203 DefaultDependencyNode slf4j = new DefaultDependencyNode(
204 new Dependency( createArtifact( "org.slf4j", "slf4j-api", "1.5.0" ), "compile" ) );
206 nodes.put( getId( slf4j.getDependency().getArtifact() ), slf4j );
208 DefaultDependencyNode plexusAPI = new DefaultDependencyNode(
209 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" ),
212 nodes.put( getId( plexusAPI.getDependency().getArtifact() ), plexusAPI );
214 DefaultDependencyNode xalan =
215 new DefaultDependencyNode( new Dependency( createArtifact( "xalan", "xalan", "2.7.0" ), "compile" ) );
217 nodes.put( getId( xalan.getDependency().getArtifact() ), xalan );
219 DefaultDependencyNode dom4j =
220 new DefaultDependencyNode( new Dependency( createArtifact( "dom4j", "dom4j", "1.6.1" ), "test" ) );
222 nodes.put( getId( dom4j.getDependency().getArtifact() ), dom4j );
224 //dom4j.setFailedUpdateScope("compile");
226 DefaultDependencyNode junit =
227 new DefaultDependencyNode( new Dependency( createArtifact( "junit", "junit", "3.8.1" ), "test" ) );
229 nodes.put( getId( junit.getDependency().getArtifact() ), junit );
231 DefaultDependencyNode easymock = new DefaultDependencyNode(
232 new Dependency( createArtifact( "easymock", "easymock", "1.2_Java1.3" ), "test" ) );
234 nodes.put( getId( easymock.getDependency().getArtifact() ), easymock );
236 DefaultDependencyNode easymockExt = new DefaultDependencyNode(
237 new Dependency( createArtifact( "easymock", "easymockclassextension", "1.2" ), "test" ) );
239 nodes.put( getId( easymockExt.getDependency().getArtifact() ), easymockExt );
241 DependencyNode mainNode = new DefaultDependencyNode(
242 new Dependency( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ), "compile" ) );
244 nodes.put( getId( mainNode.getDependency().getArtifact() ), mainNode );
246 mainNode.getChildren().add( commonsLang );
247 mainNode.getChildren().add( commonsIO );
248 mainNode.getChildren().add( slf4j );
249 mainNode.getChildren().add( plexusAPI );
250 mainNode.getChildren().add( plexusSpring );
251 mainNode.getChildren().add( xalan );
252 mainNode.getChildren().add( dom4j );
253 mainNode.getChildren().add( junit );
254 mainNode.getChildren().add( easymock );
255 mainNode.getChildren().add( easymockExt );
257 defaultRepositorySystem.setDependencyCollector( new DependencyCollector()
260 public CollectResult collectDependencies( RepositorySystemSession session, CollectRequest request )
261 throws DependencyCollectionException
263 CollectResult collectResult = new CollectResult( request );
264 collectResult.setRoot( new DefaultDependencyNode() );
265 for ( Dependency dependency : request.getDependencies() )
267 DependencyNode node = nodes.get( getId( dependency.getArtifact() ) );
270 collectResult.getRoot().getChildren().add( node );
273 return collectResult;
277 Configuration configuration = new Configuration();
278 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
279 repoConfig.setId( TEST_REPO_ID );
280 repoConfig.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
281 configuration.addManagedRepository( repoConfig );
282 config.save( configuration );
284 //artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory();
288 private Artifact createArtifact( String groupId, String artifactId, String version )
290 return new DefaultArtifact( groupId, artifactId, null, version );
293 private String getId( Artifact artifact )
295 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
299 public void testBuilderDependencies()
303 DependencyResolutionResult resolutionResult =
304 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
305 TEST_VERSION, new DependencyVisitor()
307 public boolean visitEnter( DependencyNode dependencyNode )
312 public boolean visitLeave( DependencyNode dependencyNode )
318 assertNotNull( resolutionResult );
319 assertEquals( 10, resolutionResult.getDependencies().size() );