View Javadoc
1   package org.apache.archiva.metadata.repository.storage.maven2;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
27  
28  import static org.junit.Assert.*;
29  
30  /**
31   * AbstractDefaultRepositoryContentTestCase
32   */
33  public abstract class AbstractDefaultRepositoryContentTestCase
34      extends AbstractRepositoryLayerTestCase
35  {
36      @Test
37      public void testBadPathMissingType()
38      {
39          assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
40      }
41  
42      @Test
43      public void testBadPathReleaseInSnapshotDir()
44      {
45          assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
46                         "non snapshot artifact inside of a snapshot dir" );
47      }
48  
49      @Test
50      public void testBadPathTimestampedSnapshotNotInSnapshotDir()
51      {
52          assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
53                         "Timestamped Snapshot artifact not inside of an Snapshot dir" );
54      }
55  
56      @Test
57      public void testBadPathTooShort()
58      {
59          assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
60      }
61  
62      @Test
63      public void testBadPathVersionMismatchA()
64      {
65          assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
66      }
67  
68      @Test
69      public void testBadPathVersionMismatchB()
70      {
71          assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
72      }
73  
74      @Test
75      public void testBadPathWrongArtifactId()
76      {
77          assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
78                         "wrong artifact id" );
79      }
80  
81      /**
82       * [MRM-432] Oddball version spec.
83       * Example of an oddball / unusual version spec.
84       *
85       * @throws org.apache.archiva.repository.layout.LayoutException
86       *
87       */
88      @Test
89      public void testGoodButOddVersionSpecGanymedSsh2()
90          throws LayoutException
91      {
92          String groupId = "ch.ethz.ganymed";
93          String artifactId = "ganymed-ssh2";
94          String version = "build210";
95          String classifier = null;
96          String type = "jar";
97          String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
98  
99          assertLayout( path, groupId, artifactId, version, classifier, type );
100     }
101 
102     /**
103      * [MRM-432] Oddball version spec.
104      * Example of an oddball / unusual version spec.
105      *
106      * @throws org.apache.archiva.repository.layout.LayoutException
107      *
108      */
109     @Test
110     public void testGoodButOddVersionSpecJavaxComm()
111         throws LayoutException
112     {
113         String groupId = "javax";
114         String artifactId = "comm";
115         String version = "3.0-u1";
116         String classifier = null;
117         String type = "jar";
118         String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
119 
120         assertLayout( path, groupId, artifactId, version, classifier, type );
121     }
122 
123     /**
124      * Test the ejb-client type spec.
125      * Type specs are not a 1 to 1 map to the extension.
126      * This tests that effect.
127      * @throws org.apache.archiva.repository.layout.LayoutException
128      */
129     /* TODO: Re-enabled in the future.
130     public void testGoodFooEjbClient()
131         throws LayoutException
132     {
133         String groupId = "com.foo";
134         String artifactId = "foo-client";
135         String version = "1.0";
136         String classifier = null;
137         String type = "ejb-client"; // oddball type-spec (should result in jar extension)
138         String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
139 
140         assertLayout( path, groupId, artifactId, version, classifier, type );
141     }
142     */
143 
144     /**
145      * [MRM-432] Oddball version spec.
146      * Example of an oddball / unusual version spec.
147      *
148      * @throws org.apache.archiva.repository.layout.LayoutException
149      *
150      */
151     @Test
152     public void testGoodButOddVersionSpecJavaxPersistence()
153         throws LayoutException
154     {
155         String groupId = "javax.persistence";
156         String artifactId = "ejb";
157         String version = "3.0-public_review";
158         String classifier = null;
159         String type = "jar";
160         String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
161 
162         /*
163          * The version id of "public_review" can cause problems. is it part of
164          * the version spec? or the classifier?
165          * Since the path spec below shows it in the path, then it is really
166          * part of the version spec.
167          */
168 
169         assertLayout( path, groupId, artifactId, version, classifier, type );
170     }
171 
172     @Test
173     public void testGoodComFooTool()
174         throws LayoutException
175     {
176         String groupId = "com.foo";
177         String artifactId = "foo-tool";
178         String version = "1.0";
179         String classifier = null;
180         String type = "jar";
181         String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
182 
183         assertLayout( path, groupId, artifactId, version, classifier, type );
184     }
185 
186     @Test
187     public void testGoodCommonsLang()
188         throws LayoutException
189     {
190         String groupId = "commons-lang";
191         String artifactId = "commons-lang";
192         String version = "2.1";
193         String classifier = null;
194         String type = "jar";
195         String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
196 
197         assertLayout( path, groupId, artifactId, version, classifier, type );
198     }
199 
200     /**
201      * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
202      */
203     @Test
204     public void testGoodDashedArtifactId()
205         throws LayoutException
206     {
207         String groupId = "test.maven-arch";
208         String artifactId = "test-arch";
209         String version = "2.0.3-SNAPSHOT";
210         String classifier = null;
211         String type = "pom";
212         String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
213 
214         assertLayout( path, groupId, artifactId, version, classifier, type );
215     }
216 
217     /**
218      * It may seem odd, but this is a valid artifact.
219      */
220     @Test
221     public void testGoodDotNotationArtifactId()
222         throws LayoutException
223     {
224         String groupId = "com.company.department";
225         String artifactId = "com.company.department";
226         String version = "0.2";
227         String classifier = null;
228         String type = "pom";
229         String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
230 
231         assertLayout( path, groupId, artifactId, version, classifier, type );
232     }
233 
234     /**
235      * It may seem odd, but this is a valid artifact.
236      */
237     @Test
238     public void testGoodDotNotationSameGroupIdAndArtifactId()
239         throws LayoutException
240     {
241         String groupId = "com.company.department";
242         String artifactId = "com.company.department.project";
243         String version = "0.3";
244         String classifier = null;
245         String type = "pom";
246         String path =
247             "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
248 
249         assertLayout( path, groupId, artifactId, version, classifier, type );
250     }
251 
252     /**
253      * Test the classifier, and java-source type spec.
254      *
255      * @throws org.apache.archiva.repository.layout.LayoutException
256      *
257      */
258     @Test
259     public void testGoodFooLibSources()
260         throws LayoutException
261     {
262         String groupId = "com.foo.lib";
263         String artifactId = "foo-lib";
264         String version = "2.1-alpha-1";
265         String classifier = "sources";
266         String type = "java-source"; // oddball type-spec (should result in jar extension)
267         String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
268 
269         assertLayout( path, groupId, artifactId, version, classifier, type );
270     }
271 
272     /**
273      * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
274      *
275      * @throws org.apache.archiva.repository.layout.LayoutException
276      *
277      */
278     @Test
279     public void testGoodSnapshotMavenTest()
280         throws LayoutException
281     {
282         String groupId = "org.apache.archiva.test";
283         String artifactId = "redonkulous";
284         String version = "3.1-beta-1-20050831.101112-42";
285         String classifier = null;
286         String type = "jar";
287         String path =
288             "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
289 
290         assertLayout( path, groupId, artifactId, version, classifier, type );
291     }
292 
293     /**
294      * [MRM-519] version identifiers within filename cause misidentification of version.
295      * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
296      */
297     @Test
298     public void testGoodVersionKeywordInArtifactId()
299         throws LayoutException
300     {
301         String groupId = "maven";
302         String artifactId = "maven-test-plugin";
303         String version = "1.8.2";
304         String classifier = null;
305         String type = "pom";
306         String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
307 
308         assertLayout( path, groupId, artifactId, version, classifier, type );
309     }
310 
311     /**
312      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
313      * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
314      */
315     @Test
316     public void testGoodDetectMavenTestPlugin()
317         throws LayoutException
318     {
319         String groupId = "maven";
320         String artifactId = "maven-test-plugin";
321         String version = "1.8.2";
322         String classifier = null;
323         String type = "maven-plugin";
324         String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
325 
326         assertLayout( path, groupId, artifactId, version, classifier, type );
327     }
328 
329     /**
330      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
331      */
332     @Test
333     public void testGoodDetectCoberturaMavenPlugin()
334         throws LayoutException
335     {
336         String groupId = "org.codehaus.mojo";
337         String artifactId = "cobertura-maven-plugin";
338         String version = "2.1";
339         String classifier = null;
340         String type = "maven-plugin";
341         String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
342 
343         assertLayout( path, groupId, artifactId, version, classifier, type );
344     }
345 
346     @Test
347     public void testToArtifactOnEmptyPath()
348     {
349         try
350         {
351             toArtifactReference( "" );
352             fail( "Should have failed due to empty path." );
353         }
354         catch ( LayoutException e )
355         {
356             /* expected path */
357         }
358     }
359 
360     @Test
361     public void testToArtifactOnNullPath()
362     {
363         try
364         {
365             toArtifactReference( null );
366             fail( "Should have failed due to null path." );
367         }
368         catch ( LayoutException e )
369         {
370             /* expected path */
371         }
372     }
373 
374     @Test
375     public void testToArtifactReferenceOnEmptyPath()
376     {
377         try
378         {
379             toArtifactReference( "" );
380             fail( "Should have failed due to empty path." );
381         }
382         catch ( LayoutException e )
383         {
384             /* expected path */
385         }
386     }
387 
388     @Test
389     public void testToArtifactReferenceOnNullPath()
390     {
391         try
392         {
393             toArtifactReference( null );
394             fail( "Should have failed due to null path." );
395         }
396         catch ( LayoutException e )
397         {
398             /* expected path */
399         }
400     }
401 
402     @Test
403     public void testToPathOnNullArtifactReference()
404 
405     {
406         try
407         {
408             ArtifactReference reference = null;
409             toPath( reference );
410             fail( "Should have failed due to null artifact reference." );
411         }
412         catch ( IllegalArgumentException e )
413         {
414             /* expected path */
415         }
416     }
417 
418     private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
419                                           String version, String classifier, String type )
420     {
421         String expectedId =
422             "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
423 
424         assertNotNull( expectedId + " - Should not be null.", actualReference );
425 
426         assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
427         assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
428         if ( StringUtils.isNotBlank( classifier ) )
429         {
430             assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
431         }
432         assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
433         assertEquals( expectedId + " - Type", type, actualReference.getType() );
434     }
435 
436     private void assertBadPath( String path, String reason )
437     {
438         try
439         {
440             toArtifactReference( path );
441             fail(
442                 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
443         }
444         catch ( LayoutException e )
445         {
446             /* expected path */
447         }
448     }
449 
450     /**
451      * Perform a roundtrip through the layout routines to determine success.
452      */
453     private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
454                                String type )
455         throws LayoutException
456     {
457         ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
458 
459         // --- Artifact Tests.
460 
461         // Artifact to Path 
462         assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
463 
464         // --- Artifact Reference Tests
465 
466         // Path to Artifact Reference.
467         ArtifactReference testReference = toArtifactReference( path );
468         assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
469 
470         // And back again, using test Reference from previous step.
471         assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
472     }
473 
474     protected ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
475                                               String type )
476     {
477         ArtifactReference artifact = new ArtifactReference();
478         artifact.setGroupId( groupId );
479         artifact.setArtifactId( artifactId );
480         artifact.setVersion( version );
481         artifact.setClassifier( classifier );
482         artifact.setType( type );
483         assertNotNull( artifact );
484         return artifact;
485     }
486 
487     protected abstract ArtifactReference toArtifactReference( String path )
488         throws LayoutException;
489 
490     protected abstract String toPath( ArtifactReference reference );
491 }