1 /*
2  * Copyright (c) 2016, 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 8157200 8163840 8154513
27  * @summary Tests of what information is retained across jshell tool runs
28  * @modules jdk.jshell/jdk.internal.jshell.tool
29  * @build ToolRetainTest ReplToolTesting
30  * @run testng ToolRetainTest
31  */
32 
33 import java.util.Locale;
34 import org.testng.annotations.Test;
35 
36 @Test
37 public class ToolRetainTest extends ReplToolTesting {
38 
testRetainMode()39     public void testRetainMode() {
40         test(
41                 (a) -> assertCommand(a, "/set mode trm -quiet", "|  Created new feedback mode: trm"),
42                 (a) -> assertCommand(a, "/set feedback trm", ""),
43                 (a) -> assertCommand(a, "/set format trm display '{name}:{value}'", ""),
44                 (a) -> assertCommand(a, "int x = 45", "x:45"),
45                 (a) -> assertCommand(a, "/set mode -retain trm", ""),
46                 (a) -> assertCommand(a, "/exit", "")
47         );
48         test(
49                 (a) -> assertCommandOutputContains(a, "/set mode trm",
50                         "/set format trm display \"{name}:{value}\""),
51                 (a) -> assertCommand(a, "/set feedback trm", ""),
52                 (a) -> assertCommand(a, "int x = 45", "x:45")
53         );
54     }
55 
testRetain2Mode()56     public void testRetain2Mode() {
57         test(
58                 (a) -> assertCommand(a, "/set mode trm1 -quiet", "|  Created new feedback mode: trm1"),
59                 (a) -> assertCommand(a, "/set mode -retain trm1", ""),
60                 (a) -> assertCommand(a, "/set feedback -retain trm1", ""),
61                 (a) -> assertCommand(a, "/set format trm1 display '{name}:{value}'", ""),
62                 (a) -> assertCommand(a, "int x = 66", "x:66"),
63                 (a) -> assertCommand(a, "/set mode -retain trm1", ""),
64                 (a) -> assertCommand(a, "/exit", "")
65         );
66         test(Locale.ROOT, true, new String[0], "",
67                 (a) -> assertCommand(a, "/set mode trm2 -quiet", ""),
68                 (a) -> assertCommand(a, "/set format trm2 display '{name}={value}'", ""),
69                 (a) -> assertCommand(a, "int x = 45", "x:45"),
70                 (a) -> assertCommand(a, "/set mode -retain trm2", ""),
71                 (a) -> assertCommand(a, "/exit", "")
72         );
73         test(Locale.ROOT, true, new String[0], "",
74                 (a) -> assertCommandOutputContains(a, "/set mode trm1",
75                         "/set format trm1 display \"{name}:{value}\""),
76                 (a) -> assertCommand(a, "/set format trm2 display",
77                         "|  /set format trm2 display \"{name}={value}\""),
78                 (a) -> assertCommand(a, "int x = 99", "x:99"),
79                 (a) -> assertCommand(a, "/set feedback trm2", ""),
80                 (a) -> assertCommand(a, "int z = 77", "z=77")
81         );
82     }
83 
testRetainFeedback()84     public void testRetainFeedback() {
85         test(
86                 (a) -> assertCommand(a, "/set feedback -retain verbose", "|  Feedback mode: verbose"),
87                 (a) -> assertCommand(a, "/exit", "")
88         );
89         test(
90                 (a) -> assertCommandOutputStartsWith(a, "/set feedback",
91                         "|  /set feedback -retain verbose\n" +
92                         "|  \n" +
93                         "|  "),
94                 (a) -> assertCommandOutputContains(a, "int h =8", "|  created variable h : int")
95         );
96     }
97 
testRetainFeedbackBlank()98     public void testRetainFeedbackBlank() {
99         String feedbackOut =
100                         "|  /set feedback -retain verbose\n" +
101                         "|  \n" +
102                         "|  Available feedback modes:\n" +
103                         "|     concise\n" +
104                         "|     normal\n" +
105                         "|     silent\n" +
106                         "|     verbose";
107         test(
108                 (a) -> assertCommand(a, "/set feedback verbose", "|  Feedback mode: verbose"),
109                 (a) -> assertCommand(a, "/set feedback -retain", ""),
110                 (a) -> assertCommand(a, "/set feedback", feedbackOut),
111                 (a) -> assertCommand(a, "/exit", "")
112         );
113         test(
114                 (a) -> assertCommand(a, "/set feedback", feedbackOut),
115                 (a) -> assertCommandOutputContains(a, "int qw = 5", "|  created variable qw : int")
116         );
117     }
118 
testRetainEditor()119     public void testRetainEditor() {
120         test(
121                 (a) -> assertCommand(a, "/set editor -retain nonexistent",
122                         "|  Editor set to: nonexistent\n" +
123                         "|  Editor setting retained: nonexistent"),
124                 (a) -> assertCommand(a, "/exit", "")
125         );
126         test(
127                 (a) -> assertCommand(a, "/set editor", "|  /set editor -retain nonexistent"),
128                 (a) -> assertCommandOutputContains(a, "int h =8", ""),
129                 (a) -> assertCommandOutputContains(a, "/edit h", "Edit Error:")
130         );
131     }
132 
testRetainEditorBlank()133     public void testRetainEditorBlank() {
134         test(
135                 (a) -> assertCommand(a, "/set editor nonexistent", "|  Editor set to: nonexistent"),
136                 (a) -> assertCommand(a, "/set editor -retain", "|  Editor setting retained: nonexistent"),
137                 (a) -> assertCommand(a, "/exit", "")
138         );
139         test(
140                 (a) -> assertCommandOutputContains(a, "int h =8", ""),
141                 (a) -> assertCommandOutputContains(a, "/edit h", "Edit Error:")
142         );
143     }
144 
testRetainModeNeg()145     public void testRetainModeNeg() {
146         test(
147                 (a) -> assertCommandOutputStartsWith(a, "/set mode -retain verbose",
148                         "|  Not valid with a predefined mode"),
149                 (a) -> assertCommandOutputStartsWith(a, "/set mode -retain ????",
150                         "|  Expected a feedback mode name: ????")
151         );
152     }
153 
testRetainFeedbackNeg()154     public void testRetainFeedbackNeg() {
155         test(
156                 (a) -> assertCommandOutputStartsWith(a, "/set feedback -retain babble1",
157                         "|  Does not match any current feedback mode"),
158                 (a) -> assertCommandOutputStartsWith(a, "/set mode trfn",
159                         "|  To create a new mode either the -command or the -quiet option must be used -- \n" +
160                         "|  Does not match any current feedback mode: trfn -- /set mode trfn"),
161                 (a) -> assertCommand(a, "/set mode trfn -command",
162                         "|  Created new feedback mode: trfn"),
163                 (a) -> assertCommandOutputContains(a, "/set feedback -retain trfn",
164                         "is predefined or has been retained"),
165                 (a) -> assertCommandOutputStartsWith(a, "/set feedback -retain !!!!",
166                         "|  Expected a feedback mode name: !!!!")
167         );
168     }
169 
testNoRetainMode()170     public void testNoRetainMode() {
171         test(
172                 (a) -> assertCommand(a, "/set mode trm -quiet", "|  Created new feedback mode: trm"),
173                 (a) -> assertCommand(a, "/set feedback trm", ""),
174                 (a) -> assertCommand(a, "/set format trm display '{name}:{value}'", ""),
175                 (a) -> assertCommand(a, "int x = 45", "x:45"),
176                 (a) -> assertCommand(a, "/exit", "")
177         );
178         test(
179                 (a) -> assertCommandOutputStartsWith(a, "/set feedback trm",
180                         "|  Does not match any current feedback mode"),
181                 (a) -> assertCommandOutputContains(a, "int x = 45", "==> 45")
182         );
183     }
184 
testNoRetainFeedback()185     public void testNoRetainFeedback() {
186         test(
187                 (a) -> assertCommand(a, "/set feedback verbose", "|  Feedback mode: verbose"),
188                 (a) -> assertCommand(a, "/exit", "")
189         );
190         test(
191                 (a) -> assertCommand(a, "int h =8", "h ==> 8")
192         );
193     }
194 
195 }
196