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