1/* 2 * Copyright (c) 2014, 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 7118295 27 * @summary javac does not explicitly close -Xstdout file 28 * @modules jdk.compiler/com.sun.tools.javac.main 29 * jdk.compiler/com.sun.tools.javac.util 30 * @run main StdoutCloseTest 31 */ 32 33 34import com.sun.tools.javac.util.Log; 35import com.sun.tools.javac.main.Main; 36 37import java.io.*; 38import java.util.ArrayList; 39import java.util.List; 40 41public class StdoutCloseTest { 42 43 public static void main(String[] args) throws Exception { 44 new StdoutCloseTest().test(); 45 } 46 47 static final String program = "public class Test {\n" + 48 " public boolean test() {\n" + 49 " int i;\n" + 50 " if (i > 0) return true;\n" + 51 " return false;\n" + 52 " }\n" + 53 "}\n"; 54 55 public void test() throws Exception { 56 final String sourceName = "Test.java"; 57 final String outName = "Test.out"; 58 File source = new File(sourceName); 59 PrintWriter pw = new PrintWriter(source); 60 pw.write(program); 61 pw.flush(); 62 pw.close(); 63 64 PrintWriter log = compileClass(sourceName, outName); 65 66 File outFile = new File(outName); 67 if (!outFile.exists()) { 68 throw new Exception("Output file was not created!"); 69 } 70 if (!log.checkError()) { // will return true if the stream is still open 71 log.close(); // Close output PrintWriter manually 72 throw new Exception("Output file was still open!"); 73 } 74 } 75 76 public PrintWriter compileClass(String src, String out) { 77 List<String> options = new ArrayList<>(); 78 options.add("-Xstdout"); 79 options.add(out); 80 options.add(src); 81 82 StringWriter sw = new StringWriter(); 83 PrintWriter pw = new PrintWriter(sw); 84 Main compiler = new Main("javac", pw); 85 compiler.compile(options.toArray(new String[options.size()])); 86 pw.flush(); 87 if (sw.getBuffer().length() > 0) { 88 System.err.println(sw.toString()); 89 } 90 return compiler.log.getWriter(Log.WriterKind.NOTICE); 91 } 92} 93