View Javadoc
1   package org.apache.archiva.repository.content.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 org.apache.archiva.admin.model.beans.ManagedRepository;
23  import org.apache.archiva.common.utils.FileUtil;
24  import org.apache.archiva.configuration.ArchivaConfiguration;
25  import org.apache.archiva.model.ArtifactReference;
26  import org.apache.archiva.repository.ManagedRepositoryContent;
27  import org.apache.archiva.repository.layout.LayoutException;
28  import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
29  import org.apache.commons.lang.StringUtils;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  import org.springframework.context.ApplicationContext;
34  import org.springframework.test.context.ContextConfiguration;
35  
36  import javax.inject.Inject;
37  import javax.inject.Named;
38  import java.io.File;
39  
40  import static org.junit.Assert.*;
41  
42  /**
43   * RepositoryRequestTest
44   */
45  @RunWith( ArchivaSpringJUnit4ClassRunner.class )
46  @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
47      "classpath:/spring-context-repo-request-test.xml" } )
48  public class RepositoryRequestTest
49  {
50  
51      @Inject
52      protected ApplicationContext applicationContext;
53  
54      @Inject
55      @Named( "archivaConfiguration#repo-request-test" )
56      private ArchivaConfiguration archivaConfiguration;
57  
58      private RepositoryRequest repoRequest;
59  
60      @Before
61      public void setUp()
62          throws Exception
63      {
64          repoRequest = new RepositoryRequest();
65      }
66  
67      @Test
68      public void testInvalidRequestEmptyPath()
69      {
70          assertInvalidRequest( "" );
71      }
72  
73      @Test
74      public void testInvalidRequestSlashOnly()
75      {
76          assertInvalidRequest( "//" );
77      }
78  
79      @Test
80      public void testInvalidRequestNoArtifactId()
81      {
82          assertInvalidRequest( "groupId/jars/-1.0.jar" );
83      }
84  
85  
86      @Test
87      public void testInvalidRequestTooShort()
88      {
89          assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
90      }
91  
92      @Test
93      public void testInvalidDefaultRequestBadLocation()
94      {
95          assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
96      }
97  
98      @Test( expected = LayoutException.class )
99      public void testValidLegacyGanymed()
100         throws Exception
101     {
102         assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
103                      null, "jar" );
104     }
105 
106     @Test
107     public void testValidDefaultGanymed()
108         throws Exception
109     {
110         assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
111                      "ganymed-ssh2", "build210", null, "jar" );
112     }
113 
114     @Test( expected = LayoutException.class )
115     public void testValidLegacyJavaxComm()
116         throws Exception
117     {
118         assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
119     }
120 
121     @Test
122     public void testValidDefaultJavaxComm()
123         throws Exception
124     {
125         assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
126     }
127 
128     @Test( expected = LayoutException.class )
129     public void testValidLegacyJavaxPersistence()
130         throws Exception
131     {
132         assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
133                      "3.0-public_review", null, "jar" );
134     }
135 
136     @Test
137     public void testValidDefaultJavaxPersistence()
138         throws Exception
139     {
140         assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
141                      "3.0-public_review", null, "jar" );
142     }
143 
144     @Test( expected = LayoutException.class )
145     public void testValidLegacyMavenTestPlugin()
146         throws Exception
147     {
148         assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
149     }
150 
151     @Test
152     public void testValidDefaultMavenTestPlugin()
153         throws Exception
154     {
155         assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
156                      null, "pom" );
157     }
158 
159     @Test( expected = LayoutException.class )
160     public void testValidLegacyCommonsLangJavadoc()
161         throws Exception
162     {
163         assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
164                      "javadoc", "javadoc" );
165     }
166 
167     @Test
168     public void testValidDefaultCommonsLangJavadoc()
169         throws Exception
170     {
171         assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
172                      "2.1", "javadoc", "javadoc" );
173     }
174 
175     @Test( expected = LayoutException.class )
176     public void testValidLegacyDerbyPom()
177         throws Exception
178     {
179         assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
180         // Starting slash should not prevent detection.
181         assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
182                      "pom" );
183     }
184 
185     @Test
186     public void testValidDefaultDerbyPom()
187         throws Exception
188     {
189         assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
190                      null, "pom" );
191     }
192 
193     @Test( expected = LayoutException.class )
194     public void testValidLegacyGeronimoEjbSpec()
195         throws Exception
196     {
197         assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
198                      "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
199     }
200 
201     @Test
202     public void testValidDefaultGeronimoEjbSpec()
203         throws Exception
204     {
205         assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
206                      "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
207     }
208 
209     @Test( expected = LayoutException.class )
210     public void testValidLegacyLdapSnapshot()
211         throws Exception
212     {
213         assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
214                      "0.9.1-SNAPSHOT", null, "pom" );
215     }
216 
217     @Test
218     public void testValidDefaultLdapSnapshot()
219         throws Exception
220     {
221         assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
222                      "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
223     }
224 
225     @Test( expected = LayoutException.class )
226     public void testValidLegacyTestArchSnapshot()
227         throws Exception
228     {
229         assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
230                      "2.0.3-SNAPSHOT", null, "pom" );
231     }
232 
233     @Test
234     public void testValidDefaultTestArchSnapshot()
235         throws Exception
236     {
237         assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
238                      "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
239     }
240 
241     @Test( expected = LayoutException.class )
242     public void testValidLegacyOddDottedArtifactId()
243         throws Exception
244     {
245         assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
246                      "com.company.department.project", "0.2", null, "pom" );
247     }
248 
249     @Test
250     public void testValidDefaultOddDottedArtifactId()
251         throws Exception
252     {
253         assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
254                      "com.company.department", "com.company.department.project", "0.2", null, "pom" );
255     }
256 
257     @Test( expected = LayoutException.class )
258     public void testValidLegacyTimestampedSnapshot()
259         throws Exception
260     {
261         assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
262                      "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
263     }
264 
265     @Test
266     public void testValidDefaultTimestampedSnapshot()
267         throws Exception
268     {
269         assertValid(
270             "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
271             "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
272     }
273 
274     @Test
275     public void testIsSupportFile()
276     {
277         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
278         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
279         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
280         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
281         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
282         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
283 
284         assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
285         assertFalse(
286             repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
287         assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
288         assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
289         assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
290         assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
291     }
292 
293     @Test
294     public void testIsMetadata()
295     {
296         assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
297         assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
298 
299         assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
300         assertFalse(
301             repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
302         assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
303         assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
304         assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
305         assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
306     }
307 
308     @Test
309     public void testIsMetadataSupportFile()
310     {
311         assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
312         assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
313         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
314         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
315 
316         assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
317         assertFalse( repoRequest.isMetadataSupportFile(
318             "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
319         assertFalse(
320             repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
321         assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
322         assertFalse(
323             repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
324         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
325         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
326     }
327 
328     @Test
329     public void testIsDefault()
330     {
331         assertFalse( repoRequest.isDefault( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
332         assertFalse( repoRequest.isDefault( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
333         assertFalse( repoRequest.isDefault( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
334 
335         assertTrue( repoRequest.isDefault( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
336         assertTrue( repoRequest.isDefault( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
337         assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
338         assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
339         assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
340         assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml" ) );
341         assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
342         assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
343 
344         assertFalse( repoRequest.isDefault( null ) );
345         assertFalse( repoRequest.isDefault( "" ) );
346         assertFalse( repoRequest.isDefault( "foo" ) );
347         assertFalse( repoRequest.isDefault( "some.short/path" ) );
348     }
349 
350     @Test
351     public void testIsLegacy()
352     {
353         assertTrue( repoRequest.isLegacy( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
354         assertTrue( repoRequest.isLegacy( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
355         assertTrue( repoRequest.isLegacy( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
356 
357         assertFalse( repoRequest.isLegacy( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
358         assertFalse( repoRequest.isLegacy( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
359         assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
360         assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
361         assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
362 
363         assertFalse( repoRequest.isLegacy( null ) );
364         assertFalse( repoRequest.isLegacy( "" ) );
365         assertFalse( repoRequest.isLegacy( "some.short/path" ) );
366     }
367 
368     private ManagedRepositoryContent createManagedRepo( String layout )
369         throws Exception
370     {
371         File repoRoot = new File( FileUtil.getBasedir() + "/target/test-repo" );
372         return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
373     }
374 
375     /**
376      * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
377      */
378     @Test
379     public void testToNativePathArtifactDefaultToDefaultDualExtension()
380         throws Exception
381     {
382         ManagedRepositoryContent repository = createManagedRepo( "default" );
383 
384         // Test (artifact) default to default - dual extension
385         assertEquals( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
386                       repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
387                                                 repository ) );
388     }
389 
390 
391     @Test
392     public void testToNativePathMetadataDefaultToDefault()
393         throws Exception
394     {
395         ManagedRepositoryContent repository = createManagedRepo( "default" );
396 
397         // Test (metadata) default to default
398         assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
399                       repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
400                                                 repository ) );
401     }
402 
403 
404     @Test
405     public void testNativePathBadRequestTooShort()
406         throws Exception
407     {
408         ManagedRepositoryContent repository = createManagedRepo( "default" );
409 
410         // Test bad request path (too short)
411         try
412         {
413             repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
414             fail( "Should have thrown an exception about a too short path." );
415         }
416         catch ( LayoutException e )
417         {
418             // expected path.
419         }
420     }
421 
422     @Test
423     public void testNativePathBadRequestBlank()
424         throws Exception
425     {
426         ManagedRepositoryContent repository = createManagedRepo( "default" );
427 
428         // Test bad request path (too short)
429         try
430         {
431             repoRequest.toNativePath( "", repository );
432             fail( "Should have thrown an exception about an blank request." );
433         }
434         catch ( LayoutException e )
435         {
436             // expected path.
437         }
438     }
439 
440     @Test
441     public void testNativePathBadRequestNull()
442         throws Exception
443     {
444         ManagedRepositoryContent repository = createManagedRepo( "default" );
445 
446         // Test bad request path (too short)
447         try
448         {
449             repoRequest.toNativePath( null, repository );
450             fail( "Should have thrown an exception about an null request." );
451         }
452         catch ( LayoutException e )
453         {
454             // expected path.
455         }
456     }
457 
458     @Test
459     public void testNativePathBadRequestUnknownType()
460         throws Exception
461     {
462         ManagedRepositoryContent repository = createManagedRepo( "default" );
463 
464         // Test bad request path (too short)
465         try
466         {
467             repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt", repository );
468             fail( "Should have thrown an exception about an invalid type." );
469         }
470         catch ( LayoutException e )
471         {
472             // expected path.
473         }
474     }
475 
476 
477     private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
478                               String type )
479         throws Exception
480     {
481         String expectedId =
482             "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
483                 classifier + ":" : "" ) + type;
484 
485         ArtifactReference reference = repoRequest.toArtifactReference( path );
486 
487         assertNotNull( expectedId + " - Should not be null.", reference );
488 
489         assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
490         assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
491         if ( StringUtils.isNotBlank( classifier ) )
492         {
493             assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
494         }
495         assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
496         assertEquals( expectedId + " - Type", type, reference.getType() );
497     }
498 
499     private void assertInvalidRequest( String path )
500     {
501         try
502         {
503             repoRequest.toArtifactReference( path );
504             fail( "Expected a LayoutException on an invalid path [" + path + "]" );
505         }
506         catch ( LayoutException e )
507         {
508             /* expected path */
509         }
510     }
511 
512     protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, File location,
513                                                                        String layout )
514         throws Exception
515     {
516         ManagedRepository repo = new ManagedRepository();
517         repo.setId( id );
518         repo.setName( name );
519         repo.setLocation( location.getAbsolutePath() );
520         repo.setLayout( layout );
521 
522         ManagedRepositoryContent repoContent =
523             applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
524         repoContent.setRepository( repo );
525 
526         return repoContent;
527     }
528 
529 }