test/tools/javac/6304921/TestLog.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/6304921/TestLog.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,140 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6304912
    1.30 + * @summary unit test for Log
    1.31 + */
    1.32 +import java.io.InputStream;
    1.33 +import java.io.IOException;
    1.34 +import java.io.OutputStream;
    1.35 +import java.net.URI;
    1.36 +import javax.tools.JavaFileObject;
    1.37 +import javax.tools.SimpleJavaFileObject;
    1.38 +import com.sun.tools.javac.file.JavacFileManager;
    1.39 +import com.sun.tools.javac.parser.Parser;
    1.40 +import com.sun.tools.javac.parser.ParserFactory;
    1.41 +import com.sun.tools.javac.tree.EndPosTable;
    1.42 +import com.sun.tools.javac.tree.JCTree;
    1.43 +import com.sun.tools.javac.tree.TreeScanner;
    1.44 +import com.sun.tools.javac.util.Context;
    1.45 +import com.sun.tools.javac.util.Log;
    1.46 +import com.sun.tools.javac.util.JCDiagnostic;
    1.47 +import com.sun.tools.javac.util.Options;
    1.48 +
    1.49 +public class TestLog
    1.50 +{
    1.51 +    public static void main(String... args) throws IOException {
    1.52 +        test(false);
    1.53 +        test(true);
    1.54 +    }
    1.55 +
    1.56 +    static void test(boolean genEndPos) throws IOException {
    1.57 +        Context context = new Context();
    1.58 +
    1.59 +        Options options = Options.instance(context);
    1.60 +        options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
    1.61 +
    1.62 +        Log log = Log.instance(context);
    1.63 +        log.multipleErrors = true;
    1.64 +
    1.65 +        JavacFileManager.preRegister(context);
    1.66 +        ParserFactory pfac = ParserFactory.instance(context);
    1.67 +
    1.68 +        final String text =
    1.69 +              "public class Foo {\n"
    1.70 +            + "  public static void main(String[] args) {\n"
    1.71 +            + "    if (args.length == 0)\n"
    1.72 +            + "      System.out.println(\"no args\");\n"
    1.73 +            + "    else\n"
    1.74 +            + "      System.out.println(args.length + \" args\");\n"
    1.75 +            + "  }\n"
    1.76 +            + "}\n";
    1.77 +        JavaFileObject fo = new StringJavaFileObject("Foo", text);
    1.78 +        log.useSource(fo);
    1.79 +
    1.80 +        CharSequence cs = fo.getCharContent(true);
    1.81 +        Parser parser = pfac.newParser(cs, false, genEndPos, false);
    1.82 +        JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    1.83 +        log.setEndPosTable(fo, tree.endPositions);
    1.84 +
    1.85 +        TreeScanner ts = new LogTester(log, tree.endPositions);
    1.86 +        ts.scan(tree);
    1.87 +
    1.88 +        check(log.nerrors, 4, "errors");
    1.89 +        check(log.nwarnings, 4, "warnings");
    1.90 +    }
    1.91 +
    1.92 +    private static void check(int found, int expected, String name) {
    1.93 +        if (found == expected)
    1.94 +            System.err.println(found + " " + name + " found, as expected.");
    1.95 +        else {
    1.96 +            System.err.println("incorrect number of " + name + " found.");
    1.97 +            System.err.println("expected: " + expected);
    1.98 +            System.err.println("   found: " + found);
    1.99 +            throw new IllegalStateException("test failed");
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    private static class LogTester extends TreeScanner {
   1.104 +        LogTester(Log log, EndPosTable endPosTable) {
   1.105 +            this.log = log;
   1.106 +            this.endPosTable = endPosTable;
   1.107 +        }
   1.108 +
   1.109 +        public void visitIf(JCTree.JCIf tree) {
   1.110 +            JCDiagnostic.DiagnosticPosition nil = null;
   1.111 +            // generate dummy messages to exercise the log API
   1.112 +            log.error("not.stmt");
   1.113 +            log.error(tree.pos, "not.stmt");
   1.114 +            log.error(tree.pos(), "not.stmt");
   1.115 +            log.error(nil, "not.stmt");
   1.116 +
   1.117 +            log.warning("div.zero");
   1.118 +            log.warning(tree.pos, "div.zero");
   1.119 +            log.warning(tree.pos(), "div.zero");
   1.120 +            log.warning(nil, "div.zero");
   1.121 +        }
   1.122 +
   1.123 +        private Log log;
   1.124 +        private EndPosTable endPosTable;
   1.125 +    }
   1.126 +
   1.127 +    private static class StringJavaFileObject extends SimpleJavaFileObject {
   1.128 +        StringJavaFileObject(String name, String text) {
   1.129 +            super(URI.create(name), JavaFileObject.Kind.SOURCE);
   1.130 +            this.text = text;
   1.131 +        }
   1.132 +        public CharSequence getCharContent(boolean b) {
   1.133 +            return text;
   1.134 +        }
   1.135 +        public InputStream openInputStream() {
   1.136 +            throw new UnsupportedOperationException();
   1.137 +        }
   1.138 +        public OutputStream openOutputStream() {
   1.139 +            throw new UnsupportedOperationException();
   1.140 +        }
   1.141 +        private String text;
   1.142 +    }
   1.143 +}

mercurial