Skip to content

Commit 4247744

Browse files
naotojlahodaj
andcommitted
8351435: Change the default Console implementation back to the built-in one in java.base module
Co-authored-by: Jan Lahoda <[email protected]> Reviewed-by: alanb
1 parent 9c06dcb commit 4247744

File tree

9 files changed

+117
-63
lines changed

9 files changed

+117
-63
lines changed

src/java.base/share/classes/jdk/internal/io/JdkConsoleProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,7 @@ public interface JdkConsoleProvider {
3333
/**
3434
* The module name of the JdkConsole default provider.
3535
*/
36-
String DEFAULT_PROVIDER_MODULE_NAME = "jdk.internal.le";
36+
String DEFAULT_PROVIDER_MODULE_NAME = "java.base";
3737

3838
/**
3939
* {@return the Console instance, or {@code null} if not available}

test/jdk/java/io/Console/ConsolePromptTest.java

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,68 +23,90 @@
2323

2424
/**
2525
* @test
26-
* @bug 8331681
26+
* @bug 8331681 8351435
2727
* @summary Verify the java.base's console provider handles the prompt correctly.
2828
* @library /test/lib
29-
* @run main/othervm --limit-modules java.base ConsolePromptTest
30-
* @run main/othervm -Djdk.console=java.base ConsolePromptTest
3129
*/
3230

31+
import java.lang.reflect.InvocationTargetException;
3332
import java.lang.reflect.Method;
34-
import java.util.Objects;
33+
import java.nio.file.Files;
34+
import java.nio.file.Paths;
35+
import java.util.ArrayList;
36+
import java.util.List;
3537

3638
import jdk.test.lib.process.OutputAnalyzer;
3739
import jdk.test.lib.process.ProcessTools;
40+
import jtreg.SkippedException;
3841

3942
public class ConsolePromptTest {
4043

44+
private static final List<List<String>> VARIANTS = List.of(
45+
List.of("--limit-modules", "java.base"),
46+
List.of("-Djdk.console=java.base")
47+
);
48+
4149
public static void main(String... args) throws Throwable {
4250
for (Method m : ConsolePromptTest.class.getDeclaredMethods()) {
4351
if (m.getName().startsWith("test")) {
44-
m.invoke(new ConsolePromptTest());
52+
for (List<String> variant : VARIANTS) {
53+
try {
54+
m.invoke(new ConsolePromptTest(variant));
55+
} catch (InvocationTargetException e) {
56+
if (e.getCause() instanceof SkippedException se) {
57+
throw se;
58+
} else {
59+
throw e;
60+
}
61+
}
62+
}
4563
}
4664
}
4765
}
4866

67+
private final List<String> extraParams;
68+
69+
public ConsolePromptTest(List<String> extraParams) {
70+
this.extraParams = extraParams;
71+
}
72+
4973
void testCorrectOutputReadLine() throws Exception {
50-
doRunConsoleTest("testCorrectOutputReadLine", "inp", "%s");
74+
doRunConsoleTest("testCorrectOutputReadLine");
5175
}
5276

5377
void testCorrectOutputReadPassword() throws Exception {
54-
doRunConsoleTest("testCorrectOutputReadPassword", "inp", "%s");
78+
doRunConsoleTest("testCorrectOutputReadPassword");
5579
}
5680

57-
void doRunConsoleTest(String testName,
58-
String input,
59-
String expectedOut) throws Exception {
60-
ProcessBuilder builder =
61-
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName(),
62-
testName);
63-
OutputAnalyzer output = ProcessTools.executeProcess(builder, input);
64-
65-
output.waitFor();
66-
67-
if (output.getExitValue() != 0) {
68-
throw new AssertionError("Unexpected return value: " + output.getExitValue() +
69-
", actualOut: " + output.getStdout() +
70-
", actualErr: " + output.getStderr());
81+
void doRunConsoleTest(String testName) throws Exception {
82+
// check "expect" command availability
83+
var expect = Paths.get("/usr/bin/expect");
84+
if (!Files.exists(expect) || !Files.isExecutable(expect)) {
85+
throw new SkippedException("'expect' command not found. Test ignored.");
7186
}
7287

73-
String actualOut = output.getStdout();
74-
75-
if (!Objects.equals(expectedOut, actualOut)) {
76-
throw new AssertionError("Unexpected stdout content. " +
77-
"Expected: '" + expectedOut + "'" +
78-
", got: '" + actualOut + "'");
79-
}
80-
81-
String expectedErr = "";
82-
String actualErr = output.getStderr();
83-
84-
if (!Objects.equals(expectedErr, actualErr)) {
85-
throw new AssertionError("Unexpected stderr content. " +
86-
"Expected: '" + expectedErr + "'" +
87-
", got: '" + actualErr + "'");
88+
// invoking "expect" command
89+
var testSrc = System.getProperty("test.src", ".");
90+
var jdkDir = System.getProperty("test.jdk");
91+
92+
List<String> command = new ArrayList<>();
93+
94+
command.add("expect");
95+
command.add("-n");
96+
command.add(testSrc + "/consolePrompt.exp");
97+
command.add("%s");
98+
command.add(jdkDir + "/bin/java");
99+
command.addAll(extraParams);
100+
command.add("-cp");
101+
command.add(System.getProperty("java.class.path"));
102+
command.add(ConsoleTest.class.getName());
103+
command.add(testName);
104+
105+
OutputAnalyzer output = ProcessTools.executeProcess(command.toArray(String[]::new));
106+
output.reportDiagnosticSummary();
107+
var eval = output.getExitValue();
108+
if (eval != 0) {
109+
throw new RuntimeException("Test failed. Exit value from 'expect' command: " + eval);
88110
}
89111
}
90112

test/jdk/java/io/Console/DefaultCharsetTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,15 +26,15 @@
2626

2727
/**
2828
* @test
29-
* @bug 8341975
29+
* @bug 8341975 8351435
3030
* @summary Tests the default charset. It should honor `stdout.encoding`
3131
* which should be the same as System.out.charset()
3232
* @modules jdk.internal.le
33-
* @run junit/othervm -Dstdout.encoding=UTF-8 DefaultCharsetTest
34-
* @run junit/othervm -Dstdout.encoding=ISO-8859-1 DefaultCharsetTest
35-
* @run junit/othervm -Dstdout.encoding=US-ASCII DefaultCharsetTest
36-
* @run junit/othervm -Dstdout.encoding=foo DefaultCharsetTest
37-
* @run junit/othervm DefaultCharsetTest
33+
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=UTF-8 DefaultCharsetTest
34+
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=ISO-8859-1 DefaultCharsetTest
35+
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=US-ASCII DefaultCharsetTest
36+
* @run junit/othervm -Djdk.console=jdk.internal.le -Dstdout.encoding=foo DefaultCharsetTest
37+
* @run junit/othervm -Djdk.console=jdk.internal.le DefaultCharsetTest
3838
*/
3939
public class DefaultCharsetTest {
4040
@Test

test/jdk/java/io/Console/LocaleTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,7 @@
3131

3232
/**
3333
* @test
34-
* @bug 8330276
34+
* @bug 8330276 8351435
3535
* @summary Tests Console methods that have Locale as an argument
3636
* @library /test/lib
3737
* @modules jdk.internal.le jdk.localedata
@@ -57,6 +57,7 @@ public static void main(String... args) throws Throwable {
5757
if (args.length == 0) {
5858
// no arg will launch the child process that actually perform tests
5959
var pb = ProcessTools.createTestJavaProcessBuilder(
60+
"-Djdk.console=jdk.internal.le",
6061
"LocaleTest", "dummy");
6162
var input = new File(System.getProperty("test.src", "."), "input.txt");
6263
pb.redirectInput(input);

test/jdk/java/io/Console/ModuleSelectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,11 +23,11 @@
2323

2424
/**
2525
* @test
26-
* @bug 8295803 8299689
26+
* @bug 8295803 8299689 8351435
2727
* @summary Tests System.console() returns correct Console (or null) from the expected
2828
* module.
2929
* @modules java.base/java.io:+open
30-
* @run main/othervm ModuleSelectionTest jdk.internal.le
30+
* @run main/othervm ModuleSelectionTest java.base
3131
* @run main/othervm -Djdk.console=jdk.internal.le ModuleSelectionTest jdk.internal.le
3232
* @run main/othervm -Djdk.console=java.base ModuleSelectionTest java.base
3333
* @run main/othervm --limit-modules java.base ModuleSelectionTest java.base
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright (c) 2025, 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+
set java [lrange $argv 1 end]
25+
set expected [lindex $argv 0]
26+
27+
eval spawn $java
28+
expect -- "$expected"
29+
send -- "\n"
30+
expect eof

test/jdk/java/io/IO/IO.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,9 +50,10 @@
5050

5151
/*
5252
* @test
53-
* @bug 8305457 8342936
53+
* @bug 8305457 8342936 8351435
5454
* @summary java.io.IO tests
5555
* @library /test/lib
56+
* @modules jdk.internal.le
5657
* @run junit IO
5758
*/
5859
@ExtendWith(IO.TimingExtension.class)
@@ -152,7 +153,7 @@ public void inputTestInteractive(String console, String prompt) throws Exception
152153

153154
public static Stream<Arguments> args() {
154155
// cross product: consoles x prompts
155-
return Stream.of(null, "gibberish").flatMap(console -> Stream.of(null, "?", "%s", PROMPT_NONE)
156+
return Stream.of("jdk.internal.le", "gibberish").flatMap(console -> Stream.of(null, "?", "%s", PROMPT_NONE)
156157
.map(prompt -> new String[]{console, prompt}).map(Arguments::of));
157158
}
158159
}
@@ -162,7 +163,7 @@ public static Stream<Arguments> args() {
162163
public void printTest(String mode) throws Exception {
163164
var file = Path.of(System.getProperty("test.src", "."), "Output.java")
164165
.toAbsolutePath().toString();
165-
var pb = ProcessTools.createTestJavaProcessBuilder("--enable-preview", file, mode);
166+
var pb = ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", "--enable-preview", file, mode);
166167
OutputAnalyzer output = ProcessTools.executeProcess(pb);
167168
assertEquals(0, output.getExitValue());
168169
assertTrue(output.getStderr().isEmpty());
@@ -195,7 +196,7 @@ void main() {
195196
}
196197
""");
197198
}
198-
var pb = ProcessTools.createTestJavaProcessBuilder("--enable-preview", file.toString());
199+
var pb = ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", "--enable-preview", file.toString());
199200
OutputAnalyzer output = ProcessTools.executeProcess(pb);
200201
assertEquals(0, output.getExitValue());
201202
assertTrue(output.getStderr().isEmpty());

test/jdk/jdk/internal/jline/JLineConsoleProviderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 8331535
26+
* @bug 8331535 8351435
2727
* @summary Verify the jdk.internal.le's console provider works properly.
2828
* @modules jdk.internal.le
2929
* @library /test/lib
@@ -58,7 +58,7 @@ void doRunConsoleTest(String testName,
5858
String input,
5959
String expectedOut) throws Exception {
6060
ProcessBuilder builder =
61-
ProcessTools.createTestJavaProcessBuilder(ConsoleTest.class.getName(),
61+
ProcessTools.createTestJavaProcessBuilder("-Djdk.console=jdk.internal.le", ConsoleTest.class.getName(),
6262
testName);
6363
OutputAnalyzer output = ProcessTools.executeProcess(builder, input);
6464

test/jdk/jdk/internal/jline/RedirectedStdOut.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

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

6767
output.waitFor();
@@ -153,7 +153,7 @@ void runRedirectOutOnly() throws Throwable {
153153
System.setOut(new PrintStream(new ByteArrayOutputStream()));
154154

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

158158
builder.inheritIO();
159159
builder.redirectOutput(stdout.toFile());

0 commit comments

Comments
 (0)