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.RepositoryContent;
23 import org.apache.archiva.repository.maven.AbstractRepositoryLayerTestCase;
24 import org.apache.archiva.repository.LayoutException;
25 import org.apache.archiva.repository.BaseRepositoryContentLayout;
26 import org.apache.archiva.repository.content.Artifact;
27 import org.apache.archiva.repository.content.ItemSelector;
28 import org.apache.archiva.repository.content.Namespace;
29 import org.apache.archiva.repository.content.Project;
30 import org.apache.archiva.repository.content.Version;
31 import org.apache.archiva.repository.content.base.ArchivaItemSelector;
32 import org.apache.commons.lang3.StringUtils;
33 import org.junit.Test;
35 import static org.junit.Assert.*;
38 * AbstractDefaultRepositoryContentTestCase
40 public abstract class AbstractRepositoryContentTest
41 extends AbstractRepositoryLayerTestCase
44 public void testBadPathMissingType()
46 assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
47 assertBadPathCi( "invalid/invalid/1/invalid-1", "missing type" );
51 public void testBadPathReleaseInSnapshotDir()
53 assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
54 "non snapshot artifact inside of a snapshot dir" );
55 assertBadPathCi( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
56 "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" );
65 assertBadPathCi( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
66 "Timestamped Snapshot artifact not inside of an Snapshot dir" );
70 public void testBadPathTooShort()
72 assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
73 assertBadPathCi( "invalid/invalid-1.0.jar", "path is too short" );
77 public void testBadPathVersionMismatchA()
79 assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
80 assertBadPathCi( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
84 public void testBadPathVersionMismatchB()
86 assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
87 assertBadPathCi( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
91 public void testBadPathWrongArtifactId()
93 assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
94 "wrong artifact id" );
95 assertBadPathCi( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
96 "wrong artifact id" );
100 * [MRM-432] Oddball version spec.
101 * Example of an oddball / unusual version spec.
103 * @throws LayoutException
107 public void testGoodButOddVersionSpecGanymedSsh2()
108 throws LayoutException
110 String groupId = "ch.ethz.ganymed";
111 String artifactId = "ganymed-ssh2";
112 String version = "build210";
113 String classifier = null;
115 String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
117 assertLayout( path, groupId, artifactId, version, classifier, type );
118 assertLayoutCi( path, groupId, artifactId, version, classifier, type);
122 * [MRM-432] Oddball version spec.
123 * Example of an oddball / unusual version spec.
125 * @throws LayoutException
129 public void testGoodButOddVersionSpecJavaxComm()
130 throws LayoutException
132 String groupId = "javax";
133 String artifactId = "comm";
134 String version = "3.0-u1";
135 String classifier = null;
137 String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
139 assertLayout( path, groupId, artifactId, version, classifier, type );
140 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
144 * Test the ejb-client type spec.
145 * Type specs are not a 1 to 1 map to the extension.
146 * This tests that effect.
147 * @throws LayoutException
149 /* TODO: Re-enabled in the future.
150 public void testGoodFooEjbClient()
151 throws LayoutException
153 String groupId = "com.foo";
154 String artifactId = "foo-client";
155 String version = "1.0";
156 String classifier = null;
157 String type = "ejb-client"; // oddball type-spec (should result in jar extension)
158 String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
160 assertLayout( path, groupId, artifactId, version, classifier, type );
165 * [MRM-432] Oddball version spec.
166 * Example of an oddball / unusual version spec.
168 * @throws LayoutException
172 public void testGoodButOddVersionSpecJavaxPersistence()
173 throws LayoutException
175 String groupId = "javax.persistence";
176 String artifactId = "ejb";
177 String version = "3.0-public_review";
178 String classifier = null;
180 String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
183 * The version id of "public_review" can cause problems. is it part of
184 * the version spec? or the classifier?
185 * Since the path spec below shows it in the path, then it is really
186 * part of the version spec.
189 assertLayout( path, groupId, artifactId, version, classifier, type );
190 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
194 public void testGoodComFooTool()
195 throws LayoutException
197 String groupId = "com.foo";
198 String artifactId = "foo-tool";
199 String version = "1.0";
200 String classifier = null;
202 String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
204 assertLayout( path, groupId, artifactId, version, classifier, type );
205 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
209 public void testGoodCommonsLang()
210 throws LayoutException
212 String groupId = "commons-lang";
213 String artifactId = "commons-lang";
214 String version = "2.1";
215 String classifier = null;
217 String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
219 assertLayout( path, groupId, artifactId, version, classifier, type );
220 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
224 * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
227 public void testGoodDashedArtifactId()
228 throws LayoutException
230 String groupId = "test.maven-arch";
231 String artifactId = "test-arch";
232 String version = "2.0.3-SNAPSHOT";
233 String classifier = null;
235 String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
237 assertLayout( path, groupId, artifactId, version, classifier, type );
238 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
243 * It may seem odd, but this is a valid artifact.
246 public void testGoodDotNotationArtifactId()
247 throws LayoutException
249 String groupId = "com.company.department";
250 String artifactId = "com.company.department";
251 String version = "0.2";
252 String classifier = null;
254 String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
256 assertLayout( path, groupId, artifactId, version, classifier, type );
257 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
261 * It may seem odd, but this is a valid artifact.
264 public void testGoodDotNotationSameGroupIdAndArtifactId()
265 throws LayoutException
267 String groupId = "com.company.department";
268 String artifactId = "com.company.department.project";
269 String version = "0.3";
270 String classifier = null;
273 "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
275 assertLayout( path, groupId, artifactId, version, classifier, type );
276 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
281 * Test the classifier, and java-source type spec.
283 * @throws LayoutException
287 public void testGoodFooLibSources()
288 throws LayoutException
290 String groupId = "com.foo.lib";
291 String artifactId = "foo-lib";
292 String version = "2.1-alpha-1";
293 String classifier = "sources";
294 String type = "java-source"; // oddball type-spec (should result in jar extension)
295 String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
297 assertLayout( path, groupId, artifactId, version, classifier, type );
298 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
303 * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
305 * @throws LayoutException
309 public void testGoodSnapshotMavenTest()
310 throws LayoutException
312 String groupId = "org.apache.archiva.test";
313 String artifactId = "redonkulous";
314 String version = "3.1-beta-1-20050831.101112-42";
315 String classifier = null;
318 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
320 assertLayout( path, groupId, artifactId, version, classifier, type );
321 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
325 * [MRM-519] version identifiers within filename cause misidentification of version.
326 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
329 public void testGoodVersionKeywordInArtifactId()
330 throws LayoutException
332 String groupId = "maven";
333 String artifactId = "maven-test-plugin";
334 String version = "1.8.2";
335 String classifier = null;
337 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
339 assertLayout( path, groupId, artifactId, version, classifier, type );
340 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
344 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
345 * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
348 public void testGoodDetectMavenTestPlugin()
349 throws LayoutException
351 String groupId = "maven";
352 String artifactId = "maven-test-plugin";
353 String version = "1.8.2";
354 String classifier = null;
355 String type = "maven-plugin";
356 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
358 assertLayout( path, groupId, artifactId, version, classifier, type );
359 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
364 * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
367 public void testGoodDetectCoberturaMavenPlugin()
368 throws LayoutException
370 String groupId = "org.codehaus.mojo";
371 String artifactId = "cobertura-maven-plugin";
372 String version = "2.1";
373 String classifier = null;
374 String type = "maven-plugin";
375 String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
377 assertLayout( path, groupId, artifactId, version, classifier, type );
378 assertLayoutCi( path, groupId, artifactId, version, classifier, type );
383 public void testToItemSelectorOnEmptyPath()
387 getContent( ).toItemSelector( "" );
388 fail( "toItemSelector() should have failed due to empty path." );
390 catch ( LayoutException e )
397 public void testToArtifactOnEmptyPath()
401 toArtifactReference( "" );
402 fail( "Should have failed due to empty path." );
404 catch ( LayoutException e )
411 public void testToArtifactOnNullPath()
415 toArtifactReference( null );
416 fail( "Should have failed due to null path." );
418 catch ( LayoutException e )
425 public void testToItemSelectorOnNullPath()
429 getContent().toItemSelector( null );
430 fail( "toItemSelector() should have failed due to null path." );
432 catch ( LayoutException e )
439 public void testToPathOnNullArtifactReference()
444 ArtifactReference reference = null;
446 fail( "Should have failed due to null artifact reference." );
448 catch ( IllegalArgumentException e )
454 public void testToPathOnNullItemSelector()
459 ItemSelector selector = null;
461 fail( "Should have failed due to null artifact reference." );
463 catch ( IllegalArgumentException e )
469 private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
470 String version, String classifier, String type )
473 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
475 assertNotNull( expectedId + " - Should not be null.", actualReference );
477 assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
478 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
479 if ( StringUtils.isNotBlank( classifier ) )
481 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
483 assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
484 assertEquals( expectedId + " - Type", type, actualReference.getType() );
487 private void assertItemSelector( ItemSelector actualReference, String groupId, String artifactId,
488 String version, String classifier, String type )
491 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
493 assertNotNull( expectedId + " - Should not be null.", actualReference );
495 assertEquals( expectedId + " - Group ID", groupId, actualReference.getNamespace() );
496 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
497 if ( StringUtils.isNotBlank( classifier ) )
499 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
501 assertEquals( expectedId + " - Version ID", version, actualReference.getArtifactVersion() );
502 assertEquals( expectedId + " - Type", type, actualReference.getType() );
505 private void assertBadPath( String path, String reason )
509 toArtifactReference( path );
511 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
513 catch ( LayoutException e )
519 protected void assertBadPathCi( String path, String reason )
523 toItemSelector( path );
525 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
527 catch ( LayoutException e )
534 * Perform a roundtrip through the layout routines to determine success.
536 private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
538 throws LayoutException
540 ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
542 // --- Artifact Tests.
545 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
547 // --- Artifact Reference Tests
549 // Path to Artifact Reference.
550 ArtifactReference testReference = toArtifactReference( path );
551 assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
553 // And back again, using test Reference from previous step.
554 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
557 private void assertLayoutCi( String path, String groupId, String artifactId, String version, String classifier,
559 throws LayoutException
561 ItemSelector expectedArtifact = createItemSelector( groupId, artifactId, version, classifier, type );
563 // --- Artifact Tests.
566 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
568 // --- Artifact Reference Tests
570 // Path to Artifact Reference.
571 ItemSelector testReference = toItemSelector( path );
572 assertItemSelector( testReference, groupId, artifactId, version, classifier, type );
574 // And back again, using test Reference from previous step.
575 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
577 if (getManaged()!=null)
582 if ( StringUtils.isNotEmpty( groupId ) )
584 ns = getManaged( ).getNamespace( expectedArtifact );
586 assertEquals( groupId, ns.getNamespace( ) );
588 if ( StringUtils.isNotEmpty( artifactId ) )
590 pr = getManaged( ).getProject( expectedArtifact );
592 assertEquals( artifactId, pr.getId( ) );
593 assertEquals( ns, pr.getNamespace( ) );
595 if ( StringUtils.isNotEmpty( version ) )
597 ver = getManaged( ).getVersion( expectedArtifact );
598 assertNotNull( ver );
599 assertEquals( version, ver.getVersion( ) );
600 assertEquals( pr, ver.getProject( ) );
602 Artifact artifact = getManaged( ).getArtifact( expectedArtifact );
603 assertNotNull( artifact );
604 assertEquals( artifactId, artifact.getId( ) );
605 assertEquals( ver, artifact.getVersion( ) );
610 protected ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
613 ArtifactReference artifact = new ArtifactReference();
614 artifact.setGroupId( groupId );
615 artifact.setArtifactId( artifactId );
616 artifact.setVersion( version );
617 artifact.setClassifier( classifier );
618 artifact.setType( type );
619 assertNotNull( artifact );
623 protected ItemSelector createItemSelector(String groupId, String artifactId, String version, String classifier,
625 return ArchivaItemSelector.builder( ).withNamespace( groupId )
626 .withProjectId( artifactId )
627 .withArtifactId( artifactId )
628 .withVersion( version )
629 .withClassifier( classifier )
635 protected abstract ArtifactReference toArtifactReference( String path )
636 throws LayoutException;
638 protected abstract String toPath( ArtifactReference reference );
641 protected abstract String toPath( ItemSelector selector );
643 protected abstract ItemSelector toItemSelector(String path) throws LayoutException;
645 protected abstract BaseRepositoryContentLayout getManaged();
647 protected abstract RepositoryContent getContent( );