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