View Javadoc
1   package org.apache.archiva.repository.content.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.content.PathParser;
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;
30  
31  import static org.junit.Assert.*;
32  
33  /**
34   * DefaultPathParserTest
35   *
36   * TODO: move to path translator tests
37   *
38   *
39   */
40  @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
41  @ContextConfiguration ( { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
42  public class DefaultPathParserTest
43  {
44      private PathParser parser = new DefaultPathParser();
45  
46      @Test
47      public void testBadPathMissingType()
48      {
49          // TODO: should we allow this instead?
50          assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
51      }
52  
53      @Test
54      public void testBadPathReleaseInSnapshotDir()
55      {
56          assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
57                         "non snapshot artifact inside of a snapshot dir" );
58      }
59  
60      @Test
61      public void testBadPathTimestampedSnapshotNotInSnapshotDir()
62      {
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      }
66  
67      @Test
68      public void testBadPathTooShort()
69      {
70          assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
71      }
72  
73      @Test
74      public void testBadPathVersionMismatchA()
75      {
76          assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
77      }
78  
79      @Test
80      public void testBadPathVersionMismatchB()
81      {
82          assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
83      }
84  
85      @Test
86      public void testBadPathWrongArtifactId()
87      {
88          assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
89                         "wrong artifact id" );
90      }
91  
92      /**
93       * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
94       */
95      @Test
96      public void testGoodButDualExtensions()
97          throws LayoutException
98      {
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";
105 
106         assertLayout( path, groupId, artifactId, version, classifier, type );
107     }
108 
109     @Test
110     public void testGoodButDualExtensionsWithClassifier()
111         throws LayoutException
112     {
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";
119 
120         assertLayout( path, groupId, artifactId, version, classifier, type );
121     }
122 
123     @Test
124     public void testGoodButDualExtensionsTarGz()
125         throws LayoutException
126     {
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";
133 
134         assertLayout( path, groupId, artifactId, version, classifier, type );
135     }
136 
137     @Test
138     public void testGoodButDualExtensionsTarGzAndClassifier()
139         throws LayoutException
140     {
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";
147 
148         assertLayout( path, groupId, artifactId, version, classifier, type );
149     }
150 
151     /**
152      * [MRM-432] Oddball version spec.
153      * Example of an oddball / unusual version spec.
154      *
155      * @throws org.apache.archiva.repository.layout.LayoutException
156      */
157     @Test
158     public void testGoodButOddVersionSpecGanymedSsh2()
159         throws LayoutException
160     {
161         String groupId = "ch.ethz.ganymed";
162         String artifactId = "ganymed-ssh2";
163         String version = "build210";
164         String classifier = null;
165         String type = "jar";
166         String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
167 
168         assertLayout( path, groupId, artifactId, version, classifier, type );
169     }
170 
171     /**
172      * [MRM-432] Oddball version spec.
173      * Example of an oddball / unusual version spec.
174      *
175      * @throws org.apache.archiva.repository.layout.LayoutException
176      */
177     @Test
178     public void testGoodButOddVersionSpecJavaxComm()
179         throws LayoutException
180     {
181         String groupId = "javax";
182         String artifactId = "comm";
183         String version = "3.0-u1";
184         String classifier = null;
185         String type = "jar";
186         String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
187 
188         assertLayout( path, groupId, artifactId, version, classifier, type );
189     }
190 
191     /**
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 org.apache.archiva.repository.layout.LayoutException
196      */
197     /* TODO: Re-enabled in the future.
198     public void testGoodFooEjbClient()
199         throws LayoutException
200     {
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";
207 
208         assertLayout( path, groupId, artifactId, version, classifier, type );
209     }
210     */
211 
212     /**
213      * [MRM-432] Oddball version spec.
214      * Example of an oddball / unusual version spec.
215      *
216      * @throws org.apache.archiva.repository.layout.LayoutException
217      */
218     @Test
219     public void testGoodButOddVersionSpecJavaxPersistence()
220         throws LayoutException
221     {
222         String groupId = "javax.persistence";
223         String artifactId = "ejb";
224         String version = "3.0-public_review";
225         String classifier = null;
226         String type = "jar";
227         String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
228 
229         /*
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.
234          */
235 
236         assertLayout( path, groupId, artifactId, version, classifier, type );
237     }
238 
239     @Test
240     public void testGoodComFooTool()
241         throws LayoutException
242     {
243         String groupId = "com.foo";
244         String artifactId = "foo-tool";
245         String version = "1.0";
246         String classifier = null;
247         String type = "jar";
248         String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
249 
250         assertLayout( path, groupId, artifactId, version, classifier, type );
251     }
252 
253     @Test
254     public void testGoodCommonsLang()
255         throws LayoutException
256     {
257         String groupId = "commons-lang";
258         String artifactId = "commons-lang";
259         String version = "2.1";
260         String classifier = null;
261         String type = "jar";
262         String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
263 
264         assertLayout( path, groupId, artifactId, version, classifier, type );
265     }
266 
267     @Test
268     public void testWindowsPathSeparator()
269         throws LayoutException
270     {
271         String groupId = "commons-lang";
272         String artifactId = "commons-lang";
273         String version = "2.1";
274         String classifier = null;
275         String type = "jar";
276         String path = "commons-lang\\commons-lang/2.1\\commons-lang-2.1.jar";
277 
278         assertLayout( path, groupId, artifactId, version, classifier, type );
279     }
280 
281     /**
282      * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
283      */
284     @Test
285     public void testGoodDashedArtifactId()
286         throws LayoutException
287     {
288         String groupId = "test.maven-arch";
289         String artifactId = "test-arch";
290         String version = "2.0.3-SNAPSHOT";
291         String classifier = null;
292         String type = "pom";
293         String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
294 
295         assertLayout( path, groupId, artifactId, version, classifier, type );
296     }
297 
298     /**
299      * It may seem odd, but this is a valid artifact.
300      */
301     @Test
302     public void testGoodDotNotationArtifactId()
303         throws LayoutException
304     {
305         String groupId = "com.company.department";
306         String artifactId = "com.company.department";
307         String version = "0.2";
308         String classifier = null;
309         String type = "pom";
310         String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
311 
312         assertLayout( path, groupId, artifactId, version, classifier, type );
313     }
314 
315     /**
316      * It may seem odd, but this is a valid artifact.
317      */
318     @Test
319     public void testGoodDotNotationSameGroupIdAndArtifactId()
320         throws LayoutException
321     {
322         String groupId = "com.company.department";
323         String artifactId = "com.company.department.project";
324         String version = "0.3";
325         String classifier = null;
326         String type = "pom";
327         String path =
328             "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
329 
330         assertLayout( path, groupId, artifactId, version, classifier, type );
331     }
332 
333     /**
334      * Test the classifier, and java-source type spec.
335      *
336      * @throws org.apache.archiva.repository.layout.LayoutException
337      */
338     @Test
339     public void testGoodFooLibSources()
340         throws LayoutException
341     {
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";
348 
349         assertLayout( path, groupId, artifactId, version, classifier, type );
350     }
351 
352     /**
353      * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
354      *
355      * @throws org.apache.archiva.repository.layout.LayoutException
356      */
357     @Test
358     public void testGoodSnapshotMavenTest()
359         throws LayoutException
360     {
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;
365         String type = "jar";
366         String path =
367             "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
368 
369         assertLayout( path, groupId, artifactId, version, classifier, type );
370     }
371 
372     /**
373      * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
374      *
375      * @throws org.apache.archiva.repository.layout.LayoutException
376      */
377     @Test
378     public void testGoodLongSnapshotMavenTest()
379         throws LayoutException
380     {
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;
385         String type = "pom";
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";
387 
388         assertLayout( path, groupId, artifactId, version, classifier, type );
389     }
390 
391     /**
392      * A timestamped versioned artifact but without release version part. Like on axiom trunk.
393      */
394     @Test
395     public void testBadSnapshotWithoutReleasePart()
396     {
397         assertBadPath( "org/apache/ws/commons/axiom/axiom/SNAPSHOT/axiom-20070912.093446-2.pom",
398                        "snapshot version without release part" );
399     }
400 
401     /**
402      * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
403      *
404      * @throws org.apache.archiva.repository.layout.LayoutException
405      */
406     @Test
407     public void testClassifiedSnapshotMavenTest()
408         throws LayoutException
409     {
410         String groupId = "a.group.id";
411         String artifactId = "artifact-id";
412         String version = "1.0-20070219.171202-34";
413         String classifier = "test-sources";
414         String type = "jar";
415         String path = "a/group/id/artifact-id/1.0-SNAPSHOT/artifact-id-1.0-20070219.171202-34-test-sources.jar";
416 
417         assertLayout( path, groupId, artifactId, version, classifier, type );
418     }
419 
420     /**
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.
423      */
424     @Test
425     public void testGoodVersionKeywordInArtifactId()
426         throws LayoutException
427     {
428         String groupId = "maven";
429         String artifactId = "maven-test-plugin";
430         String version = "1.8.2";
431         String classifier = null;
432         String type = "pom";
433         String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
434 
435         assertLayout( path, groupId, artifactId, version, classifier, type );
436     }
437 
438     /**
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.
441      */
442     @Test
443     public void testGoodDetectMavenTestPlugin()
444         throws LayoutException
445     {
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";
452 
453         assertLayout( path, groupId, artifactId, version, classifier, type );
454     }
455 
456     /**
457      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
458      */
459     @Test
460     public void testGoodDetectCoberturaMavenPlugin()
461         throws LayoutException
462     {
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";
469 
470         assertLayout( path, groupId, artifactId, version, classifier, type );
471     }
472 
473     @Test
474     public void testToArtifactOnEmptyPath()
475     {
476         try
477         {
478             parser.toArtifactReference( "" );
479             fail( "Should have failed due to empty path." );
480         }
481         catch ( LayoutException e )
482         {
483             /* expected path */
484         }
485     }
486 
487     @Test
488     public void testToArtifactOnNullPath()
489     {
490         try
491         {
492             parser.toArtifactReference( null );
493             fail( "Should have failed due to null path." );
494         }
495         catch ( LayoutException e )
496         {
497             /* expected path */
498         }
499     }
500 
501     @Test
502     public void testToArtifactReferenceOnEmptyPath()
503     {
504         try
505         {
506             parser.toArtifactReference( "" );
507             fail( "Should have failed due to empty path." );
508         }
509         catch ( LayoutException e )
510         {
511             /* expected path */
512         }
513     }
514 
515     @Test
516     public void testToArtifactReferenceOnNullPath()
517     {
518         try
519         {
520             parser.toArtifactReference( null );
521             fail( "Should have failed due to null path." );
522         }
523         catch ( LayoutException e )
524         {
525             /* expected path */
526         }
527     }
528 
529     /**
530      * Perform a path to artifact reference lookup, and verify the results.
531      */
532     private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
533                                String type )
534         throws LayoutException
535     {
536         // Path to Artifact Reference.
537         ArtifactReference testReference = parser.toArtifactReference( path );
538         assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
539     }
540 
541     private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
542                                           String version, String classifier, String type )
543     {
544         String expectedId =
545             "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
546 
547         assertNotNull( expectedId + " - Should not be null.", actualReference );
548 
549         assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
550         assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
551         if ( StringUtils.isNotBlank( classifier ) )
552         {
553             assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
554         }
555         assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
556         assertEquals( expectedId + " - Type", type, actualReference.getType() );
557     }
558 
559     private void assertBadPath( String path, String reason )
560     {
561         try
562         {
563             parser.toArtifactReference( path );
564             fail(
565                 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
566         }
567         catch ( LayoutException e )
568         {
569             /* expected path */
570         }
571     }
572 }