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  
23  import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24  import org.apache.maven.archiva.configuration.LegacyArtifactPath;
25  import org.apache.maven.archiva.model.ArtifactReference;
26  import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
27  import org.apache.maven.archiva.repository.layout.LayoutException;
28  
29  /**
30   * LegacyPathParserTest
31   *
32   * @version $Id: LegacyPathParserTest.java 718864 2008-11-19 06:33:35Z brett $
33   */
34  public class LegacyPathParserTest
35      extends AbstractRepositoryLayerTestCase
36  {
37      private LegacyPathParser parser = new LegacyPathParser();
38  
39      /**
40       * Configure the ArchivaConfiguration
41       * {@inheritDoc}
42       * @see org.codehaus.plexus.PlexusTestCase#setUp()
43       */
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48          ArchivaConfiguration config = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
49          LegacyArtifactPath jaxen = new LegacyArtifactPath();
50          jaxen.setPath( "jaxen/jars/jaxen-1.0-FCS-full.jar" );
51          jaxen.setArtifact( "jaxen:jaxen:1.0-FCS:full:jar" );
52          config.getConfiguration().addLegacyArtifactPath( jaxen );
53          parser.configuration = config;
54      }
55  
56  
57      public void testBadPathArtifactIdMissingA()
58      {
59          assertBadPath( "groupId/jars/-1.0.jar", "artifactId is missing" );
60      }
61  
62      public void testBadPathArtifactIdMissingB()
63      {
64          assertBadPath( "groupId/jars/1.0.jar", "artifactId is missing" );
65      }
66  
67      public void testBadPathMissingType()
68      {
69          assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
70      }
71  
72      public void testBadPathTooShort()
73      {
74          // NEW
75          assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
76      }
77  
78      public void testBadPathWrongPackageExtension()
79      {
80          assertBadPath( "org.apache.maven.test/jars/artifactId-1.0.war", "wrong package extension" );
81      }
82  
83      /**
84       * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
85       */
86      public void testGoodButDualExtensions()
87          throws LayoutException
88      {
89          String groupId = "org.project";
90          String artifactId = "example-presentation";
91          String version = "3.2.xml";
92          String type = "distribution-zip";
93          String path = "org.project/zips/example-presentation-3.2.xml.zip";
94  
95          assertLayout( path, groupId, artifactId, version, null, type );
96      }
97  
98      /**
99       * [MRM-432] Oddball version spec.
100      * Example of an oddball / unusual version spec.
101      * @throws LayoutException
102      */
103     public void testGoodButOddVersionSpecGanymedSsh2()
104         throws LayoutException
105     {
106         String groupId = "ch.ethz.ganymed";
107         String artifactId = "ganymed-ssh2";
108         String version = "build210";
109         String type = "jar";
110         String path = "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar";
111 
112         assertLayout( path, groupId, artifactId, version, null, type );
113     }
114 
115     /**
116      * [MRM-432] Oddball version spec.
117      * Example of an oddball / unusual version spec.
118      * @throws LayoutException
119      */
120     public void testGoodButOddVersionSpecJavaxComm()
121         throws LayoutException
122     {
123         String groupId = "javax";
124         String artifactId = "comm";
125         String version = "3.0-u1";
126         String type = "jar";
127         String path = "javax/jars/comm-3.0-u1.jar";
128 
129         assertLayout( path, groupId, artifactId, version, null, type );
130     }
131 
132     /**
133      * [MRM-432] Oddball version spec.
134      * Example of an oddball / unusual version spec.
135      * @throws LayoutException
136      */
137     public void testGoodButOddVersionSpecJavaxPersistence()
138         throws LayoutException
139     {
140         String groupId = "javax.persistence";
141         String artifactId = "ejb";
142         String version = "3.0-public_review";
143         String type = "jar";
144         String path = "javax.persistence/jars/ejb-3.0-public_review.jar";
145 
146         /*
147          * The version id of "public_review" can cause problems. is it part of
148          * the version spec? or the classifier?
149          */
150 
151         assertLayout( path, groupId, artifactId, version, null, type );
152     }
153 
154     public void testGoodCommonsLang()
155         throws LayoutException
156     {
157         String groupId = "commons-lang";
158         String artifactId = "commons-lang";
159         String version = "2.1";
160         String type = "jar";
161         String path = "commons-lang/jars/commons-lang-2.1.jar";
162 
163         assertLayout( path, groupId, artifactId, version, null, type );
164     }
165 
166     public void testGoodDerby()
167         throws LayoutException
168     {
169         String groupId = "org.apache.derby";
170         String artifactId = "derby";
171         String version = "10.2.2.0";
172         String type = "jar";
173         String path = "org.apache.derby/jars/derby-10.2.2.0.jar";
174 
175         assertLayout( path, groupId, artifactId, version, null, type );
176     }
177 
178     /**
179      * Test the ejb-client type spec.
180      * Type specs are not a 1 to 1 map to the extension.
181      * This tests that effect.
182      * @throws LayoutException
183      */
184     /* TODO: Re-enabled in the future.
185     public void testGoodFooEjbClient()
186         throws LayoutException
187     {
188         String groupId = "com.foo";
189         String artifactId = "foo-client";
190         String version = "1.0";
191         String type = "ejb"; // oddball type-spec (should result in jar extension)
192         String path = "com.foo/ejbs/foo-client-1.0.jar";
193 
194         assertLayout( path, groupId, artifactId, version, classifier, type );
195     }
196     */
197 
198     /**
199      * Test the classifier.
200      * @throws LayoutException
201      */
202     public void testGoodFooLibJavadoc()
203         throws LayoutException
204     {
205         String groupId = "com.foo.lib";
206         String artifactId = "foo-lib";
207         String version = "2.1-alpha-1";
208         String type = "javadoc";
209         String classifier = "javadoc";
210         String path = "com.foo.lib/javadoc.jars/foo-lib-2.1-alpha-1-javadoc.jar";
211 
212         assertLayout( path, groupId, artifactId, version, classifier, type );
213 
214         assertLayout( "com.foo.lib/javadocs/foo-lib-2.1-alpha-1-javadoc.jar", "com.foo.lib", "foo-lib", "2.1-alpha-1", "javadoc", "javadoc" );
215     }
216 
217     /**
218      * Test the classifier, and java-source type spec.
219      * @throws LayoutException
220      */
221     public void testGoodFooLibSources()
222         throws LayoutException
223     {
224         String groupId = "com.foo.lib";
225         String artifactId = "foo-lib";
226         String version = "2.1-alpha-1";
227         String type = "java-source"; // oddball type-spec (should result in jar extension)
228         String classifier= "sources";
229         String path = "com.foo.lib/java-sources/foo-lib-2.1-alpha-1-sources.jar";
230 
231         assertLayout( path, groupId, artifactId, version, classifier, type );
232     }
233 
234     /**
235      * Test the classifier, and java-source type spec.
236      * @throws LayoutException
237      */
238     public void testBadClassifierFooLibSources()
239         throws LayoutException
240     {
241         assertBadPath( "com.foo.lib/java-sources/foo-lib-2.1-alpha-1.jar", "missing required classifier" );
242         assertBadPath( "com.foo.lib/java-sources/foo-lib-2.1-alpha-1-javadoc.jar", "incorrect classifier" );
243         assertBadPath( "com.foo.lib/java-sources/foo-lib-2.1-alpha-1-other.jar", "incorrect classifier" );
244     }
245 
246     /**
247      * Test the classifier, and java-source type spec.
248      * @throws LayoutException
249      */
250     public void testGoodFooLibTestSources()
251         throws LayoutException
252     {
253         String groupId = "com.foo.lib";
254         String artifactId = "foo-lib";
255         String version = "2.1-alpha-1-test-sources";
256         String type = "jar";
257         String classifier = null; // we can't parse this type of classifier in legacy format
258         String path = "com.foo.lib/jars/foo-lib-2.1-alpha-1-test-sources.jar";
259 
260         assertLayout( path, groupId, artifactId, version, classifier, type );
261     }
262 
263     public void testGoodFooTool()
264         throws LayoutException
265     {
266         String groupId = "com.foo";
267         String artifactId = "foo-tool";
268         String version = "1.0";
269         String type = "jar";
270         String path = "com.foo/jars/foo-tool-1.0.jar";
271 
272         assertLayout( path, groupId, artifactId, version, null, type );
273     }
274 
275     public void testGoodGeronimoEjbSpec()
276         throws LayoutException
277     {
278         String groupId = "org.apache.geronimo.specs";
279         String artifactId = "geronimo-ejb_2.1_spec";
280         String version = "1.0.1";
281         String type = "jar";
282         String path = "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar";
283 
284         assertLayout( path, groupId, artifactId, version, null, type );
285     }
286 
287     public void testGoodLdapClientsPom()
288         throws LayoutException
289     {
290         String groupId = "directory-clients";
291         String artifactId = "ldap-clients";
292         String version = "0.9.1-SNAPSHOT";
293         String type = "pom";
294         String path = "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom";
295 
296         assertLayout( path, groupId, artifactId, version, null, type );
297     }
298 
299     /**
300      * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
301      * @throws LayoutException
302      */
303     public void testGoodSnapshotMavenTest()
304         throws LayoutException
305     {
306         String groupId = "org.apache.archiva.test";
307         String artifactId = "redonkulous";
308         String version = "3.1-beta-1-20050831.101112-42";
309         String type = "jar";
310         String path = "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar";
311 
312         assertLayout( path, groupId, artifactId, version, null, type );
313     }
314 
315     /**
316      * [MRM-519] version identifiers within filename cause misidentification of version.
317      * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
318      */
319     public void testGoodVersionKeywordInArtifactId()
320         throws LayoutException
321     {
322         String groupId = "maven";
323         String artifactId = "maven-test-plugin";
324         String version = "1.8.2";
325         String type = "pom";
326 
327         String path = "maven/poms/maven-test-plugin-1.8.2.pom";
328 
329         assertLayout( path, groupId, artifactId, version, null, type );
330     }
331 
332     /**
333      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
334      * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
335      */
336     public void testGoodDetectPluginMavenTest()
337         throws LayoutException
338     {
339         String groupId = "maven";
340         String artifactId = "maven-test-plugin";
341         String version = "1.8.2";
342         String type = "maven-one-plugin";
343         String path = "maven/plugins/maven-test-plugin-1.8.2.jar";
344 
345         assertLayout( path, groupId, artifactId, version, null, type );
346     }
347 
348     /**
349      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
350      */
351     public void testGoodDetectPluginAvalonMeta()
352         throws LayoutException
353     {
354         String groupId = "avalon-meta";
355         String artifactId = "avalon-meta-plugin";
356         String version = "1.1";
357         String type = "maven-one-plugin";
358         String path = "avalon-meta/plugins/avalon-meta-plugin-1.1.jar";
359 
360         assertLayout( path, groupId, artifactId, version, null, type );
361     }
362 
363     /**
364      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
365      */
366     public void testGoodDetectPluginCactusMaven()
367         throws LayoutException
368     {
369         String groupId = "cactus";
370         String artifactId = "cactus-maven";
371         String version = "1.7dev-20040815";
372         String type = "maven-one-plugin";
373         String path = "cactus/plugins/cactus-maven-1.7dev-20040815.jar";
374 
375         assertLayout( path, groupId, artifactId, version, null, type );
376     }
377 
378     /**
379      * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
380      */
381     public void testGoodDetectPluginGeronimoPackaging()
382         throws LayoutException
383     {
384         String groupId = "geronimo";
385         String artifactId = "geronimo-packaging-plugin";
386         String version = "1.0.1";
387         String type = "maven-one-plugin";
388         String path = "geronimo/plugins/geronimo-packaging-plugin-1.0.1.jar";
389 
390         assertLayout( path, groupId, artifactId, version, null, type );
391     }
392 
393     /**
394      * [MRM-594] add some hook in LegacyPathParser to allow exceptions in artifact resolution
395 	 * @since 1.1
396      */
397     public void testCustomExceptionsInArtifactResolution()
398         throws LayoutException
399     {
400         String groupId = "jaxen";
401         String artifactId = "jaxen";
402         String version = "1.0-FCS";
403         String type = "jar";
404         String classifier = "full";
405         String path = "jaxen/jars/jaxen-1.0-FCS-full.jar";
406 
407         assertLayout( path, groupId, artifactId, version, classifier, type );
408     }
409 
410     /**
411      * Perform a path to artifact reference lookup, and verify the results.
412      */
413     private void assertLayout( String path, String groupId, String artifactId, String version, String classifier, String type )
414         throws LayoutException
415     {
416         // Path to Artifact Reference.
417         ArtifactReference testReference = parser.toArtifactReference( path );
418         assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
419     }
420 
421     private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
422                                           String version, String classifier, String type )
423     {
424         String expectedId = "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
425 
426         assertNotNull( expectedId + " - Should not be null.", actualReference );
427 
428         assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
429         assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
430         assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
431         assertEquals( expectedId + " - classifier", classifier, actualReference.getClassifier() );
432         assertEquals( expectedId + " - Type", type, actualReference.getType() );
433     }
434 
435     protected void assertBadPath( String path, String reason )
436     {
437         try
438         {
439             parser.toArtifactReference( path );
440             fail( "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
441         }
442         catch ( LayoutException e )
443         {
444             /* expected path */
445         }
446     }
447 }