1 package org.apache.archiva.repository.maven.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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.filelock.FileLockManager;
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.FileType;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.RepositoryContentProvider;
29 import org.apache.archiva.repository.content.ItemSelector;
30 import org.apache.archiva.repository.content.LayoutException;
31 import org.apache.archiva.repository.maven.MavenManagedRepository;
32 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.apache.commons.lang3.StringUtils;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
42 import javax.inject.Named;
43 import java.io.IOException;
44 import java.net.URISyntaxException;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import java.util.List;
49 import static org.junit.Assert.*;
52 * RepositoryRequestTest
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
56 "classpath:/spring-context-repo-request-test.xml" } )
57 public class MavenRepositoryRequestInfoTest
61 protected ApplicationContext applicationContext;
67 @Named( "archivaConfiguration#repo-request-test" )
68 private ArchivaConfiguration archivaConfiguration;
71 List<? extends ArtifactMappingProvider> artifactMappingProviders;
74 @Named( "repositoryPathTranslator#maven2" )
75 RepositoryPathTranslator pathTranslator;
78 FileLockManager fileLockManager;
81 MavenContentHelper mavenContentHelper;
83 private MavenRepositoryRequestInfo repoRequest;
86 protected MavenManagedRepository createRepository( String id, String name, Path location ) throws IOException {
87 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, location.getParent().toAbsolutePath());
88 repo.setLocation( location.toAbsolutePath().toUri() );
92 private Path getRepositoryPath(String repoName) {
95 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
97 catch ( URISyntaxException e )
99 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
108 Path repoDir = getRepositoryPath( "default-repository" );
109 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
111 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
112 fileType.addPattern( "**/*.xml" );
113 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
115 fileTypes.afterConfigurationChange( null, "fileType", null );
117 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repository, fileTypes, fileLockManager);
118 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
119 repository.setContent(repoContent);
120 repoContent.setMavenContentHelper( mavenContentHelper );
121 repoContent.setArtifactMappingProviders( artifactMappingProviders );
122 repoContent.setPathTranslator( pathTranslator );
124 repoRequest = new MavenRepositoryRequestInfo(repository);
128 public void testInvalidRequestEmptyPath()
130 assertInvalidRequest( "" );
134 public void testInvalidRequestSlashOnly()
136 assertInvalidRequest( "//" );
140 public void testInvalidRequestNoArtifactId()
142 assertInvalidRequest( "groupId/jars/-1.0.jar" );
147 public void testInvalidRequestTooShort()
149 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
153 public void testInvalidDefaultRequestBadLocation()
155 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
158 @Test( expected = LayoutException.class )
159 public void testValidLegacyGanymed()
162 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
163 "build210", null, "jar" );
167 public void testValidDefaultGanymed()
170 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
171 "ganymed-ssh2", "build210", "build210", null, "jar" );
174 @Test( expected = LayoutException.class )
175 public void testValidLegacyJavaxComm()
178 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", "3.0-u1", null, "jar" );
182 public void testValidDefaultJavaxComm()
185 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", "3.0-u1", null, "jar" );
188 @Test( expected = LayoutException.class )
189 public void testValidLegacyJavaxPersistence()
192 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
193 "3.0-public_review", "3.0-public_review", null, "jar" );
197 public void testValidDefaultJavaxPersistence()
200 assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
201 "3.0-public_review", "3.0-public_review",null, "jar" );
204 @Test( expected = LayoutException.class )
205 public void testValidLegacyMavenTestPlugin()
208 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", "1.8.2",null, "jar" );
212 public void testValidDefaultMavenTestPlugin()
215 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2", "1.8.2",
219 @Test( expected = LayoutException.class )
220 public void testValidLegacyCommonsLangJavadoc()
223 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1", "2.1",
224 "javadoc", "javadoc" );
228 public void testValidDefaultCommonsLangJavadoc()
231 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
232 "2.1", "2.1","javadoc", "javadoc" );
235 @Test( expected = LayoutException.class )
236 public void testValidLegacyDerbyPom()
239 assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", "10.2.2.0",null, "pom" );
240 // Starting slash should not prevent detection.
241 assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", "10.2.2.0",null,
246 public void testValidDefaultDerbyPom()
249 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", "10.2.2.0",
253 @Test( expected = LayoutException.class )
254 public void testValidLegacyGeronimoEjbSpec()
257 assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
258 "geronimo-ejb_2.1_spec", "1.0.1", "1.0.1",null, "jar" );
262 public void testValidDefaultGeronimoEjbSpec()
265 assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
266 "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", "1.0.1",null, "jar" );
269 @Test( expected = LayoutException.class )
270 public void testValidLegacyLdapSnapshot()
273 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
274 "0.9.1-SNAPSHOT", "0.9.1-SNAPSHOT",null, "pom" );
278 public void testValidDefaultLdapSnapshot()
281 assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
282 "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", "0.9.1-SNAPSHOT",null, "pom" );
285 @Test( expected = LayoutException.class )
286 public void testValidLegacyTestArchSnapshot()
289 assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
290 "2.0.3-SNAPSHOT", "2.0.3-SNAPSHOT",null, "pom" );
294 public void testValidDefaultTestArchSnapshot()
297 assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
298 "test-arch", "2.0.3-SNAPSHOT", "2.0.3-SNAPSHOT",null, "pom" );
301 @Test( expected = LayoutException.class )
302 public void testValidLegacyOddDottedArtifactId()
305 assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
306 "com.company.department.project", "0.2", "0.2",null, "pom" );
310 public void testValidDefaultOddDottedArtifactId()
313 assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
314 "com.company.department", "com.company.department.project", "0.2", "0.2",null, "pom" );
317 @Test( expected = LayoutException.class )
318 public void testValidLegacyTimestampedSnapshot()
321 assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
322 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", "3.1-beta-1-20050831.101112-42", null, "jar" );
326 public void testValidDefaultTimestampedSnapshot()
330 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
331 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-SNAPSHOT", "3.1-beta-1-20050831.101112-42", null, "jar" );
335 public void testIsSupportFile()
337 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
338 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
339 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
340 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
341 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
342 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
344 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
346 repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
347 assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
348 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
349 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
350 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
354 public void testIsMetadata()
356 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
357 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
359 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
361 repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
362 assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
363 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
364 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
365 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
369 public void testIsMetadataSupportFile()
371 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
372 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
373 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
374 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
376 assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
377 assertFalse( repoRequest.isMetadataSupportFile(
378 "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
380 repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
381 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
383 repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
384 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
385 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
389 public void testIsDefault()
391 assertNotEquals( "default", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
392 assertNotEquals("default", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
393 assertNotEquals("default", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
395 assertEquals("default", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
396 assertEquals("default", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
397 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
398 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
399 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
400 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml" ) );
401 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
402 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
404 assertNotEquals("default", repoRequest.getLayout( null ) );
405 assertNotEquals("default", repoRequest.getLayout( "" ) );
406 assertNotEquals("default", repoRequest.getLayout( "foo" ) );
407 assertNotEquals("default", repoRequest.getLayout( "some.short/path" ) );
411 public void testIsLegacy()
413 assertEquals("legacy", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
414 assertEquals("legacy", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
415 assertEquals("legacy", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
417 assertNotEquals("legacy", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
418 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
419 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
420 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
421 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
423 assertNotEquals("legacy", repoRequest.getLayout( null ) );
424 assertNotEquals("legacy", repoRequest.getLayout( "" ) );
425 assertNotEquals("legacy", repoRequest.getLayout( "some.short/path" ) );
428 private ManagedRepositoryContent createManagedRepo( String layout )
431 Path repoRoot = Paths.get( FileUtils.getBasedir() + "/target/test-repo" );
432 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
436 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
439 public void testToNativePathArtifactDefaultToDefaultDualExtension()
442 ManagedRepositoryContent repository = createManagedRepo( "default" );
444 // Test (artifact) default to default - dual extension
445 assertEquals( "/org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
446 repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip") );
451 public void testToNativePathMetadataDefaultToDefault()
454 ManagedRepositoryContent repository = createManagedRepo( "default" );
456 // Test (metadata) default to default
457 assertEquals( "/org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
458 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1") );
463 public void testNativePathBadRequestTooShort()
466 ManagedRepositoryContent repository = createManagedRepo( "default" );
468 // Test bad request path (too short)
471 repoRequest.toNativePath( "org.apache.derby/license.txt");
472 fail( "Should have thrown an exception about a too short path." );
474 catch ( LayoutException e )
481 public void testNativePathBadRequestBlank()
484 ManagedRepositoryContent repository = createManagedRepo( "default" );
486 // Test bad request path (too short)
489 repoRequest.toNativePath( "");
490 fail( "Should have thrown an exception about an blank request." );
492 catch ( LayoutException e )
499 public void testNativePathBadRequestNull()
502 ManagedRepositoryContent repository = createManagedRepo( "default" );
504 // Test bad request path (too short)
507 repoRequest.toNativePath( null);
508 fail( "Should have thrown an exception about an null request." );
510 catch ( LayoutException e )
517 public void testNativePathBadRequestUnknownType()
520 ManagedRepositoryContent repository = createManagedRepo( "default" );
522 // Test bad request path (too short)
525 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt");
526 fail( "Should have thrown an exception about an invalid type." );
528 catch ( LayoutException e )
535 private void assertValid( String path, String groupId, String artifactId, String version, String artifactVersion, String classifier,
540 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
541 classifier + ":" : "" ) + type;
543 ItemSelector reference = repoRequest.toItemSelector( path );
545 assertNotNull( expectedId + " - Should not be null.", reference );
547 assertEquals( expectedId + " - Group ID", groupId, reference.getNamespace() );
548 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
549 assertEquals( expectedId + " - Artifact Version", artifactVersion, reference.getArtifactVersion( ) );
550 if ( StringUtils.isNotBlank( classifier ) )
552 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
554 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
555 assertEquals( expectedId + " - Type", type, reference.getType() );
558 private void assertInvalidRequest( String path )
562 repoRequest.toItemSelector( path );
563 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
565 catch ( LayoutException e )
571 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
575 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, archivaConfiguration.getRepositoryBaseDir());
576 repo.setLocation( location.toAbsolutePath().toUri() );
577 repo.setLayout( layout );
579 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
581 ManagedRepositoryContent repoContent =
582 provider.createManagedContent( repo );