View Javadoc
1   package org.codehaus.plexus.archiver.jar;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.IOException;
6   import java.util.Random;
7   import org.codehaus.plexus.archiver.ArchiverException;
8   import junit.framework.TestCase;
9   
10  public class JarArchiverTest
11      extends TestCase
12  {
13  
14      public void testCreateManifestOnlyJar()
15          throws IOException, ManifestException, ArchiverException
16      {
17          File jarFile = File.createTempFile( "JarArchiverTest.", ".jar" );
18          jarFile.deleteOnExit();
19  
20          JarArchiver archiver = new JarArchiver();
21          archiver.setDestFile( jarFile );
22  
23          Manifest manifest = new Manifest();
24          Manifest.Attribute attribute = new Manifest.Attribute( "Main-Class", getClass().getName() );
25  
26          manifest.addConfiguredAttribute( attribute );
27  
28          archiver.addConfiguredManifest( manifest );
29  
30          archiver.createArchive();
31      }
32  
33      public void testNonCompressed()
34          throws IOException, ManifestException, ArchiverException
35      {
36          File jarFile = new File( "target/output/jarArchiveNonCompressed.jar" );
37  
38          JarArchiver archiver = new JarArchiver();
39          archiver.setDestFile( jarFile );
40          archiver.setCompress( false );
41          archiver.addDirectory( new File( "src/test/resources/mjar179" ) );
42          archiver.createArchive();
43      }
44  
45      public void testVeryLargeJar()
46          throws IOException, ManifestException, ArchiverException
47      {
48          // Generate some number of random files that is likely to be
49          // two or three times the number of available file handles
50          File tmpDir = File.createTempFile( "veryLargeJar", null );
51          tmpDir.delete();
52          tmpDir.mkdirs();
53          Random rand = new Random();
54          for ( int i = 0; i < 45000; i++ )
55          {
56              File f = new File( tmpDir, "file" + i );
57              f.deleteOnExit();
58              FileOutputStream out = new FileOutputStream( f );
59              byte[] data = new byte[ 512 ]; // 512bytes per file
60              rand.nextBytes( data );
61              out.write( data );
62              out.flush();
63              out.close();
64          }
65  
66          File jarFile = new File( "target/output/veryLargeJar.jar" );
67  
68          JarArchiver archiver = new JarArchiver();
69          archiver.setDestFile( jarFile );
70          archiver.addDirectory( tmpDir );
71          archiver.createArchive();
72      }
73  
74  }