test/tools/javap/T6879371.java

Thu, 04 Apr 2013 19:05:42 -0700

author
katleman
date
Thu, 04 Apr 2013 19:05:42 -0700
changeset 1662
4a48f3173534
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8-b84 for changeset cfb65ca92082

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

mercurial