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.content.legacy.LegacyPathParser;
28 import org.apache.archiva.repository.layout.LayoutException;
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;
37 import javax.inject.Inject;
38 import javax.inject.Named;
41 import static org.junit.Assert.*;
44 * RepositoryRequestTest
46 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
47 @ContextConfiguration (
48 { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-repo-request-test.xml" } )
49 public class RepositoryRequestTest
53 protected ApplicationContext applicationContext;
56 @Named ( "archivaConfiguration#repo-request-test" )
57 private ArchivaConfiguration archivaConfiguration;
59 private RepositoryRequest repoRequest;
65 LegacyPathParser legacyPathParser = new LegacyPathParser( archivaConfiguration );
66 repoRequest = new RepositoryRequest( legacyPathParser );
70 public void testInvalidRequestEmptyPath()
72 assertInvalidRequest( "" );
76 public void testInvalidRequestSlashOnly()
78 assertInvalidRequest( "//" );
82 public void testInvalidRequestNoArtifactId()
84 assertInvalidRequest( "groupId/jars/-1.0.jar" );
88 public void testInvalidLegacyRequestBadLocation()
90 assertInvalidRequest( "org.apache.maven.test/jars/artifactId-1.0.war" );
94 public void testInvalidRequestTooShort()
96 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
100 public void testInvalidDefaultRequestBadLocation()
102 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
106 public void testValidLegacyGanymed()
109 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
114 public void testValidDefaultGanymed()
117 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
118 "ganymed-ssh2", "build210", null, "jar" );
122 public void testValidLegacyJavaxComm()
125 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
129 public void testValidDefaultJavaxComm()
132 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
136 public void testValidLegacyJavaxPersistence()
139 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
140 "3.0-public_review", null, "jar" );
144 public void testValidDefaultJavaxPersistence()
147 assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
148 "3.0-public_review", null, "jar" );
152 public void testValidLegacyMavenTestPlugin()
155 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
159 public void testValidDefaultMavenTestPlugin()
162 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
167 public void testValidLegacyCommonsLangJavadoc()
170 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
171 "javadoc", "javadoc" );
175 public void testValidDefaultCommonsLangJavadoc()
178 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
179 "2.1", "javadoc", "javadoc" );
183 public void testValidLegacyDerbyPom()
186 assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
187 // Starting slash should not prevent detection.
188 assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
193 public void testValidDefaultDerbyPom()
196 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
201 public void testValidLegacyGeronimoEjbSpec()
204 assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
205 "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
209 public void testValidDefaultGeronimoEjbSpec()
212 assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
213 "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
217 public void testValidLegacyLdapSnapshot()
220 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
221 "0.9.1-SNAPSHOT", null, "pom" );
225 public void testValidDefaultLdapSnapshot()
228 assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
229 "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
233 public void testValidLegacyTestArchSnapshot()
236 assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
237 "2.0.3-SNAPSHOT", null, "pom" );
241 public void testValidDefaultTestArchSnapshot()
244 assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
245 "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
249 public void testValidLegacyOddDottedArtifactId()
252 assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
253 "com.company.department.project", "0.2", null, "pom" );
257 public void testValidDefaultOddDottedArtifactId()
260 assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
261 "com.company.department", "com.company.department.project", "0.2", null, "pom" );
265 public void testValidLegacyTimestampedSnapshot()
268 assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
269 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
273 public void testValidDefaultTimestampedSnapshot()
277 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
278 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
282 public void testIsSupportFile()
284 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
285 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
286 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
287 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
288 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
289 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
291 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
293 repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
294 assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
295 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
296 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
297 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
301 public void testIsMetadata()
303 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
304 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
306 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
308 repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
309 assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
310 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
311 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
312 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
316 public void testIsMetadataSupportFile()
318 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
319 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
320 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
321 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
323 assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
324 assertFalse( repoRequest.isMetadataSupportFile(
325 "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
327 repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
328 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
330 repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
331 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
332 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
336 public void testIsDefault()
338 assertFalse( repoRequest.isDefault( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
339 assertFalse( repoRequest.isDefault( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
340 assertFalse( repoRequest.isDefault( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
342 assertTrue( repoRequest.isDefault( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
343 assertTrue( repoRequest.isDefault( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
344 assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
345 assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
346 assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
347 assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml" ) );
348 assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
349 assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
351 assertFalse( repoRequest.isDefault( null ) );
352 assertFalse( repoRequest.isDefault( "" ) );
353 assertFalse( repoRequest.isDefault( "foo" ) );
354 assertFalse( repoRequest.isDefault( "some.short/path" ) );
358 public void testIsLegacy()
360 assertTrue( repoRequest.isLegacy( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
361 assertTrue( repoRequest.isLegacy( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
362 assertTrue( repoRequest.isLegacy( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
364 assertFalse( repoRequest.isLegacy( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
365 assertFalse( repoRequest.isLegacy( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
366 assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
367 assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
368 assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
370 assertFalse( repoRequest.isLegacy( null ) );
371 assertFalse( repoRequest.isLegacy( "" ) );
372 assertFalse( repoRequest.isLegacy( "some.short/path" ) );
375 private ManagedRepositoryContent createManagedRepo( String layout )
378 File repoRoot = new File( FileUtil.getBasedir() + "/target/test-repo" );
379 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
383 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
386 public void testToNativePathArtifactDefaultToDefaultDualExtension()
389 ManagedRepositoryContent repository = createManagedRepo( "default" );
391 // Test (artifact) default to default - dual extension
392 assertEquals( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
393 repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
398 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
401 public void testToNativePathArtifactLegacyToDefaultDualExtension()
404 ManagedRepositoryContent repository = createManagedRepo( "default" );
406 // TODO: this is a good test case for maven 1 -> maven 2 link, since m2 doesn't support the distribution-zip type
408 // Test (artifact) legacy to default - dual extension
409 // NOTE: The detection of a dual extension is flawed.
410 assertEquals( "org/project/example-presentation/3.2.xml/example-presentation-3.2.xml.zip",
411 repoRequest.toNativePath( "org.project/zips/example-presentation-3.2.xml.zip", repository ) );
415 public void testToNativePathMetadataDefaultToDefault()
418 ManagedRepositoryContent repository = createManagedRepo( "default" );
420 // Test (metadata) default to default
421 assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
422 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
427 public void testNativePathPomLegacyToDefault()
430 ManagedRepositoryContent repository = createManagedRepo( "default" );
432 // Test (pom) legacy to default
433 assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom",
434 repoRequest.toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) );
438 public void testNativePathPomLegacyToLegacy()
441 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
443 // Test (pom) legacy to default
444 assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom",
445 repoRequest.toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) );
449 public void testNativePathPomLegacyToDefaultEjb()
452 ManagedRepositoryContent repository = createManagedRepo( "default" );
454 // Test (pom) legacy to default
455 String result = repoRequest.toNativePath( "mygroup/ejbs/myejb-1.0.jar", repository );
456 assertEquals( "mygroup/myejb/1.0/myejb-1.0.jar", result );
460 public void testNativePathPomLegacyToLegacyEjb()
463 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
465 // Test (pom) legacy to default
466 assertEquals( "mygroup/ejbs/myejb-1.0.jar",
467 repoRequest.toNativePath( "mygroup/ejbs/myejb-1.0.jar", repository ) );
471 public void testNativePathPomLegacyToLegacyStrutsModule()
474 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
476 // Test (pom) legacy to default
477 assertEquals( "WebPortal/struts-modules/eventsDB-1.2.3.struts-module",
478 repoRequest.toNativePath( "WebPortal/struts-modules/eventsDB-1.2.3.struts-module", repository ) );
482 public void testNativePathSupportFileLegacyToDefault()
485 ManagedRepositoryContent repository = createManagedRepo( "default" );
487 // Test (supportfile) legacy to default
488 assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1",
489 repoRequest.toNativePath( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1", repository ) );
493 public void testNativePathBadRequestTooShort()
496 ManagedRepositoryContent repository = createManagedRepo( "default" );
498 // Test bad request path (too short)
501 repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
502 fail( "Should have thrown an exception about a too short path." );
504 catch ( LayoutException e )
511 public void testNativePathBadRequestBlank()
514 ManagedRepositoryContent repository = createManagedRepo( "default" );
516 // Test bad request path (too short)
519 repoRequest.toNativePath( "", repository );
520 fail( "Should have thrown an exception about an blank request." );
522 catch ( LayoutException e )
529 public void testNativePathBadRequestNull()
532 ManagedRepositoryContent repository = createManagedRepo( "default" );
534 // Test bad request path (too short)
537 repoRequest.toNativePath( null, repository );
538 fail( "Should have thrown an exception about an null request." );
540 catch ( LayoutException e )
547 public void testNativePathBadRequestUnknownType()
550 ManagedRepositoryContent repository = createManagedRepo( "default" );
552 // Test bad request path (too short)
555 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt", repository );
556 fail( "Should have thrown an exception about an invalid type." );
558 catch ( LayoutException e )
565 public void testToNativePathLegacyMetadataDefaultToLegacy()
568 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
570 // Test (metadata) default to legacy
572 // Special Case: This direction is not supported, should throw a LayoutException.
575 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml", repository );
576 fail( "Should have thrown a LayoutException, can't translate a maven-metadata.xml to a legacy layout." );
578 catch ( LayoutException e )
585 public void testNativePathPomDefaultToLegacy()
588 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
590 // Test (pom) default to legacy
591 assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom",
592 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", repository ) );
596 public void testNativePathSupportFileDefaultToLegacy()
599 ManagedRepositoryContent repository = createManagedRepo( "legacy" );
601 // Test (supportfile) default to legacy
602 assertEquals( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1",
603 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1",
607 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
612 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
613 classifier + ":" : "" ) + type;
615 ArtifactReference reference = repoRequest.toArtifactReference( path );
617 assertNotNull( expectedId + " - Should not be null.", reference );
619 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
620 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
621 if ( StringUtils.isNotBlank( classifier ) )
623 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
625 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
626 assertEquals( expectedId + " - Type", type, reference.getType() );
629 private void assertInvalidRequest( String path )
633 repoRequest.toArtifactReference( path );
634 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
636 catch ( LayoutException e )
642 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, File location,
646 ManagedRepository repo = new ManagedRepository();
648 repo.setName( name );
649 repo.setLocation( location.getAbsolutePath() );
650 repo.setLayout( layout );
652 ManagedRepositoryContent repoContent =
653 applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
654 repoContent.setRepository( repo );