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 java.util.Collections;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.versioning.VersionRange;
30 import org.apache.maven.shared.dependency.tree.DependencyNode;
31 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
32 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34 import org.easymock.MockControl;
36 public class DependencyTreeBuilderTest
37 extends PlexusInSpringTestCase
39 private DependencyTreeBuilder builder;
41 private static final String TEST_REPO_ID = "test";
43 private static final String TEST_VERSION = "1.2.1";
45 private static final String TEST_ARTIFACT_ID = "archiva-common";
47 private static final String TEST_GROUP_ID = "org.apache.archiva";
49 private ArtifactFactory artifactFactory;
52 protected void setUp()
57 ArchivaConfiguration config = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
59 Configuration configuration = new Configuration();
60 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
61 repoConfig.setId( TEST_REPO_ID );
62 repoConfig.setLocation( getTestPath( "src/test/repositories/test" ) );
63 configuration.addManagedRepository( repoConfig );
64 config.save( configuration );
66 builder = (DependencyTreeBuilder) lookup( DependencyTreeBuilder.class, "maven2" );
67 artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.class );
70 public void testBuilder()
71 throws DependencyTreeBuilderException
73 MockControl control = MockControl.createStrictControl( DependencyNodeVisitor.class );
74 DependencyNodeVisitor visitor = (DependencyNodeVisitor) control.getMock();
76 DependencyNode springContext =
77 new DependencyNode( createArtifact( "org.springframework", "spring-context", "2.5.6" ) );
78 springContext.setPremanagedVersion( "2.5.5" );
79 DependencyNode springTest =
80 new DependencyNode( createArtifact( "org.springframework", "spring-test", "2.5.5", "test" ) );
81 DependencyNode plexusUtils =
82 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-utils", "1.4.5" ) );
83 plexusUtils.setPremanagedVersion( "1.5.1" );
84 DependencyNode slf4jLog4j12 =
85 new DependencyNode( createArtifact( "org.slf4j", "slf4j-log4j12", "1.5.0", "runtime" ) );
86 slf4jLog4j12.setPremanagedScope( "test" );
87 DependencyNode plexusLog4j = new DependencyNode(
88 createArtifact( "org.codehaus.plexus", "plexus-log4j-logging", "1.1-alpha-3", "test" ) );
89 DependencyNode log4j = new DependencyNode( createArtifact( "log4j", "log4j", "1.2.14", "test" ) );
90 DependencyNode mavenArtifact =
91 new DependencyNode( createArtifact( "org.apache.maven", "maven-artifact", "2.0.8", "test" ) );
92 DependencyNode mavenProject =
93 new DependencyNode( createArtifact( "org.apache.maven", "maven-project", "2.0.8", "test" ) );
94 DependencyNode mavenCore =
95 new DependencyNode( createArtifact( "org.apache.maven", "maven-core", "2.0.8", "test" ) );
96 DependencyNode mavenSettings =
97 new DependencyNode( createArtifact( "org.apache.maven", "maven-settings", "2.0.8", "test" ) );
98 DependencyNode mavenModel =
99 new DependencyNode( createArtifact( "org.apache.maven", "maven-model", "2.0.8", "test" ) );
100 DependencyNode plexusCommandLine =
101 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-command-line", "1.0-alpha-2", "test" ) );
102 DependencyNode plexusRegistryCommons = new DependencyNode(
103 createArtifact( "org.codehaus.plexus.registry", "plexus-registry-commons", "1.0-alpha-2", "test" ) );
104 plexusRegistryCommons.setPremanagedVersion( "1.0-alpha-3" );
105 DependencyNode plexusRegistryApi = new DependencyNode(
106 createArtifact( "org.codehaus.plexus.registry", "plexus-registry-api", "1.0-alpha-2", "test" ) );
107 plexusRegistryApi.setPremanagedVersion( "1.0-alpha-3" );
109 DependencyNode plexusSpring =
110 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-spring", "1.2", "test" ) );
111 plexusSpring.addChild( springContext );
112 plexusSpring.addChild( springTest );
113 plexusSpring.addChild( plexusUtils );
114 plexusSpring.addChild( slf4jLog4j12 );
115 plexusSpring.addChild( plexusLog4j );
116 plexusSpring.addChild( log4j );
117 plexusSpring.addChild( mavenArtifact );
118 plexusSpring.addChild( mavenProject );
119 plexusSpring.addChild( mavenCore );
120 plexusSpring.addChild( mavenSettings );
121 plexusSpring.addChild( mavenModel );
122 plexusSpring.addChild( plexusCommandLine );
123 plexusSpring.addChild( plexusRegistryCommons );
124 plexusSpring.addChild( plexusRegistryApi );
126 DependencyNode commonsLang = new DependencyNode( createArtifact( "commons-lang", "commons-lang", "2.2" ) );
127 DependencyNode commonsIO = new DependencyNode( createArtifact( "commons-io", "commons-io", "1.4" ) );
128 DependencyNode slf4j = new DependencyNode( createArtifact( "org.slf4j", "slf4j-api", "1.5.0" ) );
129 DependencyNode plexusAPI =
130 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" ) );
131 DependencyNode xalan = new DependencyNode( createArtifact( "xalan", "xalan", "2.7.0" ) );
132 DependencyNode dom4j = new DependencyNode( createArtifact( "dom4j", "dom4j", "1.6.1", "test" ) );
133 dom4j.setFailedUpdateScope( "compile" );
134 DependencyNode junit = new DependencyNode( createArtifact( "junit", "junit", "3.8.1", "test" ) );
135 DependencyNode easymock = new DependencyNode( createArtifact( "easymock", "easymock", "1.2_Java1.3", "test" ) );
136 DependencyNode easymockExt =
137 new DependencyNode( createArtifact( "easymock", "easymockclassextension", "1.2", "test" ) );
139 DependencyNode mainNode =
140 new DependencyNode( createProjectArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
141 mainNode.addChild( commonsLang );
142 mainNode.addChild( commonsIO );
143 mainNode.addChild( slf4j );
144 mainNode.addChild( plexusAPI );
145 mainNode.addChild( plexusSpring );
146 mainNode.addChild( xalan );
147 mainNode.addChild( dom4j );
148 mainNode.addChild( junit );
149 mainNode.addChild( easymock );
150 mainNode.addChild( easymockExt );
152 control.expectAndReturn( visitor.visit( mainNode ), true );
154 control.expectAndReturn( visitor.visit( commonsLang ), true );
155 control.expectAndReturn( visitor.endVisit( commonsLang ), true );
157 control.expectAndReturn( visitor.visit( commonsIO ), true );
158 control.expectAndReturn( visitor.endVisit( commonsIO ), true );
160 control.expectAndReturn( visitor.visit( slf4j ), true );
161 control.expectAndReturn( visitor.endVisit( slf4j ), true );
163 control.expectAndReturn( visitor.visit( plexusAPI ), true );
164 control.expectAndReturn( visitor.endVisit( plexusAPI ), true );
166 control.expectAndReturn( visitor.visit( plexusSpring ), true );
168 control.expectAndReturn( visitor.visit( springContext ), true );
169 control.expectAndReturn( visitor.endVisit( springContext ), true );
171 control.expectAndReturn( visitor.visit( springTest ), true );
172 control.expectAndReturn( visitor.endVisit( springTest ), true );
174 control.expectAndReturn( visitor.visit( plexusUtils ), true );
175 control.expectAndReturn( visitor.endVisit( plexusUtils ), true );
177 control.expectAndReturn( visitor.visit( slf4jLog4j12 ), true );
178 control.expectAndReturn( visitor.endVisit( slf4jLog4j12 ), true );
180 control.expectAndReturn( visitor.visit( plexusLog4j ), true );
181 control.expectAndReturn( visitor.endVisit( plexusLog4j ), true );
183 control.expectAndReturn( visitor.visit( log4j ), true );
184 control.expectAndReturn( visitor.endVisit( log4j ), true );
186 control.expectAndReturn( visitor.visit( mavenArtifact ), true );
187 control.expectAndReturn( visitor.endVisit( mavenArtifact ), true );
189 control.expectAndReturn( visitor.visit( mavenProject ), true );
190 control.expectAndReturn( visitor.endVisit( mavenProject ), true );
192 control.expectAndReturn( visitor.visit( mavenCore ), true );
193 control.expectAndReturn( visitor.endVisit( mavenCore ), true );
195 control.expectAndReturn( visitor.visit( mavenSettings ), true );
196 control.expectAndReturn( visitor.endVisit( mavenSettings ), true );
198 control.expectAndReturn( visitor.visit( mavenModel ), true );
199 control.expectAndReturn( visitor.endVisit( mavenModel ), true );
201 control.expectAndReturn( visitor.visit( plexusCommandLine ), true );
202 control.expectAndReturn( visitor.endVisit( plexusCommandLine ), true );
204 control.expectAndReturn( visitor.visit( plexusRegistryCommons ), true );
205 control.expectAndReturn( visitor.endVisit( plexusRegistryCommons ), true );
207 control.expectAndReturn( visitor.visit( plexusRegistryApi ), true );
208 control.expectAndReturn( visitor.endVisit( plexusRegistryApi ), true );
210 control.expectAndReturn( visitor.endVisit( plexusSpring ), true );
212 control.expectAndReturn( visitor.visit( xalan ), true );
213 control.expectAndReturn( visitor.endVisit( xalan ), true );
215 control.expectAndReturn( visitor.visit( dom4j ), true );
216 control.expectAndReturn( visitor.endVisit( dom4j ), true );
218 control.expectAndReturn( visitor.visit( junit ), true );
219 control.expectAndReturn( visitor.endVisit( junit ), true );
221 control.expectAndReturn( visitor.visit( easymock ), true );
222 control.expectAndReturn( visitor.endVisit( easymock ), true );
224 control.expectAndReturn( visitor.visit( easymockExt ), true );
225 control.expectAndReturn( visitor.endVisit( easymockExt ), true );
227 control.expectAndReturn( visitor.endVisit( mainNode ), true );
231 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
232 TEST_VERSION, visitor );
237 private Artifact createProjectArtifact( String groupId, String artifactId, String version )
239 return artifactFactory.createProjectArtifact( groupId, artifactId, version );
242 private Artifact createArtifact( String groupId, String artifactId, String version, String scope )
244 return artifactFactory.createDependencyArtifact( groupId, artifactId, VersionRange.createFromVersion( version ),
245 "jar", null, scope );
248 private Artifact createArtifact( String groupId, String artifactId, String version )
250 return createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE );
253 public void testBuilderMissingDependency()
254 throws DependencyTreeBuilderException
256 MockControl control = MockControl.createStrictControl( DependencyNodeVisitor.class );
257 DependencyNodeVisitor visitor = (DependencyNodeVisitor) control.getMock();
263 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
264 "not-a-version", visitor );