Path: blob/master/test/langtools/jdk/jshell/CustomInputToolBuilder.java
40931 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 824740326* @summary Verify JavaShellToolBuilder uses provided inputs27* @modules jdk.jshell28* @build KullaTesting TestingInputStream29* @run testng CustomInputToolBuilder30*/3132import java.io.ByteArrayInputStream;33import java.io.ByteArrayOutputStream;34import java.io.InputStream;35import java.io.PrintStream;36import java.util.Arrays;37import java.util.List;38import jdk.jshell.tool.JavaShellToolBuilder;39import org.testng.annotations.Test;4041import static org.testng.Assert.assertTrue;4243@Test44public class CustomInputToolBuilder extends KullaTesting {4546private static final String TEST_JDK = "test.jdk";4748public void checkCustomInput() throws Exception {49String testJdk = System.getProperty(TEST_JDK);50try {51System.clearProperty(TEST_JDK);52doTest("System.out.println(\"read: \" + System.in.read());",53"\u0005System.out.println(\"read: \" + System.in.read());",54"read: 97",55"\u0005/exit");56doTest("1 + 1", "\u00051 + 1", "$1 ==> 2", "\u0005/exit");57doTest("for (int i = 0; i < 100; i++) {\nSystem.err.println(i);\n}\n",58"\u0005for (int i = 0; i < 100; i++) {",59"\u0006System.err.println(i);", "\u0006}",60"\u0005/exit");61StringBuilder longInput = new StringBuilder();62String constant = "1_______________1";63longInput.append(constant);64for (int i = 0; i < 100; i++) {65longInput.append(" + ");66longInput.append(constant);67}68doTest(longInput.toString(), "\u0005" + longInput);69} finally {70System.setProperty(TEST_JDK, testJdk);71}72}7374private void doTest(String code, String... expectedLines) throws Exception {75doTest(false, code, expectedLines);76}7778private void doTest(boolean interactiveTerminal, String code, String... expectedLines) throws Exception {79byte[] cmdInputData = (code + "\n/exit\n").getBytes();80InputStream cmdInput = new ByteArrayInputStream(cmdInputData);81InputStream userInput = new ByteArrayInputStream("a\n".getBytes());82ByteArrayOutputStream out = new ByteArrayOutputStream();83PrintStream printOut = new PrintStream(out);8485JavaShellToolBuilder.builder()86.in(cmdInput, userInput)87.out(printOut, printOut, printOut)88.interactiveTerminal(interactiveTerminal)89.promptCapture(true)90.start("--no-startup");9192String actual = new String(out.toByteArray());93List<String> actualLines = Arrays.asList(actual.split("\\R"));9495for (String expectedLine : expectedLines) {96assertTrue(actualLines.contains(expectedLine),97"actual:\n" + actualLines + "\n, expected:\n" + expectedLine);98}99}100101public void checkInteractiveTerminal() throws Exception {102String testJdk = System.getProperty(TEST_JDK);103try {104System.clearProperty(TEST_JDK);105106//note the exact format of the output is not specified here, and the test mostly validates107//the current behavior, and shows the output changes based on the interactiveTerminal setting:108doTest(true,109"System.out.println(\"read: \" + System.in.read());",110"\u001b[?2004h\u0005System.out.println(\"read: \" + System.in.read()\u001b[2D\u001b[2C)\u001b[29D\u001b[29C;",111"\u001b[?2004lread: 97",112"\u001b[?2004h\u0005/exit");113doTest(true,114"1 + 1",115"\u001b[?2004h\u00051 + 1",116"\u001b[?2004l$1 ==> 2",117"\u001b[?2004h\u0005/exit");118} finally {119System.setProperty(TEST_JDK, testJdk);120}121}122}123124125