|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 8370865 |
| 27 | + * @summary Check that multiple comma-separated fields work |
| 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 toolbox.JavacTask |
| 33 | + * @run main MultipleFields |
| 34 | +*/ |
| 35 | + |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.nio.file.Paths; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Objects; |
| 41 | + |
| 42 | +import toolbox.TestRunner; |
| 43 | +import toolbox.JavacTask; |
| 44 | +import toolbox.JavaTask; |
| 45 | +import toolbox.Task; |
| 46 | +import toolbox.ToolBox; |
| 47 | + |
| 48 | +public class MultipleFields extends TestRunner { |
| 49 | + |
| 50 | + private ToolBox tb; |
| 51 | + |
| 52 | + public static void main(String... args) throws Exception { |
| 53 | + new MultipleFields().runTests(); |
| 54 | + } |
| 55 | + |
| 56 | + MultipleFields() { |
| 57 | + super(System.err); |
| 58 | + tb = new ToolBox(); |
| 59 | + } |
| 60 | + |
| 61 | + public void runTests() throws Exception { |
| 62 | + runTests(m -> new Object[] { Paths.get(m.getName()) }); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testWithExplicitImport(Path base) throws Exception { |
| 67 | + Path current = base.resolve("."); |
| 68 | + Path src = current.resolve("src"); |
| 69 | + Path classes = current.resolve("classes"); |
| 70 | + tb.writeFile(src.resolve("Test.java"), |
| 71 | + """ |
| 72 | + int f1, f2; |
| 73 | +
|
| 74 | + public void main() { |
| 75 | + f1 = 1; |
| 76 | + f2 = f1 + 1; |
| 77 | + System.out.println("field: " + f1 + "/" + f2); |
| 78 | + } |
| 79 | + """); |
| 80 | + |
| 81 | + Files.createDirectories(classes); |
| 82 | + |
| 83 | + new JavacTask(tb) |
| 84 | + .outdir(classes) |
| 85 | + .files(tb.findJavaFiles(src)) |
| 86 | + .run(Task.Expect.SUCCESS) |
| 87 | + .writeAll(); |
| 88 | + |
| 89 | + var out = new JavaTask(tb) |
| 90 | + .classpath(classes.toString()) |
| 91 | + .className("Test") |
| 92 | + .run() |
| 93 | + .writeAll() |
| 94 | + .getOutputLines(Task.OutputKind.STDOUT); |
| 95 | + |
| 96 | + var expectedOut = List.of("field: 1/2"); |
| 97 | + |
| 98 | + if (!Objects.equals(expectedOut, out)) { |
| 99 | + throw new AssertionError("Incorrect Output, expected: " + expectedOut + |
| 100 | + ", actual: " + out); |
| 101 | + |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments