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