Path: blob/master/test/langtools/tools/javac/6304921/TestLog.java
41149 views
/*1* Copyright (c) 2005, 2018, 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* @modules jdk.compiler/com.sun.tools.javac.file28* jdk.compiler/com.sun.tools.javac.parser29* jdk.compiler/com.sun.tools.javac.tree30* jdk.compiler/com.sun.tools.javac.util:+open31* jdk.compiler/com.sun.tools.javac.resources32*/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;52import com.sun.tools.javac.resources.CompilerProperties.Errors;53import com.sun.tools.javac.resources.CompilerProperties.Warnings;5455public class TestLog56{57public static void main(String... args) throws Exception {58test(false);59test(true);60}6162static void test(boolean genEndPos) throws Exception {63Context context = new Context();6465Options options = Options.instance(context);66options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");6768Log log = Log.instance(context);69Factory diagnosticFactory = JCDiagnostic.Factory.instance(context);70Field defaultErrorFlagsField =71JCDiagnostic.Factory.class.getDeclaredField("defaultErrorFlags");7273defaultErrorFlagsField.setAccessible(true);7475Set<DiagnosticFlag> defaultErrorFlags =76(Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory);7778defaultErrorFlags.add(DiagnosticFlag.API);7980JavacFileManager.preRegister(context);81ParserFactory pfac = ParserFactory.instance(context);8283final String text =84"public class Foo {\n"85+ " public static void main(String[] args) {\n"86+ " if (args.length == 0)\n"87+ " System.out.println(\"no args\");\n"88+ " else\n"89+ " System.out.println(args.length + \" args\");\n"90+ " }\n"91+ "}\n";92JavaFileObject fo = new StringJavaFileObject("Foo", text);93log.useSource(fo);9495CharSequence cs = fo.getCharContent(true);96Parser parser = pfac.newParser(cs, false, genEndPos, false);97JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();98log.setEndPosTable(fo, tree.endPositions);99100TreeScanner ts = new LogTester(log, tree.endPositions);101ts.scan(tree);102103check(log.nerrors, 4, "errors");104check(log.nwarnings, 4, "warnings");105}106107private static void check(int found, int expected, String name) {108if (found == expected)109System.err.println(found + " " + name + " found, as expected.");110else {111System.err.println("incorrect number of " + name + " found.");112System.err.println("expected: " + expected);113System.err.println(" found: " + found);114throw new IllegalStateException("test failed");115}116}117118private static class LogTester extends TreeScanner {119LogTester(Log log, EndPosTable endPosTable) {120this.log = log;121this.endPosTable = endPosTable;122}123124public void visitIf(JCTree.JCIf tree) {125JCDiagnostic.DiagnosticPosition nil = null;126// generate dummy messages to exercise the log API127log.error(Errors.NotStmt);128log.error(tree.pos, Errors.NotStmt);129log.error(tree.pos(), Errors.NotStmt);130log.error(nil, Errors.NotStmt);131132log.warning(Warnings.DivZero);133log.warning(tree.pos, Warnings.DivZero);134log.warning(tree.pos(), Warnings.DivZero);135log.warning(nil, Warnings.DivZero);136}137138private Log log;139private EndPosTable endPosTable;140}141142private static class StringJavaFileObject extends SimpleJavaFileObject {143StringJavaFileObject(String name, String text) {144super(URI.create(name), JavaFileObject.Kind.SOURCE);145this.text = text;146}147public CharSequence getCharContent(boolean b) {148return text;149}150public InputStream openInputStream() {151throw new UnsupportedOperationException();152}153public OutputStream openOutputStream() {154throw new UnsupportedOperationException();155}156private String text;157}158}159160161