Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/langtools/jdk/jshell/CustomInputToolBuilder.java
64440 views
1
/*
2
* Copyright (c) 2021, 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 8247403
27
* @summary Verify JavaShellToolBuilder uses provided inputs
28
* @modules jdk.jshell
29
* @build KullaTesting TestingInputStream
30
* @run testng CustomInputToolBuilder
31
*/
32
33
import java.io.ByteArrayInputStream;
34
import java.io.ByteArrayOutputStream;
35
import java.io.InputStream;
36
import java.io.PrintStream;
37
import java.util.Arrays;
38
import java.util.HashMap;
39
import java.util.List;
40
import jdk.jshell.tool.JavaShellToolBuilder;
41
import org.testng.annotations.Test;
42
43
import static org.testng.Assert.assertTrue;
44
45
@Test
46
public class CustomInputToolBuilder extends KullaTesting {
47
48
private static final String TEST_JDK = "test.jdk";
49
50
public void checkCustomInput() throws Exception {
51
String testJdk = System.getProperty(TEST_JDK);
52
try {
53
System.clearProperty(TEST_JDK);
54
doTest("System.out.println(\"read: \" + System.in.read());",
55
"\u0005System.out.println(\"read: \" + System.in.read());",
56
"read: 97",
57
"\u0005/exit");
58
doTest("1 + 1", "\u00051 + 1", "$1 ==> 2", "\u0005/exit");
59
doTest("for (int i = 0; i < 100; i++) {\nSystem.err.println(i);\n}\n",
60
"\u0005for (int i = 0; i < 100; i++) {",
61
"\u0006System.err.println(i);", "\u0006}",
62
"\u0005/exit");
63
StringBuilder longInput = new StringBuilder();
64
String constant = "1_______________1";
65
longInput.append(constant);
66
for (int i = 0; i < 100; i++) {
67
longInput.append(" + ");
68
longInput.append(constant);
69
}
70
doTest(longInput.toString(), "\u0005" + longInput);
71
} finally {
72
System.setProperty(TEST_JDK, testJdk);
73
}
74
}
75
76
private void doTest(String code, String... expectedLines) throws Exception {
77
doTest(false, code, expectedLines);
78
}
79
80
private void doTest(boolean interactiveTerminal, String code, String... expectedLines) throws Exception {
81
byte[] cmdInputData = (code + "\n/exit\n").getBytes();
82
InputStream cmdInput = new ByteArrayInputStream(cmdInputData);
83
InputStream userInput = new ByteArrayInputStream("a\n".getBytes());
84
ByteArrayOutputStream out = new ByteArrayOutputStream();
85
PrintStream printOut = new PrintStream(out);
86
87
JavaShellToolBuilder.builder()
88
.in(cmdInput, userInput)
89
.out(printOut, printOut, printOut)
90
.interactiveTerminal(interactiveTerminal)
91
.promptCapture(true)
92
.persistence(new HashMap<>())
93
.start("--no-startup");
94
95
String actual = new String(out.toByteArray());
96
List<String> actualLines = Arrays.asList(actual.split("\\R"));
97
98
for (String expectedLine : expectedLines) {
99
assertTrue(actualLines.contains(expectedLine),
100
"actual:\n" + actualLines + "\n, expected:\n" + expectedLine);
101
}
102
}
103
104
public void checkInteractiveTerminal() throws Exception {
105
String testJdk = System.getProperty(TEST_JDK);
106
try {
107
System.clearProperty(TEST_JDK);
108
109
//note the exact format of the output is not specified here, and the test mostly validates
110
//the current behavior, and shows the output changes based on the interactiveTerminal setting:
111
doTest(true,
112
"System.out.println(\"read: \" + System.in.read());",
113
"\u001b[?2004h\u0005System.out.println(\"read: \" + System.in.read()\u001b[2D\u001b[2C)\u001b[29D\u001b[29C;",
114
"\u001b[?2004lread: 97",
115
"\u001b[?2004h\u0005/exit");
116
doTest(true,
117
"1 + 1",
118
"\u001b[?2004h\u00051 + 1",
119
"\u001b[?2004l$1 ==> 2",
120
"\u001b[?2004h\u0005/exit");
121
} finally {
122
System.setProperty(TEST_JDK, testJdk);
123
}
124
}
125
}
126
127