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