|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6879371 |
|
27 * @summary javap does not close internal default file manager |
|
28 */ |
|
29 |
|
30 import java.io.*; |
|
31 import java.util.zip.*; |
|
32 |
|
33 public class T6879371 { |
|
34 public static void main(String[] args) throws Exception { |
|
35 new T6879371().run(); |
|
36 } |
|
37 |
|
38 public void run() throws Exception { |
|
39 // create a simple test class which we can put into |
|
40 // a test zip file and know that it will be used by |
|
41 // javap. |
|
42 File classDir = new File("classes"); |
|
43 classDir.mkdir(); |
|
44 |
|
45 String className = "Test"; |
|
46 File javaFile = writeTestFile(className); |
|
47 compileTestFile(classDir, javaFile); |
|
48 |
|
49 test(classDir, className, false); |
|
50 test(classDir, className, true); |
|
51 } |
|
52 |
|
53 void test(File classDir, String className, boolean useJavaUtilZip) throws Exception { |
|
54 // javac should really not be using system properties like this |
|
55 // -- it should really be using (hidden) options -- but until then |
|
56 // take care to leave system properties as we find them, so as not |
|
57 // to adversely affect other tests that might follow. |
|
58 String prev = System.getProperty("useJavaUtilZip"); |
|
59 setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null)); |
|
60 try { |
|
61 File zipFile = zip(classDir, new File(classDir + ".zip")); |
|
62 javap("-classpath", zipFile.getPath(), className); |
|
63 |
|
64 if (!zipFile.delete()) |
|
65 throw new Exception("failed to delete " + zipFile); |
|
66 } finally { |
|
67 setProperty("useJavaUtilZip", prev); |
|
68 } |
|
69 } |
|
70 |
|
71 File writeTestFile(String name) throws IOException { |
|
72 File f = new File(name + ".java"); |
|
73 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
|
74 out.println("class " + name + " { }"); |
|
75 out.close(); |
|
76 return f; |
|
77 } |
|
78 |
|
79 void compileTestFile(File classDir, File file) { |
|
80 int rc = com.sun.tools.javac.Main.compile( |
|
81 new String[] { "-d", classDir.getPath(), file.getPath() }); |
|
82 if (rc != 0) |
|
83 throw new Error("compilation failed. rc=" + rc); |
|
84 } |
|
85 |
|
86 File zip(File dir, File zipFile) throws IOException { |
|
87 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); |
|
88 for (File file: dir.listFiles()) { |
|
89 if (file.isFile()) { |
|
90 byte[] data = new byte[(int) file.length()]; |
|
91 DataInputStream in = new DataInputStream(new FileInputStream(file)); |
|
92 in.readFully(data); |
|
93 in.close(); |
|
94 zipOut.putNextEntry(new ZipEntry(file.getName())); |
|
95 zipOut.write(data, 0, data.length); |
|
96 zipOut.closeEntry(); |
|
97 } |
|
98 } |
|
99 zipOut.close(); |
|
100 return zipFile; |
|
101 } |
|
102 |
|
103 String javap(String... args) { |
|
104 StringWriter sw = new StringWriter(); |
|
105 PrintWriter out = new PrintWriter(sw); |
|
106 int rc = com.sun.tools.javap.Main.run(args, out); |
|
107 if (rc != 0) |
|
108 throw new Error("javap failed. rc=" + rc); |
|
109 out.close(); |
|
110 return sw.toString(); |
|
111 } |
|
112 |
|
113 void setProperty(String key, String value) { |
|
114 if (value != null) |
|
115 System.setProperty(key, value); |
|
116 else |
|
117 System.getProperties().remove(key); |
|
118 } |
|
119 } |