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