]> source.dussan.org Git - archiva.git/blob
cb5bd5533826c2f7d46bd532aececd86dcc60115
[archiva.git] /
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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.model.ArtifactReference;
26 import org.apache.archiva.repository.ManagedRepositoryContent;
27 import org.apache.archiva.repository.content.legacy.LegacyPathParser;
28 import org.apache.archiva.repository.layout.LayoutException;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.lang.StringUtils;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.test.context.ContextConfiguration;
36
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.io.File;
40
41 import static org.junit.Assert.*;
42
43 /**
44  * RepositoryRequestTest
45  */
46 @RunWith ( ArchivaSpringJUnit4ClassRunner.class )
47 @ContextConfiguration (
48     { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-repo-request-test.xml" } )
49 public class RepositoryRequestTest
50 {
51
52     @Inject
53     protected ApplicationContext applicationContext;
54
55     @Inject
56     @Named ( "archivaConfiguration#repo-request-test" )
57     private ArchivaConfiguration archivaConfiguration;
58
59     private RepositoryRequest repoRequest;
60
61     @Before
62     public void setUp()
63         throws Exception
64     {
65         LegacyPathParser legacyPathParser = new LegacyPathParser( archivaConfiguration );
66         repoRequest = new RepositoryRequest( legacyPathParser );
67     }
68
69     @Test
70     public void testInvalidRequestEmptyPath()
71     {
72         assertInvalidRequest( "" );
73     }
74
75     @Test
76     public void testInvalidRequestSlashOnly()
77     {
78         assertInvalidRequest( "//" );
79     }
80
81     @Test
82     public void testInvalidRequestNoArtifactId()
83     {
84         assertInvalidRequest( "groupId/jars/-1.0.jar" );
85     }
86
87     @Test
88     public void testInvalidLegacyRequestBadLocation()
89     {
90         assertInvalidRequest( "org.apache.maven.test/jars/artifactId-1.0.war" );
91     }
92
93     @Test
94     public void testInvalidRequestTooShort()
95     {
96         assertInvalidRequest( "org.apache.maven.test/artifactId-2.0.jar" );
97     }
98
99     @Test
100     public void testInvalidDefaultRequestBadLocation()
101     {
102         assertInvalidRequest( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
103     }
104
105     @Test
106     public void testValidLegacyGanymed()
107         throws Exception
108     {
109         assertValid( "ch.ethz.ganymed/jars/ganymed-ssh2-build210.jar", "ch.ethz.ganymed", "ganymed-ssh2", "build210",
110                      null, "jar" );
111     }
112
113     @Test
114     public void testValidDefaultGanymed()
115         throws Exception
116     {
117         assertValid( "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar", "ch.ethz.ganymed",
118                      "ganymed-ssh2", "build210", null, "jar" );
119     }
120
121     @Test
122     public void testValidLegacyJavaxComm()
123         throws Exception
124     {
125         assertValid( "javax/jars/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
126     }
127
128     @Test
129     public void testValidDefaultJavaxComm()
130         throws Exception
131     {
132         assertValid( "javax/comm/3.0-u1/comm-3.0-u1.jar", "javax", "comm", "3.0-u1", null, "jar" );
133     }
134
135     @Test
136     public void testValidLegacyJavaxPersistence()
137         throws Exception
138     {
139         assertValid( "javax.persistence/jars/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
140                      "3.0-public_review", null, "jar" );
141     }
142
143     @Test
144     public void testValidDefaultJavaxPersistence()
145         throws Exception
146     {
147         assertValid( "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar", "javax.persistence", "ejb",
148                      "3.0-public_review", null, "jar" );
149     }
150
151     @Test
152     public void testValidLegacyMavenTestPlugin()
153         throws Exception
154     {
155         assertValid( "maven/jars/maven-test-plugin-1.8.2.jar", "maven", "maven-test-plugin", "1.8.2", null, "jar" );
156     }
157
158     @Test
159     public void testValidDefaultMavenTestPlugin()
160         throws Exception
161     {
162         assertValid( "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom", "maven", "maven-test-plugin", "1.8.2",
163                      null, "pom" );
164     }
165
166     @Test
167     public void testValidLegacyCommonsLangJavadoc()
168         throws Exception
169     {
170         assertValid( "commons-lang/javadoc.jars/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang", "2.1",
171                      "javadoc", "javadoc" );
172     }
173
174     @Test
175     public void testValidDefaultCommonsLangJavadoc()
176         throws Exception
177     {
178         assertValid( "commons-lang/commons-lang/2.1/commons-lang-2.1-javadoc.jar", "commons-lang", "commons-lang",
179                      "2.1", "javadoc", "javadoc" );
180     }
181
182     @Test
183     public void testValidLegacyDerbyPom()
184         throws Exception
185     {
186         assertValid( "org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null, "pom" );
187         // Starting slash should not prevent detection.
188         assertValid( "/org.apache.derby/poms/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0", null,
189                      "pom" );
190     }
191
192     @Test
193     public void testValidDefaultDerbyPom()
194         throws Exception
195     {
196         assertValid( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", "org.apache.derby", "derby", "10.2.2.0",
197                      null, "pom" );
198     }
199
200     @Test
201     public void testValidLegacyGeronimoEjbSpec()
202         throws Exception
203     {
204         assertValid( "org.apache.geronimo.specs/jars/geronimo-ejb_2.1_spec-1.0.1.jar", "org.apache.geronimo.specs",
205                      "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
206     }
207
208     @Test
209     public void testValidDefaultGeronimoEjbSpec()
210         throws Exception
211     {
212         assertValid( "org/apache/geronimo/specs/geronimo-ejb_2.1_spec/1.0.1/geronimo-ejb_2.1_spec-1.0.1.jar",
213                      "org.apache.geronimo.specs", "geronimo-ejb_2.1_spec", "1.0.1", null, "jar" );
214     }
215
216     @Test
217     public void testValidLegacyLdapSnapshot()
218         throws Exception
219     {
220         assertValid( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom", "directory-clients", "ldap-clients",
221                      "0.9.1-SNAPSHOT", null, "pom" );
222     }
223
224     @Test
225     public void testValidDefaultLdapSnapshot()
226         throws Exception
227     {
228         assertValid( "directory-clients/ldap-clients/0.9.1-SNAPSHOT/ldap-clients-0.9.1-SNAPSHOT.pom",
229                      "directory-clients", "ldap-clients", "0.9.1-SNAPSHOT", null, "pom" );
230     }
231
232     @Test
233     public void testValidLegacyTestArchSnapshot()
234         throws Exception
235     {
236         assertValid( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch", "test-arch",
237                      "2.0.3-SNAPSHOT", null, "pom" );
238     }
239
240     @Test
241     public void testValidDefaultTestArchSnapshot()
242         throws Exception
243     {
244         assertValid( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom", "test.maven-arch",
245                      "test-arch", "2.0.3-SNAPSHOT", null, "pom" );
246     }
247
248     @Test
249     public void testValidLegacyOddDottedArtifactId()
250         throws Exception
251     {
252         assertValid( "com.company.department/poms/com.company.department.project-0.2.pom", "com.company.department",
253                      "com.company.department.project", "0.2", null, "pom" );
254     }
255
256     @Test
257     public void testValidDefaultOddDottedArtifactId()
258         throws Exception
259     {
260         assertValid( "com/company/department/com.company.department.project/0.2/com.company.department.project-0.2.pom",
261                      "com.company.department", "com.company.department.project", "0.2", null, "pom" );
262     }
263
264     @Test
265     public void testValidLegacyTimestampedSnapshot()
266         throws Exception
267     {
268         assertValid( "org.apache.archiva.test/jars/redonkulous-3.1-beta-1-20050831.101112-42.jar",
269                      "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
270     }
271
272     @Test
273     public void testValidDefaultTimestampedSnapshot()
274         throws Exception
275     {
276         assertValid(
277             "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar",
278             "org.apache.archiva.test", "redonkulous", "3.1-beta-1-20050831.101112-42", null, "jar" );
279     }
280
281     @Test
282     public void testIsSupportFile()
283     {
284         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.sha1" ) );
285         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.md5" ) );
286         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.asc" ) );
287         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
288         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
289         assertTrue( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
290
291         assertFalse( repoRequest.isSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
292         assertFalse(
293             repoRequest.isSupportFile( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
294         assertFalse( repoRequest.isSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
295         assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
296         assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
297         assertFalse( repoRequest.isSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
298     }
299
300     @Test
301     public void testIsMetadata()
302     {
303         assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
304         assertTrue( repoRequest.isMetadata( "org/apache/derby/derby/maven-metadata.xml" ) );
305
306         assertFalse( repoRequest.isMetadata( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
307         assertFalse(
308             repoRequest.isMetadata( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
309         assertFalse( repoRequest.isMetadata( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
310         assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
311         assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
312         assertFalse( repoRequest.isMetadata( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
313     }
314
315     @Test
316     public void testIsMetadataSupportFile()
317     {
318         assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml" ) );
319         assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml" ) );
320         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.sha1" ) );
321         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/maven-metadata.xml.md5" ) );
322
323         assertFalse( repoRequest.isMetadataSupportFile( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
324         assertFalse( repoRequest.isMetadataSupportFile(
325             "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
326         assertFalse(
327             repoRequest.isMetadataSupportFile( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
328         assertFalse( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
329         assertFalse(
330             repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
331         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
332         assertTrue( repoRequest.isMetadataSupportFile( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.md5" ) );
333     }
334
335     @Test
336     public void testIsDefault()
337     {
338         assertFalse( repoRequest.isDefault( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
339         assertFalse( repoRequest.isDefault( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
340         assertFalse( repoRequest.isDefault( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
341
342         assertTrue( repoRequest.isDefault( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
343         assertTrue( repoRequest.isDefault( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
344         assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
345         assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
346         assertTrue( repoRequest.isDefault( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
347         assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml" ) );
348         assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.sha1" ) );
349         assertTrue( repoRequest.isDefault( "eclipse/jdtcore/maven-metadata.xml.md5" ) );
350
351         assertFalse( repoRequest.isDefault( null ) );
352         assertFalse( repoRequest.isDefault( "" ) );
353         assertFalse( repoRequest.isDefault( "foo" ) );
354         assertFalse( repoRequest.isDefault( "some.short/path" ) );
355     }
356
357     @Test
358     public void testIsLegacy()
359     {
360         assertTrue( repoRequest.isLegacy( "test.maven-arch/poms/test-arch-2.0.3-SNAPSHOT.pom" ) );
361         assertTrue( repoRequest.isLegacy( "directory-clients/poms/ldap-clients-0.9.1-SNAPSHOT.pom" ) );
362         assertTrue( repoRequest.isLegacy( "commons-lang/jars/commons-lang-2.1-javadoc.jar" ) );
363
364         assertFalse( repoRequest.isLegacy( "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.jar" ) );
365         assertFalse( repoRequest.isLegacy( "org/apache/archiva/archiva-api/1.0/archiva-api-1.0.xml.zip" ) );
366         assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz" ) );
367         assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0-bin.tar.gz.pgp" ) );
368         assertFalse( repoRequest.isLegacy( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1" ) );
369
370         assertFalse( repoRequest.isLegacy( null ) );
371         assertFalse( repoRequest.isLegacy( "" ) );
372         assertFalse( repoRequest.isLegacy( "some.short/path" ) );
373     }
374
375     private ManagedRepositoryContent createManagedRepo( String layout )
376         throws Exception
377     {
378         File repoRoot = new File( FileUtil.getBasedir() + "/target/test-repo" );
379         return createManagedRepositoryContent( "test-internal", "Internal Test Repo", repoRoot, layout );
380     }
381
382     /**
383      * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
384      */
385     @Test
386     public void testToNativePathArtifactDefaultToDefaultDualExtension()
387         throws Exception
388     {
389         ManagedRepositoryContent repository = createManagedRepo( "default" );
390
391         // Test (artifact) default to default - dual extension
392         assertEquals( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
393                       repoRequest.toNativePath( "org/project/example-presentation/3.2/example-presentation-3.2.xml.zip",
394                                                 repository ) );
395     }
396
397     /**
398      * [MRM-481] Artifact requests with a .xml.zip extension fail with a 404 Error
399      */
400     @Test
401     public void testToNativePathArtifactLegacyToDefaultDualExtension()
402         throws Exception
403     {
404         ManagedRepositoryContent repository = createManagedRepo( "default" );
405
406         // TODO: this is a good test case for maven 1 -> maven 2 link, since m2 doesn't support the distribution-zip type
407
408         // Test (artifact) legacy to default - dual extension
409         // NOTE: The detection of a dual extension is flawed.
410         assertEquals( "org/project/example-presentation/3.2.xml/example-presentation-3.2.xml.zip",
411                       repoRequest.toNativePath( "org.project/zips/example-presentation-3.2.xml.zip", repository ) );
412     }
413
414     @Test
415     public void testToNativePathMetadataDefaultToDefault()
416         throws Exception
417     {
418         ManagedRepositoryContent repository = createManagedRepo( "default" );
419
420         // Test (metadata) default to default
421         assertEquals( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
422                       repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml.sha1",
423                                                 repository ) );
424     }
425
426     @Test
427     public void testNativePathPomLegacyToDefault()
428         throws Exception
429     {
430         ManagedRepositoryContent repository = createManagedRepo( "default" );
431
432         // Test (pom) legacy to default
433         assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom",
434                       repoRequest.toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) );
435     }
436
437     @Test
438     public void testNativePathPomLegacyToLegacy()
439         throws Exception
440     {
441         ManagedRepositoryContent repository = createManagedRepo( "legacy" );
442
443         // Test (pom) legacy to default
444         assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom",
445                       repoRequest.toNativePath( "org.apache.derby/poms/derby-10.2.2.0.pom", repository ) );
446     }
447
448     @Test
449     public void testNativePathPomLegacyToDefaultEjb()
450         throws Exception
451     {
452         ManagedRepositoryContent repository = createManagedRepo( "default" );
453
454         // Test (pom) legacy to default
455         String result = repoRequest.toNativePath( "mygroup/ejbs/myejb-1.0.jar", repository );
456         assertEquals( "mygroup/myejb/1.0/myejb-1.0.jar", result );
457     }
458
459     @Test
460     public void testNativePathPomLegacyToLegacyEjb()
461         throws Exception
462     {
463         ManagedRepositoryContent repository = createManagedRepo( "legacy" );
464
465         // Test (pom) legacy to default
466         assertEquals( "mygroup/ejbs/myejb-1.0.jar",
467                       repoRequest.toNativePath( "mygroup/ejbs/myejb-1.0.jar", repository ) );
468     }
469
470     @Test
471     public void testNativePathPomLegacyToLegacyStrutsModule()
472         throws Exception
473     {
474         ManagedRepositoryContent repository = createManagedRepo( "legacy" );
475
476         // Test (pom) legacy to default
477         assertEquals( "WebPortal/struts-modules/eventsDB-1.2.3.struts-module",
478                       repoRequest.toNativePath( "WebPortal/struts-modules/eventsDB-1.2.3.struts-module", repository ) );
479     }
480
481     @Test
482     public void testNativePathSupportFileLegacyToDefault()
483         throws Exception
484     {
485         ManagedRepositoryContent repository = createManagedRepo( "default" );
486
487         // Test (supportfile) legacy to default
488         assertEquals( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1",
489                       repoRequest.toNativePath( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1", repository ) );
490     }
491
492     @Test
493     public void testNativePathBadRequestTooShort()
494         throws Exception
495     {
496         ManagedRepositoryContent repository = createManagedRepo( "default" );
497
498         // Test bad request path (too short)
499         try
500         {
501             repoRequest.toNativePath( "org.apache.derby/license.txt", repository );
502             fail( "Should have thrown an exception about a too short path." );
503         }
504         catch ( LayoutException e )
505         {
506             // expected path.
507         }
508     }
509
510     @Test
511     public void testNativePathBadRequestBlank()
512         throws Exception
513     {
514         ManagedRepositoryContent repository = createManagedRepo( "default" );
515
516         // Test bad request path (too short)
517         try
518         {
519             repoRequest.toNativePath( "", repository );
520             fail( "Should have thrown an exception about an blank request." );
521         }
522         catch ( LayoutException e )
523         {
524             // expected path.
525         }
526     }
527
528     @Test
529     public void testNativePathBadRequestNull()
530         throws Exception
531     {
532         ManagedRepositoryContent repository = createManagedRepo( "default" );
533
534         // Test bad request path (too short)
535         try
536         {
537             repoRequest.toNativePath( null, repository );
538             fail( "Should have thrown an exception about an null request." );
539         }
540         catch ( LayoutException e )
541         {
542             // expected path.
543         }
544     }
545
546     @Test
547     public void testNativePathBadRequestUnknownType()
548         throws Exception
549     {
550         ManagedRepositoryContent repository = createManagedRepo( "default" );
551
552         // Test bad request path (too short)
553         try
554         {
555             repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/license.txt", repository );
556             fail( "Should have thrown an exception about an invalid type." );
557         }
558         catch ( LayoutException e )
559         {
560             // expected path.
561         }
562     }
563
564     @Test
565     public void testToNativePathLegacyMetadataDefaultToLegacy()
566         throws Exception
567     {
568         ManagedRepositoryContent repository = createManagedRepo( "legacy" );
569
570         // Test (metadata) default to legacy
571
572         // Special Case: This direction is not supported, should throw a LayoutException.
573         try
574         {
575             repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/maven-metadata.xml", repository );
576             fail( "Should have thrown a LayoutException, can't translate a maven-metadata.xml to a legacy layout." );
577         }
578         catch ( LayoutException e )
579         {
580             // expected path.
581         }
582     }
583
584     @Test
585     public void testNativePathPomDefaultToLegacy()
586         throws Exception
587     {
588         ManagedRepositoryContent repository = createManagedRepo( "legacy" );
589
590         // Test (pom) default to legacy
591         assertEquals( "org.apache.derby/poms/derby-10.2.2.0.pom",
592                       repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.pom", repository ) );
593     }
594
595     @Test
596     public void testNativePathSupportFileDefaultToLegacy()
597         throws Exception
598     {
599         ManagedRepositoryContent repository = createManagedRepo( "legacy" );
600
601         // Test (supportfile) default to legacy
602         assertEquals( "org.apache.derby/jars/derby-10.2.2.0.jar.sha1",
603                       repoRequest.toNativePath( "org/apache/derby/derby/10.2.2.0/derby-10.2.2.0.jar.sha1",
604                                                 repository ) );
605     }
606
607     private void assertValid( String path, String groupId, String artifactId, String version, String classifier,
608                               String type )
609         throws Exception
610     {
611         String expectedId =
612             "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + ( classifier != null ?
613                 classifier + ":" : "" ) + type;
614
615         ArtifactReference reference = repoRequest.toArtifactReference( path );
616
617         assertNotNull( expectedId + " - Should not be null.", reference );
618
619         assertEquals( expectedId + " - Group ID", groupId, reference.getGroupId() );
620         assertEquals( expectedId + " - Artifact ID", artifactId, reference.getArtifactId() );
621         if ( StringUtils.isNotBlank( classifier ) )
622         {
623             assertEquals( expectedId + " - Classifier", classifier, reference.getClassifier() );
624         }
625         assertEquals( expectedId + " - Version ID", version, reference.getVersion() );
626         assertEquals( expectedId + " - Type", type, reference.getType() );
627     }
628
629     private void assertInvalidRequest( String path )
630     {
631         try
632         {
633             repoRequest.toArtifactReference( path );
634             fail( "Expected a LayoutException on an invalid path [" + path + "]" );
635         }
636         catch ( LayoutException e )
637         {
638             /* expected path */
639         }
640     }
641
642     protected ManagedRepositoryContent createManagedRepositoryContent( String id, String name, File location,
643                                                                        String layout )
644         throws Exception
645     {
646         ManagedRepository repo = new ManagedRepository();
647         repo.setId( id );
648         repo.setName( name );
649         repo.setLocation( location.getAbsolutePath() );
650         repo.setLayout( layout );
651
652         ManagedRepositoryContent repoContent =
653             applicationContext.getBean( "managedRepositoryContent#" + layout, ManagedRepositoryContent.class );
654         repoContent.setRepository( repo );
655
656         return repoContent;
657     }
658
659 }