Search in sources :

Example 71 with Manifest

use of java.util.jar.Manifest in project byte-buddy by raphw.

the class PackageTypeStrategyManifestReadingTest method testManifestMainAttributesSealed.

@Test
@IntegrationRule.Enforce
public void testManifestMainAttributesSealed() throws Exception {
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(Attributes.Name.SEALED, Boolean.TRUE.toString());
    when(classLoader.getResourceAsStream("/META-INF/MANIFEST.MF")).then(new Answer<InputStream>() {

        @Override
        public InputStream answer(InvocationOnMock invocationOnMock) throws Throwable {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            manifest.write(outputStream);
            return new ByteArrayInputStream(outputStream.toByteArray());
        }
    });
    when(sealBaseLocator.findSealBase(classLoader, FOO + "." + BAR + "." + QUX)).thenReturn(url);
    PackageDefinitionStrategy packageDefinitionStrategy = new PackageDefinitionStrategy.ManifestReading(sealBaseLocator);
    PackageDefinitionStrategy.Definition definition = packageDefinitionStrategy.define(classLoader, FOO + "." + BAR, FOO + "." + BAR + "." + QUX);
    assertThat(definition.isDefined(), is(true));
    assertThat(definition.getSealBase(), is(url));
    verify(sealBaseLocator).findSealBase(classLoader, FOO + "." + BAR + "." + QUX);
    verifyNoMoreInteractions(sealBaseLocator);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 72 with Manifest

use of java.util.jar.Manifest 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 73 with Manifest

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

the class OldManifestTest method test_writeLjava_io_OutputStream.

public void test_writeLjava_io_OutputStream() throws IOException {
    byte[] b;
    Manifest manifest1 = null;
    Manifest manifest2 = null;
    try {
        manifest1 = new Manifest(new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")).openStream());
    } catch (MalformedURLException e) {
        fail("Malformed URL");
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    manifest1.write(baos);
    b = baos.toByteArray();
    File f = File.createTempFile("111", "111");
    FileOutputStream fos = new FileOutputStream(f);
    fos.close();
    try {
        manifest1.write(fos);
        fail("IOException expected");
    } catch (IOException e) {
    // expected
    }
    f.delete();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    try {
        manifest2 = new Manifest(bais);
    } catch (MalformedURLException e) {
        fail("Malformed URL");
    }
    assertTrue(manifest1.equals(manifest2));
}
Also used : MalformedURLException(java.net.MalformedURLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) File(java.io.File) URL(java.net.URL)

Example 74 with Manifest

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

the class OldManifestTest method test_write_two_versions.

public void test_write_two_versions() throws Exception {
    // It's okay to have two versions.
    Manifest m1 = new Manifest();
    m1.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    m1.getMainAttributes().put(Attributes.Name.SIGNATURE_VERSION, "2.0");
    m1.getMainAttributes().putValue("Aardvark-Version", "3.0");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    m1.write(os);
    // The Manifest-Version takes precedence,
    // and the Signature-Version gets no special treatment.
    String[] lines = new String(os.toByteArray(), "UTF-8").split("\r\n");
    assertEquals("Manifest-Version: 1.0", lines[0]);
    assertEquals("Aardvark-Version: 3.0", lines[1]);
    assertEquals("Signature-Version: 2.0", lines[2]);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest)

Example 75 with Manifest

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

the class OldManifestTest method test_clone.

public void test_clone() throws IOException {
    Manifest emptyManifest = new Manifest();
    Manifest emptyClone = (Manifest) emptyManifest.clone();
    assertTrue("Should have no entries", emptyClone.getEntries().isEmpty());
    assertTrue("Should have no main attributes", emptyClone.getMainAttributes().isEmpty());
    assertEquals(emptyClone, emptyManifest);
    assertEquals(emptyManifest.clone().getClass().getName(), "java.util.jar.Manifest");
    Manifest manifest = new Manifest(new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")).openStream());
    Manifest manifestClone = (Manifest) manifest.clone();
    manifestClone.getMainAttributes();
    checkManifest(manifestClone);
}
Also used : Manifest(java.util.jar.Manifest) URL(java.net.URL)

Aggregations

Manifest (java.util.jar.Manifest)1226 Attributes (java.util.jar.Attributes)392 File (java.io.File)391 IOException (java.io.IOException)336 JarFile (java.util.jar.JarFile)231 InputStream (java.io.InputStream)184 URL (java.net.URL)177 JarOutputStream (java.util.jar.JarOutputStream)145 FileOutputStream (java.io.FileOutputStream)131 Test (org.junit.Test)129 FileInputStream (java.io.FileInputStream)119 Jar (aQute.bnd.osgi.Jar)105 JarInputStream (java.util.jar.JarInputStream)104 Builder (aQute.bnd.osgi.Builder)99 ZipEntry (java.util.zip.ZipEntry)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)96 JarEntry (java.util.jar.JarEntry)96 ByteArrayInputStream (java.io.ByteArrayInputStream)93 ArrayList (java.util.ArrayList)83 Map (java.util.Map)83