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