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