|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8198317 |
| 27 | + * @summary Enhance JavacTool.getTask for flexibility |
| 28 | + * @library /tools/lib |
| 29 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 30 | + * jdk.compiler/com.sun.tools.javac.main |
| 31 | + * jdk.compiler/com.sun.tools.javac.util |
| 32 | + * @build toolbox.ToolBox |
| 33 | + * @run main TestContextLoggingOutput |
| 34 | + */ |
| 35 | + |
| 36 | +import java.io.StringWriter; |
| 37 | +import java.io.PrintWriter; |
| 38 | +import java.io.ByteArrayOutputStream; |
| 39 | +import java.io.PrintStream; |
| 40 | +import java.net.URI; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Arrays; |
| 43 | +import javax.tools.ToolProvider; |
| 44 | +import javax.tools.SimpleJavaFileObject; |
| 45 | +import javax.tools.JavaFileObject; |
| 46 | + |
| 47 | +import toolbox.ToolBox; |
| 48 | +import toolbox.TestRunner; |
| 49 | +import static toolbox.ToolBox.lineSeparator; |
| 50 | + |
| 51 | +public class TestContextLoggingOutput extends TestRunner { |
| 52 | + ToolBox tb; |
| 53 | + |
| 54 | + public TestContextLoggingOutput() { |
| 55 | + super(System.err); |
| 56 | + tb = new ToolBox(); |
| 57 | + } |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + TestContextLoggingOutput t = new TestContextLoggingOutput(); |
| 61 | + t.runTests(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testLogSettingInJavacTool() throws Exception { |
| 66 | + String code = """ |
| 67 | + import java.io.Serializable; |
| 68 | + class Test implements Serializable { |
| 69 | + public static final int serialVersionUID = 1; |
| 70 | + }"""; |
| 71 | + |
| 72 | + List<String> expected = Arrays.asList( |
| 73 | + "Test.java:3:29: compiler.warn.long.SVUID: Test", |
| 74 | + "1 warning"); |
| 75 | + |
| 76 | + List<? extends JavaFileObject> files = Arrays.asList(new MemFile("Test.java", code)); |
| 77 | + |
| 78 | + // Situation: out is null and the value is not set in the context. |
| 79 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 80 | + PrintStream printStream = new PrintStream(baos); |
| 81 | + PrintStream prev = System.err; |
| 82 | + try { |
| 83 | + System.setErr(printStream); |
| 84 | + ToolProvider.getSystemJavaCompiler() |
| 85 | + .getTask(null, null, null, Arrays.asList("-XDrawDiagnostics", "-Xlint:serial"), null, files) |
| 86 | + .call(); |
| 87 | + tb.checkEqual(expected, Arrays.asList(baos.toString().split(lineSeparator))); |
| 88 | + } finally { |
| 89 | + System.setErr(prev); |
| 90 | + } |
| 91 | + |
| 92 | + // Situation: out is not null and out is a PrintWriter. |
| 93 | + StringWriter stringWriter2 = new StringWriter(); |
| 94 | + PrintWriter expectedPW2 = new PrintWriter(stringWriter2); |
| 95 | + ToolProvider.getSystemJavaCompiler() |
| 96 | + .getTask(expectedPW2, null, null, Arrays.asList("-XDrawDiagnostics", "-Xlint:serial"), null, files) |
| 97 | + .call(); |
| 98 | + tb.checkEqual(expected, Arrays.asList(stringWriter2.toString().split(lineSeparator))); |
| 99 | + |
| 100 | + // Situation: out is not null and out is not a PrintWriter. |
| 101 | + StringWriter stringWriter3 = new StringWriter(); |
| 102 | + ToolProvider.getSystemJavaCompiler() |
| 103 | + .getTask(stringWriter3, null, null, Arrays.asList("-XDrawDiagnostics", "-Xlint:serial"), null, files) |
| 104 | + .call(); |
| 105 | + tb.checkEqual(expected, Arrays.asList(stringWriter3.toString().split(lineSeparator))); |
| 106 | + } |
| 107 | + |
| 108 | + class MemFile extends SimpleJavaFileObject { |
| 109 | + public final String text; |
| 110 | + |
| 111 | + MemFile(String name, String text) { |
| 112 | + super(URI.create(name), JavaFileObject.Kind.SOURCE); |
| 113 | + this.text = text; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getName() { |
| 118 | + return uri.toString(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public String getCharContent(boolean ignoreEncodingErrors) { |
| 123 | + return text; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments