Skip to content

Commit

Permalink
Fixing test for Console.readln/readLine, ensuring the proper methods …
Browse files Browse the repository at this point in the history
…are called.
  • Loading branch information
lahodaj committed Oct 29, 2024
1 parent 78bf15b commit eadbc68
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,7 @@ public String readln(String prompt) {

@Override
public String readln() {
try {
initJLineIfNeeded();
return jline.readLine();
} catch (EndOfFileException eofe) {
return null;
}
return readLine();
}

@Override
Expand All @@ -257,7 +252,12 @@ public String readLine(Locale locale, String format, Object ... args) {

@Override
public String readLine() {
return readLine(Locale.getDefault(Locale.Category.FORMAT), "");
try {
initJLineIfNeeded();
return jline.readLine();
} catch (EndOfFileException eofe) {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,7 @@ public String readln(String prompt) {
*/
@Override
public String readln() {
try {
return sendAndReceive(() -> {
remoteInput.write(Task.READ_LINE_NO_PROMPT.ordinal());
char[] line = readChars();
return new String(line);
});
} catch (IOException ex) {
throw new IOError(ex);
}
return readLine();
}

/**
Expand Down Expand Up @@ -287,7 +279,15 @@ public String readLine(Locale locale, String format, Object... args) {
*/
@Override
public String readLine() {
return readLine(Locale.getDefault(Locale.Category.FORMAT), "");
try {
return sendAndReceive(() -> {
remoteInput.write(Task.READ_LINE_NO_PROMPT.ordinal());
char[] line = readChars();
return new String(line);
});
} catch (IOException ex) {
throw new IOError(ex);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/langtools/jdk/jshell/ConsoleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @test
* @bug 8298425
* @summary Verify behavior of System.console()
* @enablePreview
* @build KullaTesting TestingInputStream
* @run testng ConsoleTest
*/
Expand Down Expand Up @@ -67,6 +68,13 @@ public String readLine(String prompt) throws IOError {
}
};
assertEval("System.console().readLine(\"expected\")", "\"AB\"");
console = new ThrowingJShellConsole() {
@Override
public String readLine() throws IOError {
return "AB";
}
};
assertEval("System.console().readLine()", "\"AB\"");
console = new ThrowingJShellConsole() {
@Override
public char[] readPassword(String prompt) throws IOError {
Expand Down Expand Up @@ -210,6 +218,10 @@ public String readLine(String prompt) throws IOError {
return console.readLine(prompt);
}
@Override
public String readLine() throws IOError {
return console.readLine();
}
@Override
public char[] readPassword(String prompt) throws IOError {
return console.readPassword(prompt);
}
Expand Down Expand Up @@ -240,6 +252,10 @@ public String readLine(String prompt) throws IOError {
throw new IllegalStateException("Not expected!");
}
@Override
public String readLine() throws IOError {
throw new IllegalStateException("Not expected!");
}
@Override
public char[] readPassword(String prompt) throws IOError {
throw new IllegalStateException("Not expected!");
}
Expand Down

0 comments on commit eadbc68

Please sign in to comment.