2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.archive.tests.java.util.jar;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
26 import java.util.jar.Attributes;
27 import java.util.jar.JarEntry;
28 import java.util.jar.JarOutputStream;
29 import java.util.jar.Manifest;
30 import java.util.zip.ZipEntry;
32 import tests.support.resource.Support_Resources;
34 public class JarOutputStreamTest extends junit.framework.TestCase {
37 * @tests java.util.jar.JarOutputStream#putNextEntry(java.util.zip.ZipEntry)
39 public void test_putNextEntryLjava_util_zip_ZipEntry() throws Exception {
40 // testClass file`s actual extension is .class, since having .class
41 // extension files in source dir causes
42 // problems on eclipse, the extension is changed into .ser or it can be
43 // anything. The file is being
44 // read by inputstream and being written to other file,
45 // as long as the content of the file is not changed, the extension does
47 final String testClass = "hyts_mainClass.ser";
48 final String entryName = "foo/bar/execjartest/MainClass.class";
50 // test whether specifying the main class in the manifest
51 // works using either /'s or .'s as a separator
52 final String[] manifestMain = {
53 "foo.bar.execjartest.MainClass",
54 "foo/bar/execjartest/MainClass"};
56 for (String element : manifestMain) {
58 // create the manifest
59 Manifest newman = new Manifest();
60 Attributes att = newman.getMainAttributes();
61 att.put(Attributes.Name.MANIFEST_VERSION, "1.0");
62 att.put(Attributes.Name.MAIN_CLASS, element);
64 File outputJar = null;
65 JarOutputStream jout = null;
67 // open the output jarfile
68 outputJar = File.createTempFile("hyts_", ".jar");
69 jout = new JarOutputStream(new FileOutputStream(outputJar),
71 jout.putNextEntry(new JarEntry(entryName));
73 File resources = Support_Resources.createTempFolder();
75 // read in the class file, and output it to the jar
76 Support_Resources.copyFile(resources, null, testClass);
77 URL jarURL = new URL((new File(resources, testClass)).toURL()
79 InputStream jis = jarURL.openStream();
81 byte[] bytes = new byte[1024];
83 while ((len = jis.read(bytes)) != -1) {
84 jout.write(bytes, 0, len);
92 // set up the VM parameters
93 String[] args = new String[2];
95 args[1] = outputJar.getAbsolutePath();
97 // It's not that simple to execute a JAR against Dalvik VM (see DalvikExecTest):
100 // // execute the JAR and read the result
101 // res = Support_Exec.execJava(args, null, true);
102 // } catch (Exception e) {
103 // fail("Exception executing test JAR: " + e);
106 // assertTrue("Error executing JAR test on: " + element
107 // + ". Result returned was incorrect.", res
108 // .startsWith("TEST"));
112 // open the output jarfile
113 outputJar = File.createTempFile("hyts_", ".jar");
114 OutputStream os = new FileOutputStream(outputJar);
115 jout = new JarOutputStream(os, newman);
117 jout.putNextEntry(new JarEntry(entryName));
118 fail("IOException expected");
119 } catch (IOException e) {
125 public void test_JarOutputStreamLjava_io_OutputStreamLjava_util_jar_Manifest()
127 File fooJar = File.createTempFile("hyts_", ".jar");
128 File barZip = File.createTempFile("hyts_", ".zip");
130 FileOutputStream fos = new FileOutputStream(fooJar);
132 Manifest man = new Manifest();
133 Attributes att = man.getMainAttributes();
134 att.put(Attributes.Name.MANIFEST_VERSION, "1.0");
135 att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo");
136 att.put(Attributes.Name.CLASS_PATH, barZip.getName());
140 JarOutputStream joutFoo = new JarOutputStream(fos, man);
141 fail("IOException expected");
142 } catch (IOException ee) {
147 public void test_JarOutputStreamLjava_io_OutputStream() throws IOException {
148 File fooJar = File.createTempFile("hyts_", ".jar");
150 FileOutputStream fos = new FileOutputStream(fooJar);
151 ZipEntry ze = new ZipEntry("Test");
154 JarOutputStream joutFoo = new JarOutputStream(fos);
155 joutFoo.putNextEntry(ze);
157 } catch (IOException ee) {
158 fail("IOException is not expected");
164 JarOutputStream joutFoo = new JarOutputStream(fos);
165 joutFoo.putNextEntry(ze);
166 fail("IOException expected");
167 } catch (IOException ee) {
173 protected void setUp() {
177 protected void tearDown() {