Path: blob/master/test/langtools/jdk/jshell/CustomInputToolBuilder.java
64440 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.HashMap;38import java.util.List;39import jdk.jshell.tool.JavaShellToolBuilder;40import org.testng.annotations.Test;4142import static org.testng.Assert.assertTrue;4344@Test45public class CustomInputToolBuilder extends KullaTesting {4647private static final String TEST_JDK = "test.jdk";4849public void checkCustomInput() throws Exception {50String testJdk = System.getProperty(TEST_JDK);51try {52System.clearProperty(TEST_JDK);53doTest("System.out.println(\"read: \" + System.in.read());",54"\u0005System.out.println(\"read: \" + System.in.read());",55"read: 97",56"\u0005/exit");57doTest("1 + 1", "\u00051 + 1", "$1 ==> 2", "\u0005/exit");58doTest("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");62StringBuilder longInput = new StringBuilder();63String constant = "1_______________1";64longInput.append(constant);65for (int i = 0; i < 100; i++) {66longInput.append(" + ");67longInput.append(constant);68}69doTest(longInput.toString(), "\u0005" + longInput);70} finally {71System.setProperty(TEST_JDK, testJdk);72}73}7475private void doTest(String code, String... expectedLines) throws Exception {76doTest(false, code, expectedLines);77}7879private void doTest(boolean interactiveTerminal, String code, String... expectedLines) throws Exception {80byte[] cmdInputData = (code + "\n/exit\n").getBytes();81InputStream cmdInput = new ByteArrayInputStream(cmdInputData);82InputStream userInput = new ByteArrayInputStream("a\n".getBytes());83ByteArrayOutputStream out = new ByteArrayOutputStream();84PrintStream printOut = new PrintStream(out);8586JavaShellToolBuilder.builder()87.in(cmdInput, userInput)88.out(printOut, printOut, printOut)89.interactiveTerminal(interactiveTerminal)90.promptCapture(true)91.persistence(new HashMap<>())92.start("--no-startup");9394String actual = new String(out.toByteArray());95List<String> actualLines = Arrays.asList(actual.split("\\R"));9697for (String expectedLine : expectedLines) {98assertTrue(actualLines.contains(expectedLine),99"actual:\n" + actualLines + "\n, expected:\n" + expectedLine);100}101}102103public void checkInteractiveTerminal() throws Exception {104String testJdk = System.getProperty(TEST_JDK);105try {106System.clearProperty(TEST_JDK);107108//note the exact format of the output is not specified here, and the test mostly validates109//the current behavior, and shows the output changes based on the interactiveTerminal setting:110doTest(true,111"System.out.println(\"read: \" + System.in.read());",112"\u001b[?2004h\u0005System.out.println(\"read: \" + System.in.read()\u001b[2D\u001b[2C)\u001b[29D\u001b[29C;",113"\u001b[?2004lread: 97",114"\u001b[?2004h\u0005/exit");115doTest(true,116"1 + 1",117"\u001b[?2004h\u00051 + 1",118"\u001b[?2004l$1 ==> 2",119"\u001b[?2004h\u0005/exit");120} finally {121System.setProperty(TEST_JDK, testJdk);122}123}124}125126127