langtools/test/tools/javac/StdoutCloseTest.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 23792 eabe3e8a29bf
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23792
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     1
/*
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     4
 *
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     8
 *
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    13
 * accompanied this code).
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    14
 *
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    18
 *
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    21
 * questions.
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    22
 */
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    23
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    24
/*
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    25
 * @test
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    26
 * @bug 7118295
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    27
 * @summary javac does not explicitly close -Xstdout file
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    28
 * @run main StdoutCloseTest
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    29
 */
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    30
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    31
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    32
import com.sun.tools.javac.util.Log;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    33
import com.sun.tools.javac.main.Main;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    34
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    35
import java.io.*;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    36
import java.util.ArrayList;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    37
import java.util.List;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    38
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    39
public class StdoutCloseTest {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    40
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    42
        new StdoutCloseTest().test();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    43
    }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    44
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    45
    static final String program = "public class Test {\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    46
                                  "  public boolean test() {\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    47
                                  "    int i;\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    48
                                  "    if (i > 0) return true;\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    49
                                  "    return false;\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    50
                                  "  }\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    51
                                  "}\n";
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    52
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    53
    public void test() throws Exception {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    54
        final String sourceName = "Test.java";
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    55
        final String outName = "Test.out";
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    56
        File source = new File(sourceName);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    57
        PrintWriter pw = new PrintWriter(source);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    58
        pw.write(program);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    59
        pw.flush();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    60
        pw.close();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    61
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    62
        PrintWriter log = compileClass(sourceName, outName);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    63
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    64
        File outFile = new File(outName);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    65
        if (!outFile.exists()) {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    66
            throw new Exception("Output file was not created!");
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    67
        }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    68
        if (!log.checkError()) { // will return true if the stream is still open
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    69
            log.close(); // Close output PrintWriter manually
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    70
            throw new Exception("Output file was still open!");
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    71
        }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    72
    }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    73
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    74
    public PrintWriter compileClass(String src, String out) {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    75
        List<String> options = new ArrayList<>();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    76
        options.add("-Xstdout");
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    77
        options.add(out);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    78
        options.add(src);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    79
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    80
        StringWriter sw = new StringWriter();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    81
        PrintWriter pw = new PrintWriter(sw);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    82
        Main compiler = new Main("javac", pw);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    83
        compiler.compile(options.toArray(new String[options.size()]));
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    84
        pw.flush();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    85
        if (sw.getBuffer().length() > 0) {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    86
            System.err.println(sw.toString());
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    87
        }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    88
        return compiler.log.getWriter(Log.WriterKind.NOTICE);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    89
    }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    90
}