View Javadoc

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