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