Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6304921/TestLog.java
38813 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 630491226* @summary unit test for Log27*/28import java.io.InputStream;29import java.io.IOException;30import java.io.OutputStream;31import java.net.URI;32import javax.tools.JavaFileObject;33import javax.tools.SimpleJavaFileObject;34import com.sun.tools.javac.file.JavacFileManager;35import com.sun.tools.javac.parser.Parser;36import com.sun.tools.javac.parser.ParserFactory;37import com.sun.tools.javac.tree.EndPosTable;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;4445public class TestLog46{47public static void main(String... args) throws IOException {48test(false);49test(true);50}5152static void test(boolean genEndPos) throws IOException {53Context context = new Context();5455Options options = Options.instance(context);56options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");5758Log log = Log.instance(context);59log.multipleErrors = true;6061JavacFileManager.preRegister(context);62ParserFactory pfac = ParserFactory.instance(context);6364final String text =65"public class Foo {\n"66+ " public static void main(String[] args) {\n"67+ " if (args.length == 0)\n"68+ " System.out.println(\"no args\");\n"69+ " else\n"70+ " System.out.println(args.length + \" args\");\n"71+ " }\n"72+ "}\n";73JavaFileObject fo = new StringJavaFileObject("Foo", text);74log.useSource(fo);7576CharSequence cs = fo.getCharContent(true);77Parser parser = pfac.newParser(cs, false, genEndPos, false);78JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();79log.setEndPosTable(fo, tree.endPositions);8081TreeScanner ts = new LogTester(log, tree.endPositions);82ts.scan(tree);8384check(log.nerrors, 4, "errors");85check(log.nwarnings, 4, "warnings");86}8788private static void check(int found, int expected, String name) {89if (found == expected)90System.err.println(found + " " + name + " found, as expected.");91else {92System.err.println("incorrect number of " + name + " found.");93System.err.println("expected: " + expected);94System.err.println(" found: " + found);95throw new IllegalStateException("test failed");96}97}9899private static class LogTester extends TreeScanner {100LogTester(Log log, EndPosTable endPosTable) {101this.log = log;102this.endPosTable = endPosTable;103}104105public void visitIf(JCTree.JCIf tree) {106JCDiagnostic.DiagnosticPosition nil = null;107// generate dummy messages to exercise the log API108log.error("not.stmt");109log.error(tree.pos, "not.stmt");110log.error(tree.pos(), "not.stmt");111log.error(nil, "not.stmt");112113log.warning("div.zero");114log.warning(tree.pos, "div.zero");115log.warning(tree.pos(), "div.zero");116log.warning(nil, "div.zero");117}118119private Log log;120private EndPosTable endPosTable;121}122123private static class StringJavaFileObject extends SimpleJavaFileObject {124StringJavaFileObject(String name, String text) {125super(URI.create(name), JavaFileObject.Kind.SOURCE);126this.text = text;127}128public CharSequence getCharContent(boolean b) {129return text;130}131public InputStream openInputStream() {132throw new UnsupportedOperationException();133}134public OutputStream openOutputStream() {135throw new UnsupportedOperationException();136}137private String text;138}139}140141142