langtools/test/tools/javac/lambda/abort/Abort.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 14553 b2fd2388dd46
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14553
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     4
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     8
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    14
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    18
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    21
 * questions.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    22
 */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    23
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    24
/*
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    25
 * @test
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    26
 * @bug 8003280
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    27
 * @summary Add lambda tests
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    28
 *  check that all diagnostics are dumped to output when compiler exits abruptly
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14553
diff changeset
    29
 * @modules jdk.compiler
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    30
 */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    31
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    32
import com.sun.source.util.JavacTask;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    33
import java.io.BufferedWriter;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    34
import java.io.IOException;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    35
import java.io.StringWriter;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    36
import java.net.URI;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    37
import java.net.URL;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    38
import java.util.Arrays;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    39
import javax.tools.Diagnostic;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    40
import javax.tools.JavaCompiler;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    41
import javax.tools.JavaFileObject;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    42
import javax.tools.SimpleJavaFileObject;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    43
import javax.tools.StandardJavaFileManager;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    44
import javax.tools.ToolProvider;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    45
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    46
public class Abort {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    47
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    48
    public static void main(String... args) throws Exception {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    49
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    50
        String SCRATCH_DIR = System.getProperty("user.dir");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    51
        JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    52
        java.io.File testDir = new java.io.File(SCRATCH_DIR);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    53
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    54
        sourceA.dumpTo(testDir);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    55
        sourceB.dumpTo(testDir);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    56
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    57
        DiagnosticChecker diagChecker = new DiagnosticChecker();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    58
        JavacTask ct = (JavacTask)javacTool.getTask(null, null, diagChecker,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    59
                Arrays.asList("-XDrawDiagnostics", "-cp", testDir.getAbsolutePath()),
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    60
                null, Arrays.asList(sourceA.asJFO(testDir)));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    61
        try {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    62
            ct.analyze();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    63
        } catch (Throwable ex) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    64
            //ignore abort exception thrown by javac
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    65
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    66
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    67
        if (!diagChecker.errorFound) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    68
            throw new AssertionError("Missing diagnostic");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    69
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    70
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    71
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    72
    static class JavaSource {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    73
        String contents;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    74
        String filename;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    75
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    76
        public JavaSource(String filename, String contents) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    77
            this.filename =  filename;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    78
            this.contents = contents;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    79
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    80
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    81
        void dumpTo(java.io.File loc) throws Exception {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    82
            java.io.File file = new java.io.File(loc, filename);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    83
            java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.FileWriter(file));
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    84
            bw.append(contents);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    85
            bw.close();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    86
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    87
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    88
        SimpleJavaFileObject asJFO(java.io.File dir) {
14553
b2fd2388dd46 8003663: lambda test fails on Windows
mcimadamore
parents: 14547
diff changeset
    89
            return new SimpleJavaFileObject(new java.io.File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    90
                @Override
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    91
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    92
                    return contents;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    93
                }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    94
            };
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    95
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    96
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    97
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    98
    static JavaSource sourceA = new JavaSource("Abort.java", "public class Abort {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    99
                                "    public static void main(String[] args) {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   100
                                "        System.out.println(C.m());\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   101
                                "    }\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   102
                                "}");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   103
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   104
    static JavaSource sourceB = new JavaSource("C.java", "package com.example;\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   105
                                "public class C {\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   106
                                "    public static String m() { return null; }\n" +
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   107
                                "}");
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   108
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   109
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   110
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   111
        boolean errorFound;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   112
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   113
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   114
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   115
                    diagnostic.getCode().contains("compiler.err.cant.access")) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   116
                errorFound = true;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   117
            }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   118
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   119
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   120
}