test/tools/javac/6304921/TestLog.java

Mon, 16 Jun 2008 13:28:00 -0700

author
jjg
date
Mon, 16 Jun 2008 13:28:00 -0700
changeset 50
b9bcea8bbe24
parent 1
9a66ca7c79fa
child 54
eaf608c64fec
permissions
-rw-r--r--

6714364: refactor javac File handling code into new javac.file package
Reviewed-by: mcimadamore

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 *
duke@1 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 21 * have any 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;
duke@1 37 import com.sun.tools.javac.parser.Scanner;
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);
duke@1 62 Scanner.Factory sfac = Scanner.Factory.instance(context);
duke@1 63 Parser.Factory pfac = Parser.Factory.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
duke@1 77 Scanner s = sfac.newScanner(fo.getCharContent(true));
duke@1 78 Parser parser = pfac.newParser(s, false, genEndPos);
duke@1 79 JCTree.JCCompilationUnit tree = parser.compilationUnit();
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 {
duke@1 101 LogTester(Log log, java.util.Map<JCTree, Integer> endPositions) {
duke@1 102 this.log = log;
duke@1 103 this.endPositions = endPositions;
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;
duke@1 121 private java.util.Map<JCTree, Integer> endPositions;
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