Search in sources :

Example 61 with JarFile

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

the class DalvikExecTest method test_execCreatedJarWithManifest.

/**
     * This test does quite the same as test_execCreatedJar, but includes a manifest.
     * Note however that the Dalvik JAR format does not require this manifest.
     * We just test whether the manifest is placed correctly within the JAR by
     * dumping its contents read as a simple text resource.
     * No! We can't do that so easily either, as there are other (parent) JARs
     * with a manifest inside, taken with precedence.
     * So we will reopen the JAR as a JarFile and check the manifest
     *  with a top level end-to-end approach.
     */
public void test_execCreatedJarWithManifest() throws IOException, InterruptedException {
    File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar");
    jarFile.deleteOnExit();
    // Create the manifest:
    Manifest manifest = new Manifest();
    Attributes attrs = manifest.getMainAttributes();
    attrs.put(Attributes.Name.MANIFEST_VERSION, "3.1415962");
    attrs.put(Attributes.Name.MAIN_CLASS, "dalvikExecTest.HelloWorld");
    attrs.put(Attributes.Name.CLASS_PATH, jarFile.getName());
    // Create a JAR output stream on the temp file using the manifest:
    JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile), manifest);
    // Define the entry for the classes.dex:
    jarOut.putNextEntry(new JarEntry("classes.dex"));
    // Fill in the classes.dex contents, i.e. the Dalvik executable code:
    // (See below for the detailed source code contents.)
    Streams.copy(Support_Resources.getResourceStream("cts_dalvikExecTest_classes.dex"), jarOut);
    // Now add a resource file:
    //
    jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource"));
    jarOut.write("This Resource contains some text.".getBytes());
    // Close the stream to the completed JAR file.
    jarOut.close();
    // The resulting JAR file contains the classes listed at the end of this text,
    // like the 'cts_dalvikExecTest.jar' as part of the resources, too.
    String res;
    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."));
    // And now reread the manifest:
    //
    JarFile jarIn = new JarFile(jarFile);
    manifest = jarIn.getManifest();
    attrs = manifest.getMainAttributes();
    assertEquals("MANIFEST_VERSION must match!", "3.1415962", attrs.get(Attributes.Name.MANIFEST_VERSION));
    assertEquals("MAIN_CLASS must match!", "dalvikExecTest.HelloWorld", attrs.get(Attributes.Name.MAIN_CLASS));
    assertEquals("CLASS_PATH must match!", jarFile.getName(), attrs.get(Attributes.Name.CLASS_PATH));
}
Also used : FileOutputStream(java.io.FileOutputStream) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 62 with JarFile

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

the class DalvikExecTest method test_execCreatedJar.

// Create a temp file, fill it with contents according to Dalvik JAR format, and execute it on dalvikvm using -classpath option.",
public void test_execCreatedJar() throws IOException, InterruptedException {
    File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar");
    jarFile.deleteOnExit();
    // Create a JAR output stream on the temp file:
    JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile));
    // Define the entry for the classes.dex:
    jarOut.putNextEntry(new JarEntry("classes.dex"));
    // Fill in the classes.dex contents, i.e. the Dalvik executable code:
    // (See below for the detailed source code contents.)
    Streams.copy(Support_Resources.getResourceStream("cts_dalvikExecTest_classes.dex"), jarOut);
    // Now add a resource file:
    //
    jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource"));
    jarOut.write("This Resource contains some text.".getBytes());
    // Close the stream to the completed JAR file.
    jarOut.close();
    // The resulting JAR file contains the classes listed at the end of this text,
    // like the 'cts_dalvikExecTest.jar' as part of the resources, too.
    String res;
    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."));
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 63 with JarFile

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

the class OldJarEntryTest method setUp.

@Override
protected void setUp() throws Exception {
    resources = Support_Resources.createTempFolder();
    Support_Resources.copyFile(resources, null, jarName);
    jarFile = new JarFile(new File(resources, jarName));
}
Also used : JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 64 with JarFile

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

the class OldJarFileTest method test_getInputStreamLjava_util_jar_JarEntry.

public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
    Support_Resources.copyFile(resources, null, jarName);
    File localFile = new File(resources, jarName);
    byte[] b = new byte[1024];
    JarFile jf = new JarFile(localFile);
    InputStream is = jf.getInputStream(jf.getEntry(entryName));
    assertTrue("Returned invalid stream", is.available() > 0);
    int r = is.read(b, 0, 1024);
    is.close();
    StringBuilder stringBuffer = new StringBuilder(r);
    for (int i = 0; i < r; i++) {
        stringBuffer.append((char) (b[i] & 0xff));
    }
    String contents = stringBuffer.toString();
    assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
    jf.close();
    jf = new JarFile(localFile);
    InputStream in = jf.getInputStream(new JarEntry("invalid"));
    assertNull("Got stream for non-existent entry", in);
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        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 expected) {
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        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 expected) {
    }
}
Also used : InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 65 with JarFile

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

the class OldJarFileTest method test_ConstructorLjava_io_File.

public void test_ConstructorLjava_io_File() throws IOException {
    try {
        new JarFile(new File("Wrong.file"));
        fail("Should throw IOException");
    } catch (IOException expected) {
    }
    Support_Resources.copyFile(resources, null, jarName);
    new JarFile(new File(resources, jarName));
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) 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