test/tools/javac/6304921/TestLog.java

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 695
3c9b64e55c5d
child 1280
5c0b3faeb0b0
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

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

mercurial