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  * @summary Verify javac handles preview files in JRT FS reasonably.
 27  * @library /tools/lib
 28  * @modules
 29  *          jdk.compiler/com.sun.tools.javac.api
 30  *          jdk.compiler/com.sun.tools.javac.main
 31  *          jdk.compiler/com.sun.tools.javac.platform
 32  *          jdk.compiler/com.sun.tools.javac.util:+open
 33  * @run junit/othervm PreviewJRTImage
 34  * @run junit/othervm --enable-preview PreviewJRTImage
 35  */
 36 
 37 import java.nio.file.Files;
 38 import java.nio.file.Path;
 39 import java.nio.file.Paths;
 40 import java.util.List;
 41 
 42 import org.junit.Test;
 43 
 44 import toolbox.JavacTask;
 45 import toolbox.Task;
 46 import toolbox.Task.Expect;
 47 import toolbox.Task.Mode;
 48 import toolbox.ToolBox;
 49 
 50 import static org.junit.Assert.*;
 51 
 52 public class PreviewJRTImage {
 53 
 54     private final ToolBox tb = new ToolBox();
 55     private final String specificationVersion = System.getProperty("java.specification.version");
 56 
 57     @Test
 58     public void testVersionInDependency() throws Exception {
 59         Path root = Paths.get(".");
 60         Path src = root.resolve("src");
 61 
 62         tb.writeJavaFiles(src,
 63                 """
 64                 import sun.misc.Unsafe;
 65                 public class Test {
 66                     void test() {
 67                         Boolean b = true;
 68                         synchronized (b) {
 69                         }
 70                         Unsafe u;
 71                     }
 72                 }
 73                 """);
 74 
 75         Path classes = root.resolve("classes");
 76         Files.createDirectories(classes);
 77 
 78         List<String> log;
 79         List<String> expected;
 80 
 81         for (String option : new String[]{"--source", "--release"}) {
 82             for (Mode mode : new Mode[]{Mode.API, Mode.CMDLINE}) {
 83                 // Without preview:
 84                 log = new JavacTask(tb, mode)
 85                         .outdir(classes)
 86                         .options(option, specificationVersion, "-XDrawDiagnostics")
 87                         .files(tb.findJavaFiles(src))
 88                         .run()
 89                         .writeAll()
 90                         .getOutputLines(Task.OutputKind.DIRECT);
 91 
 92                 expected = List.of(
 93                         "Test.java:1:16: compiler.warn.sun.proprietary: sun.misc.Unsafe",
 94                         "Test.java:7:9: compiler.warn.sun.proprietary: sun.misc.Unsafe",
 95                         "Test.java:5:9: compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class",
 96                         "3 warnings"
 97                 );
 98 
 99                 assertEquals(expected, log);
100 
101                 // With preview:
102                 log = new JavacTask(tb, mode)
103                         .outdir(classes)
104                         .options(option, specificationVersion, "--enable-preview", "-XDrawDiagnostics")
105                         .files(tb.findJavaFiles(src))
106                         .run(Expect.FAIL)
107                         .writeAll()
108                         .getOutputLines(Task.OutputKind.DIRECT);
109 
110                 expected = List.of(
111                         "Test.java:1:16: compiler.warn.sun.proprietary: sun.misc.Unsafe",
112                         "Test.java:5:9: compiler.err.type.found.req: java.lang.Boolean, (compiler.misc.type.req.identity)",
113                         "Test.java:7:9: compiler.warn.sun.proprietary: sun.misc.Unsafe",
114                         "1 error",
115                         "2 warnings"
116                 );
117 
118                 assertEquals(expected, log);
119             }
120         }
121     }
122 }