1 package org.apache.archiva.repository.content;
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.commons.lang.StringUtils;
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.AbstractRepositoryLayerTestCase;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.layout.LayoutException;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.springframework.test.context.ContextConfiguration;
34 import javax.inject.Inject;
35 import javax.inject.Named;
37 import static org.junit.Assert.*;
40 * RepositoryRequestTest
44 @ContextConfiguration(
45 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-repo-request-test.xml" } )
46 public class RepositoryRequestTest
47 extends AbstractRepositoryLayerTestCase
51 @Named( value = "archivaConfiguration#repo-request-test" )
52 private ArchivaConfiguration archivaConfiguration;
54 private RepositoryRequest repoRequest;
60 LegacyPathParser legacyPathParser = new LegacyPathParser( archivaConfiguration );
61 repoRequest = new RepositoryRequest( legacyPathParser );
65 public void testInvalidRequestEmptyPath()
67 assertInvalidRequest( "" );
71 public void testInvalidRequestSlashOnly()
73 assertInvalidRequest( "//" );
77 public void testInvalidRequestNoArtifactId()
79 assertInvalidRequest( "groupId/jars/-1.0.jar" );
83 public void testInvalidLegacyRequestBadLocation()
85 assertInvalidRequest( "org.apache.maven.test/jars/artifactId-1.0.war" );
89 public void testInvalidRequestTooShort()
91 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
95 public void testInvalidDefaultRequestBadLocation()
97 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
101 public void testValidLegacyGanymed()
104 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
109 public void testValidDefaultGanymed()
112 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
113 "ganymed-ssh2", "build210", null, "jar" );
117 public void testValidLegacyJavaxComm()
120 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
124 public void testValidDefaultJavaxComm()
127 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
131 public void testValidLegacyJavaxPersistence()
134 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
135 "3.0-public_review", null, "jar" );
139 public void testValidDefaultJavaxPersistence()
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" );
147 public void testValidLegacyMavenTestPlugin()
150 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
154 public void testValidDefaultMavenTestPlugin()
157 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
162 public void testValidLegacyCommonsLangJavadoc()
165 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
166 "javadoc", "javadoc" );
170 public void testValidDefaultCommonsLangJavadoc()
173 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
174 "2.1", "javadoc", "javadoc" );
178 public void testValidLegacyDerbyPom()
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,
188 public void testValidDefaultDerbyPom()
191 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
196 public void testValidLegacyGeronimoEjbSpec()
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" );
204 public void testValidDefaultGeronimoEjbSpec()
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" );
212 public void testValidLegacyLdapSnapshot()
215 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
216 "0.9.1-SNAPSHOT", null, "pom" );
220 public void testValidDefaultLdapSnapshot()
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" );
228 public void testValidLegacyTestArchSnapshot()
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" );
236 public void testValidDefaultTestArchSnapshot()
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" );
244 public void testValidLegacyOddDottedArtifactId()
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" );
252 public void testValidDefaultOddDottedArtifactId()
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" );
260 public void testValidLegacyTimestampedSnapshot()
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" );
268 public void testValidDefaultTimestampedSnapshot()
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" );
277 public void testIsSupportFile()
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" ) );
286 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
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" ) );
296 public void testIsMetadata()
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" ) );
301 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
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" ) );
311 public void testIsMetadataSupportFile()
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" ) );
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" ) );
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" ) );
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" ) );
331 public void testIsDefault()
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" ) );
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" ) );
346 assertFalse( repoRequest.isDefault( null ) );
347 assertFalse( repoRequest.isDefault( "" ) );
348 assertFalse( repoRequest.isDefault( "foo" ) );
349 assertFalse( repoRequest.isDefault( "some.short/path" ) );
353 public void testIsLegacy()
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" ) );
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" ) );
365 assertFalse( repoRequest.isLegacy( null ) );
366 assertFalse( repoRequest.isLegacy( "" ) );
367 assertFalse( repoRequest.isLegacy( "some.short/path" ) );
370 private ManagedRepositoryContent createManagedRepo( String layout )
373 File repoRoot = new File( FileUtil.getBasedir() + "/target/test-repo" );
374 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
378 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
381 public void testToNativePathArtifactDefaultToDefaultDualExtension()
384 ManagedRepositoryContent repository = createManagedRepo( "default" );
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",
393 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
396 public void testToNativePathArtifactLegacyToDefaultDualExtension()
399 ManagedRepositoryContent repository = createManagedRepo( "default" );
401 // TODO: this is a good test case for maven 1 -> maven 2 link, since m2 doesn't support the distribution-zip type
403 // Test (artifact) legacy to default - dual extension
404 // NOTE: The detection of a dual extension is flawed.
405 assertEquals( "org/project/example-presentation/3.2.xml/example-presentation-3.2.xml.zip",
406 repoRequest.toNativePath( "org.project/zips/example-presentation-3.2.xml.zip", repository ) );
410 public void testToNativePathMetadataDefaultToDefault()
413 ManagedRepositoryContent repository = createManagedRepo( "default" );
415 // Test (metadata) default to default
416 assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
417 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
422 public void testNativePathPomLegacyToDefault()
425 ManagedRepositoryContent repository = createManagedRepo( "default" );
427 // Test (pom) legacy to default
428 assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom",
429 repoRequest.toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) );
433 public void testNativePathPomLegacyToLegacy()
436 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
438 // Test (pom) legacy to default
439 assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom",
440 repoRequest.toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) );
444 public void testNativePathPomLegacyToDefaultEjb()
447 ManagedRepositoryContent repository = createManagedRepo( "default" );
449 // Test (pom) legacy to default
450 String result = repoRequest.toNativePath( "mygroup/ejbs/myejb-1.0.jar", repository );
451 assertEquals( "mygroup/myejb/1.0/myejb-1.0.jar", result );
455 public void testNativePathPomLegacyToLegacyEjb()
458 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
460 // Test (pom) legacy to default
461 assertEquals( "mygroup/ejbs/myejb-1.0.jar",
462 repoRequest.toNativePath( "mygroup/ejbs/myejb-1.0.jar", repository ) );
466 public void testNativePathPomLegacyToLegacyStrutsModule()
469 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
471 // Test (pom) legacy to default
472 assertEquals( "WebPortal/struts-modules/eventsDB-1.2.3.struts-module",
473 repoRequest.toNativePath( "WebPortal/struts-modules/eventsDB-1.2.3.struts-module", repository ) );
477 public void testNativePathSupportFileLegacyToDefault()
480 ManagedRepositoryContent repository = createManagedRepo( "default" );
482 // Test (supportfile) legacy to default
483 assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1",
484 repoRequest.toNativePath( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1", repository ) );
488 public void testNativePathBadRequestTooShort()
491 ManagedRepositoryContent repository = createManagedRepo( "default" );
493 // Test bad request path (too short)
496 repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
497 fail( "Should have thrown an exception about a too short path." );
499 catch ( LayoutException e )
506 public void testNativePathBadRequestBlank()
509 ManagedRepositoryContent repository = createManagedRepo( "default" );
511 // Test bad request path (too short)
514 repoRequest.toNativePath( "", repository );
515 fail( "Should have thrown an exception about an blank request." );
517 catch ( LayoutException e )
524 public void testNativePathBadRequestNull()
527 ManagedRepositoryContent repository = createManagedRepo( "default" );
529 // Test bad request path (too short)
532 repoRequest.toNativePath( null, repository );
533 fail( "Should have thrown an exception about an null request." );
535 catch ( LayoutException e )
542 public void testNativePathBadRequestUnknownType()
545 ManagedRepositoryContent repository = createManagedRepo( "default" );
547 // Test bad request path (too short)
550 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt", repository );
551 fail( "Should have thrown an exception about an invalid type." );
553 catch ( LayoutException e )
560 public void testToNativePathLegacyMetadataDefaultToLegacy()
563 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
565 // Test (metadata) default to legacy
567 // Special Case: This direction is not supported, should throw a LayoutException.
570 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml", repository );
571 fail( "Should have thrown a LayoutException, can't translate a maven-metadata.xml to a legacy layout." );
573 catch ( LayoutException e )
580 public void testNativePathPomDefaultToLegacy()
583 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
585 // Test (pom) default to legacy
586 assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom",
587 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", repository ) );
591 public void testNativePathSupportFileDefaultToLegacy()
594 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
596 // Test (supportfile) default to legacy
597 assertEquals( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1",
598 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1",
602 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
606 String expectedId = "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":"
607 + ( classifier != null ? classifier + ":" : "" ) + type;
609 ArtifactReference reference = repoRequest.toArtifactReference( path );
611 assertNotNull( expectedId + " - Should not be null.", reference );
613 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
614 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
615 if ( StringUtils.isNotBlank( classifier ) )
617 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
619 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
620 assertEquals( expectedId + " - Type", type, reference.getType() );
623 private void assertInvalidRequest( String path )
627 repoRequest.toArtifactReference( path );
628 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
630 catch ( LayoutException e )