1 package org.apache.archiva.repository.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
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.model.ArtifactReference;
23 import org.apache.archiva.repository.AbstractRepositoryLayerTestCase;
24 import org.apache.archiva.repository.layout.LayoutException;
25 import org.apache.commons.lang.StringUtils;
26 import org.junit.Test;
28 import static org.junit.Assert.*;
31 * AbstractDefaultRepositoryContentTestCase
35 public abstract class AbstractDefaultRepositoryContentTestCase
36 extends AbstractRepositoryLayerTestCase
39 public void testBadPathMissingType()
41 assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
45 public void testBadPathReleaseInSnapshotDir()
47 assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar", "non snapshot artifact inside of a snapshot dir" );
51 public void testBadPathTimestampedSnapshotNotInSnapshotDir()
53 assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
54 "Timestamped Snapshot artifact not inside of an Snapshot dir" );
58 public void testBadPathTooShort()
60 assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
64 public void testBadPathVersionMismatchA()
66 assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
70 public void testBadPathVersionMismatchB()
72 assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
76 public void testBadPathWrongArtifactId()
78 assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
79 "wrong artifact id" );
83 * [MRM-432] Oddball version spec.
84 * Example of an oddball / unusual version spec.
85 * @throws LayoutException
88 public void testGoodButOddVersionSpecGanymedSsh2()
89 throws LayoutException
91 String groupId = "ch.ethz.ganymed";
92 String artifactId = "ganymed-ssh2";
93 String version = "build210";
94 String classifier = null;
96 String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
98 assertLayout( path, groupId, artifactId, version, classifier, type );
102 * [MRM-432] Oddball version spec.
103 * Example of an oddball / unusual version spec.
104 * @throws LayoutException
107 public void testGoodButOddVersionSpecJavaxComm()
108 throws LayoutException
110 String groupId = "javax";
111 String artifactId = "comm";
112 String version = "3.0-u1";
113 String classifier = null;
115 String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
117 assertLayout( path, groupId, artifactId, version, classifier, type );
121 * Test the ejb-client type spec.
122 * Type specs are not a 1 to 1 map to the extension.
123 * This tests that effect.
124 * @throws LayoutException
126 /* TODO: Re-enabled in the future.
127 public void testGoodFooEjbClient()
128 throws LayoutException
130 String groupId = "com.foo";
131 String artifactId = "foo-client";
132 String version = "1.0";
133 String classifier = null;
134 String type = "ejb-client"; // oddball type-spec (should result in jar extension)
135 String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
137 assertLayout( path, groupId, artifactId, version, classifier, type );
142 * [MRM-432] Oddball version spec.
143 * Example of an oddball / unusual version spec.
144 * @throws LayoutException
147 public void testGoodButOddVersionSpecJavaxPersistence()
148 throws LayoutException
150 String groupId = "javax.persistence";
151 String artifactId = "ejb";
152 String version = "3.0-public_review";
153 String classifier = null;
155 String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
158 * The version id of "public_review" can cause problems. is it part of
159 * the version spec? or the classifier?
160 * Since the path spec below shows it in the path, then it is really
161 * part of the version spec.
164 assertLayout( path, groupId, artifactId, version, classifier, type );
168 public void testGoodComFooTool()
169 throws LayoutException
171 String groupId = "com.foo";
172 String artifactId = "foo-tool";
173 String version = "1.0";
174 String classifier = null;
176 String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
178 assertLayout( path, groupId, artifactId, version, classifier, type );
182 public void testGoodCommonsLang()
183 throws LayoutException
185 String groupId = "commons-lang";
186 String artifactId = "commons-lang";
187 String version = "2.1";
188 String classifier = null;
190 String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
192 assertLayout( path, groupId, artifactId, version, classifier, type );
196 * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
199 public void testGoodDashedArtifactId()
200 throws LayoutException
202 String groupId = "test.maven-arch";
203 String artifactId = "test-arch";
204 String version = "2.0.3-SNAPSHOT";
205 String classifier = null;
207 String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
209 assertLayout( path, groupId, artifactId, version, classifier, type );
213 * It may seem odd, but this is a valid artifact.
216 public void testGoodDotNotationArtifactId()
217 throws LayoutException
219 String groupId = "com.company.department";
220 String artifactId = "com.company.department";
221 String version = "0.2";
222 String classifier = null;
224 String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
226 assertLayout( path, groupId, artifactId, version, classifier, type );
230 * It may seem odd, but this is a valid artifact.
233 public void testGoodDotNotationSameGroupIdAndArtifactId()
234 throws LayoutException
236 String groupId = "com.company.department";
237 String artifactId = "com.company.department.project";
238 String version = "0.3";
239 String classifier = null;
241 String path = "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
243 assertLayout( path, groupId, artifactId, version, classifier, type );
247 * Test the classifier, and java-source type spec.
248 * @throws LayoutException
251 public void testGoodFooLibSources()
252 throws LayoutException
254 String groupId = "com.foo.lib";
255 String artifactId = "foo-lib";
256 String version = "2.1-alpha-1";
257 String classifier = "sources";
258 String type = "java-source"; // oddball type-spec (should result in jar extension)
259 String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
261 assertLayout( path, groupId, artifactId, version, classifier, type );
265 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
266 * @throws LayoutException
269 public void testGoodSnapshotMavenTest()
270 throws LayoutException
272 String groupId = "org.apache.archiva.test";
273 String artifactId = "redonkulous";
274 String version = "3.1-beta-1-20050831.101112-42";
275 String classifier = null;
277 String path = "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
279 assertLayout( path, groupId, artifactId, version, classifier, type );
283 * [MRM-519] version identifiers within filename cause misidentification of version.
284 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
287 public void testGoodVersionKeywordInArtifactId()
288 throws LayoutException
290 String groupId = "maven";
291 String artifactId = "maven-test-plugin";
292 String version = "1.8.2";
293 String classifier = null;
295 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
297 assertLayout( path, groupId, artifactId, version, classifier, type );
301 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
302 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
305 public void testGoodDetectMavenTestPlugin()
306 throws LayoutException
308 String groupId = "maven";
309 String artifactId = "maven-test-plugin";
310 String version = "1.8.2";
311 String classifier = null;
312 String type = "maven-plugin";
313 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
315 assertLayout( path, groupId, artifactId, version, classifier, type );
319 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
322 public void testGoodDetectCoberturaMavenPlugin()
323 throws LayoutException
325 String groupId = "org.codehaus.mojo";
326 String artifactId = "cobertura-maven-plugin";
327 String version = "2.1";
328 String classifier = null;
329 String type = "maven-plugin";
330 String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
332 assertLayout( path, groupId, artifactId, version, classifier, type );
336 public void testToArtifactOnEmptyPath()
340 toArtifactReference( "" );
341 fail( "Should have failed due to empty path." );
343 catch ( LayoutException e )
350 public void testToArtifactOnNullPath()
354 toArtifactReference( null );
355 fail( "Should have failed due to null path." );
357 catch ( LayoutException e )
364 public void testToArtifactReferenceOnEmptyPath()
368 toArtifactReference( "" );
369 fail( "Should have failed due to empty path." );
371 catch ( LayoutException e )
378 public void testToArtifactReferenceOnNullPath()
382 toArtifactReference( null );
383 fail( "Should have failed due to null path." );
385 catch ( LayoutException e )
392 public void testToPathOnNullArtifactReference()
397 ArtifactReference reference = null;
399 fail( "Should have failed due to null artifact reference." );
401 catch ( IllegalArgumentException e )
407 private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
408 String version, String classifier, String type )
410 String expectedId = "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier
413 assertNotNull( expectedId + " - Should not be null.", actualReference );
415 assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
416 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
417 if ( StringUtils.isNotBlank( classifier ) )
419 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
421 assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
422 assertEquals( expectedId + " - Type", type, actualReference.getType() );
425 private void assertBadPath( String path, String reason )
429 toArtifactReference( path );
430 fail( "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
432 catch ( LayoutException e )
439 * Perform a roundtrip through the layout routines to determine success.
441 private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
443 throws LayoutException
445 ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
447 // --- Artifact Tests.
450 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
452 // --- Artifact Reference Tests
454 // Path to Artifact Reference.
455 ArtifactReference testReference = toArtifactReference( path );
456 assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
458 // And back again, using test Reference from previous step.
459 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
462 private ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
465 ArtifactReference artifact = new ArtifactReference();
466 artifact.setGroupId( groupId );
467 artifact.setArtifactId( artifactId );
468 artifact.setVersion( version );
469 artifact.setClassifier( classifier );
470 artifact.setType( type );
471 assertNotNull( artifact );
475 protected abstract ArtifactReference toArtifactReference( String path )
476 throws LayoutException;
478 protected abstract String toPath( ArtifactReference reference );