test/tools/javac/6304921/TestLog.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 695
3c9b64e55c5d
child 1138
7375d4979bd3
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2005, 2010, 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.JCTree;
    39 import com.sun.tools.javac.tree.TreeScanner;
    40 import com.sun.tools.javac.util.Context;
    41 import com.sun.tools.javac.util.Log;
    42 import com.sun.tools.javac.util.JCDiagnostic;
    43 import com.sun.tools.javac.util.Options;
    45 public class TestLog
    46 {
    47     public static void main(String... args) throws IOException {
    48         test(false);
    49         test(true);
    50     }
    52     static void test(boolean genEndPos) throws IOException {
    53         Context context = new Context();
    55         Options options = Options.instance(context);
    56         options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
    58         Log log = Log.instance(context);
    59         log.multipleErrors = true;
    61         JavacFileManager.preRegister(context);
    62         ParserFactory pfac = ParserFactory.instance(context);
    64         final String text =
    65               "public class Foo {\n"
    66             + "  public static void main(String[] args) {\n"
    67             + "    if (args.length == 0)\n"
    68             + "      System.out.println(\"no args\");\n"
    69             + "    else\n"
    70             + "      System.out.println(args.length + \" args\");\n"
    71             + "  }\n"
    72             + "}\n";
    73         JavaFileObject fo = new StringJavaFileObject("Foo", text);
    74         log.useSource(fo);
    76         CharSequence cs = fo.getCharContent(true);
    77         Parser parser = pfac.newParser(cs, false, genEndPos, false);
    78         JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    79         log.setEndPosTable(fo, tree.endPositions);
    81         TreeScanner ts = new LogTester(log, tree.endPositions);
    82         ts.scan(tree);
    84         check(log.nerrors, 4, "errors");
    85         check(log.nwarnings, 4, "warnings");
    86     }
    88     private static void check(int found, int expected, String name) {
    89         if (found == expected)
    90             System.err.println(found + " " + name + " found, as expected.");
    91         else {
    92             System.err.println("incorrect number of " + name + " found.");
    93             System.err.println("expected: " + expected);
    94             System.err.println("   found: " + found);
    95             throw new IllegalStateException("test failed");
    96         }
    97     }
    99     private static class LogTester extends TreeScanner {
   100         LogTester(Log log, java.util.Map<JCTree, Integer> endPositions) {
   101             this.log = log;
   102             this.endPositions = endPositions;
   103         }
   105         public void visitIf(JCTree.JCIf tree) {
   106             JCDiagnostic.DiagnosticPosition nil = null;
   107             // generate dummy messages to exercise the log API
   108             log.error("not.stmt");
   109             log.error(tree.pos, "not.stmt");
   110             log.error(tree.pos(), "not.stmt");
   111             log.error(nil, "not.stmt");
   113             log.warning("div.zero");
   114             log.warning(tree.pos, "div.zero");
   115             log.warning(tree.pos(), "div.zero");
   116             log.warning(nil, "div.zero");
   117         }
   119         private Log log;
   120         private java.util.Map<JCTree, Integer> endPositions;
   121     }
   123     private static class StringJavaFileObject extends SimpleJavaFileObject {
   124         StringJavaFileObject(String name, String text) {
   125             super(URI.create(name), JavaFileObject.Kind.SOURCE);
   126             this.text = text;
   127         }
   128         public CharSequence getCharContent(boolean b) {
   129             return text;
   130         }
   131         public InputStream openInputStream() {
   132             throw new UnsupportedOperationException();
   133         }
   134         public OutputStream openOutputStream() {
   135             throw new UnsupportedOperationException();
   136         }
   137         private String text;
   138     }
   139 }

mercurial