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