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.maven.metadata.storage.ArtifactMappingProvider;
28 import org.apache.archiva.model.ArtifactReference;
29 import org.apache.archiva.repository.LayoutException;
30 import org.apache.archiva.repository.RepositoryContentProvider;
31 import org.apache.archiva.repository.maven.MavenManagedRepository;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.apache.commons.lang3.StringUtils;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.test.context.ContextConfiguration;
40 import javax.inject.Inject;
41 import javax.inject.Named;
42 import java.io.IOException;
43 import java.net.URISyntaxException;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.List;
48 import static org.junit.Assert.*;
51 * RepositoryRequestTest
53 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
54 @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
55 "classpath:/spring-context-repo-request-test.xml" } )
56 public class MavenRepositoryRequestInfoTest
60 protected ApplicationContext applicationContext;
66 @Named( "archivaConfiguration#repo-request-test" )
67 private ArchivaConfiguration archivaConfiguration;
70 List<? extends ArtifactMappingProvider> artifactMappingProviders;
73 FileLockManager fileLockManager;
76 MavenContentHelper mavenContentHelper;
78 private MavenRepositoryRequestInfo repoRequest;
81 protected MavenManagedRepository createRepository( String id, String name, Path location ) throws IOException {
82 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, location.getParent().toAbsolutePath());
83 repo.setLocation( location.toAbsolutePath().toUri() );
87 private Path getRepositoryPath(String repoName) {
90 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
92 catch ( URISyntaxException e )
94 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
103 Path repoDir = getRepositoryPath( "default-repository" );
104 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
106 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
107 fileType.addPattern( "**/*.xml" );
108 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
110 fileTypes.afterConfigurationChange( null, "fileType", null );
112 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
113 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
114 repository.setContent(repoContent);
115 repoContent.setMavenContentHelper( mavenContentHelper );
117 repoRequest = new MavenRepositoryRequestInfo(repository);
121 public void testInvalidRequestEmptyPath()
123 assertInvalidRequest( "" );
127 public void testInvalidRequestSlashOnly()
129 assertInvalidRequest( "//" );
133 public void testInvalidRequestNoArtifactId()
135 assertInvalidRequest( "groupId/jars/-1.0.jar" );
140 public void testInvalidRequestTooShort()
142 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
146 public void testInvalidDefaultRequestBadLocation()
148 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
151 @Test( expected = LayoutException.class )
152 public void testValidLegacyGanymed()
155 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
160 public void testValidDefaultGanymed()
163 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
164 "ganymed-ssh2", "build210", null, "jar" );
167 @Test( expected = LayoutException.class )
168 public void testValidLegacyJavaxComm()
171 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
175 public void testValidDefaultJavaxComm()
178 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
181 @Test( expected = LayoutException.class )
182 public void testValidLegacyJavaxPersistence()
185 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
186 "3.0-public_review", null, "jar" );
190 public void testValidDefaultJavaxPersistence()
193 assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
194 "3.0-public_review", null, "jar" );
197 @Test( expected = LayoutException.class )
198 public void testValidLegacyMavenTestPlugin()
201 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
205 public void testValidDefaultMavenTestPlugin()
208 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
212 @Test( expected = LayoutException.class )
213 public void testValidLegacyCommonsLangJavadoc()
216 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
217 "javadoc", "javadoc" );
221 public void testValidDefaultCommonsLangJavadoc()
224 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
225 "2.1", "javadoc", "javadoc" );
228 @Test( expected = LayoutException.class )
229 public void testValidLegacyDerbyPom()
232 assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
233 // Starting slash should not prevent detection.
234 assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
239 public void testValidDefaultDerbyPom()
242 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
246 @Test( expected = LayoutException.class )
247 public void testValidLegacyGeronimoEjbSpec()
250 assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
251 "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
255 public void testValidDefaultGeronimoEjbSpec()
258 assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
259 "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
262 @Test( expected = LayoutException.class )
263 public void testValidLegacyLdapSnapshot()
266 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
267 "0.9.1-SNAPSHOT", null, "pom" );
271 public void testValidDefaultLdapSnapshot()
274 assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
275 "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
278 @Test( expected = LayoutException.class )
279 public void testValidLegacyTestArchSnapshot()
282 assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
283 "2.0.3-SNAPSHOT", null, "pom" );
287 public void testValidDefaultTestArchSnapshot()
290 assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
291 "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
294 @Test( expected = LayoutException.class )
295 public void testValidLegacyOddDottedArtifactId()
298 assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
299 "com.company.department.project", "0.2", null, "pom" );
303 public void testValidDefaultOddDottedArtifactId()
306 assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
307 "com.company.department", "com.company.department.project", "0.2", null, "pom" );
310 @Test( expected = LayoutException.class )
311 public void testValidLegacyTimestampedSnapshot()
314 assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
315 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
319 public void testValidDefaultTimestampedSnapshot()
323 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
324 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
328 public void testIsSupportFile()
330 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
331 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
332 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
333 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
334 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
335 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
337 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
339 repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
340 assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
341 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
342 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
343 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
347 public void testIsMetadata()
349 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
350 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
352 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
354 repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
355 assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
356 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
357 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
358 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
362 public void testIsMetadataSupportFile()
364 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
365 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
366 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
367 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
369 assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
370 assertFalse( repoRequest.isMetadataSupportFile(
371 "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
373 repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
374 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
376 repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
377 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
378 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
382 public void testIsDefault()
384 assertNotEquals( "default", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
385 assertNotEquals("default", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
386 assertNotEquals("default", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
388 assertEquals("default", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
389 assertEquals("default", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
390 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
391 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
392 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
393 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml" ) );
394 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
395 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
397 assertNotEquals("default", repoRequest.getLayout( null ) );
398 assertNotEquals("default", repoRequest.getLayout( "" ) );
399 assertNotEquals("default", repoRequest.getLayout( "foo" ) );
400 assertNotEquals("default", repoRequest.getLayout( "some.short/path" ) );
404 public void testIsLegacy()
406 assertEquals("legacy", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
407 assertEquals("legacy", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
408 assertEquals("legacy", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
410 assertNotEquals("legacy", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
411 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
412 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
413 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
414 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
416 assertNotEquals("legacy", repoRequest.getLayout( null ) );
417 assertNotEquals("legacy", repoRequest.getLayout( "" ) );
418 assertNotEquals("legacy", repoRequest.getLayout( "some.short/path" ) );
421 private ManagedRepositoryContent createManagedRepo( String layout )
424 Path repoRoot = Paths.get( FileUtils.getBasedir() + "/target/test-repo" );
425 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
429 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
432 public void testToNativePathArtifactDefaultToDefaultDualExtension()
435 ManagedRepositoryContent repository = createManagedRepo( "default" );
437 // Test (artifact) default to default - dual extension
438 assertEquals( "/org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
439 repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip") );
444 public void testToNativePathMetadataDefaultToDefault()
447 ManagedRepositoryContent repository = createManagedRepo( "default" );
449 // Test (metadata) default to default
450 assertEquals( "/org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
451 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1") );
456 public void testNativePathBadRequestTooShort()
459 ManagedRepositoryContent repository = createManagedRepo( "default" );
461 // Test bad request path (too short)
464 repoRequest.toNativePath( "org.apache.derby/license.txt");
465 fail( "Should have thrown an exception about a too short path." );
467 catch ( LayoutException e )
474 public void testNativePathBadRequestBlank()
477 ManagedRepositoryContent repository = createManagedRepo( "default" );
479 // Test bad request path (too short)
482 repoRequest.toNativePath( "");
483 fail( "Should have thrown an exception about an blank request." );
485 catch ( LayoutException e )
492 public void testNativePathBadRequestNull()
495 ManagedRepositoryContent repository = createManagedRepo( "default" );
497 // Test bad request path (too short)
500 repoRequest.toNativePath( null);
501 fail( "Should have thrown an exception about an null request." );
503 catch ( LayoutException e )
510 public void testNativePathBadRequestUnknownType()
513 ManagedRepositoryContent repository = createManagedRepo( "default" );
515 // Test bad request path (too short)
518 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt");
519 fail( "Should have thrown an exception about an invalid type." );
521 catch ( LayoutException e )
528 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
533 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
534 classifier + ":" : "" ) + type;
536 ArtifactReference reference = repoRequest.toArtifactReference( path );
538 assertNotNull( expectedId + " - Should not be null.", reference );
540 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
541 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
542 if ( StringUtils.isNotBlank( classifier ) )
544 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
546 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
547 assertEquals( expectedId + " - Type", type, reference.getType() );
550 private void assertInvalidRequest( String path )
554 repoRequest.toArtifactReference( path );
555 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
557 catch ( LayoutException e )
563 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
567 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, archivaConfiguration.getRepositoryBaseDir());
568 repo.setLocation( location.toAbsolutePath().toUri() );
569 repo.setLayout( layout );
571 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
573 ManagedRepositoryContent repoContent =
574 provider.createManagedContent( repo );