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.repository.LayoutException;
22 import org.apache.archiva.repository.content.ItemSelector;
23 import org.apache.archiva.repository.content.PathParser;
24 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
25 import org.apache.commons.lang3.StringUtils;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
30 import static org.junit.Assert.*;
33 * DefaultPathParserTest
35 * TODO: move to path translator tests
39 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
40 @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
41 public class DefaultPathParserTest
43 private PathParser parser = new DefaultPathParser();
46 public void testBadPathMissingType()
48 // TODO: should we allow this instead?
49 assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
53 public void testBadPathReleaseInSnapshotDir()
55 assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
56 "non snapshot artifact inside of a snapshot dir" );
60 public void testBadPathTimestampedSnapshotNotInSnapshotDir()
62 assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
63 "Timestamped Snapshot artifact not inside of an Snapshot dir" );
67 public void testBadPathTooShort()
69 assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
73 public void testBadPathVersionMismatchA()
75 assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
79 public void testBadPathVersionMismatchB()
81 assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
85 public void testBadPathWrongArtifactId()
87 assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
88 "wrong artifact id" );
92 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
95 public void testGoodButDualExtensions()
96 throws LayoutException
98 String groupId = "org.project";
99 String artifactId = "example-presentation";
100 String version = "3.2";
101 String artifactVersion = "3.2";
102 String classifier = null;
103 String type = "xml.zip";
104 String path = "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip";
106 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
110 public void testGoodButDualExtensionsWithClassifier()
111 throws LayoutException
113 String groupId = "org.project";
114 String artifactId = "example-presentation";
115 String version = "3.2";
116 String artifactVersion = "3.2";
117 String classifier = "extras";
118 String type = "xml.zip";
119 String path = "org/project/example-presentation/3.2/example-presentation-3.2-extras.xml.zip";
121 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
125 public void testGoodButDualExtensionsTarGz()
126 throws LayoutException
128 String groupId = "org.project";
129 String artifactId = "example-distribution";
130 String version = "1.3";
131 String artifactVersion = "1.3";
132 String classifier = null;
133 String type = "tar.gz"; // no longer using distribution-tgz / distribution-zip in maven 2
134 String path = "org/project/example-distribution/1.3/example-distribution-1.3.tar.gz";
136 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
140 public void testGoodButDualExtensionsTarGzAndClassifier()
141 throws LayoutException
143 String groupId = "org.project";
144 String artifactId = "example-distribution";
145 String version = "1.3";
146 String artifactVersion = "1.3";
147 String classifier = "bin";
148 String type = "tar.gz"; // no longer using distribution-tgz / distribution-zip in maven 2
149 String path = "org/project/example-distribution/1.3/example-distribution-1.3-bin.tar.gz";
151 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
155 * [MRM-432] Oddball version spec.
156 * Example of an oddball / unusual version spec.
158 * @throws LayoutException
161 public void testGoodButOddVersionSpecGanymedSsh2()
162 throws LayoutException
164 String groupId = "ch.ethz.ganymed";
165 String artifactId = "ganymed-ssh2";
166 String version = "build210";
167 String artifactVersion = "build210";
168 String classifier = null;
170 String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
172 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
176 * [MRM-432] Oddball version spec.
177 * Example of an oddball / unusual version spec.
179 * @throws LayoutException
182 public void testGoodButOddVersionSpecJavaxComm()
183 throws LayoutException
185 String groupId = "javax";
186 String artifactId = "comm";
187 String version = "3.0-u1";
188 String artifactVersion = "3.0-u1";
189 String classifier = null;
191 String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
193 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
197 * Test the ejb-client type spec.
198 * Type specs are not a 1 to 1 map to the extension.
199 * This tests that effect.
200 * @throws LayoutException
202 /* TODO: Re-enabled in the future.
203 public void testGoodFooEjbClient()
204 throws LayoutException
206 String groupId = "com.foo";
207 String artifactId = "foo-client";
208 String version = "1.0";
209 String classifier = null;
210 String type = "ejb-client"; // oddball type-spec (should result in jar extension)
211 String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
213 assertLayout( path, groupId, artifactId, version, classifier, type );
218 * [MRM-432] Oddball version spec.
219 * Example of an oddball / unusual version spec.
221 * @throws LayoutException
224 public void testGoodButOddVersionSpecJavaxPersistence()
225 throws LayoutException
227 String groupId = "javax.persistence";
228 String artifactId = "ejb";
229 String version = "3.0-public_review";
230 String artifactVersion = "3.0-public_review";
231 String classifier = null;
233 String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
236 * The version id of "public_review" can cause problems. is it part of
237 * the version spec? or the classifier?
238 * Since the path spec below shows it in the path, then it is really
239 * part of the version spec.
242 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
246 public void testGoodComFooTool()
247 throws LayoutException
249 String groupId = "com.foo";
250 String artifactId = "foo-tool";
251 String version = "1.0";
252 String artifactVersion = "1.0";
253 String classifier = null;
255 String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
257 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
261 public void testGoodCommonsLang()
262 throws LayoutException
264 String groupId = "commons-lang";
265 String artifactId = "commons-lang";
266 String version = "2.1";
267 String artifactVersion = "2.1";
268 String classifier = null;
270 String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
272 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
276 public void testWindowsPathSeparator()
277 throws LayoutException
279 String groupId = "commons-lang";
280 String artifactId = "commons-lang";
281 String version = "2.1";
282 String artifactVersion = "2.1";
283 String classifier = null;
285 String path = "commons-lang\\commons-lang/2.1\\commons-lang-2.1.jar";
287 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
291 * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
294 public void testGoodDashedArtifactId()
295 throws LayoutException
297 String groupId = "test.maven-arch";
298 String artifactId = "test-arch";
299 String version = "2.0.3-SNAPSHOT";
300 String artifactVersion = "2.0.3-SNAPSHOT";
301 String classifier = null;
303 String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
305 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
309 * It may seem odd, but this is a valid artifact.
312 public void testGoodDotNotationArtifactId()
313 throws LayoutException
315 String groupId = "com.company.department";
316 String artifactId = "com.company.department";
317 String version = "0.2";
318 String artifactVersion = "0.2";
319 String classifier = null;
321 String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
323 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
327 * It may seem odd, but this is a valid artifact.
330 public void testGoodDotNotationSameGroupIdAndArtifactId()
331 throws LayoutException
333 String groupId = "com.company.department";
334 String artifactId = "com.company.department.project";
335 String version = "0.3";
336 String artifactVersion = "0.3";
337 String classifier = null;
340 "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
342 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
346 * Test the classifier, and java-source type spec.
348 * @throws LayoutException
351 public void testGoodFooLibSources()
352 throws LayoutException
354 String groupId = "com.foo.lib";
355 String artifactId = "foo-lib";
356 String version = "2.1-alpha-1";
357 String artifactVersion = "2.1-alpha-1";
358 String classifier = "sources";
359 String type = "java-source"; // oddball type-spec (should result in jar extension)
360 String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
362 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
366 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
368 * @throws LayoutException
371 public void testGoodSnapshotMavenTest()
372 throws LayoutException
374 String groupId = "org.apache.archiva.test";
375 String artifactId = "redonkulous";
376 String version = "3.1-beta-1-SNAPSHOT";
377 String artifactVersion = "3.1-beta-1-20050831.101112-42";
378 String classifier = null;
381 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
383 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
387 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
389 * @throws LayoutException
392 public void testGoodLongSnapshotMavenTest()
393 throws LayoutException
395 String groupId = "a.group.id";
396 String artifactId = "artifact-id";
397 String version = "1.0-abc-1.1-SNAPSHOT";
398 String artifactVersion = "1.0-abc-1.1-20080221.062205-9";
399 String classifier = null;
401 String path = "a/group/id/artifact-id/1.0-abc-1.1-SNAPSHOT/artifact-id-1.0-abc-1.1-20080221.062205-9.pom";
403 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
407 * A timestamped versioned artifact but without release version part. Like on axiom trunk.
410 public void testBadSnapshotWithoutReleasePart()
412 assertBadPath( "org/apache/ws/commons/axiom/axiom/SNAPSHOT/axiom-20070912.093446-2.pom",
413 "snapshot version without release part" );
417 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
419 * @throws LayoutException
422 public void testClassifiedSnapshotMavenTest()
423 throws LayoutException
425 String groupId = "a.group.id";
426 String artifactId = "artifact-id";
427 String version = "1.0-SNAPSHOT";
428 String artifactVersion = "1.0-20070219.171202-34";
429 String classifier = "test-sources";
431 String path = "a/group/id/artifact-id/1.0-SNAPSHOT/artifact-id-1.0-20070219.171202-34-test-sources.jar";
433 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
437 * [MRM-519] version identifiers within filename cause misidentification of version.
438 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
441 public void testGoodVersionKeywordInArtifactId()
442 throws LayoutException
444 String groupId = "maven";
445 String artifactId = "maven-test-plugin";
446 String version = "1.8.2";
447 String artifactVersion = "1.8.2";
448 String classifier = null;
450 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
452 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
456 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
457 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
460 public void testGoodDetectMavenTestPlugin()
461 throws LayoutException
463 String groupId = "maven";
464 String artifactId = "maven-test-plugin";
465 String version = "1.8.2";
466 String artifactVersion = "1.8.2";
467 String classifier = null;
468 String type = "maven-plugin";
469 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
471 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
475 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
478 public void testGoodDetectCoberturaMavenPlugin()
479 throws LayoutException
481 String groupId = "org.codehaus.mojo";
482 String artifactId = "cobertura-maven-plugin";
483 String version = "2.1";
484 String artifactVersion = "2.1";
485 String classifier = null;
486 String type = "maven-plugin";
487 String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
489 assertLayout( path, groupId, artifactId, version, artifactVersion, classifier, type );
493 public void testToArtifactOnEmptyPath()
497 parser.toItemSelector( "" );
498 fail( "Should have failed due to empty path." );
500 catch ( LayoutException e )
507 public void testToArtifactOnNullPath()
511 parser.toItemSelector( null );
512 fail( "Should have failed due to null path." );
514 catch ( LayoutException e )
521 public void testToArtifactReferenceOnEmptyPath()
525 parser.toItemSelector( "" );
526 fail( "Should have failed due to empty path." );
528 catch ( LayoutException e )
535 public void testToArtifactReferenceOnNullPath()
539 parser.toItemSelector( null );
540 fail( "Should have failed due to null path." );
542 catch ( LayoutException e )
549 * Perform a path to artifact reference lookup, and verify the results.
551 private void assertLayout( String path, String groupId, String artifactId, String version, String artifactVersion, String classifier,
553 throws LayoutException
555 // Path to Artifact Reference.
556 ItemSelector testReference = parser.toItemSelector( path );
557 assertArtifactReference( testReference, groupId, artifactId, version, artifactVersion, classifier, type );
560 private void assertArtifactReference( ItemSelector actualReference, String groupId, String artifactId,
561 String version, String artifactVersion, String classifier, String type )
564 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
566 assertNotNull( expectedId + " - Should not be null.", actualReference );
568 assertEquals( expectedId + " - Group ID", groupId, actualReference.getNamespace() );
569 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
570 assertEquals( expectedId + " - Artifact Version", artifactVersion, actualReference.getArtifactVersion( ) );
571 if ( StringUtils.isNotBlank( classifier ) )
573 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
575 assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
576 assertEquals( expectedId + " - Type", type, actualReference.getType() );
579 private void assertBadPath( String path, String reason )
583 parser.toItemSelector( path );
585 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
587 catch ( LayoutException e )