test/tools/javac/6304921/TestLog.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 111
a92b756a888f
child 695
3c9b64e55c5d
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2005, 2008, 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.parser.Scanner;
    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         Scanner.Factory sfac = Scanner.Factory.instance(context);
    64         ParserFactory pfac = ParserFactory.instance(context);
    66         final String text =
    67               "public class Foo {\n"
    68             + "  public static void main(String[] args) {\n"
    69             + "    if (args.length == 0)\n"
    70             + "      System.out.println(\"no args\");\n"
    71             + "    else\n"
    72             + "      System.out.println(args.length + \" args\");\n"
    73             + "  }\n"
    74             + "}\n";
    75         JavaFileObject fo = new StringJavaFileObject("Foo", text);
    76         log.useSource(fo);
    78         CharSequence cs = fo.getCharContent(true);
    79         Parser parser = pfac.newParser(cs, false, genEndPos, false);
    80         JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    81         log.setEndPosTable(fo, tree.endPositions);
    83         TreeScanner ts = new LogTester(log, tree.endPositions);
    84         ts.scan(tree);
    86         check(log.nerrors, 4, "errors");
    87         check(log.nwarnings, 4, "warnings");
    88     }
    90     private static void check(int found, int expected, String name) {
    91         if (found == expected)
    92             System.err.println(found + " " + name + " found, as expected.");
    93         else {
    94             System.err.println("incorrect number of " + name + " found.");
    95             System.err.println("expected: " + expected);
    96             System.err.println("   found: " + found);
    97             throw new IllegalStateException("test failed");
    98         }
    99     }
   101     private static class LogTester extends TreeScanner {
   102         LogTester(Log log, java.util.Map<JCTree, Integer> endPositions) {
   103             this.log = log;
   104             this.endPositions = endPositions;
   105         }
   107         public void visitIf(JCTree.JCIf tree) {
   108             JCDiagnostic.DiagnosticPosition nil = null;
   109             // generate dummy messages to exercise the log API
   110             log.error("not.stmt");
   111             log.error(tree.pos, "not.stmt");
   112             log.error(tree.pos(), "not.stmt");
   113             log.error(nil, "not.stmt");
   115             log.warning("div.zero");
   116             log.warning(tree.pos, "div.zero");
   117             log.warning(tree.pos(), "div.zero");
   118             log.warning(nil, "div.zero");
   119         }
   121         private Log log;
   122         private java.util.Map<JCTree, Integer> endPositions;
   123     }
   125     private static class StringJavaFileObject extends SimpleJavaFileObject {
   126         StringJavaFileObject(String name, String text) {
   127             super(URI.create(name), JavaFileObject.Kind.SOURCE);
   128             this.text = text;
   129         }
   130         public CharSequence getCharContent(boolean b) {
   131             return text;
   132         }
   133         public InputStream openInputStream() {
   134             throw new UnsupportedOperationException();
   135         }
   136         public OutputStream openOutputStream() {
   137             throw new UnsupportedOperationException();
   138         }
   139         private String text;
   140     }
   141 }

mercurial