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.model.ArtifactReference;
22 import org.apache.archiva.repository.LayoutException;
23 import org.apache.archiva.repository.content.PathParser;
24 import org.apache.archiva.repository.maven.content.DefaultPathParser;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.commons.lang3.StringUtils;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.springframework.test.context.ContextConfiguration;
31 import static org.junit.Assert.*;
34 * DefaultPathParserTest
36 * TODO: move to path translator tests
40 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
41 @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
42 public class DefaultPathParserTest
44 private PathParser parser = new DefaultPathParser();
47 public void testBadPathMissingType()
49 // TODO: should we allow this instead?
50 assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
54 public void testBadPathReleaseInSnapshotDir()
56 assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
57 "non snapshot artifact inside of a snapshot dir" );
61 public void testBadPathTimestampedSnapshotNotInSnapshotDir()
63 assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
64 "Timestamped Snapshot artifact not inside of an Snapshot dir" );
68 public void testBadPathTooShort()
70 assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
74 public void testBadPathVersionMismatchA()
76 assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
80 public void testBadPathVersionMismatchB()
82 assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
86 public void testBadPathWrongArtifactId()
88 assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
89 "wrong artifact id" );
93 * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
96 public void testGoodButDualExtensions()
97 throws LayoutException
99 String groupId = "org.project";
100 String artifactId = "example-presentation";
101 String version = "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, 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 classifier = "extras";
117 String type = "xml.zip";
118 String path = "org/project/example-presentation/3.2/example-presentation-3.2-extras.xml.zip";
120 assertLayout( path, groupId, artifactId, version, classifier, type );
124 public void testGoodButDualExtensionsTarGz()
125 throws LayoutException
127 String groupId = "org.project";
128 String artifactId = "example-distribution";
129 String version = "1.3";
130 String classifier = null;
131 String type = "tar.gz"; // no longer using distribution-tgz / distribution-zip in maven 2
132 String path = "org/project/example-distribution/1.3/example-distribution-1.3.tar.gz";
134 assertLayout( path, groupId, artifactId, version, classifier, type );
138 public void testGoodButDualExtensionsTarGzAndClassifier()
139 throws LayoutException
141 String groupId = "org.project";
142 String artifactId = "example-distribution";
143 String version = "1.3";
144 String classifier = "bin";
145 String type = "tar.gz"; // no longer using distribution-tgz / distribution-zip in maven 2
146 String path = "org/project/example-distribution/1.3/example-distribution-1.3-bin.tar.gz";
148 assertLayout( path, groupId, artifactId, version, classifier, type );
152 * [MRM-432] Oddball version spec.
153 * Example of an oddball / unusual version spec.
155 * @throws LayoutException
158 public void testGoodButOddVersionSpecGanymedSsh2()
159 throws LayoutException
161 String groupId = "ch.ethz.ganymed";
162 String artifactId = "ganymed-ssh2";
163 String version = "build210";
164 String classifier = null;
166 String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
168 assertLayout( path, groupId, artifactId, version, classifier, type );
172 * [MRM-432] Oddball version spec.
173 * Example of an oddball / unusual version spec.
175 * @throws LayoutException
178 public void testGoodButOddVersionSpecJavaxComm()
179 throws LayoutException
181 String groupId = "javax";
182 String artifactId = "comm";
183 String version = "3.0-u1";
184 String classifier = null;
186 String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
188 assertLayout( path, groupId, artifactId, version, classifier, type );
192 * Test the ejb-client type spec.
193 * Type specs are not a 1 to 1 map to the extension.
194 * This tests that effect.
195 * @throws LayoutException
197 /* TODO: Re-enabled in the future.
198 public void testGoodFooEjbClient()
199 throws LayoutException
201 String groupId = "com.foo";
202 String artifactId = "foo-client";
203 String version = "1.0";
204 String classifier = null;
205 String type = "ejb-client"; // oddball type-spec (should result in jar extension)
206 String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
208 assertLayout( path, groupId, artifactId, version, classifier, type );
213 * [MRM-432] Oddball version spec.
214 * Example of an oddball / unusual version spec.
216 * @throws LayoutException
219 public void testGoodButOddVersionSpecJavaxPersistence()
220 throws LayoutException
222 String groupId = "javax.persistence";
223 String artifactId = "ejb";
224 String version = "3.0-public_review";
225 String classifier = null;
227 String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
230 * The version id of "public_review" can cause problems. is it part of
231 * the version spec? or the classifier?
232 * Since the path spec below shows it in the path, then it is really
233 * part of the version spec.
236 assertLayout( path, groupId, artifactId, version, classifier, type );
240 public void testGoodComFooTool()
241 throws LayoutException
243 String groupId = "com.foo";
244 String artifactId = "foo-tool";
245 String version = "1.0";
246 String classifier = null;
248 String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
250 assertLayout( path, groupId, artifactId, version, classifier, type );
254 public void testGoodCommonsLang()
255 throws LayoutException
257 String groupId = "commons-lang";
258 String artifactId = "commons-lang";
259 String version = "2.1";
260 String classifier = null;
262 String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
264 assertLayout( path, groupId, artifactId, version, classifier, type );
268 public void testWindowsPathSeparator()
269 throws LayoutException
271 String groupId = "commons-lang";
272 String artifactId = "commons-lang";
273 String version = "2.1";
274 String classifier = null;
276 String path = "commons-lang\\commons-lang/2.1\\commons-lang-2.1.jar";
278 assertLayout( path, groupId, artifactId, version, classifier, type );
282 * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
285 public void testGoodDashedArtifactId()
286 throws LayoutException
288 String groupId = "test.maven-arch";
289 String artifactId = "test-arch";
290 String version = "2.0.3-SNAPSHOT";
291 String classifier = null;
293 String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
295 assertLayout( path, groupId, artifactId, version, classifier, type );
299 * It may seem odd, but this is a valid artifact.
302 public void testGoodDotNotationArtifactId()
303 throws LayoutException
305 String groupId = "com.company.department";
306 String artifactId = "com.company.department";
307 String version = "0.2";
308 String classifier = null;
310 String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
312 assertLayout( path, groupId, artifactId, version, classifier, type );
316 * It may seem odd, but this is a valid artifact.
319 public void testGoodDotNotationSameGroupIdAndArtifactId()
320 throws LayoutException
322 String groupId = "com.company.department";
323 String artifactId = "com.company.department.project";
324 String version = "0.3";
325 String classifier = null;
328 "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
330 assertLayout( path, groupId, artifactId, version, classifier, type );
334 * Test the classifier, and java-source type spec.
336 * @throws LayoutException
339 public void testGoodFooLibSources()
340 throws LayoutException
342 String groupId = "com.foo.lib";
343 String artifactId = "foo-lib";
344 String version = "2.1-alpha-1";
345 String classifier = "sources";
346 String type = "java-source"; // oddball type-spec (should result in jar extension)
347 String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
349 assertLayout( path, groupId, artifactId, version, classifier, type );
353 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
355 * @throws LayoutException
358 public void testGoodSnapshotMavenTest()
359 throws LayoutException
361 String groupId = "org.apache.archiva.test";
362 String artifactId = "redonkulous";
363 String version = "3.1-beta-1-20050831.101112-42";
364 String classifier = null;
367 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
369 assertLayout( path, groupId, artifactId, version, classifier, type );
373 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
375 * @throws LayoutException
378 public void testGoodLongSnapshotMavenTest()
379 throws LayoutException
381 String groupId = "a.group.id";
382 String artifactId = "artifact-id";
383 String version = "1.0-abc-1.1-20080221.062205-9";
384 String classifier = null;
386 String path = "a/group/id/artifact-id/1.0-abc-1.1-SNAPSHOT/artifact-id-1.0-abc-1.1-20080221.062205-9.pom";
388 assertLayout( path, groupId, artifactId, version, classifier, type );
392 * A timestamped versioned artifact but without release version part. Like on axiom trunk.
395 public void testBadSnapshotWithoutReleasePart()
397 assertBadPath( "org/apache/ws/commons/axiom/axiom/SNAPSHOT/axiom-20070912.093446-2.pom",
398 "snapshot version without release part" );
402 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
404 * @throws LayoutException
407 public void testClassifiedSnapshotMavenTest()
408 throws LayoutException
410 String groupId = "a.group.id";
411 String artifactId = "artifact-id";
412 String version = "1.0-20070219.171202-34";
413 String classifier = "test-sources";
415 String path = "a/group/id/artifact-id/1.0-SNAPSHOT/artifact-id-1.0-20070219.171202-34-test-sources.jar";
417 assertLayout( path, groupId, artifactId, version, classifier, type );
421 * [MRM-519] version identifiers within filename cause misidentification of version.
422 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
425 public void testGoodVersionKeywordInArtifactId()
426 throws LayoutException
428 String groupId = "maven";
429 String artifactId = "maven-test-plugin";
430 String version = "1.8.2";
431 String classifier = null;
433 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
435 assertLayout( path, groupId, artifactId, version, classifier, type );
439 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
440 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
443 public void testGoodDetectMavenTestPlugin()
444 throws LayoutException
446 String groupId = "maven";
447 String artifactId = "maven-test-plugin";
448 String version = "1.8.2";
449 String classifier = null;
450 String type = "maven-plugin";
451 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
453 assertLayout( path, groupId, artifactId, version, classifier, type );
457 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
460 public void testGoodDetectCoberturaMavenPlugin()
461 throws LayoutException
463 String groupId = "org.codehaus.mojo";
464 String artifactId = "cobertura-maven-plugin";
465 String version = "2.1";
466 String classifier = null;
467 String type = "maven-plugin";
468 String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
470 assertLayout( path, groupId, artifactId, version, classifier, type );
474 public void testToArtifactOnEmptyPath()
478 parser.toArtifactReference( "" );
479 fail( "Should have failed due to empty path." );
481 catch ( LayoutException e )
488 public void testToArtifactOnNullPath()
492 parser.toArtifactReference( null );
493 fail( "Should have failed due to null path." );
495 catch ( LayoutException e )
502 public void testToArtifactReferenceOnEmptyPath()
506 parser.toArtifactReference( "" );
507 fail( "Should have failed due to empty path." );
509 catch ( LayoutException e )
516 public void testToArtifactReferenceOnNullPath()
520 parser.toArtifactReference( null );
521 fail( "Should have failed due to null path." );
523 catch ( LayoutException e )
530 * Perform a path to artifact reference lookup, and verify the results.
532 private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
534 throws LayoutException
536 // Path to Artifact Reference.
537 ArtifactReference testReference = parser.toArtifactReference( path );
538 assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
541 private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
542 String version, String classifier, String type )
545 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
547 assertNotNull( expectedId + " - Should not be null.", actualReference );
549 assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
550 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
551 if ( StringUtils.isNotBlank( classifier ) )
553 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
555 assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
556 assertEquals( expectedId + " - Type", type, actualReference.getType() );
559 private void assertBadPath( String path, String reason )
563 parser.toArtifactReference( path );
565 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
567 catch ( LayoutException e )