jjg@402: /* ohair@554: * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. jjg@402: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@402: * jjg@402: * This code is free software; you can redistribute it and/or modify it jjg@402: * under the terms of the GNU General Public License version 2 only, as jjg@402: * published by the Free Software Foundation. jjg@402: * jjg@402: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@402: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@402: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@402: * version 2 for more details (a copy is included in the LICENSE file that jjg@402: * accompanied this code). jjg@402: * jjg@402: * You should have received a copy of the GNU General Public License version jjg@402: * 2 along with this work; if not, write to the Free Software Foundation, jjg@402: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@402: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@402: */ jjg@402: jjg@402: /* jjg@402: * @test jjg@402: * @bug 6879371 jjg@402: * @summary javap does not close internal default file manager jjg@402: */ jjg@402: jjg@402: import java.io.*; jjg@402: import java.util.zip.*; jjg@402: jjg@402: public class T6879371 { jjg@402: public static void main(String[] args) throws Exception { jjg@402: new T6879371().run(); jjg@402: } jjg@402: jjg@402: public void run() throws Exception { jjg@402: // create a simple test class which we can put into jjg@402: // a test zip file and know that it will be used by jjg@402: // javap. jjg@402: File classDir = new File("classes"); jjg@402: classDir.mkdir(); jjg@402: jjg@402: String className = "Test"; jjg@402: File javaFile = writeTestFile(className); jjg@402: compileTestFile(classDir, javaFile); jjg@402: jjg@402: test(classDir, className, false); jjg@402: test(classDir, className, true); jjg@402: } jjg@402: jjg@402: void test(File classDir, String className, boolean useJavaUtilZip) throws Exception { jjg@402: // javac should really not be using system properties like this jjg@402: // -- it should really be using (hidden) options -- but until then jjg@402: // take care to leave system properties as we find them, so as not jjg@402: // to adversely affect other tests that might follow. jjg@402: String prev = System.getProperty("useJavaUtilZip"); jjg@402: setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null)); jjg@402: try { jjg@402: File zipFile = zip(classDir, new File(classDir + ".zip")); jjg@402: javap("-classpath", zipFile.getPath(), className); jjg@402: jjg@402: if (!zipFile.delete()) jjg@402: throw new Exception("failed to delete " + zipFile); jjg@402: } finally { jjg@402: setProperty("useJavaUtilZip", prev); jjg@402: } jjg@402: } jjg@402: jjg@402: File writeTestFile(String name) throws IOException { jjg@402: File f = new File(name + ".java"); jjg@402: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); jjg@402: out.println("class " + name + " { }"); jjg@402: out.close(); jjg@402: return f; jjg@402: } jjg@402: jjg@402: void compileTestFile(File classDir, File file) { jjg@402: int rc = com.sun.tools.javac.Main.compile( jjg@402: new String[] { "-d", classDir.getPath(), file.getPath() }); jjg@402: if (rc != 0) jjg@402: throw new Error("compilation failed. rc=" + rc); jjg@402: } jjg@402: jjg@402: File zip(File dir, File zipFile) throws IOException { jjg@402: ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); jjg@402: for (File file: dir.listFiles()) { jjg@402: if (file.isFile()) { jjg@402: byte[] data = new byte[(int) file.length()]; jjg@402: DataInputStream in = new DataInputStream(new FileInputStream(file)); jjg@402: in.readFully(data); jjg@402: in.close(); jjg@402: zipOut.putNextEntry(new ZipEntry(file.getName())); jjg@402: zipOut.write(data, 0, data.length); jjg@402: zipOut.closeEntry(); jjg@402: } jjg@402: } jjg@402: zipOut.close(); jjg@402: return zipFile; jjg@402: } jjg@402: jjg@402: String javap(String... args) { jjg@402: StringWriter sw = new StringWriter(); jjg@402: PrintWriter out = new PrintWriter(sw); jjg@402: int rc = com.sun.tools.javap.Main.run(args, out); jjg@402: if (rc != 0) jjg@402: throw new Error("javap failed. rc=" + rc); jjg@402: out.close(); jjg@402: return sw.toString(); jjg@402: } jjg@402: jjg@402: void setProperty(String key, String value) { jjg@402: if (value != null) jjg@402: System.setProperty(key, value); jjg@402: else jjg@402: System.getProperties().remove(key); jjg@402: } jjg@402: }