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