test/tools/javac/lambda/abort/Abort.java

changeset 1415
01c9d4161882
child 1421
7538e419a588
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/abort/Abort.java	Sat Nov 17 19:01:03 2012 +0000
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8003280
    1.30 + * @summary Add lambda tests
    1.31 + *  check that all diagnostics are dumped to output when compiler exits abruptly
    1.32 + */
    1.33 +
    1.34 +import com.sun.source.util.JavacTask;
    1.35 +import java.io.BufferedWriter;
    1.36 +import java.io.IOException;
    1.37 +import java.io.StringWriter;
    1.38 +import java.net.URI;
    1.39 +import java.net.URL;
    1.40 +import java.util.Arrays;
    1.41 +import javax.tools.Diagnostic;
    1.42 +import javax.tools.JavaCompiler;
    1.43 +import javax.tools.JavaFileObject;
    1.44 +import javax.tools.SimpleJavaFileObject;
    1.45 +import javax.tools.StandardJavaFileManager;
    1.46 +import javax.tools.ToolProvider;
    1.47 +
    1.48 +public class Abort {
    1.49 +
    1.50 +    public static void main(String... args) throws Exception {
    1.51 +
    1.52 +        String SCRATCH_DIR = System.getProperty("user.dir");
    1.53 +        JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
    1.54 +        java.io.File testDir = new java.io.File(SCRATCH_DIR);
    1.55 +
    1.56 +        sourceA.dumpTo(testDir);
    1.57 +        sourceB.dumpTo(testDir);
    1.58 +
    1.59 +        DiagnosticChecker diagChecker = new DiagnosticChecker();
    1.60 +        JavacTask ct = (JavacTask)javacTool.getTask(null, null, diagChecker,
    1.61 +                Arrays.asList("-XDrawDiagnostics", "-cp", testDir.getAbsolutePath()),
    1.62 +                null, Arrays.asList(sourceA.asJFO(testDir)));
    1.63 +        try {
    1.64 +            ct.analyze();
    1.65 +        } catch (Throwable ex) {
    1.66 +            //ignore abort exception thrown by javac
    1.67 +        }
    1.68 +
    1.69 +        if (!diagChecker.errorFound) {
    1.70 +            throw new AssertionError("Missing diagnostic");
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    static class JavaSource {
    1.75 +        String contents;
    1.76 +        String filename;
    1.77 +
    1.78 +        public JavaSource(String filename, String contents) {
    1.79 +            this.filename =  filename;
    1.80 +            this.contents = contents;
    1.81 +        }
    1.82 +
    1.83 +        void dumpTo(java.io.File loc) throws Exception {
    1.84 +            java.io.File file = new java.io.File(loc, filename);
    1.85 +            java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.FileWriter(file));
    1.86 +            bw.append(contents);
    1.87 +            bw.close();
    1.88 +        }
    1.89 +
    1.90 +        SimpleJavaFileObject asJFO(java.io.File dir) {
    1.91 +            return new SimpleJavaFileObject(URI.create(dir.getAbsolutePath() + "/" + filename), JavaFileObject.Kind.SOURCE) {
    1.92 +                @Override
    1.93 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    1.94 +                    return contents;
    1.95 +                }
    1.96 +            };
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    static JavaSource sourceA = new JavaSource("Abort.java", "public class Abort {\n" +
   1.101 +                                "    public static void main(String[] args) {\n" +
   1.102 +                                "        System.out.println(C.m());\n" +
   1.103 +                                "    }\n" +
   1.104 +                                "}");
   1.105 +
   1.106 +    static JavaSource sourceB = new JavaSource("C.java", "package com.example;\n" +
   1.107 +                                "public class C {\n" +
   1.108 +                                "    public static String m() { return null; }\n" +
   1.109 +                                "}");
   1.110 +
   1.111 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.112 +
   1.113 +        boolean errorFound;
   1.114 +
   1.115 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.116 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
   1.117 +                    diagnostic.getCode().contains("compiler.err.cant.access")) {
   1.118 +                errorFound = true;
   1.119 +            }
   1.120 +        }
   1.121 +    }
   1.122 +}

mercurial