View Javadoc

1   package org.apache.maven.archiver;
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 java.io.File;
23  import java.net.URI;
24  import java.net.URL;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  import java.util.TreeSet;
34  import java.util.jar.Attributes;
35  import java.util.jar.JarFile;
36  
37  import junit.framework.TestCase;
38  
39  import org.apache.maven.artifact.Artifact;
40  import org.apache.maven.artifact.handler.ArtifactHandler;
41  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
42  import org.apache.maven.model.Build;
43  import org.apache.maven.model.Model;
44  import org.apache.maven.model.Organization;
45  import org.apache.maven.project.MavenProject;
46  import org.codehaus.plexus.archiver.jar.JarArchiver;
47  import org.codehaus.plexus.archiver.jar.Manifest;
48  import org.codehaus.plexus.util.FileUtils;
49  import org.codehaus.plexus.util.StringUtils;
50  
51  public class MavenArchiverTest
52      extends TestCase
53  {
54      static class ArtifactComparator
55          implements Comparator
56      {
57          public int compare( Object o1, Object o2 )
58          {
59              return ( (Artifact) o1 ).getArtifactId().compareTo( ( (Artifact) o2 ).getArtifactId() );
60          }
61  
62          public boolean equals( Object o )
63          {
64              return false;
65          }
66      }
67  
68      public void testGetManifestExtensionList()
69          throws Exception
70      {
71          MavenArchiver archiver = new MavenArchiver();
72  
73          Model model = new Model();
74          model.setArtifactId( "dummy" );
75  
76          MavenProject project = new MavenProject( model );
77          // we need to sort the artifacts for test purposes
78          Set artifacts = new TreeSet( new ArtifactComparator() );
79          project.setArtifacts( artifacts );
80  
81          // there should be a mock or a setter for this field.
82          ManifestConfiguration config = new ManifestConfiguration()
83          {
84              public boolean isAddExtensions()
85              {
86                  return true;
87              }
88          };
89  
90          Manifest manifest;
91  
92          manifest = archiver.getManifest( project, config );
93  
94          assertNotNull( manifest.getMainSection() );
95  
96          java.util.Enumeration enume = manifest.getSectionNames();
97          while ( enume.hasMoreElements() )
98          {
99              Manifest.Section section = manifest.getSection( enume.nextElement().toString() );
100             System.out.println( section + " " + section.getAttributeValue( "Extension-List" ) );
101         }
102 
103         assertEquals( null, manifest.getMainSection().getAttributeValue( "Extension-List" ) );
104 
105         MockArtifact artifact1 = new MockArtifact();
106         artifact1.setGroupId( "org.apache.dummy" );
107         artifact1.setArtifactId( "dummy1" );
108         artifact1.setVersion( "1.0" );
109         artifact1.setType( "dll" );
110         artifact1.setScope( "compile" );
111 
112         artifacts.add( artifact1 );
113 
114         manifest = archiver.getManifest( project, config );
115 
116         assertEquals( null, manifest.getMainSection().getAttributeValue( "Extension-List" ) );
117 
118         MockArtifact artifact2 = new MockArtifact();
119         artifact2.setGroupId( "org.apache.dummy" );
120         artifact2.setArtifactId( "dummy2" );
121         artifact2.setVersion( "1.0" );
122         artifact2.setType( "jar" );
123         artifact2.setScope( "compile" );
124 
125         artifacts.add( artifact2 );
126 
127         manifest = archiver.getManifest( project, config );
128 
129         assertEquals( "dummy2", manifest.getMainSection().getAttributeValue( "Extension-List" ) );
130 
131         MockArtifact artifact3 = new MockArtifact();
132         artifact3.setGroupId( "org.apache.dummy" );
133         artifact3.setArtifactId( "dummy3" );
134         artifact3.setVersion( "1.0" );
135         artifact3.setScope( "test" );
136         artifact3.setType( "jar" );
137 
138         artifacts.add( artifact3 );
139 
140         manifest = archiver.getManifest( project, config );
141 
142         assertEquals( "dummy2", manifest.getMainSection().getAttributeValue( "Extension-List" ) );
143 
144         MockArtifact artifact4 = new MockArtifact();
145         artifact4.setGroupId( "org.apache.dummy" );
146         artifact4.setArtifactId( "dummy4" );
147         artifact4.setVersion( "1.0" );
148         artifact4.setType( "jar" );
149         artifact4.setScope( "compile" );
150 
151         artifacts.add( artifact4 );
152 
153         manifest = archiver.getManifest( project, config );
154 
155         assertEquals( "dummy2 dummy4", manifest.getMainSection().getAttributeValue( "Extension-List" ) );
156     }
157 
158     public void testMultiClassPath()
159         throws Exception
160     {
161         final File tempFile = File.createTempFile( "maven-archiver-test-", ".jar" );
162 
163         try
164         {
165             MavenArchiver archiver = new MavenArchiver();
166 
167             Model model = new Model();
168             model.setArtifactId( "dummy" );
169 
170             MavenProject project = new MavenProject( model )
171             {
172                 public List getRuntimeClasspathElements()
173                 {
174                     return Collections.singletonList( tempFile.getAbsolutePath() );
175                 }
176             };
177 
178             // there should be a mock or a setter for this field.
179             ManifestConfiguration manifestConfig = new ManifestConfiguration()
180             {
181                 public boolean isAddClasspath()
182                 {
183                     return true;
184                 }
185             };
186 
187             MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration();
188             archiveConfiguration.setManifest( manifestConfig );
189             archiveConfiguration.addManifestEntry( "Class-Path", "help/" );
190 
191             Manifest manifest = archiver.getManifest( project, archiveConfiguration );
192             String classPath = manifest.getMainSection().getAttribute( "Class-Path" ).getValue();
193             assertTrue( "User specified Class-Path entry was not added to manifest", classPath.indexOf( "help/" ) != -1 );
194             assertTrue( "Class-Path generated by addClasspath was not added to manifest",
195                         classPath.indexOf( tempFile.getName() ) != -1 );
196         }
197         finally
198         {
199             tempFile.delete();
200         }
201 
202     }
203 
204     public void testRecreation()
205         throws Exception
206     {
207         File jarFile = new File( "target/test/dummy.jar" );
208         jarFile.delete();
209         assertFalse( jarFile.exists() );
210         JarArchiver jarArchiver = new JarArchiver();
211         jarArchiver.setDestFile( jarFile );
212 
213         MavenArchiver archiver = new MavenArchiver();
214         archiver.setArchiver( jarArchiver );
215         archiver.setOutputFile( jarArchiver.getDestFile() );
216 
217         MavenProject project = getDummyProject();
218 
219         MavenArchiveConfiguration config = new MavenArchiveConfiguration();
220         config.setForced( false );
221 
222         FileUtils.deleteDirectory( "target/maven-archiver" );
223         archiver.createArchive( project, config );
224         assertTrue( jarFile.exists() );
225         jarFile.setLastModified( System.currentTimeMillis() - 60000L );
226         long time = jarFile.lastModified();
227 
228         List files = FileUtils.getFiles( new File( "target/maven-archiver" ), "**/**", null, true );
229         for ( Iterator i = files.iterator(); i.hasNext(); )
230         {
231             File f = (File) i.next();
232             f.setLastModified( time );
233         }
234 
235         archiver.createArchive( project, config );
236         assertEquals( jarFile.lastModified(), time );
237 
238         config.setForced( true );
239         archiver.createArchive( project, config );
240         assertTrue( jarFile.lastModified() > time );
241     }
242 
243     public void testNotGenerateImplementationVersionForMANIFESTMF()
244         throws Exception
245     {
246         JarFile jar = null;
247         try
248         {
249             File jarFile = new File( "target/test/dummy.jar" );
250             jarFile.delete();
251             assertFalse( jarFile.exists() );
252             JarArchiver jarArchiver = new JarArchiver();
253             jarArchiver.setDestFile( jarFile );
254 
255             MavenArchiver archiver = new MavenArchiver();
256             archiver.setArchiver( jarArchiver );
257             archiver.setOutputFile( jarArchiver.getDestFile() );
258 
259             MavenProject project = getDummyProject();
260 
261             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
262             config.setForced( true );
263             config.getManifest().setAddDefaultImplementationEntries( false );
264             archiver.createArchive( project, config );
265             assertTrue( jarFile.exists() );
266 
267             jar = new JarFile( jarFile );
268             Map entries = jar.getManifest().getMainAttributes();
269             assertFalse( entries.containsKey( Attributes.Name.IMPLEMENTATION_VERSION ) ); // "Implementation-Version"
270         }
271         finally
272         {
273             // cleanup streams
274             if ( jar != null )
275             {
276                 jar.close();
277             }
278         }
279     }
280 
281     public void testGenerateImplementationVersionForMANIFESTMF()
282         throws Exception
283     {
284         JarFile jar = null;
285         try
286         {
287             File jarFile = new File( "target/test/dummy.jar" );
288             jarFile.delete();
289             assertFalse( jarFile.exists() );
290             JarArchiver jarArchiver = new JarArchiver();
291             jarArchiver.setDestFile( jarFile );
292 
293             MavenArchiver archiver = new MavenArchiver();
294             archiver.setArchiver( jarArchiver );
295             archiver.setOutputFile( jarArchiver.getDestFile() );
296 
297             MavenProject project = getDummyProject();
298             String ls = System.getProperty( "line.separator" );
299             project.setDescription( "foo " + ls + " bar " );
300             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
301             config.setForced( true );
302             config.getManifest().setAddDefaultImplementationEntries( true );
303             config.addManifestEntry( "Description", project.getDescription() );
304             archiver.createArchive( project, config );
305             assertTrue( jarFile.exists() );
306 
307             jar = new JarFile( jarFile );
308 
309             Map entries = jar.getManifest().getMainAttributes();
310 
311             assertTrue( entries.containsKey( Attributes.Name.IMPLEMENTATION_VERSION ) );
312             assertEquals( "0.1", entries.get( Attributes.Name.IMPLEMENTATION_VERSION ) );
313         }
314         finally
315         {
316             // cleanup streams
317             if ( jar != null )
318             {
319                 jar.close();
320             }
321         }
322     }
323 
324     public void testCarriageReturnInManifestEntry()
325         throws Exception
326     {
327         JarFile jar = null;
328         try
329         {
330             File jarFile = new File( "target/test/dummy.jar" );
331             jarFile.delete();
332             assertFalse( jarFile.exists() );
333             JarArchiver jarArchiver = new JarArchiver();
334             jarArchiver.setDestFile( jarFile );
335 
336             MavenArchiver archiver = new MavenArchiver();
337             archiver.setArchiver( jarArchiver );
338             archiver.setOutputFile( jarArchiver.getDestFile() );
339 
340             MavenProject project = getDummyProject();
341             String ls = System.getProperty( "line.separator" );
342             project.setDescription( "foo " + ls + " bar " );
343             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
344             config.setForced( true );
345             config.getManifest().setAddDefaultImplementationEntries( true );
346             config.addManifestEntry( "Description", project.getDescription() );
347             // config.addManifestEntry( "EntryWithTab", " foo tab " + ( '\u0009' ) + ( '\u0009' ) + " bar tab" + (
348             // '\u0009' ) );
349             archiver.createArchive( project, config );
350             assertTrue( jarFile.exists() );
351 
352             jar = new JarFile( jarFile );
353 
354             Attributes attributes = jar.getManifest().getMainAttributes();
355             assertTrue( project.getDescription().indexOf( ls ) > 0 );
356             Attributes.Name description = new Attributes.Name( "Description" );
357             String value = attributes.getValue( description );
358             assertNotNull( value );
359             assertFalse( value.indexOf( ls ) > 0 );
360         }
361         finally
362         {
363             // cleanup streams
364             if ( jar != null )
365             {
366                 jar.close();
367             }
368         }
369     }
370 
371     public void testManifestEntries()
372         throws Exception
373     {
374         JarFile jar = null;
375         try
376         {
377             File jarFile = new File( "target/test/dummy.jar" );
378             jarFile.delete();
379             assertFalse( jarFile.exists() );
380             JarArchiver jarArchiver = new JarArchiver();
381             jarArchiver.setDestFile( jarFile );
382 
383             MavenArchiver archiver = new MavenArchiver();
384             archiver.setArchiver( jarArchiver );
385             archiver.setOutputFile( jarArchiver.getDestFile() );
386 
387             MavenProject project = getDummyProject();
388             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
389             config.setForced( true );
390             config.getManifest().setAddDefaultImplementationEntries( true );
391             config.getManifest().setAddDefaultSpecificationEntries( true );
392 
393             Map manifestEntries = new HashMap();
394             manifestEntries.put( "foo", "bar" );
395             manifestEntries.put( "first-name", "olivier" );
396             manifestEntries.put( "keyWithEmptyValue", null );
397             config.setManifestEntries( manifestEntries );
398 
399             ManifestSection manifestSection = new ManifestSection();
400             manifestSection.setName( "UserSection" );
401             manifestSection.addManifestEntry( "key", "value" );
402             List manifestSections = new ArrayList();
403             manifestSections.add( manifestSection );
404             config.setManifestSections( manifestSections );
405             config.getManifest().setMainClass( "org.apache.maven.Foo" );
406             archiver.createArchive( project, config );
407             assertTrue( jarFile.exists() );
408             jar = new JarFile( jarFile );
409 
410             Attributes manifest = jar.getManifest().getMainAttributes();
411 
412             assertEquals( "Apache Maven", manifest.get( new Attributes.Name( "Created-By" ) ) );
413             assertEquals( "archiver test", manifest.get( Attributes.Name.SPECIFICATION_TITLE ) );// "Specification-Title"
414             // ) );
415             assertEquals( "0.1", manifest.get( Attributes.Name.SPECIFICATION_VERSION ) );// "Specification-Version" ) );
416             assertEquals( "Apache", manifest.get( Attributes.Name.SPECIFICATION_VENDOR ) );// "Specification-Vendor" )
417             // );
418 
419             assertEquals( "archiver test", manifest.get( Attributes.Name.IMPLEMENTATION_TITLE ) );// "Implementation-Title"
420             // ) );
421             assertEquals( "0.1", manifest.get( Attributes.Name.IMPLEMENTATION_VERSION ) );// "Implementation-Version" )
422             // );
423             assertEquals( "org.apache.dummy", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR_ID ) );// "Implementation-Vendor-Id"
424             // ) );
425             assertEquals( "Apache", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR ) );// "Implementation-Vendor" )
426             // );
427             assertEquals( "org.apache.maven.Foo", manifest.get( Attributes.Name.MAIN_CLASS ) );// "Main-Class" ) );
428 
429             assertEquals( "bar", manifest.get( new Attributes.Name( "foo" ) ) );
430             assertEquals( "olivier", manifest.get( new Attributes.Name( "first-name" ) ) );
431 
432             assertEquals( System.getProperty( "java.version" ), manifest.get( new Attributes.Name( "Build-Jdk" ) ) );
433             assertEquals( System.getProperty( "user.name" ), manifest.get( new Attributes.Name( "Built-By" ) ) );
434 
435             assertTrue( StringUtils.isEmpty( manifest.getValue( new Attributes.Name( "keyWithEmptyValue" ) ) ) );
436             assertTrue( manifest.containsKey( new Attributes.Name( "keyWithEmptyValue" ) ) );
437 
438             manifest = jar.getManifest().getAttributes( "UserSection" );
439 
440             assertEquals( "value", manifest.get( new Attributes.Name( "key" ) ) );
441 
442         }
443         finally
444         {
445             // cleanup streams
446             if ( jar != null )
447             {
448                 jar.close();
449             }
450         }
451     }
452 
453     /**
454      * Test to make sure that manifest sections are present in the manifest prior to the archive has been created.
455      */
456     public void testManifestSections()
457         throws Exception
458     {
459         MavenArchiver archiver = new MavenArchiver();
460 
461         MavenProject project = getDummyProject();
462         MavenArchiveConfiguration config = new MavenArchiveConfiguration();
463 
464         ManifestSection manifestSection = new ManifestSection();
465         manifestSection.setName( "SectionOne" );
466         manifestSection.addManifestEntry( "key", "value" );
467         List manifestSections = new ArrayList();
468         manifestSections.add( manifestSection );
469         config.setManifestSections( manifestSections );
470 
471         Manifest manifest = archiver.getManifest( project, config );
472 
473         Manifest.Section section = manifest.getSection( "SectionOne" );
474         assertNotNull( "The section is not present in the manifest as it should be.", section );
475 
476         Manifest.Attribute attribute = section.getAttribute( "key" );
477         assertNotNull( "The attribute we are looking for is not present in the section.", attribute );
478         assertEquals( "The value of the attribute is wrong.", "value", attribute.getValue() );
479     }
480 
481     public void testDefaultClassPathValue()
482         throws Exception
483     {
484         MavenProject project = getDummyProject();
485         JarFile jar = null;
486         try
487         {
488             File jarFile = new File( "target/test/dummy.jar" );
489             jarFile.delete();
490             assertFalse( jarFile.exists() );
491             JarArchiver jarArchiver = new JarArchiver();
492             jarArchiver.setDestFile( jarFile );
493 
494             MavenArchiver archiver = new MavenArchiver();
495             archiver.setArchiver( jarArchiver );
496             archiver.setOutputFile( jarArchiver.getDestFile() );
497 
498             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
499             config.setForced( true );
500             config.getManifest().setAddDefaultImplementationEntries( true );
501             config.getManifest().setAddDefaultSpecificationEntries( true );
502             config.getManifest().setMainClass( "org.apache.maven.Foo" );
503             config.getManifest().setAddClasspath( true );
504             archiver.createArchive( project, config );
505             assertTrue( jarFile.exists() );
506             jar = new JarFile( jarFile );
507 
508             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
509             assertNotNull( classPath );
510             String[] classPathEntries = StringUtils.split( classPath, " " );
511             assertEquals( "dummy1-1.0.jar", classPathEntries[0] );
512             assertEquals( "dummy2-1.5.jar", classPathEntries[1] );
513             assertEquals( "dummy3-2.0.jar", classPathEntries[2] );
514         }
515         finally
516         {
517             // cleanup streams
518             if ( jar != null )
519             {
520                 jar.close();
521             }
522         }
523     }
524 
525     public void testDefaultClassPathValue_WithSnapshot()
526         throws Exception
527     {
528         MavenProject project = getDummyProjectWithSnapshot();
529         JarFile jar = null;
530         try
531         {
532             File jarFile = new File( "target/test/dummy.jar" );
533             jarFile.delete();
534             assertFalse( jarFile.exists() );
535             JarArchiver jarArchiver = new JarArchiver();
536             jarArchiver.setDestFile( jarFile );
537 
538             MavenArchiver archiver = new MavenArchiver();
539             archiver.setArchiver( jarArchiver );
540             archiver.setOutputFile( jarArchiver.getDestFile() );
541 
542             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
543             config.setForced( true );
544             config.getManifest().setAddDefaultImplementationEntries( true );
545             config.getManifest().setAddDefaultSpecificationEntries( true );
546             config.getManifest().setMainClass( "org.apache.maven.Foo" );
547             config.getManifest().setAddClasspath( true );
548             archiver.createArchive( project, config );
549             assertTrue( jarFile.exists() );
550             jar = new JarFile( jarFile );
551 
552             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
553             assertNotNull( classPath );
554             String[] classPathEntries = StringUtils.split( classPath, " " );
555             assertEquals( "dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
556             assertEquals( "dummy2-1.5.jar", classPathEntries[1] );
557             assertEquals( "dummy3-2.0.jar", classPathEntries[2] );
558         }
559         finally
560         {
561             // cleanup streams
562             if ( jar != null )
563             {
564                 jar.close();
565             }
566         }
567     }
568 
569     public void testMavenRepoClassPathValue()
570         throws Exception
571     {
572         MavenProject project = getDummyProject();
573         JarFile jar = null;
574         try
575         {
576             File jarFile = new File( "target/test/dummy.jar" );
577             jarFile.delete();
578             assertFalse( jarFile.exists() );
579             JarArchiver jarArchiver = new JarArchiver();
580             jarArchiver.setDestFile( jarFile );
581 
582             MavenArchiver archiver = new MavenArchiver();
583             archiver.setArchiver( jarArchiver );
584             archiver.setOutputFile( jarArchiver.getDestFile() );
585 
586             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
587             config.setForced( true );
588             config.getManifest().setAddDefaultImplementationEntries( true );
589             config.getManifest().setAddDefaultSpecificationEntries( true );
590             config.getManifest().setMainClass( "org.apache.maven.Foo" );
591             config.getManifest().setAddClasspath( true );
592             config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY );
593             archiver.createArchive( project, config );
594             assertTrue( jarFile.exists() );
595             jar = new JarFile( jarFile );
596 
597             Manifest manifest = archiver.getManifest( project, config );
598             String[] classPathEntries =
599                 StringUtils.split(
600                                    new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ),
601                                    " " );
602             assertEquals( "org/apache/dummy/dummy1/1.0/dummy1-1.0.jar", classPathEntries[0] );
603             assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
604             assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
605 
606             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
607             assertNotNull( classPath );
608             classPathEntries = StringUtils.split( classPath, " " );
609             assertEquals( "org/apache/dummy/dummy1/1.0/dummy1-1.0.jar", classPathEntries[0] );
610             assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
611             assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
612 
613         }
614         finally
615         {
616             // cleanup streams
617             if ( jar != null )
618             {
619                 jar.close();
620             }
621         }
622     }
623 
624     public void testMavenRepoClassPathValue_WithSnapshot()
625         throws Exception
626     {
627         MavenProject project = getDummyProjectWithSnapshot();
628         JarFile jar = null;
629         try
630         {
631             File jarFile = new File( "target/test/dummy.jar" );
632             jarFile.delete();
633             assertFalse( jarFile.exists() );
634             JarArchiver jarArchiver = new JarArchiver();
635             jarArchiver.setDestFile( jarFile );
636 
637             MavenArchiver archiver = new MavenArchiver();
638             archiver.setArchiver( jarArchiver );
639             archiver.setOutputFile( jarArchiver.getDestFile() );
640 
641             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
642             config.setForced( true );
643             config.getManifest().setAddDefaultImplementationEntries( true );
644             config.getManifest().setAddDefaultSpecificationEntries( true );
645             config.getManifest().setMainClass( "org.apache.maven.Foo" );
646             config.getManifest().setAddClasspath( true );
647             config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY );
648             archiver.createArchive( project, config );
649             assertTrue( jarFile.exists() );
650             jar = new JarFile( jarFile );
651 
652             Manifest manifest = archiver.getManifest( project, config );
653             String[] classPathEntries =
654                 StringUtils.split(
655                                    new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ),
656                                    " " );
657             assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
658             assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
659             assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
660 
661             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
662             assertNotNull( classPath );
663             classPathEntries = StringUtils.split( classPath, " " );
664             assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
665             assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] );
666             assertEquals( "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar", classPathEntries[2] );
667 
668         }
669         finally
670         {
671             // cleanup streams
672             if ( jar != null )
673             {
674                 jar.close();
675             }
676         }
677     }
678 
679     public void testCustomClassPathValue()
680         throws Exception
681     {
682         MavenProject project = getDummyProject();
683         JarFile jar = null;
684         try
685         {
686             File jarFile = new File( "target/test/dummy.jar" );
687             jarFile.delete();
688             assertFalse( jarFile.exists() );
689             JarArchiver jarArchiver = new JarArchiver();
690             jarArchiver.setDestFile( jarFile );
691 
692             MavenArchiver archiver = new MavenArchiver();
693             archiver.setArchiver( jarArchiver );
694             archiver.setOutputFile( jarArchiver.getDestFile() );
695 
696             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
697             config.setForced( true );
698             config.getManifest().setAddDefaultImplementationEntries( true );
699             config.getManifest().setAddDefaultSpecificationEntries( true );
700             config.getManifest().setMainClass( "org.apache.maven.Foo" );
701             config.getManifest().setAddClasspath( true );
702             config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM );
703             config.getManifest().setCustomClasspathLayout(
704                                                            "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.version}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" );
705             archiver.createArchive( project, config );
706             assertTrue( jarFile.exists() );
707             jar = new JarFile( jarFile );
708 
709             Manifest manifest = archiver.getManifest( project, config );
710             String[] classPathEntries =
711                 StringUtils.split(
712                                    new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ),
713                                    " " );
714             assertEquals( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", classPathEntries[0] );
715             assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
716             assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
717 
718             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
719             assertNotNull( classPath );
720             classPathEntries = StringUtils.split( classPath, " " );
721             assertEquals( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", classPathEntries[0] );
722             assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
723             assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
724 
725         }
726         finally
727         {
728             // cleanup streams
729             if ( jar != null )
730             {
731                 jar.close();
732             }
733         }
734     }
735 
736     public void testCustomClassPathValue_WithSnapshotResolvedVersion()
737         throws Exception
738     {
739         MavenProject project = getDummyProjectWithSnapshot();
740         JarFile jar = null;
741         try
742         {
743             File jarFile = new File( "target/test/dummy.jar" );
744             jarFile.delete();
745             assertFalse( jarFile.exists() );
746             JarArchiver jarArchiver = new JarArchiver();
747             jarArchiver.setDestFile( jarFile );
748 
749             MavenArchiver archiver = new MavenArchiver();
750             archiver.setArchiver( jarArchiver );
751             archiver.setOutputFile( jarArchiver.getDestFile() );
752 
753             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
754             config.setForced( true );
755             config.getManifest().setAddDefaultImplementationEntries( true );
756             config.getManifest().setAddDefaultSpecificationEntries( true );
757             config.getManifest().setMainClass( "org.apache.maven.Foo" );
758             config.getManifest().setAddClasspath( true );
759             config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM );
760             config.getManifest().setCustomClasspathLayout(
761                                                            "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" );
762             archiver.createArchive( project, config );
763             assertTrue( jarFile.exists() );
764             jar = new JarFile( jarFile );
765 
766             Manifest manifest = archiver.getManifest( project, config );
767             String[] classPathEntries =
768                 StringUtils.split(
769                                    new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ),
770                                    " " );
771             assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
772             assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
773             assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
774 
775             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
776             assertNotNull( classPath );
777             classPathEntries = StringUtils.split( classPath, " " );
778             assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] );
779             assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
780             assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
781 
782         }
783         finally
784         {
785             // cleanup streams
786             if ( jar != null )
787             {
788                 jar.close();
789             }
790         }
791     }
792 
793     public void testCustomClassPathValue_WithSnapshotForcingBaseVersion()
794         throws Exception
795     {
796         MavenProject project = getDummyProjectWithSnapshot();
797         JarFile jar = null;
798         try
799         {
800             File jarFile = new File( "target/test/dummy.jar" );
801             jarFile.delete();
802             assertFalse( jarFile.exists() );
803             JarArchiver jarArchiver = new JarArchiver();
804             jarArchiver.setDestFile( jarFile );
805 
806             MavenArchiver archiver = new MavenArchiver();
807             archiver.setArchiver( jarArchiver );
808             archiver.setOutputFile( jarArchiver.getDestFile() );
809 
810             MavenArchiveConfiguration config = new MavenArchiveConfiguration();
811             config.setForced( true );
812             config.getManifest().setAddDefaultImplementationEntries( true );
813             config.getManifest().setAddDefaultSpecificationEntries( true );
814             config.getManifest().setMainClass( "org.apache.maven.Foo" );
815             config.getManifest().setAddClasspath( true );
816             config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM );
817             config.getManifest().setCustomClasspathLayout(
818                                                            "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}" );
819             archiver.createArchive( project, config );
820             assertTrue( jarFile.exists() );
821             jar = new JarFile( jarFile );
822 
823             Manifest manifest = archiver.getManifest( project, config );
824             String[] classPathEntries =
825                 StringUtils.split(
826                                    new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ),
827                                    " " );
828             assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar", classPathEntries[0] );
829             assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
830             assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
831 
832             String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH );
833             assertNotNull( classPath );
834             classPathEntries = StringUtils.split( classPath, " " );
835             assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar", classPathEntries[0] );
836             assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] );
837             assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] );
838 
839         }
840         finally
841         {
842             // cleanup streams
843             if ( jar != null )
844             {
845                 jar.close();
846             }
847         }
848     }
849 
850     // ----------------------------------------
851     // common methods for testing
852     // ----------------------------------------
853 
854     private MavenProject getDummyProject()
855     {
856         Model model = new Model();
857         model.setGroupId( "org.apache.dummy" );
858         model.setArtifactId( "dummy" );
859         model.setVersion( "0.1" );
860         MavenProject project = new MavenProject( model );
861 
862         project.setPluginArtifacts( Collections.EMPTY_SET );
863         project.setReportArtifacts( Collections.EMPTY_SET );
864         project.setExtensionArtifacts( Collections.EMPTY_SET );
865         project.setRemoteArtifactRepositories( Collections.EMPTY_LIST );
866         project.setPluginArtifactRepositories( Collections.EMPTY_LIST );
867 
868         File pomFile = new File( "src/test/resources/pom.xml" );
869         pomFile.setLastModified( System.currentTimeMillis() - 60000L );
870         project.setFile( pomFile );
871         Build build = new Build();
872         build.setDirectory( "target" );
873         build.setOutputDirectory( "target" );
874         project.setBuild( build );
875         project.setName( "archiver test" );
876         Organization organization = new Organization();
877         organization.setName( "Apache" );
878         project.setOrganization( organization );
879         MockArtifact artifact = new MockArtifact();
880         artifact.setGroupId( "org.apache.dummy" );
881         artifact.setArtifactId( "dummy" );
882         artifact.setVersion( "0.1" );
883         artifact.setType( "jar" );
884         artifact.setArtifactHandler( new DefaultArtifactHandler( "jar" ) );
885         project.setArtifact( artifact );
886 
887         ArtifactHandler artifactHandler = new ArtifactHandler()
888         {
889 
890             public String getClassifier()
891             {
892                 return null;
893             }
894 
895             public String getDirectory()
896             {
897                 return null;
898             }
899 
900             public String getExtension()
901             {
902                 return "jar";
903             }
904 
905             public String getLanguage()
906             {
907                 return null;
908             }
909 
910             public String getPackaging()
911             {
912                 return null;
913             }
914 
915             public boolean isAddedToClasspath()
916             {
917                 return true;
918             }
919 
920             public boolean isIncludesDependencies()
921             {
922                 return false;
923             }
924 
925         };
926 
927         Set artifacts = new TreeSet( new ArtifactComparator() );
928 
929         MockArtifact artifact1 = new MockArtifact();
930         artifact1.setGroupId( "org.apache.dummy" );
931         artifact1.setArtifactId( "dummy1" );
932         artifact1.setVersion( "1.0" );
933         artifact1.setType( "jar" );
934         artifact1.setScope( "runtime" );
935         artifact1.setFile( getClasspathFile( artifact1.getArtifactId() + "-" + artifact1.getVersion() + ".jar" ) );
936 
937         artifact1.setArtifactHandler( artifactHandler );
938 
939         artifacts.add( artifact1 );
940 
941         MockArtifact artifact2 = new MockArtifact();
942         artifact2.setGroupId( "org.apache.dummy.foo" );
943         artifact2.setArtifactId( "dummy2" );
944         artifact2.setVersion( "1.5" );
945         artifact2.setType( "jar" );
946         artifact2.setScope( "runtime" );
947         artifact2.setFile( getClasspathFile( artifact2.getArtifactId() + "-" + artifact2.getVersion() + ".jar" ) );
948 
949         artifact2.setArtifactHandler( artifactHandler );
950         artifacts.add( artifact2 );
951 
952         MockArtifact artifact3 = new MockArtifact();
953         artifact3.setGroupId( "org.apache.dummy.bar" );
954         artifact3.setArtifactId( "dummy3" );
955         artifact3.setVersion( "2.0" );
956         artifact3.setScope( "runtime" );
957         artifact3.setType( "jar" );
958         artifact3.setFile( getClasspathFile( artifact3.getArtifactId() + "-" + artifact3.getVersion() + ".jar" ) );
959         artifact3.setArtifactHandler( artifactHandler );
960         artifacts.add( artifact3 );
961 
962         project.setArtifacts( artifacts );
963 
964         return project;
965     }
966 
967     private MavenProject getDummyProjectWithSnapshot()
968     {
969         Model model = new Model();
970         model.setGroupId( "org.apache.dummy" );
971         model.setArtifactId( "dummy" );
972         model.setVersion( "0.1" );
973         MavenProject project = new MavenProject( model );
974 
975         project.setPluginArtifacts( Collections.EMPTY_SET );
976         project.setReportArtifacts( Collections.EMPTY_SET );
977         project.setExtensionArtifacts( Collections.EMPTY_SET );
978         project.setRemoteArtifactRepositories( Collections.EMPTY_LIST );
979         project.setPluginArtifactRepositories( Collections.EMPTY_LIST );
980 
981         File pomFile = new File( "src/test/resources/pom.xml" );
982         pomFile.setLastModified( System.currentTimeMillis() - 60000L );
983         project.setFile( pomFile );
984         Build build = new Build();
985         build.setDirectory( "target" );
986         build.setOutputDirectory( "target" );
987         project.setBuild( build );
988         project.setName( "archiver test" );
989         Organization organization = new Organization();
990         organization.setName( "Apache" );
991         project.setOrganization( organization );
992         MockArtifact artifact = new MockArtifact();
993         artifact.setGroupId( "org.apache.dummy" );
994         artifact.setArtifactId( "dummy" );
995         artifact.setVersion( "0.1" );
996         artifact.setType( "jar" );
997         artifact.setArtifactHandler( new DefaultArtifactHandler( "jar" ) );
998         project.setArtifact( artifact );
999 
1000         ArtifactHandler artifactHandler = new ArtifactHandler()
1001         {
1002 
1003             public String getClassifier()
1004             {
1005                 return null;
1006             }
1007 
1008             public String getDirectory()
1009             {
1010                 return null;
1011             }
1012 
1013             public String getExtension()
1014             {
1015                 return "jar";
1016             }
1017 
1018             public String getLanguage()
1019             {
1020                 return null;
1021             }
1022 
1023             public String getPackaging()
1024             {
1025                 return null;
1026             }
1027 
1028             public boolean isAddedToClasspath()
1029             {
1030                 return true;
1031             }
1032 
1033             public boolean isIncludesDependencies()
1034             {
1035                 return false;
1036             }
1037 
1038         };
1039 
1040         Set artifacts = new TreeSet( new ArtifactComparator() );
1041 
1042         MockArtifact artifact1 = new MockArtifact();
1043         artifact1.setGroupId( "org.apache.dummy" );
1044         artifact1.setArtifactId( "dummy1" );
1045         artifact1.setSnapshotVersion( "1.1-20081022.112233-1", "1.1-SNAPSHOT" );
1046         artifact1.setType( "jar" );
1047         artifact1.setScope( "runtime" );
1048         artifact1.setFile( getClasspathFile( artifact1.getArtifactId() + "-" + artifact1.getVersion() + ".jar" ) );
1049 
1050         artifact1.setArtifactHandler( artifactHandler );
1051 
1052         artifacts.add( artifact1 );
1053 
1054         MockArtifact artifact2 = new MockArtifact();
1055         artifact2.setGroupId( "org.apache.dummy.foo" );
1056         artifact2.setArtifactId( "dummy2" );
1057         artifact2.setVersion( "1.5" );
1058         artifact2.setType( "jar" );
1059         artifact2.setScope( "runtime" );
1060         artifact2.setFile( getClasspathFile( artifact2.getArtifactId() + "-" + artifact2.getVersion() + ".jar" ) );
1061 
1062         artifact2.setArtifactHandler( artifactHandler );
1063         artifacts.add( artifact2 );
1064 
1065         MockArtifact artifact3 = new MockArtifact();
1066         artifact3.setGroupId( "org.apache.dummy.bar" );
1067         artifact3.setArtifactId( "dummy3" );
1068         artifact3.setVersion( "2.0" );
1069         artifact3.setScope( "runtime" );
1070         artifact3.setType( "jar" );
1071         artifact3.setFile( getClasspathFile( artifact3.getArtifactId() + "-" + artifact3.getVersion() + ".jar" ) );
1072         artifact3.setArtifactHandler( artifactHandler );
1073         artifacts.add( artifact3 );
1074 
1075         project.setArtifacts( artifacts );
1076 
1077         return project;
1078     }
1079 
1080     private File getClasspathFile( String file )
1081     {
1082         URL resource = Thread.currentThread().getContextClassLoader().getResource( file );
1083         if ( resource == null )
1084         {
1085             fail( "Cannot retrieve java.net.URL for file: " + file + " on the current test classpath." );
1086         }
1087 
1088         URI uri = new File( resource.getPath() ).toURI().normalize();
1089         File result = new File( uri.getPath().replaceAll( "%20", " " ) );
1090 
1091         return result;
1092     }
1093 }