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

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6304912
aoqi@0 27 * @summary unit test for Log
aoqi@0 28 */
aoqi@0 29 import java.io.InputStream;
aoqi@0 30 import java.io.IOException;
aoqi@0 31 import java.io.OutputStream;
aoqi@0 32 import java.net.URI;
aoqi@0 33 import javax.tools.JavaFileObject;
aoqi@0 34 import javax.tools.SimpleJavaFileObject;
aoqi@0 35 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 36 import com.sun.tools.javac.parser.Parser;
aoqi@0 37 import com.sun.tools.javac.parser.ParserFactory;
aoqi@0 38 import com.sun.tools.javac.tree.EndPosTable;
aoqi@0 39 import com.sun.tools.javac.tree.JCTree;
aoqi@0 40 import com.sun.tools.javac.tree.TreeScanner;
aoqi@0 41 import com.sun.tools.javac.util.Context;
aoqi@0 42 import com.sun.tools.javac.util.Log;
aoqi@0 43 import com.sun.tools.javac.util.JCDiagnostic;
aoqi@0 44 import com.sun.tools.javac.util.Options;
aoqi@0 45
aoqi@0 46 public class TestLog
aoqi@0 47 {
aoqi@0 48 public static void main(String... args) throws IOException {
aoqi@0 49 test(false);
aoqi@0 50 test(true);
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 static void test(boolean genEndPos) throws IOException {
aoqi@0 54 Context context = new Context();
aoqi@0 55
aoqi@0 56 Options options = Options.instance(context);
aoqi@0 57 options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
aoqi@0 58
aoqi@0 59 Log log = Log.instance(context);
aoqi@0 60 log.multipleErrors = true;
aoqi@0 61
aoqi@0 62 JavacFileManager.preRegister(context);
aoqi@0 63 ParserFactory pfac = ParserFactory.instance(context);
aoqi@0 64
aoqi@0 65 final String text =
aoqi@0 66 "public class Foo {\n"
aoqi@0 67 + " public static void main(String[] args) {\n"
aoqi@0 68 + " if (args.length == 0)\n"
aoqi@0 69 + " System.out.println(\"no args\");\n"
aoqi@0 70 + " else\n"
aoqi@0 71 + " System.out.println(args.length + \" args\");\n"
aoqi@0 72 + " }\n"
aoqi@0 73 + "}\n";
aoqi@0 74 JavaFileObject fo = new StringJavaFileObject("Foo", text);
aoqi@0 75 log.useSource(fo);
aoqi@0 76
aoqi@0 77 CharSequence cs = fo.getCharContent(true);
aoqi@0 78 Parser parser = pfac.newParser(cs, false, genEndPos, false);
aoqi@0 79 JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
aoqi@0 80 log.setEndPosTable(fo, tree.endPositions);
aoqi@0 81
aoqi@0 82 TreeScanner ts = new LogTester(log, tree.endPositions);
aoqi@0 83 ts.scan(tree);
aoqi@0 84
aoqi@0 85 check(log.nerrors, 4, "errors");
aoqi@0 86 check(log.nwarnings, 4, "warnings");
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 private static void check(int found, int expected, String name) {
aoqi@0 90 if (found == expected)
aoqi@0 91 System.err.println(found + " " + name + " found, as expected.");
aoqi@0 92 else {
aoqi@0 93 System.err.println("incorrect number of " + name + " found.");
aoqi@0 94 System.err.println("expected: " + expected);
aoqi@0 95 System.err.println(" found: " + found);
aoqi@0 96 throw new IllegalStateException("test failed");
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 private static class LogTester extends TreeScanner {
aoqi@0 101 LogTester(Log log, EndPosTable endPosTable) {
aoqi@0 102 this.log = log;
aoqi@0 103 this.endPosTable = endPosTable;
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 public void visitIf(JCTree.JCIf tree) {
aoqi@0 107 JCDiagnostic.DiagnosticPosition nil = null;
aoqi@0 108 // generate dummy messages to exercise the log API
aoqi@0 109 log.error("not.stmt");
aoqi@0 110 log.error(tree.pos, "not.stmt");
aoqi@0 111 log.error(tree.pos(), "not.stmt");
aoqi@0 112 log.error(nil, "not.stmt");
aoqi@0 113
aoqi@0 114 log.warning("div.zero");
aoqi@0 115 log.warning(tree.pos, "div.zero");
aoqi@0 116 log.warning(tree.pos(), "div.zero");
aoqi@0 117 log.warning(nil, "div.zero");
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 private Log log;
aoqi@0 121 private EndPosTable endPosTable;
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 private static class StringJavaFileObject extends SimpleJavaFileObject {
aoqi@0 125 StringJavaFileObject(String name, String text) {
aoqi@0 126 super(URI.create(name), JavaFileObject.Kind.SOURCE);
aoqi@0 127 this.text = text;
aoqi@0 128 }
aoqi@0 129 public CharSequence getCharContent(boolean b) {
aoqi@0 130 return text;
aoqi@0 131 }
aoqi@0 132 public InputStream openInputStream() {
aoqi@0 133 throw new UnsupportedOperationException();
aoqi@0 134 }
aoqi@0 135 public OutputStream openOutputStream() {
aoqi@0 136 throw new UnsupportedOperationException();
aoqi@0 137 }
aoqi@0 138 private String text;
aoqi@0 139 }
aoqi@0 140 }

mercurial