package org.codehaus.plexus.archiver.jar;

import org.codehaus.plexus.archiver.ArchiverException;

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

public class JarArchiverTest
    extends TestCase
{

    public void testCreateManifestOnlyJar()
        throws IOException, ManifestException, ArchiverException
    {
        File jarFile = File.createTempFile( "JarArchiverTest.", ".jar" );
        jarFile.deleteOnExit();

        JarArchiver archiver = new JarArchiver();
        archiver.setDestFile( jarFile );

        Manifest manifest = new Manifest();
        Manifest.Attribute attribute = new Manifest.Attribute( "Main-Class", getClass().getName() );

        manifest.addConfiguredAttribute( attribute );

        archiver.addConfiguredManifest( manifest );

        archiver.createArchive();
    }

    public void testNonCompressed()
        throws IOException, ManifestException, ArchiverException
    {
        File jarFile = new File("target/output/jarArchiveNonCompressed.jar" );

        JarArchiver archiver = new JarArchiver();
        archiver.setDestFile( jarFile );
        archiver.setCompress( false );
        archiver.addDirectory( new File( "src/test/resources/mjar179" ) );
        archiver.createArchive();
    }

}

@@ -128,27 +128,85 @@
 java.io.
-IOException
+FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.util.Random
 ;%0A%0Aimpor
@@ -1329,10 +1329,1022 @@
 %0A    %7D%0A%0A
+    public void testVeryLargeJar()%0A        throws IOException, ManifestException, ArchiverException%0A    %7B%0A        // Generate some number of random files that is likely to be%0A        // two or three times the number of available file handles%0A        File tmpDir = File.createTempFile( %22veryLargeJar%22, null );%0A        tmpDir.delete();%0A        tmpDir.mkdirs();%0A        Random rand = new Random();%0A        for ( int i = 0; i %3C 15000; i++ )%0A        %7B%0A           File f = new File( tmpDir, %22file%22 + i );%0A           f.deleteOnExit();%0A           FileOutputStream out = new FileOutputStream(f);%0A           byte%5B%5D data = new byte%5B10240%5D; // 10kb per file%0A           rand.nextBytes( data );%0A           out.write( data );%0A           out.flush();%0A           out.close();%0A        %7D%0A%0A        File jarFile = new File( %22target/output/veryLargeJar.jar%22 );%0A%0A        JarArchiver archiver = new JarArchiver();%0A        archiver.setDestFile( jarFile );%0A        archiver.addDirectory( tmpDir );%0A        archiver.createArchive();%0A    %7D%0A
 %7D%0A

Test case that reproduces "too many open files" problem from #6.