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.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.Configuration;
25 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.factory.ArtifactFactory;
28 import org.apache.maven.artifact.versioning.VersionRange;
29 import org.apache.maven.shared.dependency.tree.DependencyNode;
30 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
31 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
32 import org.easymock.MockControl;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.springframework.test.context.ContextConfiguration;
38 import javax.inject.Inject;
39 import javax.inject.Named;
41 import java.util.Collections;
42 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
44 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
46 public class DependencyTreeBuilderTest
50 @Named (value = "dependencyTreeBuilder#maven2")
51 private DependencyTreeBuilder builder;
53 private static final String TEST_REPO_ID = "test";
55 private static final String TEST_VERSION = "1.2.1";
57 private static final String TEST_ARTIFACT_ID = "archiva-common";
59 private static final String TEST_GROUP_ID = "org.apache.archiva";
61 private ArtifactFactory artifactFactory;
63 @Inject @Named(value = "archivaConfiguration#test")
64 ArchivaConfiguration config;
72 Configuration configuration = new Configuration();
73 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
74 repoConfig.setId( TEST_REPO_ID );
75 repoConfig.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
76 configuration.addManagedRepository( repoConfig );
77 config.save( configuration );
79 artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory();
83 public void testBuilder()
84 throws DependencyTreeBuilderException
86 MockControl control = MockControl.createStrictControl( DependencyNodeVisitor.class );
87 DependencyNodeVisitor visitor = (DependencyNodeVisitor) control.getMock();
89 DependencyNode springContext =
90 new DependencyNode( createArtifact( "org.springframework", "spring-context", "2.5.6" ) );
91 springContext.setPremanagedVersion( "2.5.5" );
92 DependencyNode springTest =
93 new DependencyNode( createArtifact( "org.springframework", "spring-test", "2.5.5", "test" ) );
94 DependencyNode plexusUtils =
95 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-utils", "1.4.5" ) );
96 plexusUtils.setPremanagedVersion( "1.5.1" );
97 DependencyNode slf4jLog4j12 =
98 new DependencyNode( createArtifact( "org.slf4j", "slf4j-log4j12", "1.5.0", "runtime" ) );
99 slf4jLog4j12.setPremanagedScope( "test" );
100 DependencyNode plexusLog4j = new DependencyNode(
101 createArtifact( "org.codehaus.plexus", "plexus-log4j-logging", "1.1-alpha-3", "test" ) );
102 DependencyNode log4j = new DependencyNode( createArtifact( "log4j", "log4j", "1.2.14", "test" ) );
103 DependencyNode mavenArtifact =
104 new DependencyNode( createArtifact( "org.apache.maven", "maven-artifact", "2.0.8", "test" ) );
105 DependencyNode mavenProject =
106 new DependencyNode( createArtifact( "org.apache.maven", "maven-project", "2.0.8", "test" ) );
107 DependencyNode mavenCore =
108 new DependencyNode( createArtifact( "org.apache.maven", "maven-core", "2.0.8", "test" ) );
109 DependencyNode mavenSettings =
110 new DependencyNode( createArtifact( "org.apache.maven", "maven-settings", "2.0.8", "test" ) );
111 DependencyNode mavenModel =
112 new DependencyNode( createArtifact( "org.apache.maven", "maven-model", "2.0.8", "test" ) );
113 DependencyNode plexusCommandLine =
114 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-command-line", "1.0-alpha-2", "test" ) );
115 DependencyNode plexusRegistryCommons = new DependencyNode(
116 createArtifact( "org.codehaus.plexus.registry", "plexus-registry-commons", "1.0-alpha-2", "test" ) );
117 plexusRegistryCommons.setPremanagedVersion( "1.0-alpha-3" );
118 DependencyNode plexusRegistryApi = new DependencyNode(
119 createArtifact( "org.codehaus.plexus.registry", "plexus-registry-api", "1.0-alpha-2", "test" ) );
120 plexusRegistryApi.setPremanagedVersion( "1.0-alpha-3" );
122 DependencyNode plexusSpring =
123 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-spring", "1.2", "test" ) );
124 plexusSpring.addChild( springContext );
125 plexusSpring.addChild( springTest );
126 plexusSpring.addChild( plexusUtils );
127 plexusSpring.addChild( slf4jLog4j12 );
128 plexusSpring.addChild( plexusLog4j );
129 plexusSpring.addChild( log4j );
130 plexusSpring.addChild( mavenArtifact );
131 plexusSpring.addChild( mavenProject );
132 plexusSpring.addChild( mavenCore );
133 plexusSpring.addChild( mavenSettings );
134 plexusSpring.addChild( mavenModel );
135 plexusSpring.addChild( plexusCommandLine );
136 plexusSpring.addChild( plexusRegistryCommons );
137 plexusSpring.addChild( plexusRegistryApi );
139 DependencyNode commonsLang = new DependencyNode( createArtifact( "commons-lang", "commons-lang", "2.2" ) );
140 DependencyNode commonsIO = new DependencyNode( createArtifact( "commons-io", "commons-io", "1.4" ) );
141 DependencyNode slf4j = new DependencyNode( createArtifact( "org.slf4j", "slf4j-api", "1.5.0" ) );
142 DependencyNode plexusAPI =
143 new DependencyNode( createArtifact( "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" ) );
144 DependencyNode xalan = new DependencyNode( createArtifact( "xalan", "xalan", "2.7.0" ) );
145 DependencyNode dom4j = new DependencyNode( createArtifact( "dom4j", "dom4j", "1.6.1", "test" ) );
146 dom4j.setFailedUpdateScope( "compile" );
147 DependencyNode junit = new DependencyNode( createArtifact( "junit", "junit", "3.8.1", "test" ) );
148 DependencyNode easymock = new DependencyNode( createArtifact( "easymock", "easymock", "1.2_Java1.3", "test" ) );
149 DependencyNode easymockExt =
150 new DependencyNode( createArtifact( "easymock", "easymockclassextension", "1.2", "test" ) );
152 DependencyNode mainNode =
153 new DependencyNode( createProjectArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ) );
154 mainNode.addChild( commonsLang );
155 mainNode.addChild( commonsIO );
156 mainNode.addChild( slf4j );
157 mainNode.addChild( plexusAPI );
158 mainNode.addChild( plexusSpring );
159 mainNode.addChild( xalan );
160 mainNode.addChild( dom4j );
161 mainNode.addChild( junit );
162 mainNode.addChild( easymock );
163 mainNode.addChild( easymockExt );
165 control.expectAndReturn( visitor.visit( mainNode ), true );
167 control.expectAndReturn( visitor.visit( commonsLang ), true );
168 control.expectAndReturn( visitor.endVisit( commonsLang ), true );
170 control.expectAndReturn( visitor.visit( commonsIO ), true );
171 control.expectAndReturn( visitor.endVisit( commonsIO ), true );
173 control.expectAndReturn( visitor.visit( slf4j ), true );
174 control.expectAndReturn( visitor.endVisit( slf4j ), true );
176 control.expectAndReturn( visitor.visit( plexusAPI ), true );
177 control.expectAndReturn( visitor.endVisit( plexusAPI ), true );
179 control.expectAndReturn( visitor.visit( plexusSpring ), true );
181 control.expectAndReturn( visitor.visit( springContext ), true );
182 control.expectAndReturn( visitor.endVisit( springContext ), true );
184 control.expectAndReturn( visitor.visit( springTest ), true );
185 control.expectAndReturn( visitor.endVisit( springTest ), true );
187 control.expectAndReturn( visitor.visit( plexusUtils ), true );
188 control.expectAndReturn( visitor.endVisit( plexusUtils ), true );
190 control.expectAndReturn( visitor.visit( slf4jLog4j12 ), true );
191 control.expectAndReturn( visitor.endVisit( slf4jLog4j12 ), true );
193 control.expectAndReturn( visitor.visit( plexusLog4j ), true );
194 control.expectAndReturn( visitor.endVisit( plexusLog4j ), true );
196 control.expectAndReturn( visitor.visit( log4j ), true );
197 control.expectAndReturn( visitor.endVisit( log4j ), true );
199 control.expectAndReturn( visitor.visit( mavenArtifact ), true );
200 control.expectAndReturn( visitor.endVisit( mavenArtifact ), true );
202 control.expectAndReturn( visitor.visit( mavenProject ), true );
203 control.expectAndReturn( visitor.endVisit( mavenProject ), true );
205 control.expectAndReturn( visitor.visit( mavenCore ), true );
206 control.expectAndReturn( visitor.endVisit( mavenCore ), true );
208 control.expectAndReturn( visitor.visit( mavenSettings ), true );
209 control.expectAndReturn( visitor.endVisit( mavenSettings ), true );
211 control.expectAndReturn( visitor.visit( mavenModel ), true );
212 control.expectAndReturn( visitor.endVisit( mavenModel ), true );
214 control.expectAndReturn( visitor.visit( plexusCommandLine ), true );
215 control.expectAndReturn( visitor.endVisit( plexusCommandLine ), true );
217 control.expectAndReturn( visitor.visit( plexusRegistryCommons ), true );
218 control.expectAndReturn( visitor.endVisit( plexusRegistryCommons ), true );
220 control.expectAndReturn( visitor.visit( plexusRegistryApi ), true );
221 control.expectAndReturn( visitor.endVisit( plexusRegistryApi ), true );
223 control.expectAndReturn( visitor.endVisit( plexusSpring ), true );
225 control.expectAndReturn( visitor.visit( xalan ), true );
226 control.expectAndReturn( visitor.endVisit( xalan ), true );
228 control.expectAndReturn( visitor.visit( dom4j ), true );
229 control.expectAndReturn( visitor.endVisit( dom4j ), true );
231 control.expectAndReturn( visitor.visit( junit ), true );
232 control.expectAndReturn( visitor.endVisit( junit ), true );
234 control.expectAndReturn( visitor.visit( easymock ), true );
235 control.expectAndReturn( visitor.endVisit( easymock ), true );
237 control.expectAndReturn( visitor.visit( easymockExt ), true );
238 control.expectAndReturn( visitor.endVisit( easymockExt ), true );
240 control.expectAndReturn( visitor.endVisit( mainNode ), true );
244 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
245 TEST_VERSION, visitor );
250 private Artifact createProjectArtifact( String groupId, String artifactId, String version )
252 return artifactFactory.createProjectArtifact( groupId, artifactId, version );
255 private Artifact createArtifact( String groupId, String artifactId, String version, String scope )
257 return artifactFactory.createDependencyArtifact( groupId, artifactId, VersionRange.createFromVersion( version ),
258 "jar", null, scope );
261 private Artifact createArtifact( String groupId, String artifactId, String version )
263 return createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE );
267 public void testBuilderMissingDependency()
268 throws DependencyTreeBuilderException
270 MockControl control = MockControl.createStrictControl( DependencyNodeVisitor.class );
271 DependencyNodeVisitor visitor = (DependencyNodeVisitor) control.getMock();
277 builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
278 "not-a-version", visitor );