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