Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
/ jdk20 Public archive

Commit

Permalink
8299689: Make use of JLine for Console as "opt-in"
Browse files Browse the repository at this point in the history
Reviewed-by: jpai, alanb
  • Loading branch information
naotoj committed Jan 9, 2023
1 parent 1f141bd commit d49851a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
10 changes: 8 additions & 2 deletions src/java.base/share/classes/java/io/Console.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -608,7 +608,13 @@ public Console console() {
@SuppressWarnings("removal")
private static Console instantiateConsole(boolean istty) {
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 = () -> {
var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,18 +28,12 @@

/**
* 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 {
/**
* 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
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, 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.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,11 +23,11 @@

/**
* @test
* @bug 8295803
* @bug 8295803 8299689
* @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
39 changes: 28 additions & 11 deletions test/jdk/java/io/Console/RedirectTest.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,33 +28,50 @@

/**
* @test
* @bug 8295803
* @bug 8295803 8299689
* @summary Tests System.console() works with standard input redirection.
* @library /test/lib
* @run main RedirectTest
* @run main/othervm -Djdk.console=jdk.internal.le RedirectTest
*/
public class RedirectTest {
private static final String SYSPROP = "jdk.console";

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.createTestJvm("RedirectTest", "dummy");
var pb = ProcessTools.createTestJvm(
"-D" + SYSPROP + "=" + System.getProperty(SYSPROP, ""),
"RedirectTest", "dummy");
var input = new File(System.getProperty("test.src", "."), "input.txt");
pb.redirectInput(input);
var oa = ProcessTools.executeProcess(pb);
var output = oa.asLines();
var expected = Files.readAllLines(input.toPath());
if (!output.equals(expected)) {
throw new RuntimeException("""
if (oa.getExitValue() == 1) {
System.out.println("System.console() returns null. Ignoring the test.");
} else {
var output = oa.asLines();
var expected = Files.readAllLines(input.toPath());
if (!output.equals(expected)) {
throw new RuntimeException("""
Standard out had unexpected strings:
Actual output: %s
Expected output: %s
""".formatted(output, expected));
} else {
oa.shouldHaveExitValue(0);
System.out.println("Redirect succeeded.");
}
}
oa.shouldHaveExitValue(0);
} else {
var con = System.console();
String line;
while ((line = con.readLine()) != null) {
System.out.println(line);
if (con != null) {
String line;
while ((line = con.readLine()) != null) {
System.out.println(line);
}
} else {
// Exit with 1
System.exit(1);
}
}
}
Expand Down

1 comment on commit d49851a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.