-
Notifications
You must be signed in to change notification settings - Fork 199
/
ExternalEditorTest.java
274 lines (248 loc) · 9.89 KB
/
ExternalEditorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Testing external editor.
* @bug 8143955 8080843 8163816 8143006 8169828 8171130 8162989 8210808
* @modules jdk.jshell/jdk.internal.jshell.tool
* @build ReplToolTesting CustomEditor EditorTestBase
* @run testng ExternalEditorTest
* @key intermittent
*/
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
public class ExternalEditorTest extends EditorTestBase {
private static Path executionScript;
private static ServerSocket listener;
private DataInputStream inputStream;
private DataOutputStream outputStream;
@Override
public void writeSource(String s) {
try {
outputStream.writeInt(CustomEditor.SOURCE_CODE);
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
outputStream.writeInt(bytes.length);
outputStream.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public String getSource() {
return readString(CustomEditor.GET_SOURCE_CODE);
}
private void sendCode(int code) {
try {
outputStream.writeInt(code);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public void accept() {
sendCode(CustomEditor.ACCEPT_CODE);
}
@Override
public void exit() {
sendCode(CustomEditor.EXIT_CODE);
inputStream = null;
outputStream = null;
}
@Override
public void cancel() {
sendCode(CustomEditor.CANCEL_CODE);
}
protected String getFilename() {
return readString(CustomEditor.GET_FILENAME);
}
private String readString(int code) {
try {
outputStream.writeInt(code);
int length = inputStream.readInt();
byte[] bytes = new byte[length];
inputStream.readFully(bytes);
return new String(bytes, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public void testEditor(boolean defaultStartup, String[] args, ReplTest... tests) {
ReplTest[] t = new ReplTest[tests.length + 1];
t[0] = a -> assertCommandCheckOutput(a, "/set editor " + executionScript,
assertStartsWith("| Editor set to: " + executionScript));
System.arraycopy(tests, 0, t, 1, tests.length);
super.testEditor(defaultStartup, args, t);
}
@Test
public void testStatementSemicolonAddition() {
testEditor(
a -> assertCommand(a, "if (true) {}", ""),
a -> assertCommand(a, "if (true) {} else {}", ""),
a -> assertCommand(a, "Object o", "o ==> null"),
a -> assertCommand(a, "if (true) o = new Object() { int x; }", ""),
a -> assertCommand(a, "if (true) o = new Object() { int y; }", ""),
a -> assertCommand(a, "System.err.flush()", ""), // test still ; for expression statement
a -> assertEditOutput(a, "/ed", "", () -> {
assertEquals(getSource(),
"if (true) {}\n" +
"if (true) {} else {}\n" +
"Object o;\n" +
"if (true) o = new Object() { int x; };\n" +
"if (true) o = new Object() { int y; };\n" +
"System.err.flush();\n");
exit();
})
);
}
@Test
public void testTempFileDeleted() {
String[] fna = new String[1];
testEditor(
a -> assertVariable(a, "int", "a", "0", "0"),
a -> assertEditOutput(a, "/ed 1", "a ==> 10", () -> {
fna[0] = getFilename();
assertTrue(Files.exists(Paths.get(fna[0])), "Test set-up failed: " + fna[0]);
writeSource("\n\n\nint a = 10;\n\n\n");
exit();
}),
a -> assertCommand(a, "if (true) {} else {}", "")
);
assertFalse(Files.exists(Paths.get(fna[0])), "File not deleted: " + fna[0]);
}
private static boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}
@BeforeClass
public static void setUpExternalEditorTest() throws IOException {
listener = new ServerSocket(0);
listener.setSoTimeout(30000);
int localPort = listener.getLocalPort();
executionScript = Paths.get(isWindows() ? "editor.bat" : "editor.sh").toAbsolutePath();
Path java = Paths.get(System.getProperty("java.home")).resolve("bin").resolve("java");
try (BufferedWriter writer = Files.newBufferedWriter(executionScript)) {
if(!isWindows()) {
writer.append(java.toString()).append(" ")
.append(" -cp ").append(System.getProperty("java.class.path"))
.append(" CustomEditor ").append(Integer.toString(localPort)).append(" $@");
executionScript.toFile().setExecutable(true);
} else {
writer.append(java.toString()).append(" ")
.append(" -cp ").append(System.getProperty("java.class.path"))
.append(" CustomEditor ").append(Integer.toString(localPort)).append(" %*");
}
}
}
private Future<?> task;
@Override
void assertEdit(boolean after, String cmd,
Consumer<String> checkInput, Consumer<String> checkOutput, Action action) {
if (!after) {
setCommandInput(cmd + "\n");
task = getExecutor().submit(() -> {
try (Socket socket = listener.accept()) {
inputStream = new DataInputStream(socket.getInputStream());
outputStream = new DataOutputStream(socket.getOutputStream());
checkInput.accept(getSource());
action.accept();
} catch (SocketTimeoutException e) {
fail("Socket timeout exception.\n Output: " + getCommandOutput() +
"\n, error: " + getCommandErrorOutput());
} catch (Throwable e) {
shutdownEditor();
if (e instanceof AssertionError) {
throw (AssertionError) e;
}
throw new RuntimeException(e);
}
});
} else {
try {
task.get();
checkOutput.accept(getCommandOutput());
} catch (ExecutionException e) {
if (e.getCause() instanceof AssertionError) {
throw (AssertionError) e.getCause();
}
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Override
public void shutdownEditor() {
if (outputStream != null) {
exit();
}
}
@Test
public void setUnknownEditor() {
test(
a -> assertCommand(a, "/set editor UNKNOWN", "| Editor set to: UNKNOWN"),
a -> assertCommand(a, "int a;", null),
a -> assertCommandOutputStartsWith(a, "/ed 1",
"| Edit Error:")
);
}
@Test(enabled = false) // TODO 8159229
public void testRemoveTempFile() {
test(new String[]{"--no-startup"},
a -> assertCommandCheckOutput(a, "/set editor " + executionScript,
assertStartsWith("| Editor set to: " + executionScript)),
a -> assertVariable(a, "int", "a", "0", "0"),
a -> assertEditOutput(a, "/ed 1", assertStartsWith("| Edit Error: Failure in read edit file:"), () -> {
sendCode(CustomEditor.REMOVE_CODE);
exit();
}),
a -> assertCommandCheckOutput(a, "/vars", assertVariables())
);
}
@AfterClass
public static void shutdown() throws IOException {
executorShutdown();
if (listener != null) {
listener.close();
}
}
}