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.common.utils.FileUtils;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.model.ArtifactReference;
25 import org.apache.archiva.repository.LayoutException;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.RepositoryContentProvider;
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;
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
42 import static org.junit.Assert.*;
45 * RepositoryRequestTest
47 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
48 @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
49 "classpath:/spring-context-repo-request-test.xml" } )
50 public class RepositoryRequestTest
54 protected ApplicationContext applicationContext;
57 @Named( "archivaConfiguration#repo-request-test" )
58 private ArchivaConfiguration archivaConfiguration;
60 private RepositoryRequest repoRequest;
66 repoRequest = new RepositoryRequest();
70 public void testInvalidRequestEmptyPath()
72 assertInvalidRequest( "" );
76 public void testInvalidRequestSlashOnly()
78 assertInvalidRequest( "//" );
82 public void testInvalidRequestNoArtifactId()
84 assertInvalidRequest( "groupId/jars/-1.0.jar" );
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" );
100 @Test( expected = LayoutException.class )
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" );
116 @Test( expected = LayoutException.class )
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" );
130 @Test( expected = LayoutException.class )
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" );
146 @Test( expected = LayoutException.class )
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",
161 @Test( expected = LayoutException.class )
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" );
177 @Test( expected = LayoutException.class )
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",
195 @Test( expected = LayoutException.class )
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" );
211 @Test( expected = LayoutException.class )
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" );
227 @Test( expected = LayoutException.class )
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" );
243 @Test( expected = LayoutException.class )
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" );
259 @Test( expected = LayoutException.class )
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 Path repoRoot = Paths.get( FileUtils.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",
394 public void testToNativePathMetadataDefaultToDefault()
397 ManagedRepositoryContent repository = createManagedRepo( "default" );
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",
407 public void testNativePathBadRequestTooShort()
410 ManagedRepositoryContent repository = createManagedRepo( "default" );
412 // Test bad request path (too short)
415 repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
416 fail( "Should have thrown an exception about a too short path." );
418 catch ( LayoutException e )
425 public void testNativePathBadRequestBlank()
428 ManagedRepositoryContent repository = createManagedRepo( "default" );
430 // Test bad request path (too short)
433 repoRequest.toNativePath( "", repository );
434 fail( "Should have thrown an exception about an blank request." );
436 catch ( LayoutException e )
443 public void testNativePathBadRequestNull()
446 ManagedRepositoryContent repository = createManagedRepo( "default" );
448 // Test bad request path (too short)
451 repoRequest.toNativePath( null, repository );
452 fail( "Should have thrown an exception about an null request." );
454 catch ( LayoutException e )
461 public void testNativePathBadRequestUnknownType()
464 ManagedRepositoryContent repository = createManagedRepo( "default" );
466 // Test bad request path (too short)
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." );
472 catch ( LayoutException e )
479 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
484 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
485 classifier + ":" : "" ) + type;
487 ArtifactReference reference = repoRequest.toArtifactReference( path );
489 assertNotNull( expectedId + " - Should not be null.", reference );
491 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
492 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
493 if ( StringUtils.isNotBlank( classifier ) )
495 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
497 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
498 assertEquals( expectedId + " - Type", type, reference.getType() );
501 private void assertInvalidRequest( String path )
505 repoRequest.toArtifactReference( path );
506 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
508 catch ( LayoutException e )
514 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
518 MavenManagedRepository repo = new MavenManagedRepository( id, name, archivaConfiguration.getRepositoryBaseDir());
519 repo.setLocation( location.toAbsolutePath().toUri() );
520 repo.setLayout( layout );
522 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
524 ManagedRepositoryContent repoContent =
525 provider.createManagedContent( repo );