You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ap...@apache.org on 2006/12/21 13:11:32 UTC
svn commit: r489335 - in /harmony/enhanced/classlib/trunk:
modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/
support/src/test/java/tests/resources/ support/src/test/java/tests/support/
Author: apetrenko
Date: Thu Dec 21 04:11:31 2006
New Revision: 489335
URL: http://svn.apache.org/viewvc?view=rev&rev=489335
Log:
Patch for HARMONY-1671 "Tests for java -jar ... execution"
Added:
harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java (with props)
harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java (with props)
harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Bar.ser (with props)
harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Foo.ser (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AllTests.java
harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java
Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AllTests.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AllTests.java?view=diff&rev=489335&r1=489334&r2=489335
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AllTests.java (original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AllTests.java Thu Dec 21 04:11:31 2006
@@ -37,10 +37,12 @@
suite.addTestSuite(AttributesTest.class);
suite.addTestSuite(JarEntryTest.class);
suite.addTestSuite(JarExceptionTest.class);
+ suite.addTestSuite(JarExecTest.class);
suite.addTestSuite(JarFileTest.class);
suite.addTestSuite(JarInputStreamTest.class);
suite.addTestSuite(JarOutputStreamTest.class);
suite.addTestSuite(ManifestTest.class);
+ suite.addTestSuite(ZipExecTest.class);
// $JUnit-END$
return suite;
}
Added: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java?view=auto&rev=489335
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java Thu Dec 21 04:11:31 2006
@@ -0,0 +1,255 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.archive.tests.java.util.jar;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import tests.support.Support_Exec;
+import tests.support.resource.Support_Resources;
+
+/**
+ *
+ * tests for various cases of java -jar ... execution
+ *
+ */
+
+public class JarExecTest extends junit.framework.TestCase {
+ /**
+ * regression test for HARMONY-1562 issue
+ *
+ */
+ public void test_1562() throws Exception {
+ // create the manifest
+ 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");
+
+ File outputJar = File.createTempFile("hyts_", ".jar");
+ outputJar.deleteOnExit();
+ JarOutputStream jout = new JarOutputStream(new FileOutputStream(outputJar),
+ man);
+ File resources = Support_Resources.createTempFolder();
+
+ for (String jarClass : new String[] {"Foo", "Bar"}) {
+ jout.putNextEntry(new JarEntry("foo/bar/execjartest/" + jarClass + ".class"));
+ jout.write(getResource(resources, "hyts_" + jarClass + ".ser"));
+ }
+
+ jout.close();
+
+
+ // set up the VM parameters
+ String[] args = new String[] {"-jar", outputJar.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+ /**
+ * tests Class-Path entry in manifest
+ * @throws Exception in case of troubles
+ */
+ public void test_jar_class_path() throws Exception {
+ File fooJar = File.createTempFile("hyts_", ".jar");
+ File barJar = File.createTempFile("hyts_", ".jar");
+ fooJar.deleteOnExit();
+ barJar.deleteOnExit();
+
+ // create the manifest
+ 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, barJar.getName());
+
+ File resources = Support_Resources.createTempFolder();
+
+ JarOutputStream joutFoo = new JarOutputStream(new FileOutputStream(fooJar), man);
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ joutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ joutFoo.close();
+
+ JarOutputStream joutBar = new JarOutputStream(new FileOutputStream(barJar));
+ joutBar.putNextEntry(new JarEntry("foo/bar/execjartest/Bar.class"));
+ joutBar.write(getResource(resources, "hyts_Bar.ser"));
+ joutBar.close();
+
+ String[] args = new String[] {"-jar", fooJar.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+
+ //rewrite manifest so it contains not only reference to bar but useless entries as well
+ att.put(Attributes.Name.CLASS_PATH, "xx yy zz " + barJar.getName());
+ joutFoo = new JarOutputStream(new FileOutputStream(fooJar), man);
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ joutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ joutFoo.close();
+ // execute the JAR and read the result
+ res = Support_Exec.execJava(args, null, false);
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+
+
+ //play with relative file names - put relative path as ../<parent dir name>/xx.jar
+ att.put(Attributes.Name.CLASS_PATH, ".." + File.separator + barJar.getParentFile().getName() + File.separator + barJar.getName());
+ joutFoo = new JarOutputStream(new FileOutputStream(fooJar), man);
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ joutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ joutFoo.close();
+ // execute the JAR and read the result
+ res = Support_Exec.execJava(args, null, false);
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+ /**
+ * tests case when Main-Class is not in the jar launched but in another jar referenced by Class-Path
+ * @throws Exception in case of troubles
+ */
+ public void test_main_class_in_another_jar() throws Exception {
+ File fooJar = File.createTempFile("hyts_", ".jar");
+ File barJar = File.createTempFile("hyts_", ".jar");
+ fooJar.deleteOnExit();
+ barJar.deleteOnExit();
+
+ // create the manifest
+ 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, fooJar.getName());
+
+ File resources = Support_Resources.createTempFolder();
+
+ JarOutputStream joutFoo = new JarOutputStream(new FileOutputStream(fooJar));
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ joutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ joutFoo.close();
+
+ JarOutputStream joutBar = new JarOutputStream(new FileOutputStream(barJar), man);
+ joutBar.putNextEntry(new JarEntry("foo/bar/execjartest/Bar.class"));
+ joutBar.write(getResource(resources, "hyts_Bar.ser"));
+ joutBar.close();
+
+ String[] args = new String[] {"-jar", barJar.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+ public void test_classpath() throws Exception {
+ File resources = Support_Resources.createTempFolder();
+
+ File fooJar = File.createTempFile("hyts_", ".jar");
+ fooJar.deleteOnExit();
+
+ JarOutputStream joutFoo = new JarOutputStream(new FileOutputStream(fooJar));
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ joutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Bar.class"));
+ joutFoo.write(getResource(resources, "hyts_Bar.ser"));
+ joutFoo.close();
+
+ String[] args = new String[] {"foo.bar.execjartest.Foo"};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, new String[] { "CLASSPATH=" + fooJar.getAbsolutePath() }, false);
+
+ assertTrue("Error executing class from ClassPath : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+
+ //ok - next try - add -cp to path - it should override env
+ File booJar = File.createTempFile("hyts_", ".jar");
+ booJar.deleteOnExit();
+
+ JarOutputStream joutBoo = new JarOutputStream(new FileOutputStream(booJar));
+ joutBoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ String booBody = new String(getResource(resources, "hyts_Foo.ser"));
+ booBody = booBody.replaceFirst("FOO", "BOO");
+ joutBoo.write(booBody.getBytes());
+ joutBoo.putNextEntry(new JarEntry("foo/bar/execjartest/Bar.class"));
+ String farBody = new String(getResource(resources, "hyts_Bar.ser"));
+ farBody = farBody.replaceFirst("BAR", "FAR");
+ joutBoo.write(farBody.getBytes());
+ joutBoo.close();
+
+ res = Support_Exec.execJava(args, new String[] {booJar.getAbsolutePath()}, new String[] { "CLASSPATH=" + fooJar.getAbsolutePath() }, false);
+
+ assertTrue("Error executing class specified by -cp : result returned was incorrect.", res
+ .startsWith("BOOFAR"));
+
+ //now add -jar option - it should override env and classpath
+ 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");
+
+ File zooJar = File.createTempFile("hyts_", ".jar");
+ zooJar.deleteOnExit();
+
+ JarOutputStream joutZoo = new JarOutputStream(new FileOutputStream(zooJar), man);
+ joutZoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ String zooBody = new String(getResource(resources, "hyts_Foo.ser"));
+ zooBody = zooBody.replaceFirst("FOO", "ZOO");
+ joutZoo.write(zooBody.getBytes());
+ joutZoo.putNextEntry(new JarEntry("foo/bar/execjartest/Bar.class"));
+ String zarBody = new String(getResource(resources, "hyts_Bar.ser"));
+ zarBody = zarBody.replaceFirst("BAR", "ZAR");
+ joutZoo.write(zarBody.getBytes());
+ joutZoo.close();
+
+ args = new String[] {"-jar", zooJar.getAbsolutePath()};
+
+ res = Support_Exec.execJava(args, new String[] {booJar.getAbsolutePath()}, new String[] { "CLASSPATH=" + fooJar.getAbsolutePath() }, false);
+
+ assertTrue("Error executing class specified by -cp : result returned was incorrect.", res
+ .startsWith("ZOOZAR"));
+ }
+
+ private static byte[] getResource(File tempDir, String resourceName) throws IOException {
+ Support_Resources.copyFile(tempDir, null, resourceName);
+ File resourceFile = new File(tempDir, resourceName);
+ resourceFile.deleteOnExit();
+
+ //read whole resource data into memory
+ byte[] resourceBody = new byte[(int) resourceFile.length()];
+ FileInputStream fis = new FileInputStream(resourceFile);
+ fis.read(resourceBody);
+ fis.close();
+
+ return resourceBody;
+ }
+
+}
Propchange: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java?view=auto&rev=489335
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java Thu Dec 21 04:11:31 2006
@@ -0,0 +1,265 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.archive.tests.java.util.jar;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import tests.support.Support_Exec;
+import tests.support.resource.Support_Resources;
+
+/**
+ *
+ * tests for various cases of java -jar ... execution with .zip files as args
+ * some tests are just copy of JarExecTest ones
+ */
+
+public class ZipExecTest extends junit.framework.TestCase {
+ public void test_1562() throws Exception {
+ 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");
+
+ File outputZip = File.createTempFile("hyts_", ".zip");
+ outputZip.deleteOnExit();
+ ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(outputZip));
+ File resources = Support_Resources.createTempFolder();
+
+ for (String zipClass : new String[] {"Foo", "Bar"}) {
+ zout.putNextEntry(new ZipEntry("foo/bar/execjartest/" + zipClass + ".class"));
+ zout.write(getResource(resources, "hyts_" + zipClass + ".ser"));
+ }
+
+ zout.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ man.write(zout);
+ zout.close();
+
+
+ // set up the VM parameters
+ String[] args = new String[] {"-jar", outputZip.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing ZIP : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+ /**
+ * tests Class-Path entry in manifest
+ * @throws Exception in case of troubles
+ */
+ public void test_zip_class_path() throws Exception {
+ File fooZip = File.createTempFile("hyts_", ".zip");
+ File barZip = File.createTempFile("hyts_", ".zip");
+ fooZip.deleteOnExit();
+ barZip.deleteOnExit();
+
+ // create the manifest
+ 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());
+
+ File resources = Support_Resources.createTempFolder();
+
+ ZipOutputStream zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip));
+ zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ man.write(zoutFoo);
+ zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class"));
+ zoutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ zoutFoo.close();
+
+ ZipOutputStream zoutBar = new ZipOutputStream(new FileOutputStream(barZip));
+ zoutBar.putNextEntry(new ZipEntry("foo/bar/execjartest/Bar.class"));
+ zoutBar.write(getResource(resources, "hyts_Bar.ser"));
+ zoutBar.close();
+
+ String[] args = new String[] {"-jar", fooZip.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+
+ //rewrite manifest so it contains not only reference to bar but useless entries as well
+ att.put(Attributes.Name.CLASS_PATH, "xx yy zz " + barZip.getName());
+ zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip));
+ zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ man.write(zoutFoo);
+ zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class"));
+ zoutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ zoutFoo.close();
+ // execute the JAR and read the result
+ res = Support_Exec.execJava(args, null, false);
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+
+
+ //play with relative file names - put relative path as ../<parent dir name>/xx.zip
+ att.put(Attributes.Name.CLASS_PATH, ".." + File.separator + barZip.getParentFile().getName() + File.separator + barZip.getName());
+ zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip));
+ zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ man.write(zoutFoo);
+ zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class"));
+ zoutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ zoutFoo.close();
+ // execute the ZIP and read the result
+ res = Support_Exec.execJava(args, null, false);
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+
+ public void test_zip_jar_mix() throws Exception {
+ File fooJar = File.createTempFile("hyts_", ".jar");
+ File barZip = File.createTempFile("hyts_", ".zip");
+ fooJar.deleteOnExit();
+ barZip.deleteOnExit();
+
+ // create the manifest
+ 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());
+
+ File resources = Support_Resources.createTempFolder();
+
+ JarOutputStream joutFoo = new JarOutputStream(new FileOutputStream(fooJar), man);
+ joutFoo.putNextEntry(new JarEntry("foo/bar/execjartest/Foo.class"));
+ joutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ joutFoo.close();
+
+ ZipOutputStream zoutBar = new ZipOutputStream(new FileOutputStream(barZip));
+ zoutBar.putNextEntry(new ZipEntry("foo/bar/execjartest/Bar.class"));
+ zoutBar.write(getResource(resources, "hyts_Bar.ser"));
+ zoutBar.close();
+
+ String[] args = new String[] {"-jar", fooJar.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+ public void test_zip_jar_mix_1() throws Exception {
+ File fooZip = File.createTempFile("hyts_", ".zip");
+ File barJar = File.createTempFile("hyts_", ".jar");
+ fooZip.deleteOnExit();
+ barJar.deleteOnExit();
+
+ // create the manifest
+ 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, barJar.getName());
+
+ File resources = Support_Resources.createTempFolder();
+
+ ZipOutputStream zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip));
+ zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ man.write(zoutFoo);
+ zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class"));
+ zoutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ zoutFoo.close();
+
+ JarOutputStream joutBar = new JarOutputStream(new FileOutputStream(barJar));
+ joutBar.putNextEntry(new ZipEntry("foo/bar/execjartest/Bar.class"));
+ joutBar.write(getResource(resources, "hyts_Bar.ser"));
+ joutBar.close();
+
+ String[] args = new String[] {"-jar", fooZip.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing ZIP : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+ /**
+ * tests case when Main-Class is not in the zip launched but in another zip referenced by Class-Path
+ * @throws Exception in case of troubles
+ */
+ public void test_main_class_in_another_zip() throws Exception {
+ File fooZip = File.createTempFile("hyts_", ".zip");
+ File barZip = File.createTempFile("hyts_", ".zip");
+ fooZip.deleteOnExit();
+ barZip.deleteOnExit();
+
+ // create the manifest
+ 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, fooZip.getName());
+
+ File resources = Support_Resources.createTempFolder();
+
+ ZipOutputStream zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip));
+ zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class"));
+ zoutFoo.write(getResource(resources, "hyts_Foo.ser"));
+ zoutFoo.close();
+
+ ZipOutputStream zoutBar = new ZipOutputStream(new FileOutputStream(barZip));
+ zoutBar.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ man.write(zoutBar);
+
+ zoutBar.putNextEntry(new ZipEntry("foo/bar/execjartest/Bar.class"));
+ zoutBar.write(getResource(resources, "hyts_Bar.ser"));
+ zoutBar.close();
+
+ String[] args = new String[] {"-jar", barZip.getAbsolutePath()};
+
+ // execute the JAR and read the result
+ String res = Support_Exec.execJava(args, null, false);
+
+ assertTrue("Error executing JAR : result returned was incorrect.", res
+ .startsWith("FOOBAR"));
+ }
+
+
+ private static byte[] getResource(File tempDir, String resourceName) throws IOException {
+ Support_Resources.copyFile(tempDir, null, resourceName);
+ File resourceFile = new File(tempDir, resourceName);
+ resourceFile.deleteOnExit();
+
+ //read whole resource data into memory
+ byte[] resourceBody = new byte[(int) resourceFile.length()];
+ FileInputStream fis = new FileInputStream(resourceFile);
+ fis.read(resourceBody);
+ fis.close();
+
+ return resourceBody;
+ }
+
+}
Propchange: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Bar.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Bar.ser?view=auto&rev=489335
==============================================================================
Binary file - no diff available.
Propchange: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Bar.ser
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Foo.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Foo.ser?view=auto&rev=489335
==============================================================================
Binary file - no diff available.
Propchange: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/hyts_Foo.ser
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Modified: harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java?view=diff&rev=489335&r1=489334&r2=489335
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java (original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java Thu Dec 21 04:11:31 2006
@@ -49,6 +49,29 @@
proc.destroy();
return output.toString();
}
+
+ public static String execJava(String[] args, String[] classpath, String[] envp,
+ boolean displayOutput) throws IOException, InterruptedException {
+ // this function returns the output of the process as a string
+ Object[] execArgs = execJavaNoSystemClasspath(args, classpath, envp, displayOutput);
+ Process proc = (Process) execArgs[0];
+
+ StringBuffer output = new StringBuffer();
+ InputStream in = proc.getInputStream();
+ int result;
+ byte[] bytes = new byte[1024];
+ while ((result = in.read(bytes)) != -1) {
+ output.append(new String(bytes, 0, result));
+ if (displayOutput)
+ System.out.write(bytes, 0, result);
+ }
+ in.close();
+ proc.waitFor();
+ checkStderr(execArgs);
+ proc.destroy();
+ return output.toString();
+ }
+
public static void checkStderr(Object[] execArgs) {
StringBuffer errBuf = (StringBuffer) execArgs[1];
@@ -58,6 +81,73 @@
}
}
}
+
+ private static Object[] execJavaNoSystemClasspath(String[] args, String[] classpath, String[] envp,
+ final boolean displayOutput) throws IOException, InterruptedException {
+ // this function returns the resulting process from the exec
+ int baseArgs = 0;
+ String[] execArgs = null;
+ baseArgs = (classpath == null) ? 1 : 3;
+ execArgs = new String[baseArgs + args.length];
+ String executable = System.getProperty("java.home");
+ if (!executable.endsWith(File.separator))
+ executable += File.separator;
+ executable += "bin" + File.separator;
+ execArgs[0] = executable + "java";
+
+ String classPathString = "";
+ if (classpath != null) {
+ for (int i = 0; i < classpath.length; i++) {
+ if( i != 0) {
+ classPathString += File.pathSeparator;
+ }
+ classPathString += classpath[i];
+ }
+ execArgs[1] = "-cp";
+ execArgs[2] = "\"" + classPathString + "\"";
+ }
+
+ for (int i = 0; i < args.length; i++)
+ execArgs[baseArgs + i] = args[i];
+
+ final Process proc = Runtime.getRuntime().exec(execArgs, envp);
+ final StringBuffer errBuf = new StringBuffer();
+ Thread errThread = new Thread(new Runnable() {
+ public void run() {
+ synchronized (errBuf) {
+ synchronized (proc) {
+ proc.notifyAll();
+ }
+ InputStream err = proc.getErrorStream();
+ int result;
+ byte[] bytes = new byte[1024];
+ try {
+ while ((result = err.read(bytes)) != -1) {
+ if(displayOutput) {
+ System.err.write(bytes, 0, result);
+ }
+ errBuf.append(new String(bytes));
+ }
+ err.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PrintStream printer = new PrintStream(out);
+ e.printStackTrace(printer);
+ printer.close();
+ errBuf.append(new String(out.toByteArray()));
+ }
+ }
+ }
+ });
+ synchronized (proc) {
+ errThread.start();
+ // wait for errThread to start
+ proc.wait();
+ }
+ return new Object[] { proc, errBuf };
+ }
+
public static Object[] execJava2(String[] args, String[] classpath,
boolean displayOutput) throws IOException, InterruptedException {