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