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
18package org.apache.harmony.archive.tests.java.util.jar;
19
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24
25import java.io.File;
26import java.io.FileOutputStream;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.OutputStream;
30import java.net.URL;
31import java.util.jar.Attributes;
32import java.util.jar.JarEntry;
33import java.util.jar.JarOutputStream;
34import java.util.jar.Manifest;
35import java.util.zip.ZipEntry;
36
37import tests.support.resource.Support_Resources;
38
39@TestTargetClass(JarOutputStream.class)
40public class JarOutputStreamTest extends junit.framework.TestCase {
41
42    /**
43     * @tests java.util.jar.JarOutputStream#putNextEntry(java.util.zip.ZipEntry)
44     */
45    @TestTargetNew(
46        level = TestLevel.PARTIAL_COMPLETE,
47        method = "putNextEntry",
48        args = {java.util.zip.ZipEntry.class}
49    )
50    public void test_putNextEntryLjava_util_zip_ZipEntry() throws Exception {
51        // testClass file`s actual extension is .class, since having .class
52        // extension files in source dir causes
53        // problems on eclipse, the extension is changed into .ser or it can be
54        // anything. The file is being
55        // read by inputstream and being written to other file,
56        // as long as the content of the file is not changed, the extension does
57        // not matter
58        final String testClass = "hyts_mainClass.ser";
59        final String entryName = "foo/bar/execjartest/MainClass.class";
60
61        // test whether specifying the main class in the manifest
62        // works using either /'s or .'s as a separator
63        final String[] manifestMain = {
64                "foo.bar.execjartest.MainClass",
65                "foo/bar/execjartest/MainClass"};
66
67        for (String element : manifestMain) {
68
69            // create the manifest
70            Manifest newman = new Manifest();
71            Attributes att = newman.getMainAttributes();
72            att.put(Attributes.Name.MANIFEST_VERSION, "1.0");
73            att.put(Attributes.Name.MAIN_CLASS, element);
74
75            File outputJar = null;
76            JarOutputStream jout = null;
77
78            // open the output jarfile
79            outputJar = File.createTempFile("hyts_", ".jar");
80            jout = new JarOutputStream(new FileOutputStream(outputJar),
81                    newman);
82            jout.putNextEntry(new JarEntry(entryName));
83
84            File resources = Support_Resources.createTempFolder();
85
86            // read in the class file, and output it to the jar
87            Support_Resources.copyFile(resources, null, testClass);
88            URL jarURL = new URL((new File(resources, testClass)).toURL()
89                    .toString());
90            InputStream jis = jarURL.openStream();
91
92            byte[] bytes = new byte[1024];
93            int len;
94            while ((len = jis.read(bytes)) != -1) {
95                jout.write(bytes, 0, len);
96            }
97
98            jout.flush();
99            jout.close();
100            jis.close();
101
102            String res = null;
103            // set up the VM parameters
104            String[] args = new String[2];
105            args[0] = "-jar";
106            args[1] = outputJar.getAbsolutePath();
107
108// It's not that simple to execute a JAR against Dalvik VM (see DalvikExecTest):
109//
110//            try {
111//                // execute the JAR and read the result
112//                res = Support_Exec.execJava(args, null, true);
113//            } catch (Exception e) {
114//                fail("Exception executing test JAR: " + e);
115//            }
116//
117//            assertTrue("Error executing JAR test on: " + element
118//                    + ". Result returned was incorrect.", res
119//                    .startsWith("TEST"));
120            outputJar.delete();
121
122            try {
123                // open the output jarfile
124                outputJar = File.createTempFile("hyts_", ".jar");
125                OutputStream os = new FileOutputStream(outputJar);
126                jout = new JarOutputStream(os, newman);
127                os.close();
128                jout.putNextEntry(new JarEntry(entryName));
129                fail("IOException expected");
130            } catch (IOException e) {
131                // expected
132            }
133        }
134    }
135
136    @TestTargetNew(
137        level = TestLevel.PARTIAL_COMPLETE,
138        notes = "Checks IOException",
139        method = "JarOutputStream",
140        args = {java.io.OutputStream.class, java.util.jar.Manifest.class}
141    )
142    public void test_JarOutputStreamLjava_io_OutputStreamLjava_util_jar_Manifest()
143            throws IOException {
144        File fooJar = File.createTempFile("hyts_", ".jar");
145        File barZip = File.createTempFile("hyts_", ".zip");
146
147        FileOutputStream fos = new FileOutputStream(fooJar);
148
149        Manifest man = new Manifest();
150        Attributes att = man.getMainAttributes();
151        att.put(Attributes.Name.MANIFEST_VERSION, "1.0");
152        att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo");
153        att.put(Attributes.Name.CLASS_PATH, barZip.getName());
154
155        fos.close();
156        try {
157            JarOutputStream joutFoo = new JarOutputStream(fos, man);
158            fail("IOException expected");
159        } catch (IOException ee) {
160            // expected
161        }
162    }
163
164    @TestTargetNew(
165        level = TestLevel.COMPLETE,
166        notes = "Can not check IOException",
167        method = "JarOutputStream",
168        args = {java.io.OutputStream.class}
169    )
170    public void test_JarOutputStreamLjava_io_OutputStream() throws IOException {
171        File fooJar = File.createTempFile("hyts_", ".jar");
172
173        FileOutputStream fos = new FileOutputStream(fooJar);
174        ZipEntry ze = new ZipEntry("Test");
175
176        try {
177            JarOutputStream joutFoo = new JarOutputStream(fos);
178            joutFoo.putNextEntry(ze);
179            joutFoo.write(33);
180        } catch (IOException ee) {
181            fail("IOException is not expected");
182        }
183
184        fos.close();
185        fooJar.delete();
186        try {
187            JarOutputStream joutFoo = new JarOutputStream(fos);
188            joutFoo.putNextEntry(ze);
189            fail("IOException expected");
190        } catch (IOException ee) {
191            // expected
192        }
193    }
194
195    @Override
196    protected void setUp() {
197    }
198
199    @Override
200    protected void tearDown() {
201    }
202
203}
204