|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 8287885 8296656 7016187 |
| 27 | + * @summary Verify proper function of the "output-file-clash" lint flag |
| 28 | + * @requires os.family == "mac" |
| 29 | + * @library /tools/lib |
| 30 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 31 | + * jdk.compiler/com.sun.tools.javac.main |
| 32 | + * jdk.compiler/com.sun.tools.javac.util |
| 33 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 34 | + * @run main OutputFileClashTest |
| 35 | +*/ |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.nio.file.Paths; |
| 40 | +import java.util.List; |
| 41 | +import java.util.regex.Pattern; |
| 42 | + |
| 43 | +import toolbox.TestRunner; |
| 44 | +import toolbox.ToolBox; |
| 45 | +import toolbox.JavacTask; |
| 46 | +import toolbox.Task; |
| 47 | + |
| 48 | +public class OutputFileClashTest extends TestRunner { |
| 49 | + |
| 50 | + protected ToolBox tb; |
| 51 | + |
| 52 | + public OutputFileClashTest() { |
| 53 | + super(System.err); |
| 54 | + tb = new ToolBox(); |
| 55 | + } |
| 56 | + |
| 57 | + protected void runTests() throws Exception { |
| 58 | + runTests(m -> new Object[] { Paths.get(m.getName()) }); |
| 59 | + } |
| 60 | + |
| 61 | + Path[] findJavaFiles(Path... paths) throws IOException { |
| 62 | + return tb.findJavaFiles(paths); |
| 63 | + } |
| 64 | + |
| 65 | +// Note: in these tests, it's indeterminate which output file gets written first. |
| 66 | +// So we compare the log output to a regex that matches the error either way. |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testBug8287885(Path base) throws Exception { |
| 70 | + testClash(base, |
| 71 | + """ |
| 72 | + public class Test { |
| 73 | + void method1() { |
| 74 | + enum ABC { A, B, C; }; // becomes "Test$1ABC.class" |
| 75 | + } |
| 76 | + void method2() { |
| 77 | + enum Abc { A, B, C; }; // becomes "Test$1Abc.class" |
| 78 | + } |
| 79 | + } |
| 80 | + """, |
| 81 | + "- compiler.warn.output.file.clash: .*Test\\$1(ABC|Abc)\\.class"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testBug8296656(Path base) throws Exception { |
| 86 | + testClash(base, |
| 87 | + """ |
| 88 | + public class Test { |
| 89 | + @interface Annotation { |
| 90 | + interface foo { } |
| 91 | + @interface Foo { } |
| 92 | + } |
| 93 | + } |
| 94 | + """, |
| 95 | + "- compiler.warn.output.file.clash: .*Test\\$Annotation\\$(foo|Foo)\\.class"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testCombiningAcuteAccent(Path base) throws Exception { |
| 100 | + testClash(base, |
| 101 | + """ |
| 102 | + public class Test { |
| 103 | + interface Cafe\u0301 { // macos normalizes "e" + U0301 -> U00e9 |
| 104 | + } |
| 105 | + interface Caf\u00e9 { |
| 106 | + } |
| 107 | + } |
| 108 | + """, |
| 109 | + "- compiler.warn.output.file.clash: .*Test\\$Caf.*\\.class"); |
| 110 | + } |
| 111 | + |
| 112 | + private void testClash(Path base, String javaSource, String regex) throws Exception { |
| 113 | + |
| 114 | + // Compile source |
| 115 | + Path src = base.resolve("src"); |
| 116 | + tb.writeJavaFiles(src, javaSource); |
| 117 | + Path classes = base.resolve("classes"); |
| 118 | + tb.createDirectories(classes); |
| 119 | + Path headers = base.resolve("headers"); |
| 120 | + tb.createDirectories(headers); |
| 121 | + List<String> log = new JavacTask(tb, Task.Mode.CMDLINE) |
| 122 | + .options("-XDrawDiagnostics", "-Werror", "-Xlint:output-file-clash") |
| 123 | + .outdir(classes) |
| 124 | + .headerdir(headers) |
| 125 | + .files(findJavaFiles(src)) |
| 126 | + .run(Task.Expect.FAIL) |
| 127 | + .writeAll() |
| 128 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 129 | + |
| 130 | + // Find expected error line |
| 131 | + Pattern pattern = Pattern.compile(regex); |
| 132 | + if (!log.stream().anyMatch(line -> pattern.matcher(line).matches())) |
| 133 | + throw new Exception("expected error not found: \"" + regex + "\""); |
| 134 | + } |
| 135 | + |
| 136 | + public static void main(String... args) throws Exception { |
| 137 | + new OutputFileClashTest().runTests(); |
| 138 | + } |
| 139 | +} |
0 commit comments