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