langtools/test/tools/javac/StdoutCloseTest.java
author lana
Thu, 21 Jan 2016 09:46:01 -0800
changeset 35340 38f7386ed942
parent 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk-9+102 for changeset 7e3feb2ba5b3
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 23792
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23792
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
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 23792
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.main
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 23792
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.util
23792
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    30
 * @run main StdoutCloseTest
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
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    33
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    34
import com.sun.tools.javac.util.Log;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    35
import com.sun.tools.javac.main.Main;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    36
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    37
import java.io.*;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    38
import java.util.ArrayList;
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    39
import java.util.List;
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 class StdoutCloseTest {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    42
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    44
        new StdoutCloseTest().test();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    45
    }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    46
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    47
    static final String program = "public class Test {\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    48
                                  "  public boolean test() {\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    49
                                  "    int i;\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    50
                                  "    if (i > 0) return true;\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    51
                                  "    return false;\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    52
                                  "  }\n" +
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    53
                                  "}\n";
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    54
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    55
    public void test() throws Exception {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    56
        final String sourceName = "Test.java";
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    57
        final String outName = "Test.out";
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    58
        File source = new File(sourceName);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    59
        PrintWriter pw = new PrintWriter(source);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    60
        pw.write(program);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    61
        pw.flush();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    62
        pw.close();
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
        PrintWriter log = compileClass(sourceName, outName);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    65
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    66
        File outFile = new File(outName);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    67
        if (!outFile.exists()) {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    68
            throw new Exception("Output file was not created!");
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    69
        }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    70
        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
    71
            log.close(); // Close output PrintWriter manually
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    72
            throw new Exception("Output file was still open!");
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
    }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    75
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    76
    public PrintWriter compileClass(String src, String out) {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    77
        List<String> options = new ArrayList<>();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    78
        options.add("-Xstdout");
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    79
        options.add(out);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    80
        options.add(src);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    81
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    82
        StringWriter sw = new StringWriter();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    83
        PrintWriter pw = new PrintWriter(sw);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    84
        Main compiler = new Main("javac", pw);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    85
        compiler.compile(options.toArray(new String[options.size()]));
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    86
        pw.flush();
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    87
        if (sw.getBuffer().length() > 0) {
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    88
            System.err.println(sw.toString());
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
        return compiler.log.getWriter(Log.WriterKind.NOTICE);
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    91
    }
eabe3e8a29bf 7118295: javac does not explicitly close -Xstdout file
kizune
parents:
diff changeset
    92
}