8331535: Incorrect prompt for Console.readLine
8331681: Test that jdk.internal.io.JdkConsole does not interpret prompts Reviewed-by: naoto, asotona
This commit is contained in:
parent
3e3f7cf4bd
commit
5a8df4106a
src
jdk.internal.le/share/classes/jdk/internal/org/jline
jdk.jshell/share/classes/jdk/internal/jshell/tool
test
jdk
langtools/jdk/jshell
@ -98,7 +98,7 @@ public class JdkConsoleProviderImpl implements JdkConsoleProvider {
|
||||
public String readLine(String fmt, Object ... args) {
|
||||
try {
|
||||
initJLineIfNeeded();
|
||||
return jline.readLine(fmt.formatted(args));
|
||||
return jline.readLine(fmt.formatted(args).replace("%", "%%"));
|
||||
} catch (EndOfFileException eofe) {
|
||||
return null;
|
||||
}
|
||||
@ -113,7 +113,8 @@ public class JdkConsoleProviderImpl implements JdkConsoleProvider {
|
||||
public char[] readPassword(String fmt, Object ... args) {
|
||||
try {
|
||||
initJLineIfNeeded();
|
||||
return jline.readLine(fmt.formatted(args), '\0').toCharArray();
|
||||
return jline.readLine(fmt.formatted(args).replace("%", "%%"), '\0')
|
||||
.toCharArray();
|
||||
} catch (EndOfFileException eofe) {
|
||||
return null;
|
||||
} finally {
|
||||
|
@ -1007,7 +1007,7 @@ class ConsoleIOContext extends IOContext {
|
||||
input.setState(State.WAIT);
|
||||
Display.DISABLE_CR = true;
|
||||
in.setHistory(userInputHistory);
|
||||
return in.readLine(prompt, mask);
|
||||
return in.readLine(prompt.replace("%", "%%"), mask);
|
||||
} catch (UserInterruptException ex) {
|
||||
throw new InterruptedIOException();
|
||||
} finally {
|
||||
|
104
test/jdk/java/io/Console/ConsolePromptTest.java
Normal file
104
test/jdk/java/io/Console/ConsolePromptTest.java
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2024, 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
|
||||
* @bug 8331681
|
||||
* @summary Verify the java.base's console provider handles the prompt correctly.
|
||||
* @library /test/lib
|
||||
* @run main/othervm --limit-modules java.base ConsolePromptTest
|
||||
* @run main/othervm -Djdk.console=java.base ConsolePromptTest
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
|
||||
public class ConsolePromptTest {
|
||||
|
||||
public static void main(String... args) throws Throwable {
|
||||
for (Method m : ConsolePromptTest.class.getDeclaredMethods()) {
|
||||
if (m.getName().startsWith("test")) {
|
||||
m.invoke(new ConsolePromptTest());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testCorrectOutputReadLine() throws Exception {
|
||||
doRunConsoleTest("testCorrectOutputReadLine", "inp", "%s");
|
||||
}
|
||||
|
||||
void testCorrectOutputReadPassword() throws Exception {
|
||||
doRunConsoleTest("testCorrectOutputReadPassword", "inp", "%s");
|
||||
}
|
||||
|
||||
void doRunConsoleTest(String testName,
|
||||
String input,
|
||||
String expectedOut) throws Exception {
|
||||
ProcessBuilder builder =
|
||||
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName(),
|
||||
testName);
|
||||
OutputAnalyzer output = ProcessTools.executeProcess(builder, input);
|
||||
|
||||
output.waitFor();
|
||||
|
||||
if (output.getExitValue() != 0) {
|
||||
throw new AssertionError("Unexpected return value: " + output.getExitValue() +
|
||||
", actualOut: " + output.getStdout() +
|
||||
", actualErr: " + output.getStderr());
|
||||
}
|
||||
|
||||
String actualOut = output.getStdout();
|
||||
|
||||
if (!Objects.equals(expectedOut, actualOut)) {
|
||||
throw new AssertionError("Unexpected stdout content. " +
|
||||
"Expected: '" + expectedOut + "'" +
|
||||
", got: '" + actualOut + "'");
|
||||
}
|
||||
|
||||
String expectedErr = "";
|
||||
String actualErr = output.getStderr();
|
||||
|
||||
if (!Objects.equals(expectedErr, actualErr)) {
|
||||
throw new AssertionError("Unexpected stderr content. " +
|
||||
"Expected: '" + expectedErr + "'" +
|
||||
", got: '" + actualErr + "'");
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConsoleTest {
|
||||
public static void main(String... args) {
|
||||
switch (args[0]) {
|
||||
case "testCorrectOutputReadLine" ->
|
||||
System.console().readLine("%%s");
|
||||
case "testCorrectOutputReadPassword" ->
|
||||
System.console().readPassword("%%s");
|
||||
default -> throw new UnsupportedOperationException(args[0]);
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
104
test/jdk/jdk/internal/jline/JLineConsoleProviderTest.java
Normal file
104
test/jdk/jdk/internal/jline/JLineConsoleProviderTest.java
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (c) 2024, 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
|
||||
* @bug 8331535
|
||||
* @summary Verify the jdk.internal.le's console provider works properly.
|
||||
* @modules jdk.internal.le
|
||||
* @library /test/lib
|
||||
* @run main/othervm -Djdk.console=jdk.internal.le JLineConsoleProviderTest
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
|
||||
public class JLineConsoleProviderTest {
|
||||
|
||||
public static void main(String... args) throws Throwable {
|
||||
for (Method m : JLineConsoleProviderTest.class.getDeclaredMethods()) {
|
||||
if (m.getName().startsWith("test")) {
|
||||
m.invoke(new JLineConsoleProviderTest());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testCorrectOutputReadLine() throws Exception {
|
||||
doRunConsoleTest("testCorrectOutputReadLine", "inp", "%s");
|
||||
}
|
||||
|
||||
void testCorrectOutputReadPassword() throws Exception {
|
||||
doRunConsoleTest("testCorrectOutputReadPassword", "inp", "%s");
|
||||
}
|
||||
|
||||
void doRunConsoleTest(String testName,
|
||||
String input,
|
||||
String expectedOut) throws Exception {
|
||||
ProcessBuilder builder =
|
||||
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName(),
|
||||
testName);
|
||||
OutputAnalyzer output = ProcessTools.executeProcess(builder, input);
|
||||
|
||||
output.waitFor();
|
||||
|
||||
if (output.getExitValue() != 0) {
|
||||
throw new AssertionError("Unexpected return value: " + output.getExitValue() +
|
||||
", actualOut: " + output.getStdout() +
|
||||
", actualErr: " + output.getStderr());
|
||||
}
|
||||
|
||||
String actualOut = output.getStdout();
|
||||
|
||||
if (!Objects.equals(expectedOut, actualOut)) {
|
||||
throw new AssertionError("Unexpected stdout content. " +
|
||||
"Expected: '" + expectedOut + "'" +
|
||||
", got: '" + actualOut + "'");
|
||||
}
|
||||
|
||||
String expectedErr = "";
|
||||
String actualErr = output.getStderr();
|
||||
|
||||
if (!Objects.equals(expectedErr, actualErr)) {
|
||||
throw new AssertionError("Unexpected stderr content. " +
|
||||
"Expected: '" + expectedErr + "'" +
|
||||
", got: '" + actualErr + "'");
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConsoleTest {
|
||||
public static void main(String... args) {
|
||||
switch (args[0]) {
|
||||
case "testCorrectOutputReadLine" ->
|
||||
System.console().readLine("%%s");
|
||||
case "testCorrectOutputReadPassword" ->
|
||||
System.console().readPassword("%%s");
|
||||
default -> throw new UnsupportedOperationException(args[0]);
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
63
test/langtools/jdk/jshell/ConsoleToolTest.java
Normal file
63
test/langtools/jdk/jshell/ConsoleToolTest.java
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2024, 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
|
||||
* @bug 8331535
|
||||
* @summary Test the JShell tool Console handling
|
||||
* @modules jdk.internal.le/jdk.internal.org.jline.reader
|
||||
* jdk.jshell/jdk.internal.jshell.tool:+open
|
||||
* @build ConsoleToolTest ReplToolTesting
|
||||
* @run testng ConsoleToolTest
|
||||
*/
|
||||
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class ConsoleToolTest extends ReplToolTesting {
|
||||
|
||||
@Test
|
||||
public void testOutput() {
|
||||
test(
|
||||
a -> {assertCommandWithOutputAndTerminal(a,
|
||||
"System.console().readLine(\"%%s\");\ninput", //newline automatically appended
|
||||
"$1 ==> \"input\"",
|
||||
"""
|
||||
\u0005System.console().readLine(\"%%s\");
|
||||
%sinput
|
||||
""");},
|
||||
a -> {assertCommandWithOutputAndTerminal(a,
|
||||
"System.console().readPassword(\"%%s\");\ninput!", //newline automatically appended
|
||||
"$2 ==> char[6] { 'i', 'n', 'p', 'u', 't', '!' }",
|
||||
"""
|
||||
\u0005System.console().readPassword(\"%%s\");
|
||||
%s
|
||||
""");}
|
||||
);
|
||||
}
|
||||
|
||||
void assertCommandWithOutputAndTerminal(boolean a, String command, String out, String terminalOut) {
|
||||
assertCommand(a, command, out, null, null, null, null, terminalOut);
|
||||
}
|
||||
|
||||
}
|
@ -221,6 +221,12 @@ public class ReplToolTesting {
|
||||
return s;
|
||||
}
|
||||
|
||||
public String getTerminalOutput() {
|
||||
String s = normalizeLineEndings("\r\n", console.data.toString());
|
||||
console.data.reset();
|
||||
return s;
|
||||
}
|
||||
|
||||
public void test(ReplTest... tests) {
|
||||
test(new String[0], tests);
|
||||
}
|
||||
@ -476,6 +482,7 @@ public class ReplToolTesting {
|
||||
|
||||
public void dropClass(boolean after, String cmd, String name, String output) {
|
||||
dropKey(after, cmd, name, classes, output);
|
||||
|
||||
}
|
||||
|
||||
public void dropImport(boolean after, String cmd, String name, String output) {
|
||||
@ -532,6 +539,11 @@ public class ReplToolTesting {
|
||||
|
||||
public void assertCommand(boolean after, String cmd, String out, String err,
|
||||
String userinput, String print, String usererr) {
|
||||
assertCommand(after, cmd, out, err, userinput, print, usererr, null);
|
||||
}
|
||||
|
||||
public void assertCommand(boolean after, String cmd, String out, String err,
|
||||
String userinput, String print, String usererr, String terminalOut) {
|
||||
if (!after) {
|
||||
if (userinput != null) {
|
||||
setUserInput(userinput);
|
||||
@ -546,6 +558,7 @@ public class ReplToolTesting {
|
||||
assertOutput(getCommandErrorOutput(), err, "command error: " + cmd);
|
||||
assertOutput(getUserOutput(), print, "user output: " + cmd);
|
||||
assertOutput(getUserErrorOutput(), usererr, "user error: " + cmd);
|
||||
assertOutput(getTerminalOutput(), terminalOut, "terminal output: " + cmd);
|
||||
}
|
||||
}
|
||||
|
||||
@ -565,7 +578,11 @@ public class ReplToolTesting {
|
||||
}
|
||||
|
||||
private String normalizeLineEndings(String text) {
|
||||
return ANSI_CODE_PATTERN.matcher(text.replace(System.getProperty("line.separator"), "\n")).replaceAll("");
|
||||
return normalizeLineEndings(System.getProperty("line.separator"), text);
|
||||
}
|
||||
|
||||
private String normalizeLineEndings(String lineSeparator, String text) {
|
||||
return ANSI_CODE_PATTERN.matcher(text.replace(lineSeparator, "\n")).replaceAll("");
|
||||
}
|
||||
private static final Pattern ANSI_CODE_PATTERN = Pattern.compile("\033\\[[\060-\077]*[\040-\057]*[\100-\176]");
|
||||
|
||||
@ -846,6 +863,7 @@ public class ReplToolTesting {
|
||||
|
||||
class PromptedCommandOutputStream extends OutputStream {
|
||||
private final ReplTest[] tests;
|
||||
private final ByteArrayOutputStream data = new ByteArrayOutputStream();
|
||||
private int index = 0;
|
||||
PromptedCommandOutputStream(ReplTest[] tests) {
|
||||
this.tests = tests;
|
||||
@ -861,7 +879,8 @@ public class ReplToolTesting {
|
||||
fail("Did not exit Repl tool after test");
|
||||
}
|
||||
++index;
|
||||
} // For now, anything else is thrown away
|
||||
}
|
||||
data.write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user