Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, 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
Expand Down Expand Up @@ -33,7 +33,7 @@ public interface JdkConsoleProvider {
/**
* The module name of the JdkConsole default provider.
*/
String DEFAULT_PROVIDER_MODULE_NAME = "jdk.internal.le";
String DEFAULT_PROVIDER_MODULE_NAME = "java.base";

/**
* {@return the Console instance, or {@code null} if not available}
Expand Down
96 changes: 59 additions & 37 deletions test/jdk/java/io/Console/ConsolePromptTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, 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
Expand All @@ -23,68 +23,90 @@

/**
* @test
* @bug 8331681
* @bug 8331681 8351435
* @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.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jtreg.SkippedException;

public class ConsolePromptTest {

private static final List<List<String>> VARIANTS = List.of(
List.of("--limit-modules", "java.base"),
List.of("-Djdk.console=java.base")
);

public static void main(String... args) throws Throwable {
for (Method m : ConsolePromptTest.class.getDeclaredMethods()) {
if (m.getName().startsWith("test")) {
m.invoke(new ConsolePromptTest());
for (List<String> variant : VARIANTS) {
try {
m.invoke(new ConsolePromptTest(variant));
} catch (InvocationTargetException e) {
if (e.getCause() instanceof SkippedException se) {
throw se;
} else {
throw e;
}
}
}
}
}
}

private final List<String> extraParams;

public ConsolePromptTest(List<String> extraParams) {
this.extraParams = extraParams;
}

void testCorrectOutputReadLine() throws Exception {
doRunConsoleTest("testCorrectOutputReadLine", "inp", "%s");
doRunConsoleTest("testCorrectOutputReadLine");
}

void testCorrectOutputReadPassword() throws Exception {
doRunConsoleTest("testCorrectOutputReadPassword", "inp", "%s");
doRunConsoleTest("testCorrectOutputReadPassword");
}

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());
void doRunConsoleTest(String testName) throws Exception {
// check "expect" command availability
var expect = Paths.get("/usr/bin/expect");
if (!Files.exists(expect) || !Files.isExecutable(expect)) {
throw new SkippedException("'expect' command not found. Test ignored.");
}

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 + "'");
// invoking "expect" command
var testSrc = System.getProperty("test.src", ".");
var jdkDir = System.getProperty("test.jdk");

List<String> command = new ArrayList<>();

command.add("expect");
command.add("-n");
command.add(testSrc + "/consolePrompt.exp");
command.add("%s");
command.add(jdkDir + "/bin/java");
command.addAll(extraParams);
command.add("-cp");
command.add(System.getProperty("java.class.path"));
command.add(ConsoleTest.class.getName());
command.add(testName);

OutputAnalyzer output = ProcessTools.executeProcess(command.toArray(String[]::new));
output.reportDiagnosticSummary();
var eval = output.getExitValue();
if (eval != 0) {
throw new RuntimeException("Test failed. Exit value from 'expect' command: " + eval);
}
}

Expand Down
14 changes: 7 additions & 7 deletions test/jdk/java/io/Console/DefaultCharsetTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, 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
Expand All @@ -26,15 +26,15 @@

/**
* @test
* @bug 8341975
* @bug 8341975 8351435
* @summary Tests the default charset. It should honor `stdout.encoding`
* which should be the same as System.out.charset()
* @modules jdk.internal.le
* @run junit/othervm -Dstdout.encoding=UTF-8 DefaultCharsetTest
* @run junit/othervm -Dstdout.encoding=ISO-8859-1 DefaultCharsetTest
* @run junit/othervm -Dstdout.encoding=US-ASCII DefaultCharsetTest
* @run junit/othervm -Dstdout.encoding=foo DefaultCharsetTest
* @run junit/othervm DefaultCharsetTest
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=UTF-8 DefaultCharsetTest
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=ISO-8859-1 DefaultCharsetTest
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=US-ASCII DefaultCharsetTest
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=foo DefaultCharsetTest
* @run junit/othervm -Djdk.console=jdk.internal.le DefaultCharsetTest
*/
public class DefaultCharsetTest {
@Test
Expand Down
5 changes: 3 additions & 2 deletions test/jdk/java/io/Console/LocaleTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, 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
Expand Down Expand Up @@ -31,7 +31,7 @@

/**
* @test
* @bug 8330276
* @bug 8330276 8351435
* @summary Tests Console methods that have Locale as an argument
* @library /test/lib
* @modules jdk.internal.le jdk.localedata
Expand All @@ -57,6 +57,7 @@ public static void main(String... args) throws Throwable {
if (args.length == 0) {
// no arg will launch the child process that actually perform tests
var pb = ProcessTools.createTestJavaProcessBuilder(
"-Djdk.console=jdk.internal.le",
"LocaleTest", "dummy");
var input = new File(System.getProperty("test.src", "."), "input.txt");
pb.redirectInput(input);
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/java/io/Console/ModuleSelectionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, 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
Expand All @@ -23,11 +23,11 @@

/**
* @test
* @bug 8295803 8299689
* @bug 8295803 8299689 8351435
* @summary Tests System.console() returns correct Console (or null) from the expected
* module.
* @modules java.base/java.io:+open
* @run main/othervm ModuleSelectionTest jdk.internal.le
* @run main/othervm ModuleSelectionTest java.base
* @run main/othervm -Djdk.console=jdk.internal.le ModuleSelectionTest jdk.internal.le
* @run main/othervm -Djdk.console=java.base ModuleSelectionTest java.base
* @run main/othervm --limit-modules java.base ModuleSelectionTest java.base
Expand Down
30 changes: 30 additions & 0 deletions test/jdk/java/io/Console/consolePrompt.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright (c) 2025, 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.
#

set java [lrange $argv 1 end]
set expected [lindex $argv 0]

eval spawn $java
expect -- "$expected"
send -- "\n"
expect eof
11 changes: 6 additions & 5 deletions test/jdk/java/io/IO/IO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, 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
Expand Down Expand Up @@ -50,9 +50,10 @@

/*
* @test
* @bug 8305457 8342936
* @bug 8305457 8342936 8351435
* @summary java.io.IO tests
* @library /test/lib
* @modules jdk.internal.le
* @run junit IO
*/
@ExtendWith(IO.TimingExtension.class)
Expand Down Expand Up @@ -152,7 +153,7 @@ public void inputTestInteractive(String console, String prompt) throws Exception

public static Stream<Arguments> args() {
// cross product: consoles x prompts
return Stream.of(null, "gibberish").flatMap(console -> Stream.of(null, "?", "%s", PROMPT_NONE)
return Stream.of("jdk.internal.le", "gibberish").flatMap(console -> Stream.of(null, "?", "%s", PROMPT_NONE)
.map(prompt -> new String[]{console, prompt}).map(Arguments::of));
}
}
Expand All @@ -162,7 +163,7 @@ public static Stream<Arguments> args() {
public void printTest(String mode) throws Exception {
var file = Path.of(System.getProperty("test.src", "."), "Output.java")
.toAbsolutePath().toString();
var pb = ProcessTools.createTestJavaProcessBuilder("--enable-preview", file, mode);
var pb = ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", "--enable-preview", file, mode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you looked into changing the test so that it runs twice, the second with the jline provider?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The child java process Output.java calls System.console(), so the launcher has to be run with -Djdk.console=jdk.internal.le, otherwise NPE is thrown in Output.java.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, no console in the child process for this implementation.

OutputAnalyzer output = ProcessTools.executeProcess(pb);
assertEquals(0, output.getExitValue());
assertTrue(output.getStderr().isEmpty());
Expand Down Expand Up @@ -195,7 +196,7 @@ void main() {
}
""");
}
var pb = ProcessTools.createTestJavaProcessBuilder("--enable-preview", file.toString());
var pb = ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", "--enable-preview", file.toString());
OutputAnalyzer output = ProcessTools.executeProcess(pb);
assertEquals(0, output.getExitValue());
assertTrue(output.getStderr().isEmpty());
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/jdk/internal/jline/JLineConsoleProviderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, 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
Expand All @@ -23,7 +23,7 @@

/**
* @test
* @bug 8331535
* @bug 8331535 8351435
* @summary Verify the jdk.internal.le's console provider works properly.
* @modules jdk.internal.le
* @library /test/lib
Expand Down Expand Up @@ -58,7 +58,7 @@ void doRunConsoleTest(String testName,
String input,
String expectedOut) throws Exception {
ProcessBuilder builder =
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName(),
ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", ConsoleTest.class.getName(),
testName);
OutputAnalyzer output = ProcessTools.executeProcess(builder, input);

Expand Down
8 changes: 4 additions & 4 deletions test/jdk/jdk/internal/jline/RedirectedStdOut.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, 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
Expand All @@ -23,7 +23,7 @@

/**
* @test
* @bug 8330998
* @bug 8330998 8351435
* @summary Verify that even if the stdout is redirected java.io.Console will
* use it for writing.
* @modules jdk.internal.le
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void main(String... args) throws Throwable {
//this test is weaker, but more reliable:
void runRedirectAllTest() throws Exception {
ProcessBuilder builder =
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName());
ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", ConsoleTest.class.getName());
OutputAnalyzer output = ProcessTools.executeProcess(builder);

output.waitFor();
Expand Down Expand Up @@ -153,7 +153,7 @@ void runRedirectOutOnly() throws Throwable {
System.setOut(new PrintStream(new ByteArrayOutputStream()));

ProcessBuilder builder =
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName());
ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", ConsoleTest.class.getName());

builder.inheritIO();
builder.redirectOutput(stdout.toFile());
Expand Down