author | coleenp |
Wed, 15 Nov 2017 08:14:31 -0500 | |
changeset 47894 | 352b17f62ff7 |
parent 47216 | 71c04702a3d5 |
child 48275 | b2190c70a1ac |
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 |
import java.io.IOException; |
|
25 |
import java.io.InputStream; |
|
26 |
import java.io.OutputStream; |
|
27 |
import java.io.OutputStreamWriter; |
|
28 |
import java.io.PrintStream; |
|
29 |
import java.io.Writer; |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
30 |
import java.text.MessageFormat; |
44459 | 31 |
import java.util.HashMap; |
32 |
import java.util.Locale; |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
33 |
import java.util.ResourceBundle; |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
34 |
import java.util.logging.Level; |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
35 |
import java.util.logging.Logger; |
44459 | 36 |
import java.util.regex.Matcher; |
37 |
import java.util.regex.Pattern; |
|
38 |
||
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
39 |
import jdk.jshell.JShell; |
44459 | 40 |
import jdk.jshell.tool.JavaShellToolBuilder; |
41 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
42 |
public class UITesting { |
44459 | 43 |
|
45757
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
44 |
private final boolean laxLineEndings; |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
45 |
|
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
46 |
public UITesting() { |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
47 |
this(false); |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
48 |
} |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
49 |
|
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
50 |
public UITesting(boolean laxLineEndings) { |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
51 |
this.laxLineEndings = laxLineEndings; |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
52 |
} |
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
53 |
|
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
54 |
protected void doRunTest(Test test) throws Exception { |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
55 |
// turn on logging of launch failures |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
56 |
Logger.getLogger("jdk.jshell.execution").setLevel(Level.ALL); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44570
diff
changeset
|
57 |
|
44459 | 58 |
PipeInputStream input = new PipeInputStream(); |
59 |
StringBuilder out = new StringBuilder(); |
|
60 |
PrintStream outS = new PrintStream(new OutputStream() { |
|
61 |
@Override public void write(int b) throws IOException { |
|
62 |
synchronized (out) { |
|
63 |
System.out.print((char) b); |
|
64 |
out.append((char) b); |
|
65 |
out.notifyAll(); |
|
66 |
} |
|
67 |
} |
|
44821
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
68 |
@Override public void write(byte[] b, int off, int len) throws IOException { |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
69 |
synchronized (out) { |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
70 |
String data = new String(b, off, len); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
71 |
System.out.print(data); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
72 |
out.append(data); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
73 |
out.notifyAll(); |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
74 |
} |
c436e60e7af8
8179002: jdk/jshell/MergedTabShiftTabExpressionTest.java fails intermittently
jlahoda
parents:
44811
diff
changeset
|
75 |
} |
44459 | 76 |
}); |
77 |
Thread runner = new Thread(() -> { |
|
78 |
try { |
|
79 |
JavaShellToolBuilder.builder() |
|
80 |
.in(input, input) |
|
81 |
.out(outS) |
|
82 |
.err(outS) |
|
83 |
.promptCapture(true) |
|
84 |
.persistence(new HashMap<>()) |
|
85 |
.locale(Locale.US) |
|
86 |
.run("--no-startup"); |
|
87 |
} catch (Exception ex) { |
|
88 |
throw new IllegalStateException(ex); |
|
89 |
} |
|
90 |
}); |
|
91 |
||
92 |
Writer inputSink = new OutputStreamWriter(input.createOutput()) { |
|
93 |
@Override |
|
94 |
public void write(String str) throws IOException { |
|
95 |
super.write(str); |
|
96 |
flush(); |
|
97 |
} |
|
98 |
}; |
|
99 |
||
100 |
runner.start(); |
|
101 |
||
102 |
try { |
|
103 |
waitOutput(out, "\u0005"); |
|
104 |
test.test(inputSink, out); |
|
105 |
} finally { |
|
106 |
inputSink.write("\003\003/exit"); |
|
107 |
||
108 |
runner.join(1000); |
|
109 |
if (runner.isAlive()) { |
|
110 |
runner.stop(); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
115 |
protected interface Test { |
44459 | 116 |
public void test(Writer inputSink, StringBuilder out) throws Exception; |
117 |
} |
|
118 |
||
119 |
private static final long TIMEOUT; |
|
120 |
||
121 |
static { |
|
122 |
long factor; |
|
123 |
||
124 |
try { |
|
125 |
factor = (long) Double.parseDouble(System.getProperty("test.timeout.factor", "1")); |
|
126 |
} catch (NumberFormatException ex) { |
|
127 |
factor = 1; |
|
128 |
} |
|
129 |
TIMEOUT = 60_000 * factor; |
|
130 |
} |
|
131 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
132 |
protected void waitOutput(StringBuilder out, String expected) { |
45757
1017cd5d6506
8183021: JShell tests: Fix failing tests on Windows
jlahoda
parents:
44821
diff
changeset
|
133 |
expected = expected.replaceAll("\n", laxLineEndings ? "\r?\n" : System.getProperty("line.separator")); |
44459 | 134 |
Pattern expectedPattern = Pattern.compile(expected, Pattern.DOTALL); |
135 |
synchronized (out) { |
|
136 |
long s = System.currentTimeMillis(); |
|
137 |
||
138 |
while (true) { |
|
139 |
Matcher m = expectedPattern.matcher(out); |
|
140 |
if (m.find()) { |
|
46923
0172e4350f65
8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents:
45757
diff
changeset
|
141 |
out.delete(0, m.end()); |
44459 | 142 |
return ; |
143 |
} |
|
144 |
long e = System.currentTimeMillis(); |
|
145 |
if ((e - s) > TIMEOUT) { |
|
146 |
throw new IllegalStateException("Timeout waiting for: " + quote(expected) + ", actual output so far: " + quote(out.toString())); |
|
147 |
} |
|
148 |
try { |
|
149 |
out.wait(TIMEOUT); |
|
150 |
} catch (InterruptedException ex) { |
|
151 |
ex.printStackTrace(); |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
private String quote(String original) { |
|
158 |
StringBuilder output = new StringBuilder(); |
|
159 |
||
160 |
for (char c : original.toCharArray()) { |
|
161 |
if (c < 32) { |
|
162 |
output.append(String.format("\\u%04X", (int) c)); |
|
163 |
} else { |
|
164 |
output.append(c); |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
return output.toString(); |
|
169 |
} |
|
170 |
||
44570
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
171 |
protected String clearOut(String what) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
172 |
return backspace(what.length()) + space(what.length()) + backspace(what.length()); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
173 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
174 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
175 |
protected String backspace(int n) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
176 |
return fill(n, '\010'); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
177 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
178 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
179 |
protected String space(int n) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
180 |
return fill(n, ' '); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
181 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
182 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
183 |
private String fill(int n, char c) { |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
184 |
StringBuilder result = new StringBuilder(n); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
185 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
186 |
while (n-- > 0) |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
187 |
result.append(c); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
188 |
|
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
189 |
return result.toString(); |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
190 |
} |
7fec4e13a5cf
8178077: jshell tool: crash on ctrl-up or ctrl-down
jlahoda
parents:
44459
diff
changeset
|
191 |
|
44811
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
192 |
private final ResourceBundle resources; |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
193 |
{ |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
194 |
resources = ResourceBundle.getBundle("jdk.internal.jshell.tool.resources.l10n", Locale.US, JShell.class.getModule()); |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
195 |
} |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
196 |
|
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
197 |
protected String getResource(String key) { |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
198 |
return resources.getString(key); |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
199 |
} |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
200 |
|
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
201 |
protected String getMessage(String key, Object... args) { |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
202 |
return MessageFormat.format(resources.getString(key), args); |
32c15978a3af
8178035: MergedTabShiftTabTest sometimes time outs
jlahoda
parents:
44683
diff
changeset
|
203 |
} |
44459 | 204 |
private static class PipeInputStream extends InputStream { |
205 |
||
206 |
private static final int INITIAL_SIZE = 128; |
|
207 |
private int[] buffer = new int[INITIAL_SIZE]; |
|
208 |
private int start; |
|
209 |
private int end; |
|
210 |
private boolean closed; |
|
211 |
||
212 |
@Override |
|
213 |
public synchronized int read() throws IOException { |
|
214 |
if (start == end && !closed) { |
|
215 |
inputNeeded(); |
|
216 |
} |
|
217 |
while (start == end) { |
|
218 |
if (closed) { |
|
219 |
return -1; |
|
220 |
} |
|
221 |
try { |
|
222 |
wait(); |
|
223 |
} catch (InterruptedException ex) { |
|
224 |
//ignore |
|
225 |
} |
|
226 |
} |
|
227 |
try { |
|
228 |
return buffer[start]; |
|
229 |
} finally { |
|
230 |
start = (start + 1) % buffer.length; |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public synchronized int read(byte[] b, int off, int len) throws IOException { |
|
236 |
if (b == null) { |
|
237 |
throw new NullPointerException(); |
|
238 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
239 |
throw new IndexOutOfBoundsException(); |
|
240 |
} else if (len == 0) { |
|
241 |
return 0; |
|
242 |
} |
|
243 |
||
244 |
int c = read(); |
|
245 |
if (c == -1) { |
|
246 |
return -1; |
|
247 |
} |
|
248 |
b[off] = (byte)c; |
|
249 |
||
250 |
int totalRead = 1; |
|
251 |
while (totalRead < len && start != end) { |
|
252 |
int r = read(); |
|
253 |
if (r == (-1)) |
|
254 |
break; |
|
255 |
b[off + totalRead++] = (byte) r; |
|
256 |
} |
|
257 |
return totalRead; |
|
258 |
} |
|
259 |
||
260 |
protected void inputNeeded() throws IOException {} |
|
261 |
||
262 |
private synchronized void write(int b) { |
|
263 |
if (closed) { |
|
264 |
throw new IllegalStateException("Already closed."); |
|
265 |
} |
|
266 |
int newEnd = (end + 1) % buffer.length; |
|
267 |
if (newEnd == start) { |
|
268 |
//overflow: |
|
269 |
int[] newBuffer = new int[buffer.length * 2]; |
|
270 |
int rightPart = (end > start ? end : buffer.length) - start; |
|
271 |
int leftPart = end > start ? 0 : start - 1; |
|
272 |
System.arraycopy(buffer, start, newBuffer, 0, rightPart); |
|
273 |
System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart); |
|
274 |
buffer = newBuffer; |
|
275 |
start = 0; |
|
276 |
end = rightPart + leftPart; |
|
277 |
newEnd = end + 1; |
|
278 |
} |
|
279 |
buffer[end] = b; |
|
280 |
end = newEnd; |
|
281 |
notifyAll(); |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
public synchronized void close() { |
|
286 |
closed = true; |
|
287 |
notifyAll(); |
|
288 |
} |
|
289 |
||
290 |
public OutputStream createOutput() { |
|
291 |
return new OutputStream() { |
|
292 |
@Override public void write(int b) throws IOException { |
|
293 |
PipeInputStream.this.write(b); |
|
294 |
} |
|
295 |
@Override |
|
296 |
public void write(byte[] b, int off, int len) throws IOException { |
|
297 |
for (int i = 0 ; i < len ; i++) { |
|
298 |
write(Byte.toUnsignedInt(b[off + i])); |
|
299 |
} |
|
300 |
} |
|
301 |
@Override |
|
302 |
public void close() throws IOException { |
|
303 |
PipeInputStream.this.close(); |
|
304 |
} |
|
305 |
}; |
|
306 |
} |
|
307 |
||
308 |
} |
|
309 |
||
310 |
} |