author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 47050 | langtools/test/jdk/jshell/ToolTabSnippetTest.java@72ec64aeaa57 |
child 47484 | d0cb66d8cbf1 |
child 47517 | b5ad886110b3 |
permissions | -rw-r--r-- |
44459 | 1 |
/* |
2 |
* Copyright (c) 2017, 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 |
|
47050 | 26 |
* @bug 8177076 8185426 |
44459 | 27 |
* @modules |
28 |
* jdk.compiler/com.sun.tools.javac.api |
|
29 |
* jdk.compiler/com.sun.tools.javac.main |
|
47050 | 30 |
* jdk.jshell/jdk.internal.jshell.tool |
44459 | 31 |
* jdk.jshell/jdk.internal.jshell.tool.resources:open |
32 |
* jdk.jshell/jdk.jshell:open |
|
33 |
* @library /tools/lib |
|
34 |
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask |
|
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
35 |
* @build Compiler UITesting |
46185
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
44811
diff
changeset
|
36 |
* @build ToolTabSnippetTest |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
44811
diff
changeset
|
37 |
* @run testng/timeout=300 ToolTabSnippetTest |
44459 | 38 |
*/ |
39 |
||
40 |
import java.io.IOException; |
|
41 |
import java.lang.reflect.Field; |
|
42 |
import java.nio.file.Files; |
|
43 |
import java.nio.file.Path; |
|
44 |
import java.nio.file.Paths; |
|
45 |
import java.util.Arrays; |
|
47050 | 46 |
import java.util.concurrent.CountDownLatch; |
44459 | 47 |
import java.util.jar.JarEntry; |
48 |
import java.util.jar.JarOutputStream; |
|
49 |
import java.util.regex.Pattern; |
|
50 |
||
47050 | 51 |
import jdk.internal.jshell.tool.ConsoleIOContextTestSupport; |
44459 | 52 |
import org.testng.annotations.Test; |
53 |
||
54 |
@Test |
|
46185
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
44811
diff
changeset
|
55 |
public class ToolTabSnippetTest extends UITesting { |
44459 | 56 |
|
57 |
public void testExpression() throws Exception { |
|
58 |
Path classes = prepareZip(); |
|
59 |
doRunTest((inputSink, out) -> { |
|
60 |
inputSink.write("/env -class-path " + classes.toString() + "\n"); |
|
61 |
waitOutput(out, Pattern.quote(getResource("jshell.msg.set.restore")) + "\n\u0005"); |
|
62 |
inputSink.write("import jshelltest.*;\n"); |
|
63 |
waitOutput(out, "\n\u0005"); |
|
64 |
||
65 |
//-> <tab> |
|
66 |
inputSink.write("\011"); |
|
67 |
waitOutput(out, getMessage("jshell.console.completion.all.completions.number", "[0-9]+")); |
|
68 |
inputSink.write("\011"); |
|
69 |
waitOutput(out, ".*String.*StringBuilder.*\n\r\u0005"); |
|
70 |
||
71 |
//new JShellTes<tab> |
|
72 |
inputSink.write("new JShellTes\011"); |
|
73 |
waitOutput(out, "t\nJShellTest\\( JShellTestAux\\( \n\r\u0005new JShellTest"); |
|
74 |
||
75 |
//new JShellTest<tab> |
|
76 |
inputSink.write("\011"); |
|
77 |
waitOutput(out, "JShellTest\\( JShellTestAux\\( \n" + |
|
78 |
"\n" + |
|
79 |
Pattern.quote(getResource("jshell.console.completion.current.signatures")) + "\n" + |
|
80 |
"jshelltest.JShellTest\n" + |
|
81 |
"\n" + |
|
82 |
Pattern.quote(getResource("jshell.console.see.documentation")) + "\n" + |
|
83 |
"\r\u0005new JShellTest"); |
|
84 |
inputSink.write("\011"); |
|
85 |
waitOutput(out, "jshelltest.JShellTest\n" + |
|
86 |
"JShellTest 0\n" + |
|
87 |
"\r\u0005new JShellTest"); |
|
88 |
inputSink.write("\011"); |
|
89 |
waitOutput(out, "JShellTest\\( JShellTestAux\\( \n" + |
|
90 |
"\n" + |
|
91 |
Pattern.quote(getResource("jshell.console.completion.current.signatures")) + "\n" + |
|
92 |
"jshelltest.JShellTest\n" + |
|
93 |
"\n" + |
|
94 |
Pattern.quote(getResource("jshell.console.see.documentation")) + "\n" + |
|
95 |
"\r\u0005new JShellTest"); |
|
96 |
||
97 |
//new JShellTest(<tab> |
|
98 |
inputSink.write("(\011"); |
|
99 |
waitOutput(out, "\\(\n" + |
|
100 |
Pattern.quote(getResource("jshell.console.completion.current.signatures")) + "\n" + |
|
101 |
"JShellTest\\(String str\\)\n" + |
|
102 |
"JShellTest\\(String str, int i\\)\n" + |
|
103 |
"\n" + |
|
104 |
Pattern.quote(getResource("jshell.console.see.documentation")) + "\n" + |
|
105 |
"\r\u0005new JShellTest\\("); |
|
106 |
inputSink.write("\011"); |
|
107 |
waitOutput(out, "JShellTest\\(String str\\)\n" + |
|
108 |
"JShellTest 1\n" + |
|
109 |
"1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" + |
|
110 |
"\n" + |
|
111 |
Pattern.quote(getResource("jshell.console.see.next.page")) + "\n" + |
|
112 |
"\r\u0005new JShellTest\\("); |
|
113 |
inputSink.write("\011"); |
|
114 |
waitOutput(out, "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" + |
|
115 |
"\n" + |
|
116 |
Pattern.quote(getResource("jshell.console.see.next.javadoc")) + "\n" + |
|
117 |
"\r\u0005new JShellTest\\("); |
|
118 |
inputSink.write("\011"); |
|
119 |
waitOutput(out, "JShellTest\\(String str, int i\\)\n" + |
|
120 |
"JShellTest 2\n" + |
|
121 |
"\n" + |
|
122 |
getMessage("jshell.console.completion.all.completions.number", "[0-9]+") + "\n" + |
|
123 |
"\r\u0005new JShellTest\\("); |
|
124 |
inputSink.write("\011"); |
|
125 |
waitOutput(out, ".*String.*StringBuilder.*\n\r\u0005new JShellTest\\("); |
|
126 |
||
127 |
inputSink.write("\u0003String str = \"\";\nnew JShellTest("); |
|
128 |
waitOutput(out, "\u0005new JShellTest\\("); |
|
129 |
||
130 |
inputSink.write("\011"); |
|
131 |
waitOutput(out, "\n" + |
|
132 |
"str \n" + |
|
133 |
"\n" + |
|
134 |
Pattern.quote(getResource("jshell.console.completion.current.signatures")) + "\n" + |
|
135 |
"JShellTest\\(String str\\)\n" + |
|
136 |
"JShellTest\\(String str, int i\\)\n" + |
|
137 |
"\n" + |
|
138 |
Pattern.quote(getResource("jshell.console.see.documentation")) + "\n" + |
|
139 |
"\r\u0005new JShellTest\\("); |
|
140 |
inputSink.write("\011"); |
|
141 |
waitOutput(out, "JShellTest\\(String str\\)\n" + |
|
142 |
"JShellTest 1\n" + |
|
143 |
"1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" + |
|
144 |
"\n" + |
|
145 |
Pattern.quote(getResource("jshell.console.see.next.page")) + "\n" + |
|
146 |
"\r\u0005new JShellTest\\("); |
|
147 |
inputSink.write("\011"); |
|
148 |
waitOutput(out, "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n" + |
|
149 |
"\n" + |
|
150 |
Pattern.quote(getResource("jshell.console.see.next.javadoc")) + "\n" + |
|
151 |
"\r\u0005new JShellTest\\("); |
|
152 |
inputSink.write("\011"); |
|
153 |
waitOutput(out, "JShellTest\\(String str, int i\\)\n" + |
|
154 |
"JShellTest 2\n" + |
|
155 |
"\n" + |
|
156 |
getMessage("jshell.console.completion.all.completions.number", "[0-9]+") + "\n" + |
|
157 |
"\r\u0005new JShellTest\\("); |
|
158 |
inputSink.write("\011"); |
|
159 |
waitOutput(out, ".*String.*StringBuilder.*\n\r\u0005new JShellTest\\("); |
|
160 |
||
161 |
inputSink.write("\u0003JShellTest t = new JShellTest\011"); |
|
162 |
waitOutput(out, "\u0005JShellTest t = new JShellTest\n" + |
|
163 |
"JShellTest\\( \n" + |
|
164 |
"\n" + |
|
165 |
Pattern.quote(getResource("jshell.console.completion.current.signatures")) + "\n" + |
|
166 |
"jshelltest.JShellTest\n" + |
|
167 |
"\n" + |
|
168 |
Pattern.quote(getResource("jshell.console.completion.all.completions")) + "\n" + |
|
169 |
"\r\u0005JShellTest t = new JShellTest"); |
|
170 |
inputSink.write("\011"); |
|
171 |
waitOutput(out, "JShellTest\\( JShellTestAux\\( \n" + |
|
172 |
"\n" + |
|
173 |
Pattern.quote(getResource("jshell.console.see.documentation")) + "\n" + |
|
174 |
"\r\u0005JShellTest t = new JShellTest"); |
|
175 |
||
176 |
inputSink.write("\u0003JShellTest t = new \011"); |
|
177 |
waitOutput(out, "\u0005JShellTest t = new \n" + |
|
178 |
"JShellTest\\( \n" + |
|
179 |
"\n" + |
|
180 |
getMessage("jshell.console.completion.all.completions.number", "[0-9]+") + "\n" + |
|
181 |
"\r\u0005JShellTest t = new "); |
|
182 |
inputSink.write("\011"); |
|
183 |
waitOutput(out, ".*String.*StringBuilder.*\n\r\u0005JShellTest t = new "); |
|
184 |
||
185 |
inputSink.write("\u0003class JShelX{}\n"); |
|
186 |
inputSink.write("new JShel\011"); |
|
187 |
waitOutput(out, "\u0005new JShel\n" + |
|
188 |
"JShelX\\(\\) JShellTest\\( JShellTestAux\\( \n" + |
|
189 |
"\r\u0005new JShel"); |
|
190 |
||
191 |
//no crash: |
|
192 |
inputSink.write("\u0003new Stringbuil\011"); |
|
193 |
waitOutput(out, "\u0005new Stringbuil\u0007"); |
|
194 |
}); |
|
195 |
} |
|
196 |
||
47050 | 197 |
public void testCleaningCompletionTODO() throws Exception { |
198 |
doRunTest((inputSink, out) -> { |
|
199 |
CountDownLatch testCompleteComputationStarted = new CountDownLatch(1); |
|
200 |
CountDownLatch testCompleteComputationContinue = new CountDownLatch(1); |
|
201 |
ConsoleIOContextTestSupport.IMPL = new ConsoleIOContextTestSupport() { |
|
202 |
@Override |
|
203 |
protected void willComputeCompletionCallback() { |
|
204 |
if (testCompleteComputationStarted != null) { |
|
205 |
testCompleteComputationStarted.countDown(); |
|
206 |
} |
|
207 |
if (testCompleteComputationContinue != null) { |
|
208 |
try { |
|
209 |
testCompleteComputationContinue.await(); |
|
210 |
} catch (InterruptedException ex) { |
|
211 |
throw new IllegalStateException(ex); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
}; |
|
216 |
//-> <tab> |
|
217 |
inputSink.write("\011"); |
|
218 |
testCompleteComputationStarted.await(); |
|
219 |
//-> <tab><tab> |
|
220 |
inputSink.write("\011\011"); |
|
221 |
testCompleteComputationContinue.countDown(); |
|
222 |
waitOutput(out, "\u0005"); |
|
223 |
//-> <tab> |
|
224 |
inputSink.write("\011"); |
|
225 |
waitOutput(out, "\u0005"); |
|
226 |
ConsoleIOContextTestSupport.IMPL = null; |
|
227 |
}); |
|
228 |
} |
|
229 |
||
44459 | 230 |
private Path prepareZip() { |
231 |
String clazz1 = |
|
232 |
"package jshelltest;\n" + |
|
233 |
"/**JShellTest 0" + |
|
234 |
" */\n" + |
|
235 |
"public class JShellTest {\n" + |
|
236 |
" /**JShellTest 1\n" + |
|
237 |
" * <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1\n" + |
|
238 |
" * <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1\n" + |
|
239 |
" * <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1 <p>1\n" + |
|
240 |
" */\n" + |
|
241 |
" public JShellTest(String str) {}\n" + |
|
242 |
" /**JShellTest 2" + |
|
243 |
" */\n" + |
|
244 |
" public JShellTest(String str, int i) {}\n" + |
|
245 |
"}\n"; |
|
246 |
||
247 |
String clazz2 = |
|
248 |
"package jshelltest;\n" + |
|
249 |
"/**JShellTestAux 0" + |
|
250 |
" */\n" + |
|
251 |
"public class JShellTestAux {\n" + |
|
252 |
" /**JShellTest 1" + |
|
253 |
" */\n" + |
|
254 |
" public JShellTestAux(String str) { }\n" + |
|
255 |
" /**JShellTest 2" + |
|
256 |
" */\n" + |
|
257 |
" public JShellTestAux(String str, int i) { }\n" + |
|
258 |
"}\n"; |
|
259 |
||
260 |
Path srcZip = Paths.get("src.zip"); |
|
261 |
||
262 |
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) { |
|
263 |
out.putNextEntry(new JarEntry("jshelltest/JShellTest.java")); |
|
264 |
out.write(clazz1.getBytes()); |
|
265 |
out.putNextEntry(new JarEntry("jshelltest/JShellTestAux.java")); |
|
266 |
out.write(clazz2.getBytes()); |
|
267 |
} catch (IOException ex) { |
|
268 |
throw new IllegalStateException(ex); |
|
269 |
} |
|
270 |
||
271 |
compiler.compile(clazz1, clazz2); |
|
272 |
||
273 |
try { |
|
274 |
Field availableSources = Class.forName("jdk.jshell.SourceCodeAnalysisImpl").getDeclaredField("availableSourcesOverride"); |
|
275 |
availableSources.setAccessible(true); |
|
276 |
availableSources.set(null, Arrays.asList(srcZip)); |
|
277 |
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException | ClassNotFoundException ex) { |
|
278 |
throw new IllegalStateException(ex); |
|
279 |
} |
|
280 |
||
281 |
return compiler.getClassDir(); |
|
282 |
} |
|
283 |
//where: |
|
284 |
private final Compiler compiler = new Compiler(); |
|
285 |
||
286 |
} |