1 /*
  2  * Copyright (c) 2018, 2022, 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 8199193
 27  * @summary Tests for the --enable-preview option
 28  * @run testng ToolEnablePreviewTest
 29  * @ignore 8316628
 30  */
 31 
 32 import java.io.IOException;
 33 import java.io.Writer;
 34 import java.nio.file.Files;
 35 import java.nio.file.Path;
 36 import java.nio.file.Paths;
 37 import org.testng.annotations.Test;
 38 
 39 import static org.testng.Assert.assertTrue;
 40 
 41 public class ToolEnablePreviewTest extends ReplToolTesting {
 42 
 43     @Test
 44     public void testOptionDebug() {
 45         String release = System.getProperty("java.specification.version");
 46         test(
 47                 (a) -> assertCommand(a, "/debug b",
 48                         "RemoteVM Options: []\n"
 49                         + "Compiler options: []"),
 50                 (a) -> assertCommand(a, "/env --enable-preview",
 51                         "|  Setting new options and restoring state."),
 52                 (a) -> assertCommandCheckOutput(a, "/debug b", s -> {
 53                     assertTrue(s.contains("RemoteVM Options: [--enable-preview]"));
 54                     assertTrue(s.contains("Compiler options: [-source, " + release + ", --enable-preview]")
 55                             || s.contains("Compiler options: [--enable-preview, -source, " + release + "]"),
 56                             "\nExpected -- " + "Compiler options: [-source, " + release + ", --enable-preview]"
 57                             + "\nOr -- " + "Compiler options: [--enable-preview, -source, " + release + "]"
 58                             + "\nBut got -- " + s);
 59                 })
 60         );
 61     }
 62 
 63     @Test
 64     public void testCommandLineFlag() {
 65         String release = System.getProperty("java.specification.version");
 66         test(new String[] {"--enable-preview"},
 67                 (a) -> assertCommandCheckOutput(a, "/debug b", s -> {
 68                     assertTrue(s.contains("RemoteVM Options: [--enable-preview]"));
 69                     assertTrue(s.contains("Compiler options: [-source, " + release + ", --enable-preview]")
 70                             || s.contains("Compiler options: [--enable-preview, -source, " + release + "]"),
 71                             "\nExpected -- " + "Compiler options: [-source, " + release + ", --enable-preview]"
 72                             + "\nOr -- " + "Compiler options: [--enable-preview, -source, " + release + "]"
 73                             + "\nBut got -- " + s);
 74                 })
 75         );
 76     }
 77 
 78     @Test
 79     public void testCompilerTestFlagEnv() throws IOException {
 80         Path startupFile = Paths.get("startup.repl");
 81         try (Writer w = Files.newBufferedWriter(startupFile)) {
 82             w.write("""
 83                     import java.util.function.*;
 84                     """);
 85         }
 86         test(new String[] {"-C", "-XDforcePreview", "-startup", startupFile.toString()},
 87                 (a) -> assertCommandOutputContains(a, "Function<Integer,Integer> f = (var i) -> i + i",
 88                         "Error", "preview feature"),
 89                 (a) -> assertCommand(a, "/env --enable-preview",
 90                         "|  Setting new options and restoring state."),
 91                 (a) -> assertCommandOutputContains(a, "Function<Integer,Integer> f = (var i) -> i + i",
 92                         "f ==> ")
 93         );
 94     }
 95 
 96     @Test
 97     public void testCompilerTestFlag() {
 98         test(new String[] {"-C", "-XDforcePreview", "--enable-preview"},
 99                 (a) -> assertCommandOutputContains(a, "Function<Integer,Integer> f = (var i) -> i + i",
100                         "f ==> "),
101                 (a) -> assertCommandOutputContains(a, "f.apply(2)", "==> 4")
102         );
103     }
104 
105 }