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.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.content.ItemSelector;
28 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
29 import org.apache.archiva.model.ArtifactReference;
30 import org.apache.archiva.repository.LayoutException;
31 import org.apache.archiva.repository.RepositoryContentProvider;
32 import org.apache.archiva.repository.maven.MavenManagedRepository;
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 FileLockManager fileLockManager;
77 MavenContentHelper mavenContentHelper;
79 private MavenRepositoryRequestInfo repoRequest;
82 protected MavenManagedRepository createRepository( String id, String name, Path location ) throws IOException {
83 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, location.getParent().toAbsolutePath());
84 repo.setLocation( location.toAbsolutePath().toUri() );
88 private Path getRepositoryPath(String repoName) {
91 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
93 catch ( URISyntaxException e )
95 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
104 Path repoDir = getRepositoryPath( "default-repository" );
105 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
107 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
108 fileType.addPattern( "**/*.xml" );
109 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
111 fileTypes.afterConfigurationChange( null, "fileType", null );
113 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
114 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
115 repository.setContent(repoContent);
116 repoContent.setMavenContentHelper( mavenContentHelper );
118 repoRequest = new MavenRepositoryRequestInfo(repository);
122 public void testInvalidRequestEmptyPath()
124 assertInvalidRequest( "" );
128 public void testInvalidRequestSlashOnly()
130 assertInvalidRequest( "//" );
134 public void testInvalidRequestNoArtifactId()
136 assertInvalidRequest( "groupId/jars/-1.0.jar" );
141 public void testInvalidRequestTooShort()
143 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
147 public void testInvalidDefaultRequestBadLocation()
149 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
152 @Test( expected = LayoutException.class )
153 public void testValidLegacyGanymed()
156 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
157 "build210", null, "jar" );
161 public void testValidDefaultGanymed()
164 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
165 "ganymed-ssh2", "build210", "build210", null, "jar" );
168 @Test( expected = LayoutException.class )
169 public void testValidLegacyJavaxComm()
172 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", "3.0-u1", null, "jar" );
176 public void testValidDefaultJavaxComm()
179 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", "3.0-u1", null, "jar" );
182 @Test( expected = LayoutException.class )
183 public void testValidLegacyJavaxPersistence()
186 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
187 "3.0-public_review", "3.0-public_review", null, "jar" );
191 public void testValidDefaultJavaxPersistence()
194 assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
195 "3.0-public_review", "3.0-public_review",null, "jar" );
198 @Test( expected = LayoutException.class )
199 public void testValidLegacyMavenTestPlugin()
202 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", "1.8.2",null, "jar" );
206 public void testValidDefaultMavenTestPlugin()
209 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",
213 @Test( expected = LayoutException.class )
214 public void testValidLegacyCommonsLangJavadoc()
217 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1", "2.1",
218 "javadoc", "javadoc" );
222 public void testValidDefaultCommonsLangJavadoc()
225 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
226 "2.1", "2.1","javadoc", "javadoc" );
229 @Test( expected = LayoutException.class )
230 public void testValidLegacyDerbyPom()
233 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" );
234 // Starting slash should not prevent detection.
235 assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", "10.2.2.0",null,
240 public void testValidDefaultDerbyPom()
243 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",
247 @Test( expected = LayoutException.class )
248 public void testValidLegacyGeronimoEjbSpec()
251 assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
252 "geronimo-ejb_2.1_spec", "1.0.1", "1.0.1",null, "jar" );
256 public void testValidDefaultGeronimoEjbSpec()
259 assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
260 "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", "1.0.1",null, "jar" );
263 @Test( expected = LayoutException.class )
264 public void testValidLegacyLdapSnapshot()
267 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
268 "0.9.1-SNAPSHOT", "0.9.1-SNAPSHOT",null, "pom" );
272 public void testValidDefaultLdapSnapshot()
275 assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
276 "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", "0.9.1-SNAPSHOT",null, "pom" );
279 @Test( expected = LayoutException.class )
280 public void testValidLegacyTestArchSnapshot()
283 assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
284 "2.0.3-SNAPSHOT", "2.0.3-SNAPSHOT",null, "pom" );
288 public void testValidDefaultTestArchSnapshot()
291 assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
292 "test-arch", "2.0.3-SNAPSHOT", "2.0.3-SNAPSHOT",null, "pom" );
295 @Test( expected = LayoutException.class )
296 public void testValidLegacyOddDottedArtifactId()
299 assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
300 "com.company.department.project", "0.2", "0.2",null, "pom" );
304 public void testValidDefaultOddDottedArtifactId()
307 assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
308 "com.company.department", "com.company.department.project", "0.2", "0.2",null, "pom" );
311 @Test( expected = LayoutException.class )
312 public void testValidLegacyTimestampedSnapshot()
315 assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
316 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", "3.1-beta-1-20050831.101112-42", null, "jar" );
320 public void testValidDefaultTimestampedSnapshot()
324 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
325 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-SNAPSHOT", "3.1-beta-1-20050831.101112-42", null, "jar" );
329 public void testIsSupportFile()
331 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
332 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
333 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
334 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
335 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
336 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
338 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
340 repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
341 assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
342 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
343 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
344 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
348 public void testIsMetadata()
350 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
351 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
353 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
355 repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
356 assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
357 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
358 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
359 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
363 public void testIsMetadataSupportFile()
365 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
366 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
367 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
368 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
370 assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
371 assertFalse( repoRequest.isMetadataSupportFile(
372 "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
374 repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
375 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
377 repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
378 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
379 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
383 public void testIsDefault()
385 assertNotEquals( "default", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
386 assertNotEquals("default", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
387 assertNotEquals("default", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
389 assertEquals("default", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
390 assertEquals("default", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
391 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
392 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
393 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
394 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml" ) );
395 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
396 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
398 assertNotEquals("default", repoRequest.getLayout( null ) );
399 assertNotEquals("default", repoRequest.getLayout( "" ) );
400 assertNotEquals("default", repoRequest.getLayout( "foo" ) );
401 assertNotEquals("default", repoRequest.getLayout( "some.short/path" ) );
405 public void testIsLegacy()
407 assertEquals("legacy", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
408 assertEquals("legacy", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
409 assertEquals("legacy", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
411 assertNotEquals("legacy", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
412 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
413 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
414 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
415 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
417 assertNotEquals("legacy", repoRequest.getLayout( null ) );
418 assertNotEquals("legacy", repoRequest.getLayout( "" ) );
419 assertNotEquals("legacy", repoRequest.getLayout( "some.short/path" ) );
422 private ManagedRepositoryContent createManagedRepo( String layout )
425 Path repoRoot = Paths.get( FileUtils.getBasedir() + "/target/test-repo" );
426 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
430 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
433 public void testToNativePathArtifactDefaultToDefaultDualExtension()
436 ManagedRepositoryContent repository = createManagedRepo( "default" );
438 // Test (artifact) default to default - dual extension
439 assertEquals( "/org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
440 repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip") );
445 public void testToNativePathMetadataDefaultToDefault()
448 ManagedRepositoryContent repository = createManagedRepo( "default" );
450 // Test (metadata) default to default
451 assertEquals( "/org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
452 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1") );
457 public void testNativePathBadRequestTooShort()
460 ManagedRepositoryContent repository = createManagedRepo( "default" );
462 // Test bad request path (too short)
465 repoRequest.toNativePath( "org.apache.derby/license.txt");
466 fail( "Should have thrown an exception about a too short path." );
468 catch ( LayoutException e )
475 public void testNativePathBadRequestBlank()
478 ManagedRepositoryContent repository = createManagedRepo( "default" );
480 // Test bad request path (too short)
483 repoRequest.toNativePath( "");
484 fail( "Should have thrown an exception about an blank request." );
486 catch ( LayoutException e )
493 public void testNativePathBadRequestNull()
496 ManagedRepositoryContent repository = createManagedRepo( "default" );
498 // Test bad request path (too short)
501 repoRequest.toNativePath( null);
502 fail( "Should have thrown an exception about an null request." );
504 catch ( LayoutException e )
511 public void testNativePathBadRequestUnknownType()
514 ManagedRepositoryContent repository = createManagedRepo( "default" );
516 // Test bad request path (too short)
519 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt");
520 fail( "Should have thrown an exception about an invalid type." );
522 catch ( LayoutException e )
529 private void assertValid( String path, String groupId, String artifactId, String version, String artifactVersion, String classifier,
534 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
535 classifier + ":" : "" ) + type;
537 ItemSelector reference = repoRequest.toItemSelector( path );
539 assertNotNull( expectedId + " - Should not be null.", reference );
541 assertEquals( expectedId + " - Group ID", groupId, reference.getNamespace() );
542 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
543 assertEquals( expectedId + " - Artifact Version", artifactVersion, reference.getArtifactVersion( ) );
544 if ( StringUtils.isNotBlank( classifier ) )
546 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
548 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
549 assertEquals( expectedId + " - Type", type, reference.getType() );
552 private void assertInvalidRequest( String path )
556 repoRequest.toItemSelector( path );
557 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
559 catch ( LayoutException e )
565 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
569 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, archivaConfiguration.getRepositoryBaseDir());
570 repo.setLocation( location.toAbsolutePath().toUri() );
571 repo.setLayout( layout );
573 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
575 ManagedRepositoryContent repoContent =
576 provider.createManagedContent( repo );