Search in sources :

Example 56 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_getEntryLjava_lang_String.

/**
     * @throws IOException
     * java.util.jar.JarFile#getJarEntry(java.lang.String)
     */
public void test_getEntryLjava_lang_String() throws IOException {
    try {
        Support_Resources.copyFile(resources, null, jarName);
        JarFile jarFile = new JarFile(new File(resources, jarName));
        assertEquals("Error in returned entry", 311, jarFile.getEntry(entryName).getSize());
        jarFile.close();
    } catch (Exception e) {
        fail("Exception during test: " + e.toString());
    }
    Support_Resources.copyFile(resources, null, jarName);
    JarFile jarFile = new JarFile(new File(resources, jarName));
    Enumeration<JarEntry> enumeration = jarFile.entries();
    assertTrue(enumeration.hasMoreElements());
    while (enumeration.hasMoreElements()) {
        JarEntry je = enumeration.nextElement();
        jarFile.getEntry(je.getName());
    }
    enumeration = jarFile.entries();
    assertTrue(enumeration.hasMoreElements());
    JarEntry je = enumeration.nextElement();
    try {
        jarFile.close();
        jarFile.getEntry(je.getName());
    // fail("IllegalStateException expected.");
    } catch (IllegalStateException ee) {
    // Per documentation exception
    // may be thrown.
    // expected
    }
}
Also used : JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

Example 57 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_ConstructorLjava_io_File.

/**
     * java.util.jar.JarFile#JarFile(java.io.File)
     */
public void test_ConstructorLjava_io_File() {
    try {
        JarFile jarFile = new JarFile(new File("Wrong.file"));
        fail("Should throw IOException");
    } catch (IOException e) {
    // expected
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        JarFile jarFile = new JarFile(new File(resources, jarName));
    } catch (IOException e) {
        fail("Should not throw IOException");
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 58 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_ConstructorLjava_lang_StringZ.

/**
     * java.util.jar.JarFile#JarFile(java.lang.String, boolean)
     */
public void test_ConstructorLjava_lang_StringZ() {
    try {
        JarFile jarFile = new JarFile("Wrong.file", false);
        fail("Should throw IOException");
    } catch (IOException e) {
    // expected
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        String fileName = (new File(resources, jarName)).getCanonicalPath();
        JarFile jarFile = new JarFile(fileName, true);
    } catch (IOException e) {
        fail("Should not throw IOException");
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 59 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_getInputStreamLjava_util_jar_JarEntry.

/**
     * @throws IOException
     * java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry)
     */
public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
    File localFile = null;
    try {
        Support_Resources.copyFile(resources, null, jarName);
        localFile = new File(resources, jarName);
    } catch (Exception e) {
        fail("Failed to create local file: " + e);
    }
    byte[] b = new byte[1024];
    try {
        JarFile jf = new JarFile(localFile);
        java.io.InputStream is = jf.getInputStream(jf.getEntry(entryName));
        // BEGIN android-removed
        // jf.close();
        // END android-removed
        assertTrue("Returned invalid stream", is.available() > 0);
        int r = is.read(b, 0, 1024);
        is.close();
        StringBuffer sb = new StringBuffer(r);
        for (int i = 0; i < r; i++) {
            sb.append((char) (b[i] & 0xff));
        }
        String contents = sb.toString();
        assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
        // BEGIN android-added
        jf.close();
    // END android-added
    } catch (Exception e) {
        fail("Exception during test: " + e.toString());
    }
    try {
        JarFile jf = new JarFile(localFile);
        InputStream in = jf.getInputStream(new JarEntry("invalid"));
        assertNull("Got stream for non-existent entry", in);
    } catch (Exception e) {
        fail("Exception during test 2: " + e);
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        JarFile jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.getInputStream(jre);
    // InputStream returned in any way, exception can be thrown in case
    // of reading from this stream only.
    // fail("Should throw ZipException");
    } catch (ZipException ee) {
    // expected
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        JarFile jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.close();
        jf.getInputStream(jre);
        // InputStream returned in any way, exception can be thrown in case
        // of reading from this stream only.
        // The same for IOException
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException ee) {
    // expected
    }
}
Also used : InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream)

Example 60 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class DalvikExecTest method test_execExistingJar.

// Execute an existing JAR on dalvikvm using -classpath option.",
public void test_execExistingJar() throws IOException, InterruptedException {
    String res;
    File jarFile;
    if (System.getProperty("java.vendor").contains("Android")) {
        //
        // Test against Android:
        //
        File tempDir = Support_Resources.createTempFolder();
        jarFile = Support_Resources.copyFile(tempDir, null, "cts_dalvikExecTest.jar");
        res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld");
        assertEquals("Hello Android World!", "Hello Android World!\n", res);
        res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper");
        assertTrue("Android Resource Dumper started", res.contains("Android Resource Dumper started"));
        assertTrue("This Resource contains some text.", res.contains("This Resource contains some text."));
    } else {
    //
    // Test against RI:
    //
    // Do nothing!
    }
}
Also used : JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JarFile (java.util.jar.JarFile)509 File (java.io.File)289 JarEntry (java.util.jar.JarEntry)230 IOException (java.io.IOException)212 URL (java.net.URL)92 ZipEntry (java.util.zip.ZipEntry)92 InputStream (java.io.InputStream)90 Manifest (java.util.jar.Manifest)81 ZipFile (java.util.zip.ZipFile)74 FileOutputStream (java.io.FileOutputStream)71 Test (org.junit.Test)66 ArrayList (java.util.ArrayList)54 JarURLConnection (java.net.JarURLConnection)48 Attributes (java.util.jar.Attributes)43 JarOutputStream (java.util.jar.JarOutputStream)41 MalformedURLException (java.net.MalformedURLException)31 FileInputStream (java.io.FileInputStream)30 Support_PlatformFile (tests.support.Support_PlatformFile)26 Enumeration (java.util.Enumeration)24 HashSet (java.util.HashSet)20