8299689: Make use of JLine for Console as "opt-in"

Reviewed-by: jpai, alanb
This commit is contained in:
Naoto Sato 2023-01-09 18:23:42 +00:00
parent 1f141bd7a9
commit d49851a8b8
4 changed files with 41 additions and 24 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -608,7 +608,13 @@ public class Console implements Flushable
@SuppressWarnings("removal") @SuppressWarnings("removal")
private static Console instantiateConsole(boolean istty) { private static Console instantiateConsole(boolean istty) {
try { try {
// Try loading providers /*
* The JdkConsole provider used for Console instantiation can be specified
* with the system property "jdk.console", whose value designates the module
* name of the implementation, and which defaults to "java.base". If no
* providers are available, or instantiation failed, java.base built-in
* Console implementation is used.
*/
PrivilegedAction<Console> pa = () -> { PrivilegedAction<Console> pa = () -> {
var consModName = System.getProperty("jdk.console", var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME); JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,18 +28,12 @@ import java.nio.charset.Charset;
/** /**
* Service provider interface for JdkConsole implementations. * Service provider interface for JdkConsole implementations.
* The provider used for instantiating JdkConsole instance can be
* specified with the system property "jdk.console", whose value
* designates the module name of the implementation, and which defaults
* to "jdk.internal.le" (jline). If no providers is available,
* or instantiation failed, java.base built-in Console implementation
* is used.
*/ */
public interface JdkConsoleProvider { public interface JdkConsoleProvider {
/** /**
* The module name of the JdkConsole default provider. * 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} * {@return the Console instance, or {@code null} if not available}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,11 +23,11 @@
/** /**
* @test * @test
* @bug 8295803 * @bug 8295803 8299689
* @summary Tests System.console() returns correct Console (or null) from the expected * @summary Tests System.console() returns correct Console (or null) from the expected
* module. * module.
* @modules java.base/java.io:+open * @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=jdk.internal.le ModuleSelectionTest jdk.internal.le
* @run main/othervm -Djdk.console=java.base ModuleSelectionTest java.base * @run main/othervm -Djdk.console=java.base ModuleSelectionTest java.base
* @run main/othervm --limit-modules java.base ModuleSelectionTest java.base * @run main/othervm --limit-modules java.base ModuleSelectionTest java.base

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,18 +28,27 @@ import jdk.test.lib.process.ProcessTools;
/** /**
* @test * @test
* @bug 8295803 * @bug 8295803 8299689
* @summary Tests System.console() works with standard input redirection. * @summary Tests System.console() works with standard input redirection.
* @library /test/lib * @library /test/lib
* @run main RedirectTest
* @run main/othervm -Djdk.console=jdk.internal.le RedirectTest
*/ */
public class RedirectTest { public class RedirectTest {
private static final String SYSPROP = "jdk.console";
public static void main(String... args) throws Throwable { public static void main(String... args) throws Throwable {
if (args.length == 0) { if (args.length == 0) {
// no arg will launch the child process that actually perform tests // no arg will launch the child process that actually perform tests
var pb = ProcessTools.createTestJvm("RedirectTest", "dummy"); var pb = ProcessTools.createTestJvm(
"-D" + SYSPROP + "=" + System.getProperty(SYSPROP, ""),
"RedirectTest", "dummy");
var input = new File(System.getProperty("test.src", "."), "input.txt"); var input = new File(System.getProperty("test.src", "."), "input.txt");
pb.redirectInput(input); pb.redirectInput(input);
var oa = ProcessTools.executeProcess(pb); var oa = ProcessTools.executeProcess(pb);
if (oa.getExitValue() == 1) {
System.out.println("System.console() returns null. Ignoring the test.");
} else {
var output = oa.asLines(); var output = oa.asLines();
var expected = Files.readAllLines(input.toPath()); var expected = Files.readAllLines(input.toPath());
if (!output.equals(expected)) { if (!output.equals(expected)) {
@ -48,14 +57,22 @@ public class RedirectTest {
Actual output: %s Actual output: %s
Expected output: %s Expected output: %s
""".formatted(output, expected)); """.formatted(output, expected));
} } else {
oa.shouldHaveExitValue(0); oa.shouldHaveExitValue(0);
System.out.println("Redirect succeeded.");
}
}
} else { } else {
var con = System.console(); var con = System.console();
if (con != null) {
String line; String line;
while ((line = con.readLine()) != null) { while ((line = con.readLine()) != null) {
System.out.println(line); System.out.println(line);
} }
} else {
// Exit with 1
System.exit(1);
}
} }
} }
} }