1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/* 2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more 3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * contributor license agreements. See the NOTICE file distributed with 4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * this work for additional information regarding copyright ownership. 5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * (the "License"); you may not use this file except in compliance with 7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * the License. You may obtain a copy of the License at 8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Unless required by applicable law or agreed to in writing, software 12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * See the License for the specific language governing permissions and 15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * limitations under the License. 16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage org.apache.harmony.archive.tests.java.util.jar; 18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 19561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.File; 20561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.FileOutputStream; 21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.InputStream; 22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.io.IOException; 23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.net.URL; 24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.jar.Attributes; 25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.jar.JarEntry; 26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.jar.JarOutputStream; 27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.jar.Manifest; 28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.zip.ZipEntry; 29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport tests.support.Support_Exec; 31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport tests.support.resource.Support_Resources; 32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class JarOutputStreamTest extends junit.framework.TestCase { 34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.util.jar.JarOutputStream#putNextEntry(java.util.zip.ZipEntry) 37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_putNextEntryLjava_util_zip_ZipEntry() throws Exception { 39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // testClass file`s actual extension is .class, since having .class 40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // extension files in source dir causes 41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // problems on eclipse, the extension is changed into .ser or it can be 42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // anything. The file is being 43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // read by inputstream and being written to other file, 44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // as long as the content of the file is not changed, the extension does 45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // not matter 46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes final String testClass = "hyts_mainClass.ser"; 47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes final String entryName = "foo/bar/execjartest/MainClass.class"; 48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // test whether specifying the main class in the manifest 50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // works using either /'s or .'s as a separator 51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes final String[] manifestMain = { "foo.bar.execjartest.MainClass", 52561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes "foo/bar/execjartest/MainClass" }; 53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes for (String element : manifestMain) { 55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // create the manifest 57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Manifest newman = new Manifest(); 58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Attributes att = newman.getMainAttributes(); 59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes att.put(Attributes.Name.MANIFEST_VERSION, "1.0"); 60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes att.put(Attributes.Name.MAIN_CLASS, element); 61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes File outputJar = null; 63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes JarOutputStream jout = null; 64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // open the output jarfile 66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes outputJar = File.createTempFile("hyts_", ".jar"); 67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes jout = new JarOutputStream(new FileOutputStream(outputJar), 68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes newman); 69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes jout.putNextEntry(new JarEntry(entryName)); 70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 71561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes File resources = Support_Resources.createTempFolder(); 72561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // read in the class file, and output it to the jar 74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Support_Resources.copyFile(resources, null, testClass); 75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes URL jarURL = new URL((new File(resources, testClass)).toURL() 76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes .toString()); 77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes InputStream jis = jarURL.openStream(); 78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes byte[] bytes = new byte[1024]; 79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes int len; 80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes while ((len = jis.read(bytes)) != -1) { 81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes jout.write(bytes, 0, len); 82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes jout.flush(); 84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes jout.close(); 85561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes jis.close(); 86561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes String res = null; 88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // set up the VM parameters 89561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes String[] args = new String[2]; 90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes args[0] = "-jar"; 91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes args[1] = outputJar.getAbsolutePath(); 92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // execute the JAR and read the result 94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes res = Support_Exec.execJava(args, null, true); 95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Error executing JAR test on: " + element 97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes + ". Result returned was incorrect.", res 98561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes .startsWith("TEST")); 99561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes outputJar.delete(); 100561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 101561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 102561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 103561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_JarOutputStreamLjava_io_OutputStreamLjava_util_jar_Manifest() 104561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes throws IOException { 105561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes File fooJar = File.createTempFile("hyts_", ".jar"); 106561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes File barZip = File.createTempFile("hyts_", ".zip"); 107561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 108561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes FileOutputStream fos = new FileOutputStream(fooJar); 109561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 110561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Manifest man = new Manifest(); 111561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Attributes att = man.getMainAttributes(); 112561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes att.put(Attributes.Name.MANIFEST_VERSION, "1.0"); 113561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo"); 114561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes att.put(Attributes.Name.CLASS_PATH, barZip.getName()); 115561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 116561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fos.close(); 117561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 118561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes new JarOutputStream(fos, man); 119561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("IOException expected"); 120561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (IOException ee) { 121561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // expected 122561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 123561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 124561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 125561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes new JarOutputStream(fos, null); 126561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("NullPointerException expected"); 127561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (NullPointerException ee) { 128561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // expected 129561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 130561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 131561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 132561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_JarOutputStreamLjava_io_OutputStream() throws IOException { 133561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes File fooJar = File.createTempFile("hyts_", ".jar"); 134561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 135561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes FileOutputStream fos = new FileOutputStream(fooJar); 136561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes ZipEntry ze = new ZipEntry("Test"); 137561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 138561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 139561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes JarOutputStream joutFoo = new JarOutputStream(fos); 140561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes joutFoo.putNextEntry(ze); 141561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes joutFoo.write(33); 142561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (IOException ee) { 143561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("IOException is not expected"); 144561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 145561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 146561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fos.close(); 147561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fooJar.delete(); 148561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 149561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes JarOutputStream joutFoo = new JarOutputStream(fos); 150561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes joutFoo.putNextEntry(ze); 151561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("IOException expected"); 152561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (IOException ee) { 153561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // expected 154561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 155561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 156561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 157561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes @Override 158561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes protected void setUp() { 159561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 160561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 161561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes @Override 162561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes protected void tearDown() { 163561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 164561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 165561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes} 166