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.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.sonatype.aether.RepositorySystem;
32 import org.sonatype.aether.RepositorySystemSession;
33 import org.sonatype.aether.artifact.Artifact;
34 import org.sonatype.aether.collection.CollectRequest;
35 import org.sonatype.aether.collection.CollectResult;
36 import org.sonatype.aether.collection.DependencyCollectionException;
37 import org.sonatype.aether.graph.Dependency;
38 import org.sonatype.aether.graph.DependencyNode;
39 import org.sonatype.aether.graph.DependencyVisitor;
40 import org.sonatype.aether.impl.DependencyCollector;
41 import org.sonatype.aether.impl.internal.DefaultRepositorySystem;
42 import org.sonatype.aether.util.artifact.DefaultArtifact;
43 import org.sonatype.aether.util.graph.DefaultDependencyNode;
44 import org.springframework.test.context.ContextConfiguration;
46 import javax.inject.Inject;
47 import javax.inject.Named;
49 import java.util.Collections;
50 import java.util.HashMap;
53 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
54 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
55 public class DependencyTreeBuilderTestMaven3
59 @Named( value = "dependencyTreeBuilder#maven3" )
60 private Maven3DependencyTreeBuilder builder;
63 private PlexusSisuBridge plexusSisuBridge;
65 private static final String TEST_REPO_ID = "test";
67 private static final String TEST_VERSION = "1.2.1";
69 private static final String TEST_ARTIFACT_ID = "archiva-common";
71 private static final String TEST_GROUP_ID = "org.apache.archiva";
73 private DefaultRepositorySystem defaultRepositorySystem;
76 final Map<String, DependencyNode> nodes = new HashMap<String, DependencyNode>();
79 @Named( value = "archivaConfiguration#test" )
80 ArchivaConfiguration config;
88 defaultRepositorySystem = (DefaultRepositorySystem) plexusSisuBridge.lookup( RepositorySystem.class );
90 DefaultDependencyNode springContext = new DefaultDependencyNode(
91 new Dependency( createArtifact( "org.springframework", "spring-context", "2.5.6" ), "compile" ) );
93 springContext.setPremanagedVersion( "2.5.5" );
95 nodes.put( getId( springContext.getDependency().getArtifact() ), springContext );
97 DefaultDependencyNode springTest = new DefaultDependencyNode(
98 new Dependency( createArtifact( "org.springframework", "spring-test", "2.5.5" ), "test" ) );
100 nodes.put( getId( springTest.getDependency().getArtifact() ), springTest );
102 DefaultDependencyNode plexusUtils = new DefaultDependencyNode(
103 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-utils", "1.4.5" ), "compile" ) );
105 plexusUtils.setPremanagedVersion( "1.5.1" );
107 nodes.put( getId( plexusUtils.getDependency().getArtifact() ), plexusUtils );
109 DefaultDependencyNode slf4jLog4j12 = new DefaultDependencyNode(
110 new Dependency( createArtifact( "org.slf4j", "slf4j-log4j12", "1.5.0" ), "runtime" ) );
112 slf4jLog4j12.setPremanagedScope( "test" );
114 nodes.put( getId( slf4jLog4j12.getDependency().getArtifact() ), slf4jLog4j12 );
116 DefaultDependencyNode plexusLog4j = new DefaultDependencyNode(
117 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-log4j-logging", "1.1-alpha-3" ), "test" ) );
119 nodes.put( getId( plexusLog4j.getDependency().getArtifact() ), plexusLog4j );
121 DefaultDependencyNode log4j =
122 new DefaultDependencyNode( new Dependency( createArtifact( "log4j", "log4j", "1.2.14" ), "test" ) );
124 nodes.put( getId( log4j.getDependency().getArtifact() ), log4j );
126 DefaultDependencyNode mavenArtifact = new DefaultDependencyNode(
127 new Dependency( createArtifact( "org.apache.maven", "maven-artifact", "2.0.8" ), "test" ) );
129 nodes.put( getId( mavenArtifact.getDependency().getArtifact() ), mavenArtifact );
131 DefaultDependencyNode mavenProject = new DefaultDependencyNode(
132 new Dependency( createArtifact( "org.apache.maven", "maven-project", "2.0.8" ), "test" ) );
134 nodes.put( getId( mavenProject.getDependency().getArtifact() ), mavenProject );
136 DefaultDependencyNode mavenCore = new DefaultDependencyNode(
137 new Dependency( createArtifact( "org.apache.maven", "maven-core", "2.0.8" ), "test" ) );
139 nodes.put( getId( mavenCore.getDependency().getArtifact() ), mavenCore );
141 DefaultDependencyNode mavenSettings = new DefaultDependencyNode(
142 new Dependency( createArtifact( "org.apache.maven", "maven-settings", "2.0.8" ), "test" ) );
144 nodes.put( getId( mavenSettings.getDependency().getArtifact() ), mavenSettings );
146 DefaultDependencyNode mavenModel = new DefaultDependencyNode(
147 new Dependency( createArtifact( "org.apache.maven", "maven-model", "2.0.8" ), "test" ) );
149 nodes.put( getId( mavenModel.getDependency().getArtifact() ), mavenModel );
151 DefaultDependencyNode plexusCommandLine = new DefaultDependencyNode(
152 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-command-line", "1.0-alpha-2" ), "test" ) );
154 nodes.put( getId( plexusCommandLine.getDependency().getArtifact() ), plexusCommandLine );
156 DefaultDependencyNode plexusRegistryCommons = new DefaultDependencyNode(
157 new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-commons", "1.0-alpha-2" ),
160 nodes.put( getId( plexusRegistryCommons.getDependency().getArtifact() ), plexusRegistryCommons );
162 plexusRegistryCommons.setPremanagedVersion( "1.0-alpha-3" );
164 DefaultDependencyNode plexusRegistryApi = new DefaultDependencyNode(
165 new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-api", "1.0-alpha-2" ),
168 nodes.put( getId( plexusRegistryApi.getDependency().getArtifact() ), plexusRegistryApi );
170 plexusRegistryApi.setPremanagedVersion( "1.0-alpha-3" );
172 DefaultDependencyNode plexusSpring = new DefaultDependencyNode(
173 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-spring", "1.2" ), "test" ) );
175 nodes.put( getId( plexusSpring.getDependency().getArtifact() ), plexusSpring );
177 plexusSpring.getChildren().add( springContext );
178 plexusSpring.getChildren().add( springTest );
179 plexusSpring.getChildren().add( plexusUtils );
180 plexusSpring.getChildren().add( slf4jLog4j12 );
181 plexusSpring.getChildren().add( plexusLog4j );
182 plexusSpring.getChildren().add( log4j );
183 plexusSpring.getChildren().add( mavenArtifact );
184 plexusSpring.getChildren().add( mavenProject );
185 plexusSpring.getChildren().add( mavenCore );
186 plexusSpring.getChildren().add( mavenSettings );
187 plexusSpring.getChildren().add( mavenModel );
188 plexusSpring.getChildren().add( plexusCommandLine );
189 plexusSpring.getChildren().add( plexusRegistryCommons );
190 plexusSpring.getChildren().add( plexusRegistryApi );
192 DefaultDependencyNode commonsLang = new DefaultDependencyNode(
193 new Dependency( createArtifact( "commons-lang", "commons-lang", "2.2" ), "compile" ) );
195 nodes.put( getId( commonsLang.getDependency().getArtifact() ), commonsLang );
197 DefaultDependencyNode commonsIO = new DefaultDependencyNode(
198 new Dependency( createArtifact( "commons-io", "commons-io", "1.4" ), "compile" ) );
200 nodes.put( getId( commonsIO.getDependency().getArtifact() ), commonsIO );
202 DefaultDependencyNode slf4j = new DefaultDependencyNode(
203 new Dependency( createArtifact( "org.slf4j", "slf4j-api", "1.5.0" ), "compile" ) );
205 nodes.put( getId( slf4j.getDependency().getArtifact() ), slf4j );
207 DefaultDependencyNode plexusAPI = new DefaultDependencyNode(
208 new Dependency( createArtifact( "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" ),
211 nodes.put( getId( plexusAPI.getDependency().getArtifact() ), plexusAPI );
213 DefaultDependencyNode xalan =
214 new DefaultDependencyNode( new Dependency( createArtifact( "xalan", "xalan", "2.7.0" ), "compile" ) );
216 nodes.put( getId( xalan.getDependency().getArtifact() ), xalan );
218 DefaultDependencyNode dom4j =
219 new DefaultDependencyNode( new Dependency( createArtifact( "dom4j", "dom4j", "1.6.1" ), "test" ) );
221 nodes.put( getId( dom4j.getDependency().getArtifact() ), dom4j );
223 //dom4j.setFailedUpdateScope("compile");
225 DefaultDependencyNode junit =
226 new DefaultDependencyNode( new Dependency( createArtifact( "junit", "junit", "3.8.1" ), "test" ) );
228 nodes.put( getId( junit.getDependency().getArtifact() ), junit );
230 DefaultDependencyNode easymock = new DefaultDependencyNode(
231 new Dependency( createArtifact( "easymock", "easymock", "1.2_Java1.3" ), "test" ) );
233 nodes.put( getId( easymock.getDependency().getArtifact() ), easymock );
235 DefaultDependencyNode easymockExt = new DefaultDependencyNode(
236 new Dependency( createArtifact( "easymock", "easymockclassextension", "1.2" ), "test" ) );
238 nodes.put( getId( easymockExt.getDependency().getArtifact() ), easymockExt );
240 DependencyNode mainNode = new DefaultDependencyNode(
241 new Dependency( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ), "compile" ) );
243 nodes.put( getId( mainNode.getDependency().getArtifact() ), mainNode );
245 mainNode.getChildren().add( commonsLang );
246 mainNode.getChildren().add( commonsIO );
247 mainNode.getChildren().add( slf4j );
248 mainNode.getChildren().add( plexusAPI );
249 mainNode.getChildren().add( plexusSpring );
250 mainNode.getChildren().add( xalan );
251 mainNode.getChildren().add( dom4j );
252 mainNode.getChildren().add( junit );
253 mainNode.getChildren().add( easymock );
254 mainNode.getChildren().add( easymockExt );
256 defaultRepositorySystem.setDependencyCollector( new DependencyCollector()
259 public CollectResult collectDependencies( RepositorySystemSession session, CollectRequest request )
260 throws DependencyCollectionException
262 CollectResult collectResult = new CollectResult( request );
263 collectResult.setRoot( new DefaultDependencyNode() );
264 for ( Dependency dependency : request.getDependencies() )
266 DependencyNode node = nodes.get( getId( dependency.getArtifact() ) );
269 collectResult.getRoot().getChildren().add( node );
272 return collectResult;
276 Configuration configuration = new Configuration();
277 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
278 repoConfig.setId( TEST_REPO_ID );
279 repoConfig.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
280 configuration.addManagedRepository( repoConfig );
281 config.save( configuration );
283 //artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory();
287 private Artifact createArtifact( String groupId, String artifactId, String version )
289 return new DefaultArtifact( groupId, artifactId, null, version );
292 private String getId( Artifact artifact )
294 return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
298 public void testBuilderDependencies()
302 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
303 TEST_VERSION, new DependencyVisitor()
305 public boolean visitEnter( DependencyNode dependencyNode )
310 public boolean visitLeave( DependencyNode dependencyNode )