test/tools/javac/6304921/TestLog.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2005, 2012, 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  */
    24 /*
    25  * @test
    26  * @bug 6304912
    27  * @summary unit test for Log
    28  */
    29 import java.io.InputStream;
    30 import java.io.IOException;
    31 import java.io.OutputStream;
    32 import java.net.URI;
    33 import javax.tools.JavaFileObject;
    34 import javax.tools.SimpleJavaFileObject;
    35 import com.sun.tools.javac.file.JavacFileManager;
    36 import com.sun.tools.javac.parser.Parser;
    37 import com.sun.tools.javac.parser.ParserFactory;
    38 import com.sun.tools.javac.tree.EndPosTable;
    39 import com.sun.tools.javac.tree.JCTree;
    40 import com.sun.tools.javac.tree.TreeScanner;
    41 import com.sun.tools.javac.util.Context;
    42 import com.sun.tools.javac.util.Log;
    43 import com.sun.tools.javac.util.JCDiagnostic;
    44 import com.sun.tools.javac.util.Options;
    46 public class TestLog
    47 {
    48     public static void main(String... args) throws IOException {
    49         test(false);
    50         test(true);
    51     }
    53     static void test(boolean genEndPos) throws IOException {
    54         Context context = new Context();
    56         Options options = Options.instance(context);
    57         options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
    59         Log log = Log.instance(context);
    60         log.multipleErrors = true;
    62         JavacFileManager.preRegister(context);
    63         ParserFactory pfac = ParserFactory.instance(context);
    65         final String text =
    66               "public class Foo {\n"
    67             + "  public static void main(String[] args) {\n"
    68             + "    if (args.length == 0)\n"
    69             + "      System.out.println(\"no args\");\n"
    70             + "    else\n"
    71             + "      System.out.println(args.length + \" args\");\n"
    72             + "  }\n"
    73             + "}\n";
    74         JavaFileObject fo = new StringJavaFileObject("Foo", text);
    75         log.useSource(fo);
    77         CharSequence cs = fo.getCharContent(true);
    78         Parser parser = pfac.newParser(cs, false, genEndPos, false);
    79         JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    80         log.setEndPosTable(fo, tree.endPositions);
    82         TreeScanner ts = new LogTester(log, tree.endPositions);
    83         ts.scan(tree);
    85         check(log.nerrors, 4, "errors");
    86         check(log.nwarnings, 4, "warnings");
    87     }
    89     private static void check(int found, int expected, String name) {
    90         if (found == expected)
    91             System.err.println(found + " " + name + " found, as expected.");
    92         else {
    93             System.err.println("incorrect number of " + name + " found.");
    94             System.err.println("expected: " + expected);
    95             System.err.println("   found: " + found);
    96             throw new IllegalStateException("test failed");
    97         }
    98     }
   100     private static class LogTester extends TreeScanner {
   101         LogTester(Log log, EndPosTable endPosTable) {
   102             this.log = log;
   103             this.endPosTable = endPosTable;
   104         }
   106         public void visitIf(JCTree.JCIf tree) {
   107             JCDiagnostic.DiagnosticPosition nil = null;
   108             // generate dummy messages to exercise the log API
   109             log.error("not.stmt");
   110             log.error(tree.pos, "not.stmt");
   111             log.error(tree.pos(), "not.stmt");
   112             log.error(nil, "not.stmt");
   114             log.warning("div.zero");
   115             log.warning(tree.pos, "div.zero");
   116             log.warning(tree.pos(), "div.zero");
   117             log.warning(nil, "div.zero");
   118         }
   120         private Log log;
   121         private EndPosTable endPosTable;
   122     }
   124     private static class StringJavaFileObject extends SimpleJavaFileObject {
   125         StringJavaFileObject(String name, String text) {
   126             super(URI.create(name), JavaFileObject.Kind.SOURCE);
   127             this.text = text;
   128         }
   129         public CharSequence getCharContent(boolean b) {
   130             return text;
   131         }
   132         public InputStream openInputStream() {
   133             throw new UnsupportedOperationException();
   134         }
   135         public OutputStream openOutputStream() {
   136             throw new UnsupportedOperationException();
   137         }
   138         private String text;
   139     }
   140 }

mercurial