OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / archive / tests / java / util / jar / JarOutputStreamTest.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.apache.harmony.archive.tests.java.util.jar;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.URL;
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;
31
32 import tests.support.resource.Support_Resources;
33
34 public class JarOutputStreamTest extends junit.framework.TestCase {
35
36     /**
37      * @tests java.util.jar.JarOutputStream#putNextEntry(java.util.zip.ZipEntry)
38      */
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
46         // not matter
47         final String testClass = "hyts_mainClass.ser";
48         final String entryName = "foo/bar/execjartest/MainClass.class";
49
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"};
55
56         for (String element : manifestMain) {
57
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);
63
64             File outputJar = null;
65             JarOutputStream jout = null;
66
67             // open the output jarfile
68             outputJar = File.createTempFile("hyts_", ".jar");
69             jout = new JarOutputStream(new FileOutputStream(outputJar),
70                     newman);
71             jout.putNextEntry(new JarEntry(entryName));
72
73             File resources = Support_Resources.createTempFolder();
74
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()
78                     .toString());
79             InputStream jis = jarURL.openStream();
80
81             byte[] bytes = new byte[1024];
82             int len;
83             while ((len = jis.read(bytes)) != -1) {
84                 jout.write(bytes, 0, len);
85             }
86
87             jout.flush();
88             jout.close();
89             jis.close();
90
91             String res = null;
92             // set up the VM parameters
93             String[] args = new String[2];
94             args[0] = "-jar";
95             args[1] = outputJar.getAbsolutePath();
96
97 // It's not that simple to execute a JAR against Dalvik VM (see DalvikExecTest):
98 //
99 //            try {
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);
104 //            }
105 //
106 //            assertTrue("Error executing JAR test on: " + element
107 //                    + ". Result returned was incorrect.", res
108 //                    .startsWith("TEST"));
109             outputJar.delete();
110
111             try {
112                 // open the output jarfile
113                 outputJar = File.createTempFile("hyts_", ".jar");
114                 OutputStream os = new FileOutputStream(outputJar);
115                 jout = new JarOutputStream(os, newman);
116                 os.close();
117                 jout.putNextEntry(new JarEntry(entryName));
118                 fail("IOException expected");
119             } catch (IOException e) {
120                 // expected
121             }
122         }
123     }
124
125     public void test_JarOutputStreamLjava_io_OutputStreamLjava_util_jar_Manifest()
126             throws IOException {
127         File fooJar = File.createTempFile("hyts_", ".jar");
128         File barZip = File.createTempFile("hyts_", ".zip");
129
130         FileOutputStream fos = new FileOutputStream(fooJar);
131
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());
137
138         fos.close();
139         try {
140             JarOutputStream joutFoo = new JarOutputStream(fos, man);
141             fail("IOException expected");
142         } catch (IOException ee) {
143             // expected
144         }
145     }
146
147     public void test_JarOutputStreamLjava_io_OutputStream() throws IOException {
148         File fooJar = File.createTempFile("hyts_", ".jar");
149
150         FileOutputStream fos = new FileOutputStream(fooJar);
151         ZipEntry ze = new ZipEntry("Test");
152
153         try {
154             JarOutputStream joutFoo = new JarOutputStream(fos);
155             joutFoo.putNextEntry(ze);
156             joutFoo.write(33);
157         } catch (IOException ee) {
158             fail("IOException is not expected");
159         }
160
161         fos.close();
162         fooJar.delete();
163         try {
164             JarOutputStream joutFoo = new JarOutputStream(fos);
165             joutFoo.putNextEntry(ze);
166             fail("IOException expected");
167         } catch (IOException ee) {
168             // expected
169         }
170     }
171
172     @Override
173     protected void setUp() {
174     }
175
176     @Override
177     protected void tearDown() {
178     }
179
180 }