1 package org.apache.archiva.metadata.repository.storage.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.model.ArtifactReference;
23 import org.apache.archiva.repository.AbstractRepositoryLayerTestCase;
24 import org.apache.archiva.repository.layout.LayoutException;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.commons.lang.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 * AbstractDefaultRepositoryContentTestCase
36 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
37 @ContextConfiguration ( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-no-mock-conf.xml" } )
38 public abstract class AbstractDefaultRepositoryContentTestCase
39 extends AbstractRepositoryLayerTestCase
42 public void testBadPathMissingType()
44 assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
48 public void testBadPathReleaseInSnapshotDir()
50 assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
51 "non snapshot artifact inside of a snapshot dir" );
55 public void testBadPathTimestampedSnapshotNotInSnapshotDir()
57 assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
58 "Timestamped Snapshot artifact not inside of an Snapshot dir" );
62 public void testBadPathTooShort()
64 assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
68 public void testBadPathVersionMismatchA()
70 assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
74 public void testBadPathVersionMismatchB()
76 assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
80 public void testBadPathWrongArtifactId()
82 assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
83 "wrong artifact id" );
87 * [MRM-432] Oddball version spec.
88 * Example of an oddball / unusual version spec.
90 * @throws org.apache.archiva.repository.layout.LayoutException
94 public void testGoodButOddVersionSpecGanymedSsh2()
95 throws LayoutException
97 String groupId = "ch.ethz.ganymed";
98 String artifactId = "ganymed-ssh2";
99 String version = "build210";
100 String classifier = null;
102 String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
104 assertLayout( path, groupId, artifactId, version, classifier, type );
108 * [MRM-432] Oddball version spec.
109 * Example of an oddball / unusual version spec.
111 * @throws org.apache.archiva.repository.layout.LayoutException
115 public void testGoodButOddVersionSpecJavaxComm()
116 throws LayoutException
118 String groupId = "javax";
119 String artifactId = "comm";
120 String version = "3.0-u1";
121 String classifier = null;
123 String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
125 assertLayout( path, groupId, artifactId, version, classifier, type );
129 * Test the ejb-client type spec.
130 * Type specs are not a 1 to 1 map to the extension.
131 * This tests that effect.
132 * @throws org.apache.archiva.repository.layout.LayoutException
134 /* TODO: Re-enabled in the future.
135 public void testGoodFooEjbClient()
136 throws LayoutException
138 String groupId = "com.foo";
139 String artifactId = "foo-client";
140 String version = "1.0";
141 String classifier = null;
142 String type = "ejb-client"; // oddball type-spec (should result in jar extension)
143 String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
145 assertLayout( path, groupId, artifactId, version, classifier, type );
150 * [MRM-432] Oddball version spec.
151 * Example of an oddball / unusual version spec.
153 * @throws org.apache.archiva.repository.layout.LayoutException
157 public void testGoodButOddVersionSpecJavaxPersistence()
158 throws LayoutException
160 String groupId = "javax.persistence";
161 String artifactId = "ejb";
162 String version = "3.0-public_review";
163 String classifier = null;
165 String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
168 * The version id of "public_review" can cause problems. is it part of
169 * the version spec? or the classifier?
170 * Since the path spec below shows it in the path, then it is really
171 * part of the version spec.
174 assertLayout( path, groupId, artifactId, version, classifier, type );
178 public void testGoodComFooTool()
179 throws LayoutException
181 String groupId = "com.foo";
182 String artifactId = "foo-tool";
183 String version = "1.0";
184 String classifier = null;
186 String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
188 assertLayout( path, groupId, artifactId, version, classifier, type );
192 public void testGoodCommonsLang()
193 throws LayoutException
195 String groupId = "commons-lang";
196 String artifactId = "commons-lang";
197 String version = "2.1";
198 String classifier = null;
200 String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
202 assertLayout( path, groupId, artifactId, version, classifier, type );
206 * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
209 public void testGoodDashedArtifactId()
210 throws LayoutException
212 String groupId = "test.maven-arch";
213 String artifactId = "test-arch";
214 String version = "2.0.3-SNAPSHOT";
215 String classifier = null;
217 String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
219 assertLayout( path, groupId, artifactId, version, classifier, type );
223 * It may seem odd, but this is a valid artifact.
226 public void testGoodDotNotationArtifactId()
227 throws LayoutException
229 String groupId = "com.company.department";
230 String artifactId = "com.company.department";
231 String version = "0.2";
232 String classifier = null;
234 String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
236 assertLayout( path, groupId, artifactId, version, classifier, type );
240 * It may seem odd, but this is a valid artifact.
243 public void testGoodDotNotationSameGroupIdAndArtifactId()
244 throws LayoutException
246 String groupId = "com.company.department";
247 String artifactId = "com.company.department.project";
248 String version = "0.3";
249 String classifier = null;
252 "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
254 assertLayout( path, groupId, artifactId, version, classifier, type );
258 * Test the classifier, and java-source type spec.
260 * @throws org.apache.archiva.repository.layout.LayoutException
264 public void testGoodFooLibSources()
265 throws LayoutException
267 String groupId = "com.foo.lib";
268 String artifactId = "foo-lib";
269 String version = "2.1-alpha-1";
270 String classifier = "sources";
271 String type = "java-source"; // oddball type-spec (should result in jar extension)
272 String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
274 assertLayout( path, groupId, artifactId, version, classifier, type );
278 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
280 * @throws org.apache.archiva.repository.layout.LayoutException
284 public void testGoodSnapshotMavenTest()
285 throws LayoutException
287 String groupId = "org.apache.archiva.test";
288 String artifactId = "redonkulous";
289 String version = "3.1-beta-1-20050831.101112-42";
290 String classifier = null;
293 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
295 assertLayout( path, groupId, artifactId, version, classifier, type );
299 * [MRM-519] version identifiers within filename cause misidentification of version.
300 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
303 public void testGoodVersionKeywordInArtifactId()
304 throws LayoutException
306 String groupId = "maven";
307 String artifactId = "maven-test-plugin";
308 String version = "1.8.2";
309 String classifier = null;
311 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
313 assertLayout( path, groupId, artifactId, version, classifier, type );
317 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
318 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
321 public void testGoodDetectMavenTestPlugin()
322 throws LayoutException
324 String groupId = "maven";
325 String artifactId = "maven-test-plugin";
326 String version = "1.8.2";
327 String classifier = null;
328 String type = "maven-plugin";
329 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
331 assertLayout( path, groupId, artifactId, version, classifier, type );
335 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
338 public void testGoodDetectCoberturaMavenPlugin()
339 throws LayoutException
341 String groupId = "org.codehaus.mojo";
342 String artifactId = "cobertura-maven-plugin";
343 String version = "2.1";
344 String classifier = null;
345 String type = "maven-plugin";
346 String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
348 assertLayout( path, groupId, artifactId, version, classifier, type );
352 public void testToArtifactOnEmptyPath()
356 toArtifactReference( "" );
357 fail( "Should have failed due to empty path." );
359 catch ( LayoutException e )
366 public void testToArtifactOnNullPath()
370 toArtifactReference( null );
371 fail( "Should have failed due to null path." );
373 catch ( LayoutException e )
380 public void testToArtifactReferenceOnEmptyPath()
384 toArtifactReference( "" );
385 fail( "Should have failed due to empty path." );
387 catch ( LayoutException e )
394 public void testToArtifactReferenceOnNullPath()
398 toArtifactReference( null );
399 fail( "Should have failed due to null path." );
401 catch ( LayoutException e )
408 public void testToPathOnNullArtifactReference()
413 ArtifactReference reference = null;
415 fail( "Should have failed due to null artifact reference." );
417 catch ( IllegalArgumentException e )
423 private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
424 String version, String classifier, String type )
427 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
429 assertNotNull( expectedId + " - Should not be null.", actualReference );
431 assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
432 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
433 if ( StringUtils.isNotBlank( classifier ) )
435 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
437 assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
438 assertEquals( expectedId + " - Type", type, actualReference.getType() );
441 private void assertBadPath( String path, String reason )
445 toArtifactReference( path );
447 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
449 catch ( LayoutException e )
456 * Perform a roundtrip through the layout routines to determine success.
458 private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
460 throws LayoutException
462 ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
464 // --- Artifact Tests.
467 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
469 // --- Artifact Reference Tests
471 // Path to Artifact Reference.
472 ArtifactReference testReference = toArtifactReference( path );
473 assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
475 // And back again, using test Reference from previous step.
476 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
479 protected ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
482 ArtifactReference artifact = new ArtifactReference();
483 artifact.setGroupId( groupId );
484 artifact.setArtifactId( artifactId );
485 artifact.setVersion( version );
486 artifact.setClassifier( classifier );
487 artifact.setType( type );
488 assertNotNull( artifact );
492 protected abstract ArtifactReference toArtifactReference( String path )
493 throws LayoutException;
495 protected abstract String toPath( ArtifactReference reference );