]> source.dussan.org Git - archiva.git/blob
f0ff715823fc39ff968bb851e591c91aa1d60ac4
[archiva.git] /
1 package org.apache.archiva.dependency.tree.maven2;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
37 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import java.io.File;
42 import java.util.Collections;
43
44 @RunWith( SpringJUnit4ClassRunner.class )
45 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
46 public class DependencyTreeBuilderTest
47     extends TestCase
48 {
49     @Inject
50     @Named (value = "dependencyTreeBuilder#maven2")
51     private DependencyTreeBuilder builder;
52
53     private static final String TEST_REPO_ID = "test";
54
55     private static final String TEST_VERSION = "1.2.1";
56
57     private static final String TEST_ARTIFACT_ID = "archiva-common";
58
59     private static final String TEST_GROUP_ID = "org.apache.archiva";
60
61     private ArtifactFactory artifactFactory;
62
63     @Inject  @Named(value = "archivaConfiguration#test")
64     ArchivaConfiguration config;
65
66     @Before
67     public void setUp()
68         throws Exception
69     {
70         super.setUp();
71
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 );
78
79         artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory();
80     }
81
82     @Test
83     public void testBuilder()
84         throws DependencyTreeBuilderException
85     {
86         MockControl control = MockControl.createStrictControl( DependencyNodeVisitor.class );
87         DependencyNodeVisitor visitor = (DependencyNodeVisitor) control.getMock();
88
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" );
121
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 );
138
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" ) );
151
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 );
164
165         control.expectAndReturn( visitor.visit( mainNode ), true );
166
167         control.expectAndReturn( visitor.visit( commonsLang ), true );
168         control.expectAndReturn( visitor.endVisit( commonsLang ), true );
169
170         control.expectAndReturn( visitor.visit( commonsIO ), true );
171         control.expectAndReturn( visitor.endVisit( commonsIO ), true );
172
173         control.expectAndReturn( visitor.visit( slf4j ), true );
174         control.expectAndReturn( visitor.endVisit( slf4j ), true );
175
176         control.expectAndReturn( visitor.visit( plexusAPI ), true );
177         control.expectAndReturn( visitor.endVisit( plexusAPI ), true );
178
179         control.expectAndReturn( visitor.visit( plexusSpring ), true );
180
181         control.expectAndReturn( visitor.visit( springContext ), true );
182         control.expectAndReturn( visitor.endVisit( springContext ), true );
183
184         control.expectAndReturn( visitor.visit( springTest ), true );
185         control.expectAndReturn( visitor.endVisit( springTest ), true );
186
187         control.expectAndReturn( visitor.visit( plexusUtils ), true );
188         control.expectAndReturn( visitor.endVisit( plexusUtils ), true );
189
190         control.expectAndReturn( visitor.visit( slf4jLog4j12 ), true );
191         control.expectAndReturn( visitor.endVisit( slf4jLog4j12 ), true );
192
193         control.expectAndReturn( visitor.visit( plexusLog4j ), true );
194         control.expectAndReturn( visitor.endVisit( plexusLog4j ), true );
195
196         control.expectAndReturn( visitor.visit( log4j ), true );
197         control.expectAndReturn( visitor.endVisit( log4j ), true );
198
199         control.expectAndReturn( visitor.visit( mavenArtifact ), true );
200         control.expectAndReturn( visitor.endVisit( mavenArtifact ), true );
201
202         control.expectAndReturn( visitor.visit( mavenProject ), true );
203         control.expectAndReturn( visitor.endVisit( mavenProject ), true );
204
205         control.expectAndReturn( visitor.visit( mavenCore ), true );
206         control.expectAndReturn( visitor.endVisit( mavenCore ), true );
207
208         control.expectAndReturn( visitor.visit( mavenSettings ), true );
209         control.expectAndReturn( visitor.endVisit( mavenSettings ), true );
210
211         control.expectAndReturn( visitor.visit( mavenModel ), true );
212         control.expectAndReturn( visitor.endVisit( mavenModel ), true );
213
214         control.expectAndReturn( visitor.visit( plexusCommandLine ), true );
215         control.expectAndReturn( visitor.endVisit( plexusCommandLine ), true );
216
217         control.expectAndReturn( visitor.visit( plexusRegistryCommons ), true );
218         control.expectAndReturn( visitor.endVisit( plexusRegistryCommons ), true );
219
220         control.expectAndReturn( visitor.visit( plexusRegistryApi ), true );
221         control.expectAndReturn( visitor.endVisit( plexusRegistryApi ), true );
222
223         control.expectAndReturn( visitor.endVisit( plexusSpring ), true );
224
225         control.expectAndReturn( visitor.visit( xalan ), true );
226         control.expectAndReturn( visitor.endVisit( xalan ), true );
227
228         control.expectAndReturn( visitor.visit( dom4j ), true );
229         control.expectAndReturn( visitor.endVisit( dom4j ), true );
230
231         control.expectAndReturn( visitor.visit( junit ), true );
232         control.expectAndReturn( visitor.endVisit( junit ), true );
233
234         control.expectAndReturn( visitor.visit( easymock ), true );
235         control.expectAndReturn( visitor.endVisit( easymock ), true );
236
237         control.expectAndReturn( visitor.visit( easymockExt ), true );
238         control.expectAndReturn( visitor.endVisit( easymockExt ), true );
239
240         control.expectAndReturn( visitor.endVisit( mainNode ), true );
241
242         control.replay();
243
244         builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
245                                      TEST_VERSION, visitor );
246
247         control.verify();
248     }
249
250     private Artifact createProjectArtifact( String groupId, String artifactId, String version )
251     {
252         return artifactFactory.createProjectArtifact( groupId, artifactId, version );
253     }
254
255     private Artifact createArtifact( String groupId, String artifactId, String version, String scope )
256     {
257         return artifactFactory.createDependencyArtifact( groupId, artifactId, VersionRange.createFromVersion( version ),
258                                                          "jar", null, scope );
259     }
260
261     private Artifact createArtifact( String groupId, String artifactId, String version )
262     {
263         return createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE );
264     }
265
266     @Test
267     public void testBuilderMissingDependency()
268         throws DependencyTreeBuilderException
269     {
270         MockControl control = MockControl.createStrictControl( DependencyNodeVisitor.class );
271         DependencyNodeVisitor visitor = (DependencyNodeVisitor) control.getMock();
272
273         // not visited
274
275         control.replay();
276
277         builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
278                                      "not-a-version", visitor );
279
280         control.verify();
281     }
282 }