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