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.ManagedRepositoryContent;
26 import org.apache.archiva.repository.layout.LayoutException;
27 import org.apache.archiva.repository.maven2.MavenManagedRepository;
28 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
29 import org.apache.commons.lang.StringUtils;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.test.context.ContextConfiguration;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
41 import static org.junit.Assert.*;
44 * RepositoryRequestTest
46 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
47 @ContextConfiguration( { "classpath*:/META-INF/spring-context.xml",
48 "classpath:/spring-context-repo-request-test.xml" } )
49 public class RepositoryRequestTest
53 protected ApplicationContext applicationContext;
56 @Named( "archivaConfiguration#repo-request-test" )
57 private ArchivaConfiguration archivaConfiguration;
59 private RepositoryRequest repoRequest;
65 repoRequest = new RepositoryRequest();
69 public void testInvalidRequestEmptyPath()
71 assertInvalidRequest( "" );
75 public void testInvalidRequestSlashOnly()
77 assertInvalidRequest( "//" );
81 public void testInvalidRequestNoArtifactId()
83 assertInvalidRequest( "groupId/jars/-1.0.jar" );
88 public void testInvalidRequestTooShort()
90 assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
94 public void testInvalidDefaultRequestBadLocation()
96 assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
99 @Test( expected = LayoutException.class )
100 public void testValidLegacyGanymed()
103 assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
108 public void testValidDefaultGanymed()
111 assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
112 "ganymed-ssh2", "build210", null, "jar" );
115 @Test( expected = LayoutException.class )
116 public void testValidLegacyJavaxComm()
119 assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
123 public void testValidDefaultJavaxComm()
126 assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
129 @Test( expected = LayoutException.class )
130 public void testValidLegacyJavaxPersistence()
133 assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
134 "3.0-public_review", null, "jar" );
138 public void testValidDefaultJavaxPersistence()
141 assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
142 "3.0-public_review", null, "jar" );
145 @Test( expected = LayoutException.class )
146 public void testValidLegacyMavenTestPlugin()
149 assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
153 public void testValidDefaultMavenTestPlugin()
156 assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
160 @Test( expected = LayoutException.class )
161 public void testValidLegacyCommonsLangJavadoc()
164 assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
165 "javadoc", "javadoc" );
169 public void testValidDefaultCommonsLangJavadoc()
172 assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
173 "2.1", "javadoc", "javadoc" );
176 @Test( expected = LayoutException.class )
177 public void testValidLegacyDerbyPom()
180 assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
181 // Starting slash should not prevent detection.
182 assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
187 public void testValidDefaultDerbyPom()
190 assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
194 @Test( expected = LayoutException.class )
195 public void testValidLegacyGeronimoEjbSpec()
198 assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
199 "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
203 public void testValidDefaultGeronimoEjbSpec()
206 assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
207 "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
210 @Test( expected = LayoutException.class )
211 public void testValidLegacyLdapSnapshot()
214 assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
215 "0.9.1-SNAPSHOT", null, "pom" );
219 public void testValidDefaultLdapSnapshot()
222 assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
223 "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
226 @Test( expected = LayoutException.class )
227 public void testValidLegacyTestArchSnapshot()
230 assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
231 "2.0.3-SNAPSHOT", null, "pom" );
235 public void testValidDefaultTestArchSnapshot()
238 assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
239 "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
242 @Test( expected = LayoutException.class )
243 public void testValidLegacyOddDottedArtifactId()
246 assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
247 "com.company.department.project", "0.2", null, "pom" );
251 public void testValidDefaultOddDottedArtifactId()
254 assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
255 "com.company.department", "com.company.department.project", "0.2", null, "pom" );
258 @Test( expected = LayoutException.class )
259 public void testValidLegacyTimestampedSnapshot()
262 assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
263 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
267 public void testValidDefaultTimestampedSnapshot()
271 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
272 "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
276 public void testIsSupportFile()
278 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
279 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
280 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
281 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
282 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
283 assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
285 assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
287 repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
288 assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
289 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
290 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
291 assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
295 public void testIsMetadata()
297 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
298 assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
300 assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
302 repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
303 assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
304 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
305 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
306 assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
310 public void testIsMetadataSupportFile()
312 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
313 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
314 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
315 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
317 assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
318 assertFalse( repoRequest.isMetadataSupportFile(
319 "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
321 repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
322 assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
324 repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
325 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
326 assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
330 public void testIsDefault()
332 assertFalse( repoRequest.isDefault( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
333 assertFalse( repoRequest.isDefault( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
334 assertFalse( repoRequest.isDefault( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
336 assertTrue( repoRequest.isDefault( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
337 assertTrue( repoRequest.isDefault( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
338 assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
339 assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
340 assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
341 assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml" ) );
342 assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
343 assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
345 assertFalse( repoRequest.isDefault( null ) );
346 assertFalse( repoRequest.isDefault( "" ) );
347 assertFalse( repoRequest.isDefault( "foo" ) );
348 assertFalse( repoRequest.isDefault( "some.short/path" ) );
352 public void testIsLegacy()
354 assertTrue( repoRequest.isLegacy( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
355 assertTrue( repoRequest.isLegacy( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
356 assertTrue( repoRequest.isLegacy( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
358 assertFalse( repoRequest.isLegacy( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
359 assertFalse( repoRequest.isLegacy( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
360 assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
361 assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
362 assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
364 assertFalse( repoRequest.isLegacy( null ) );
365 assertFalse( repoRequest.isLegacy( "" ) );
366 assertFalse( repoRequest.isLegacy( "some.short/path" ) );
369 private ManagedRepositoryContent createManagedRepo( String layout )
372 Path repoRoot = Paths.get( FileUtils.getBasedir() + "/target/test-repo" );
373 return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
377 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
380 public void testToNativePathArtifactDefaultToDefaultDualExtension()
383 ManagedRepositoryContent repository = createManagedRepo( "default" );
385 // Test (artifact) default to default - dual extension
386 assertEquals( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
387 repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
393 public void testToNativePathMetadataDefaultToDefault()
396 ManagedRepositoryContent repository = createManagedRepo( "default" );
398 // Test (metadata) default to default
399 assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
400 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
406 public void testNativePathBadRequestTooShort()
409 ManagedRepositoryContent repository = createManagedRepo( "default" );
411 // Test bad request path (too short)
414 repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
415 fail( "Should have thrown an exception about a too short path." );
417 catch ( LayoutException e )
424 public void testNativePathBadRequestBlank()
427 ManagedRepositoryContent repository = createManagedRepo( "default" );
429 // Test bad request path (too short)
432 repoRequest.toNativePath( "", repository );
433 fail( "Should have thrown an exception about an blank request." );
435 catch ( LayoutException e )
442 public void testNativePathBadRequestNull()
445 ManagedRepositoryContent repository = createManagedRepo( "default" );
447 // Test bad request path (too short)
450 repoRequest.toNativePath( null, repository );
451 fail( "Should have thrown an exception about an null request." );
453 catch ( LayoutException e )
460 public void testNativePathBadRequestUnknownType()
463 ManagedRepositoryContent repository = createManagedRepo( "default" );
465 // Test bad request path (too short)
468 repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt", repository );
469 fail( "Should have thrown an exception about an invalid type." );
471 catch ( LayoutException e )
478 private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
483 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
484 classifier + ":" : "" ) + type;
486 ArtifactReference reference = repoRequest.toArtifactReference( path );
488 assertNotNull( expectedId + " - Should not be null.", reference );
490 assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
491 assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
492 if ( StringUtils.isNotBlank( classifier ) )
494 assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
496 assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
497 assertEquals( expectedId + " - Type", type, reference.getType() );
500 private void assertInvalidRequest( String path )
504 repoRequest.toArtifactReference( path );
505 fail( "Expected a LayoutException on an invalid path [" + path + "]" );
507 catch ( LayoutException e )
513 protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, Path location,
517 MavenManagedRepository repo = new MavenManagedRepository( id, name);
518 repo.setLocation( location.toAbsolutePath().toUri() );
519 repo.setLayout( layout );
521 ManagedRepositoryContent repoContent =
522 applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
523 repoContent.setRepository( repo );