T6879371.java revision 2942:08092deced3f
1/*
2 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6879371
27 * @summary javap does not close internal default file manager
28 * @modules jdk.jdeps
29 */
30
31import java.io.*;
32import java.util.zip.*;
33
34public class T6879371 {
35    public static void main(String[] args) throws Exception {
36        new T6879371().run();
37    }
38
39    public void run() throws Exception {
40        // create a simple test class which we can put into
41        // a test zip file and know that it will be used by
42        // javap.
43        File classDir = new File("classes");
44        classDir.mkdir();
45
46        String className = "Test";
47        File javaFile = writeTestFile(className);
48        compileTestFile(classDir, javaFile);
49
50        test(classDir, className, false);
51        test(classDir, className, true);
52    }
53
54    void test(File classDir, String className, boolean useJavaUtilZip) throws Exception {
55        // javac should really not be using system properties like this
56        // -- it should really be using (hidden) options -- but until then
57        // take care to leave system properties as we find them, so as not
58        // to adversely affect other tests that might follow.
59        String prev = System.getProperty("useJavaUtilZip");
60        setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null));
61        try {
62            File zipFile = zip(classDir, new File(classDir + ".zip"));
63            javap("-classpath", zipFile.getPath(), className);
64
65            if (!zipFile.delete())
66                throw new Exception("failed to delete " + zipFile);
67        } finally {
68            setProperty("useJavaUtilZip", prev);
69        }
70    }
71
72    File writeTestFile(String name) throws IOException {
73        File f = new File(name + ".java");
74        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
75        out.println("class " + name + " { }");
76        out.close();
77        return f;
78    }
79
80    void compileTestFile(File classDir, File file) {
81        int rc = com.sun.tools.javac.Main.compile(
82           new String[] { "-d", classDir.getPath(), file.getPath() });
83        if (rc != 0)
84            throw new Error("compilation failed. rc=" + rc);
85    }
86
87    File zip(File dir, File zipFile) throws IOException {
88        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
89        for (File file: dir.listFiles()) {
90            if (file.isFile()) {
91                byte[] data = new byte[(int) file.length()];
92                DataInputStream in = new DataInputStream(new FileInputStream(file));
93                in.readFully(data);
94                in.close();
95                zipOut.putNextEntry(new ZipEntry(file.getName()));
96                zipOut.write(data, 0, data.length);
97                zipOut.closeEntry();
98            }
99        }
100        zipOut.close();
101        return zipFile;
102    }
103
104    String javap(String... args) {
105        StringWriter sw = new StringWriter();
106        PrintWriter out = new PrintWriter(sw);
107        int rc = com.sun.tools.javap.Main.run(args, out);
108        if (rc != 0)
109            throw new Error("javap failed. rc=" + rc);
110        out.close();
111        return sw.toString();
112    }
113
114    void setProperty(String key, String value) {
115        if (value != null)
116            System.setProperty(key, value);
117        else
118            System.getProperties().remove(key);
119    }
120}
121