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

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

mercurial