FileDocCategorySizeDatePackage
JarOutputStreamTest.javaAPI DocAndroid 1.5 API7547Wed May 06 22:41:02 BST 2009org.apache.harmony.archive.tests.java.util.jar

JarOutputStreamTest

public class JarOutputStreamTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
protected voidsetUp()

    
protected voidtearDown()

    
public voidtest_JarOutputStreamLjava_io_OutputStream()

        File fooJar = File.createTempFile("hyts_", ".jar");

        FileOutputStream fos = new FileOutputStream(fooJar);
        ZipEntry ze = new ZipEntry("Test");

        try {
            JarOutputStream joutFoo = new JarOutputStream(fos);
            joutFoo.putNextEntry(ze);
            joutFoo.write(33);
        } catch (IOException ee) {
            fail("IOException is not expected");
        }

        fos.close();
        fooJar.delete();
        try {
            JarOutputStream joutFoo = new JarOutputStream(fos);
            joutFoo.putNextEntry(ze);
            fail("IOException expected");
        } catch (IOException ee) {
            // expected
        }
    
public voidtest_JarOutputStreamLjava_io_OutputStreamLjava_util_jar_Manifest()

        File fooJar = File.createTempFile("hyts_", ".jar");
        File barZip = File.createTempFile("hyts_", ".zip");

        FileOutputStream fos = new FileOutputStream(fooJar);

        Manifest man = new Manifest();
        Attributes att = man.getMainAttributes();
        att.put(Attributes.Name.MANIFEST_VERSION, "1.0");
        att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo");
        att.put(Attributes.Name.CLASS_PATH, barZip.getName());

        fos.close();
        try {
            JarOutputStream joutFoo = new JarOutputStream(fos, man);
            fail("IOException expected");
        } catch (IOException ee) {
            // expected
        }
    
public voidtest_putNextEntryLjava_util_zip_ZipEntry()

tests
java.util.jar.JarOutputStream#putNextEntry(java.util.zip.ZipEntry)

        // testClass file`s actual extension is .class, since having .class
        // extension files in source dir causes
        // problems on eclipse, the extension is changed into .ser or it can be
        // anything. The file is being
        // read by inputstream and being written to other file,
        // as long as the content of the file is not changed, the extension does
        // not matter
        final String testClass = "hyts_mainClass.ser";
        final String entryName = "foo/bar/execjartest/MainClass.class";

        // test whether specifying the main class in the manifest
        // works using either /'s or .'s as a separator
        final String[] manifestMain = {
                "foo.bar.execjartest.MainClass",
                "foo/bar/execjartest/MainClass"};

        for (String element : manifestMain) {

            // create the manifest
            Manifest newman = new Manifest();
            Attributes att = newman.getMainAttributes();
            att.put(Attributes.Name.MANIFEST_VERSION, "1.0");
            att.put(Attributes.Name.MAIN_CLASS, element);

            File outputJar = null;
            JarOutputStream jout = null;

            try {
                // open the output jarfile
                outputJar = File.createTempFile("hyts_", ".jar");
                jout = new JarOutputStream(new FileOutputStream(outputJar),
                        newman);
                jout.putNextEntry(new JarEntry(entryName));
            } catch (Exception e) {
                fail("Error creating JarOutputStream: " + e);
            }
            File resources = Support_Resources.createTempFolder();
            try {
                // read in the class file, and output it to the jar
                Support_Resources.copyFile(resources, null, testClass);
                URL jarURL = new URL((new File(resources, testClass)).toURL()
                        .toString());
                InputStream jis = jarURL.openStream();

                byte[] bytes = new byte[1024];
                int len;
                while ((len = jis.read(bytes)) != -1) {
                    jout.write(bytes, 0, len);
                }

                jout.flush();
                jout.close();
                jis.close();
            } catch (Exception e) {
                fail("Error writing JAR file for testing: " + e);
            }
            String res = null;
            // set up the VM parameters
            String[] args = new String[2];
            args[0] = "-jar";
            args[1] = outputJar.getAbsolutePath();

// It's not that simple to execute a JAR against Dalvik VM (see DalvikExecTest):
//
//            try {
//                // execute the JAR and read the result
//                res = Support_Exec.execJava(args, null, true);
//            } catch (Exception e) {
//                fail("Exception executing test JAR: " + e);
//            }
//
//            assertTrue("Error executing JAR test on: " + element
//                    + ". Result returned was incorrect.", res
//                    .startsWith("TEST"));
            outputJar.delete();

            try {
                // open the output jarfile
                outputJar = File.createTempFile("hyts_", ".jar");
                OutputStream os = new FileOutputStream(outputJar);
                jout = new JarOutputStream(os, newman);
                os.close();
                jout.putNextEntry(new JarEntry(entryName));
                fail("IOException expected");
            } catch (IOException e) {
                // expected
            }
        }