]> source.dussan.org Git - archiva.git/blob
a03e70291134f40de33eb35b35fb19cdb75b75f1
[archiva.git] /
1 package org.apache.archiva.metadata.repository.storage.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.common.utils.FileUtil;
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.configuration.ProxyConnectorConfiguration;
28 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.metadata.model.Dependency;
31 import org.apache.archiva.metadata.model.License;
32 import org.apache.archiva.metadata.model.MailingList;
33 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
34 import org.apache.archiva.metadata.repository.filter.AllFilter;
35 import org.apache.archiva.metadata.repository.filter.Filter;
36 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
37 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
38 import org.apache.archiva.proxy.common.WagonFactory;
39 import org.apache.archiva.proxy.common.WagonFactoryRequest;
40 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.maven.wagon.Wagon;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.springframework.test.context.ContextConfiguration;
47
48 import javax.inject.Inject;
49 import javax.inject.Named;
50 import java.io.File;
51 import java.io.IOException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
56
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.when;
59
60 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
61 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
62 public class Maven2RepositoryMetadataResolverMRM1411Test
63     extends TestCase
64 {
65     private static final Filter<String> ALL = new AllFilter<String>();
66
67     @Inject
68     @Named ( value = "repositoryStorage#maven2" )
69     private Maven2RepositoryStorage storage;
70
71     private static final String TEST_REPO_ID = "test";
72
73     private static final String TEST_REMOTE_REPO_ID = "central";
74
75     private static final String ASF_SCM_CONN_BASE = "scm:svn:http://svn.apache.org/repos/asf/";
76
77     private static final String ASF_SCM_DEV_CONN_BASE = "scm:svn:https://svn.apache.org/repos/asf/";
78
79     private static final String ASF_SCM_VIEWVC_BASE = "http://svn.apache.org/viewvc/";
80
81     private static final String TEST_SCM_CONN_BASE = "scm:svn:http://svn.example.com/repos/";
82
83     private static final String TEST_SCM_DEV_CONN_BASE = "scm:svn:https://svn.example.com/repos/";
84
85     private static final String TEST_SCM_URL_BASE = "http://svn.example.com/repos/";
86
87     private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e";
88
89     private static final String EMPTY_SHA1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
90
91     @Inject
92     private ArchivaConfiguration configuration;
93
94     private WagonFactory wagonFactory;
95
96     ManagedRepositoryConfiguration testRepo;
97
98     Configuration c;
99
100     @Before
101     @Override
102     public void setUp()
103         throws Exception
104     {
105         super.setUp();
106
107         c = new Configuration();
108         testRepo = new ManagedRepositoryConfiguration();
109         testRepo.setId( TEST_REPO_ID );
110         testRepo.setLocation( new File( "target/test-repository" ).getAbsolutePath() );
111         testRepo.setReleases( true );
112         testRepo.setSnapshots( true );
113         c.addManagedRepository( testRepo );
114
115         RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
116         testRemoteRepo.setId( TEST_REMOTE_REPO_ID );
117         testRemoteRepo.setLayout( "default" );
118         testRemoteRepo.setName( "Central Repository" );
119         testRemoteRepo.setUrl( "http://central.repo.com/maven2" );
120         testRemoteRepo.setTimeout( 10 );
121         c.addRemoteRepository( testRemoteRepo );
122
123         ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
124         proxyConnector.setSourceRepoId( TEST_REPO_ID );
125         proxyConnector.setTargetRepoId( TEST_REMOTE_REPO_ID );
126         proxyConnector.setDisabled( false );
127         c.addProxyConnector( proxyConnector );
128
129         configuration.save( c );
130
131         assertTrue( c.getManagedRepositories().get( 0 ).isSnapshots() );
132         assertTrue( c.getManagedRepositories().get( 0 ).isReleases() );
133
134         wagonFactory = mock( WagonFactory.class );
135
136         storage.setWagonFactory( wagonFactory );
137
138         Wagon wagon = new MockWagon();
139         when( wagonFactory.getWagon(
140             new WagonFactoryRequest( "wagon#http", new HashMap<String, String>() ) ) ).thenReturn( wagon );
141     }
142
143     // Tests for MRM-1411 - START
144     @Test
145     public void testGetProjectVersionMetadataWithParentSuccessful()
146         throws Exception
147     {
148         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
149                                     "target/test-repository/com/example/test/test-artifact-module-a" );
150
151         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
152             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
153
154         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
155         assertEquals( "jar", facet.getPackaging() );
156         assertEquals( "http://maven.apache.org", metadata.getUrl() );
157         assertEquals( "com.example.test", facet.getParent().getGroupId() );
158         assertEquals( "test-artifact-root", facet.getParent().getArtifactId() );
159         assertEquals( "1.0", facet.getParent().getVersion() );
160         assertEquals( "test-artifact-module-a", facet.getArtifactId() );
161         assertEquals( "com.example.test", facet.getGroupId() );
162         assertNull( metadata.getCiManagement() );
163         assertNotNull( metadata.getDescription() );
164
165         checkApacheLicense( metadata );
166
167         assertEquals( "1.0", metadata.getId() );
168         assertEquals( "Test Artifact :: Module A", metadata.getName() );
169         String path = "test-artifact/trunk/test-artifact-module-a";
170         assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
171         assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
172         assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
173
174         List<Dependency> dependencies = metadata.getDependencies();
175         assertEquals( 2, dependencies.size() );
176         assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
177         assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
178
179         List<String> paths = new ArrayList<String>();
180         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
181         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
182         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
183
184         deleteTestArtifactWithParent( paths );
185     }
186
187     @Test
188     public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured()
189         throws Exception
190     {
191         // remove configuration
192         Configuration config = configuration.getConfiguration();
193         RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById( TEST_REMOTE_REPO_ID );
194         config.removeRemoteRepository( remoteRepo );
195
196         configuration.save( config );
197
198         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
199                                     "target/test-repository/com/example/test/test-artifact-module-a" );
200
201         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
202             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-artifact-module-a", "1.0" ) );
203         assertEquals( "1.0", metadata.getId() );
204
205         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
206         assertNotNull( facet );
207         assertEquals( "com.example.test", facet.getGroupId() );
208         assertEquals( "test-artifact-module-a", facet.getArtifactId() );
209         assertEquals( "jar", facet.getPackaging() );
210
211         List<String> paths = new ArrayList<String>();
212         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
213         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
214         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
215
216         deleteTestArtifactWithParent( paths );
217     }
218
219     @Test
220     public void testGetProjectVersionMetadataWithParentNotInAnyRemoteRepo()
221         throws Exception
222     {
223         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-artifact-module-a",
224                                     "target/test-repository/com/example/test/test-artifact-module-a" );
225
226         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
227             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "missing-parent", "1.1" ) );
228
229         assertEquals( "1.1", metadata.getId() );
230
231         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
232         assertNotNull( facet );
233         assertEquals( "com.example.test", facet.getGroupId() );
234         assertEquals( "missing-parent", facet.getArtifactId() );
235         assertEquals( "jar", facet.getPackaging() );
236
237         List<String> paths = new ArrayList<String>();
238         paths.add( "target/test-repository/com/example/test/test-artifact-module-a" );
239         paths.add( "target/test-repository/com/example/test/test-artifact-parent" );
240         paths.add( "target/test-repository/com/example/test/test-artifact-root" );
241
242         deleteTestArtifactWithParent( paths );
243     }
244
245     @Test
246     public void testGetProjectVersionMetadataWithParentSnapshotVersion()
247         throws Exception
248     {
249
250         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
251                                     "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
252
253         //copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-root",
254         //                            "target/test-repository/com/example/test/test-snapshot-artifact-root" );
255
256         ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
257             new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
258                                      "1.1-SNAPSHOT" ) );
259
260         MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet( MavenProjectFacet.FACET_ID );
261         assertEquals( "jar", facet.getPackaging() );
262         assertEquals( "com.example.test", facet.getParent().getGroupId() );
263         assertEquals( "test-snapshot-artifact-root", facet.getParent().getArtifactId() );
264         assertEquals( "1.1-SNAPSHOT", facet.getParent().getVersion() );
265         assertEquals( "test-snapshot-artifact-module-a", facet.getArtifactId() );
266         assertEquals( "com.example.test", facet.getGroupId() );
267         assertNull( metadata.getCiManagement() );
268         assertNotNull( metadata.getDescription() );
269
270         checkApacheLicense( metadata );
271
272         assertEquals( "1.1-SNAPSHOT", metadata.getId() );
273         assertEquals( "Test Snapshot Artifact :: Module A", metadata.getName() );
274         String path = "test-snapshot-artifact/trunk/test-snapshot-artifact-module-a";
275         assertEquals( TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection() );
276         assertEquals( TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection() );
277         assertEquals( TEST_SCM_URL_BASE + path, metadata.getScm().getUrl() );
278
279         List<Dependency> dependencies = metadata.getDependencies();
280         assertEquals( 2, dependencies.size() );
281         assertDependency( dependencies.get( 0 ), "commons-io", "commons-io", "1.4" );
282         assertDependency( dependencies.get( 1 ), "junit", "junit", "3.8.1", "test" );
283
284         List<String> paths = new ArrayList<String>();
285         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
286         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
287
288         deleteTestArtifactWithParent( paths );
289     }
290
291     @Test
292     public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed()
293         throws Exception
294     {
295         testRepo.setSnapshots( false );
296         configuration.save( c );
297         assertFalse( c.getManagedRepositories().get( 0 ).isSnapshots() );
298         copyTestArtifactWithParent( "target/test-classes/com/example/test/test-snapshot-artifact-module-a",
299                                     "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
300
301         try
302         {
303             ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(
304                 new ReadMetadataRequest( TEST_REPO_ID, "com.example.test", "test-snapshot-artifact-module-a",
305                                          "1.1-SNAPSHOT" ) );
306             fail( "Should not be found" );
307         }
308         catch ( RepositoryStorageRuntimeException e )
309         {
310         }
311
312         List<String> paths = new ArrayList<String>();
313         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-module-a" );
314         paths.add( "target/test-repository/com/example/test/test-snapshot-artifact-root" );
315
316         deleteTestArtifactWithParent( paths );
317     }
318     // Tests for MRM-1411 - END
319
320     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )
321     {
322         assertDependency( dependency, groupId, artifactId, version, "compile" );
323     }
324
325     private void assertDependency( Dependency dependency, String groupId, String artifactId, String version,
326                                    String scope )
327     {
328         assertEquals( artifactId, dependency.getArtifactId() );
329         assertEquals( "jar", dependency.getType() );
330         assertEquals( version, dependency.getVersion() );
331         assertEquals( groupId, dependency.getGroupId() );
332         assertEquals( scope, dependency.getScope() );
333         assertNull( dependency.getClassifier() );
334         assertNull( dependency.getSystemPath() );
335     }
336
337     private void assertArtifact( ArtifactMetadata artifact, String id, int size, String sha1, String md5 )
338     {
339         assertEquals( id, artifact.getId() );
340         assertEquals( md5, artifact.getMd5() );
341         assertEquals( sha1, artifact.getSha1() );
342         assertEquals( size, artifact.getSize() );
343         assertEquals( "org.codehaus.plexus", artifact.getNamespace() );
344         assertEquals( "plexus-spring", artifact.getProject() );
345         assertEquals( "1.2", artifact.getVersion() );
346         assertEquals( TEST_REPO_ID, artifact.getRepositoryId() );
347     }
348
349     private void assertMailingList( MailingList mailingList, String name, String archive, String post, String subscribe,
350                                     String unsubscribe, List<String> otherArchives, boolean allowPost )
351     {
352         assertEquals( archive, mailingList.getMainArchiveUrl() );
353         if ( allowPost )
354         {
355             assertEquals( post, mailingList.getPostAddress() );
356         }
357         else
358         {
359             assertNull( mailingList.getPostAddress() );
360         }
361         assertEquals( subscribe, mailingList.getSubscribeAddress() );
362         assertEquals( unsubscribe, mailingList.getUnsubscribeAddress() );
363         assertEquals( name, mailingList.getName() );
364         assertEquals( otherArchives, mailingList.getOtherArchives() );
365     }
366
367     private void assertMailingList( String prefix, MailingList mailingList, String name, boolean allowPost,
368                                     String nabbleUrl )
369     {
370         List<String> otherArchives = new ArrayList<String>();
371         otherArchives.add( "http://www.mail-archive.com/" + prefix + "@archiva.apache.org" );
372         if ( nabbleUrl != null )
373         {
374             otherArchives.add( nabbleUrl );
375         }
376         otherArchives.add( "http://markmail.org/list/org.apache.archiva." + prefix );
377         assertMailingList( mailingList, name, "http://mail-archives.apache.org/mod_mbox/archiva-" + prefix + "/",
378                            prefix + "@archiva.apache.org", prefix + "-subscribe@archiva.apache.org",
379                            prefix + "-unsubscribe@archiva.apache.org", otherArchives, allowPost );
380     }
381
382     private void checkApacheLicense( ProjectVersionMetadata metadata )
383     {
384         assertEquals( Arrays.asList( new License( "The Apache Software License, Version 2.0",
385                                                   "http://www.apache.org/licenses/LICENSE-2.0.txt" ) ),
386                       metadata.getLicenses() );
387     }
388
389     private void checkOrganizationApache( ProjectVersionMetadata metadata )
390     {
391         assertEquals( "The Apache Software Foundation", metadata.getOrganization().getName() );
392         assertEquals( "http://www.apache.org/", metadata.getOrganization().getUrl() );
393     }
394
395     private void deleteTestArtifactWithParent( List<String> pathsToBeDeleted )
396         throws IOException
397     {
398         for ( String path : pathsToBeDeleted )
399         {
400             File dir = new File( FileUtil.getBasedir(), path );
401             FileUtils.deleteDirectory( dir );
402
403             assertFalse( dir.exists() );
404         }
405         File dest = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-module-a" );
406         File parentPom =
407             new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-parent" );
408         File rootPom = new File( FileUtil.getBasedir(), "target/test-repository/com/example/test/test-artifact-root" );
409
410         FileUtils.deleteDirectory( dest );
411         FileUtils.deleteDirectory( parentPom );
412         FileUtils.deleteDirectory( rootPom );
413
414         assertFalse( dest.exists() );
415         assertFalse( parentPom.exists() );
416         assertFalse( rootPom.exists() );
417     }
418
419     private File copyTestArtifactWithParent( String srcPath, String destPath )
420         throws IOException
421     {
422         File src = new File( FileUtil.getBasedir(), srcPath );
423         File dest = new File( FileUtil.getBasedir(), destPath );
424
425         FileUtils.copyDirectory( src, dest );
426         assertTrue( dest.exists() );
427         return dest;
428     }
429
430 }