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