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.filelock.FileLockManager;
23 import org.apache.archiva.common.utils.FileUtils;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileType;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
28 import org.apache.archiva.model.ArtifactReference;
29 import org.apache.archiva.repository.LayoutException;
30 import org.apache.archiva.repository.ManagedRepositoryContent;
31 import org.apache.archiva.repository.RepositoryContentProvider;
32 import org.apache.archiva.repository.maven2.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;
76 private MavenRepositoryRequestInfo repoRequest;
79 protected MavenManagedRepository createRepository( String id, String name, Path location ) throws IOException {
80 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, location.getParent().toAbsolutePath());
81 repo.setLocation( location.toAbsolutePath().toUri() );
85 private Path getRepositoryPath(String repoName) {
88 return Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/" + repoName ).toURI( ) );
90 catch ( URISyntaxException e )
92 throw new RuntimeException( "Could not resolve repository path " + e.getMessage( ), e );
101 Path repoDir = getRepositoryPath( "default-repository" );
102 MavenManagedRepository repository = createRepository( "testRepo", "Unit Test Repo", repoDir );
104 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
105 fileType.addPattern( "**/*.xml" );
106 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
108 fileTypes.afterConfigurationChange( null, "fileType", null );
110 ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
111 //repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
112 repository.setContent(repoContent);
113 repoRequest = new MavenRepositoryRequestInfo(repository);
117 public void testInvalidRequestEmptyPath()
119 assertInvalidRequest( "" );
123 public void testInvalidRequestSlashOnly()
125 assertInvalidRequest( "//" );
129 public void testInvalidRequestNoArtifactId()
131 assertInvalidRequest( "groupId/jars/-1.0.jar" );
136 public void testInvalidRequestTooShort()
138 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
142 public void testInvalidDefaultRequestBadLocation()
144 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
147 @Test( expected = LayoutException.class )
148 public void testValidLegacyGanymed()
151 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
156 public void testValidDefaultGanymed()
159 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
160 "ganymed-ssh2", "build210", null, "jar" );
163 @Test( expected = LayoutException.class )
164 public void testValidLegacyJavaxComm()
167 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
171 public void testValidDefaultJavaxComm()
174 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
177 @Test( expected = LayoutException.class )
178 public void testValidLegacyJavaxPersistence()
181 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
182 "3.0-public_review", null, "jar" );
186 public void testValidDefaultJavaxPersistence()
189 assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
190 "3.0-public_review", null, "jar" );
193 @Test( expected = LayoutException.class )
194 public void testValidLegacyMavenTestPlugin()
197 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
201 public void testValidDefaultMavenTestPlugin()
204 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
208 @Test( expected = LayoutException.class )
209 public void testValidLegacyCommonsLangJavadoc()
212 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
213 "javadoc", "javadoc" );
217 public void testValidDefaultCommonsLangJavadoc()
220 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
221 "2.1", "javadoc", "javadoc" );
224 @Test( expected = LayoutException.class )
225 public void testValidLegacyDerbyPom()
228 assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
229 // Starting slash should not prevent detection.
230 assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
235 public void testValidDefaultDerbyPom()
238 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
242 @Test( expected = LayoutException.class )
243 public void testValidLegacyGeronimoEjbSpec()
246 assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
247 "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
251 public void testValidDefaultGeronimoEjbSpec()
254 assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
255 "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
258 @Test( expected = LayoutException.class )
259 public void testValidLegacyLdapSnapshot()
262 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
263 "0.9.1-SNAPSHOT", null, "pom" );
267 public void testValidDefaultLdapSnapshot()
270 assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
271 "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
274 @Test( expected = LayoutException.class )
275 public void testValidLegacyTestArchSnapshot()
278 assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
279 "2.0.3-SNAPSHOT", null, "pom" );
283 public void testValidDefaultTestArchSnapshot()
286 assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
287 "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
290 @Test( expected = LayoutException.class )
291 public void testValidLegacyOddDottedArtifactId()
294 assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
295 "com.company.department.project", "0.2", null, "pom" );
299 public void testValidDefaultOddDottedArtifactId()
302 assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
303 "com.company.department", "com.company.department.project", "0.2", null, "pom" );
306 @Test( expected = LayoutException.class )
307 public void testValidLegacyTimestampedSnapshot()
310 assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
311 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
315 public void testValidDefaultTimestampedSnapshot()
319 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
320 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
324 public void testIsSupportFile()
326 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
327 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
328 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
329 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
330 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
331 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
333 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
335 repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
336 assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
337 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
338 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
339 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
343 public void testIsMetadata()
345 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
346 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
348 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
350 repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
351 assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
352 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
353 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
354 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
358 public void testIsMetadataSupportFile()
360 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
361 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
362 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
363 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
365 assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
366 assertFalse( repoRequest.isMetadataSupportFile(
367 "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
369 repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
370 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
372 repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
373 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
374 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
378 public void testIsDefault()
380 assertNotEquals( "default", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
381 assertNotEquals("default", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
382 assertNotEquals("default", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
384 assertEquals("default", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
385 assertEquals("default", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
386 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
387 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
388 assertEquals("default", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
389 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml" ) );
390 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
391 assertEquals("default", repoRequest.getLayout( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
393 assertNotEquals("default", repoRequest.getLayout( null ) );
394 assertNotEquals("default", repoRequest.getLayout( "" ) );
395 assertNotEquals("default", repoRequest.getLayout( "foo" ) );
396 assertNotEquals("default", repoRequest.getLayout( "some.short/path" ) );
400 public void testIsLegacy()
402 assertEquals("legacy", repoRequest.getLayout( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
403 assertEquals("legacy", repoRequest.getLayout( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
404 assertEquals("legacy", repoRequest.getLayout( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
406 assertNotEquals("legacy", repoRequest.getLayout( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
407 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
408 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
409 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
410 assertNotEquals("legacy", repoRequest.getLayout( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
412 assertNotEquals("legacy", repoRequest.getLayout( null ) );
413 assertNotEquals("legacy", repoRequest.getLayout( "" ) );
414 assertNotEquals("legacy", repoRequest.getLayout( "some.short/path" ) );
417 private ManagedRepositoryContent createManagedRepo( String layout )
420 Path repoRoot = Paths.get( FileUtils.getBasedir() + "/target/test-repo" );
421 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
425 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
428 public void testToNativePathArtifactDefaultToDefaultDualExtension()
431 ManagedRepositoryContent repository = createManagedRepo( "default" );
433 // Test (artifact) default to default - dual extension
434 assertEquals( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
435 repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip") );
440 public void testToNativePathMetadataDefaultToDefault()
443 ManagedRepositoryContent repository = createManagedRepo( "default" );
445 // Test (metadata) default to default
446 assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
447 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1") );
452 public void testNativePathBadRequestTooShort()
455 ManagedRepositoryContent repository = createManagedRepo( "default" );
457 // Test bad request path (too short)
460 repoRequest.toNativePath( "org.apache.derby/license.txt");
461 fail( "Should have thrown an exception about a too short path." );
463 catch ( LayoutException e )
470 public void testNativePathBadRequestBlank()
473 ManagedRepositoryContent repository = createManagedRepo( "default" );
475 // Test bad request path (too short)
478 repoRequest.toNativePath( "");
479 fail( "Should have thrown an exception about an blank request." );
481 catch ( LayoutException e )
488 public void testNativePathBadRequestNull()
491 ManagedRepositoryContent repository = createManagedRepo( "default" );
493 // Test bad request path (too short)
496 repoRequest.toNativePath( null);
497 fail( "Should have thrown an exception about an null request." );
499 catch ( LayoutException e )
506 public void testNativePathBadRequestUnknownType()
509 ManagedRepositoryContent repository = createManagedRepo( "default" );
511 // Test bad request path (too short)
514 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt");
515 fail( "Should have thrown an exception about an invalid type." );
517 catch ( LayoutException e )
524 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
529 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
530 classifier + ":" : "" ) + type;
532 ArtifactReference reference = repoRequest.toArtifactReference( path );
534 assertNotNull( expectedId + " - Should not be null.", reference );
536 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
537 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
538 if ( StringUtils.isNotBlank( classifier ) )
540 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
542 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
543 assertEquals( expectedId + " - Type", type, reference.getType() );
546 private void assertInvalidRequest( String path )
550 repoRequest.toArtifactReference( path );
551 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
553 catch ( LayoutException e )
559 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
563 MavenManagedRepository repo = MavenManagedRepository.newLocalInstance( id, name, archivaConfiguration.getRepositoryBaseDir());
564 repo.setLocation( location.toAbsolutePath().toUri() );
565 repo.setLayout( layout );
567 RepositoryContentProvider provider = applicationContext.getBean( "repositoryContentProvider#maven", RepositoryContentProvider.class );
569 ManagedRepositoryContent repoContent =
570 provider.createManagedContent( repo );