1 package org.apache.archiva.repository.content.maven2;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
36 import javax.inject.Inject;
37 import javax.inject.Named;
40 import static org.junit.Assert.*;
43 * RepositoryRequestTest
45 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
46 @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
47 "classpath:/spring-context-repo-request-test.xml" } )
48 public class RepositoryRequestTest
52 protected ApplicationContext applicationContext;
55 @Named( "archivaConfiguration#repo-request-test" )
56 private ArchivaConfiguration archivaConfiguration;
58 private RepositoryRequest repoRequest;
64 repoRequest = new RepositoryRequest();
68 public void testInvalidRequestEmptyPath()
70 assertInvalidRequest( "" );
74 public void testInvalidRequestSlashOnly()
76 assertInvalidRequest( "//" );
80 public void testInvalidRequestNoArtifactId()
82 assertInvalidRequest( "groupId/jars/-1.0.jar" );
87 public void testInvalidRequestTooShort()
89 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
93 public void testInvalidDefaultRequestBadLocation()
95 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
98 @Test( expected = LayoutException.class )
99 public void testValidLegacyGanymed()
102 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
107 public void testValidDefaultGanymed()
110 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
111 "ganymed-ssh2", "build210", null, "jar" );
114 @Test( expected = LayoutException.class )
115 public void testValidLegacyJavaxComm()
118 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
122 public void testValidDefaultJavaxComm()
125 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
128 @Test( expected = LayoutException.class )
129 public void testValidLegacyJavaxPersistence()
132 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
133 "3.0-public_review", null, "jar" );
137 public void testValidDefaultJavaxPersistence()
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" );
144 @Test( expected = LayoutException.class )
145 public void testValidLegacyMavenTestPlugin()
148 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
152 public void testValidDefaultMavenTestPlugin()
155 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
159 @Test( expected = LayoutException.class )
160 public void testValidLegacyCommonsLangJavadoc()
163 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
164 "javadoc", "javadoc" );
168 public void testValidDefaultCommonsLangJavadoc()
171 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
172 "2.1", "javadoc", "javadoc" );
175 @Test( expected = LayoutException.class )
176 public void testValidLegacyDerbyPom()
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,
186 public void testValidDefaultDerbyPom()
189 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
193 @Test( expected = LayoutException.class )
194 public void testValidLegacyGeronimoEjbSpec()
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" );
202 public void testValidDefaultGeronimoEjbSpec()
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" );
209 @Test( expected = LayoutException.class )
210 public void testValidLegacyLdapSnapshot()
213 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
214 "0.9.1-SNAPSHOT", null, "pom" );
218 public void testValidDefaultLdapSnapshot()
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" );
225 @Test( expected = LayoutException.class )
226 public void testValidLegacyTestArchSnapshot()
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" );
234 public void testValidDefaultTestArchSnapshot()
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" );
241 @Test( expected = LayoutException.class )
242 public void testValidLegacyOddDottedArtifactId()
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" );
250 public void testValidDefaultOddDottedArtifactId()
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" );
257 @Test( expected = LayoutException.class )
258 public void testValidLegacyTimestampedSnapshot()
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" );
266 public void testValidDefaultTimestampedSnapshot()
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" );
275 public void testIsSupportFile()
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" ) );
284 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
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" ) );
294 public void testIsMetadata()
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" ) );
299 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
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" ) );
309 public void testIsMetadataSupportFile()
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" ) );
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" ) );
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" ) );
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" ) );
329 public void testIsDefault()
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" ) );
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" ) );
344 assertFalse( repoRequest.isDefault( null ) );
345 assertFalse( repoRequest.isDefault( "" ) );
346 assertFalse( repoRequest.isDefault( "foo" ) );
347 assertFalse( repoRequest.isDefault( "some.short/path" ) );
351 public void testIsLegacy()
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" ) );
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" ) );
363 assertFalse( repoRequest.isLegacy( null ) );
364 assertFalse( repoRequest.isLegacy( "" ) );
365 assertFalse( repoRequest.isLegacy( "some.short/path" ) );
368 private ManagedRepositoryContent createManagedRepo( String layout )
371 File repoRoot = new File( FileUtil.getBasedir() + "/target/test-repo" );
372 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
376 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
379 public void testToNativePathArtifactDefaultToDefaultDualExtension()
382 ManagedRepositoryContent repository = createManagedRepo( "default" );
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",
392 public void testToNativePathMetadataDefaultToDefault()
395 ManagedRepositoryContent repository = createManagedRepo( "default" );
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",
405 public void testNativePathBadRequestTooShort()
408 ManagedRepositoryContent repository = createManagedRepo( "default" );
410 // Test bad request path (too short)
413 repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
414 fail( "Should have thrown an exception about a too short path." );
416 catch ( LayoutException e )
423 public void testNativePathBadRequestBlank()
426 ManagedRepositoryContent repository = createManagedRepo( "default" );
428 // Test bad request path (too short)
431 repoRequest.toNativePath( "", repository );
432 fail( "Should have thrown an exception about an blank request." );
434 catch ( LayoutException e )
441 public void testNativePathBadRequestNull()
444 ManagedRepositoryContent repository = createManagedRepo( "default" );
446 // Test bad request path (too short)
449 repoRequest.toNativePath( null, repository );
450 fail( "Should have thrown an exception about an null request." );
452 catch ( LayoutException e )
459 public void testNativePathBadRequestUnknownType()
462 ManagedRepositoryContent repository = createManagedRepo( "default" );
464 // Test bad request path (too short)
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." );
470 catch ( LayoutException e )
477 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
482 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
483 classifier + ":" : "" ) + type;
485 ArtifactReference reference = repoRequest.toArtifactReference( path );
487 assertNotNull( expectedId + " - Should not be null.", reference );
489 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
490 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
491 if ( StringUtils.isNotBlank( classifier ) )
493 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
495 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
496 assertEquals( expectedId + " - Type", type, reference.getType() );
499 private void assertInvalidRequest( String path )
503 repoRequest.toArtifactReference( path );
504 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
506 catch ( LayoutException e )
512 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, File location,
516 ManagedRepository repo = new ManagedRepository();
518 repo.setName( name );
519 repo.setLocation( location.getAbsolutePath() );
520 repo.setLayout( layout );
522 ManagedRepositoryContent repoContent =
523 applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
524 repoContent.setRepository( repo );