|
| 1 | +package net.jrodolfo.java_evolution.java06.console_api; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +class ConsoleApiExamplesTest { |
| 8 | + |
| 9 | + private final ConsoleApiExamples examples = new ConsoleApiExamples(); |
| 10 | + |
| 11 | + @Test |
| 12 | + void currentProcessCanReportConsoleAvailabilityWithoutRequiringATerminal() { |
| 13 | + ConsoleApiExamples.ConsoleAvailability availability = examples.currentConsoleAvailability(); |
| 14 | + |
| 15 | + assertThat(availability.message()) |
| 16 | + .as("System.console() may be present in a terminal and absent under Maven, IDEs, CI, or redirected processes") |
| 17 | + .isNotBlank(); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void nullConsoleUsesSafeUnavailablePath() { |
| 22 | + ConsoleApiExamples.ConsoleAvailability availability = examples.consoleAvailability(null); |
| 23 | + |
| 24 | + assertThat(availability.available()) |
| 25 | + .as("code using System.console() must handle the normal null result") |
| 26 | + .isFalse(); |
| 27 | + assertThat(availability.message()) |
| 28 | + .as("the unavailable result should explain the process-boundary condition") |
| 29 | + .contains("System.console()") |
| 30 | + .contains("not available"); |
| 31 | + assertThat(examples.greetUser(null)) |
| 32 | + .as("application code should have a deterministic fallback when no console is attached") |
| 33 | + .isEqualTo("console unavailable"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void fakeConsoleMakesLineInputAndPromptingTestable() { |
| 38 | + FakeConsoleSession console = new FakeConsoleSession("Rodolfo", new char[] { 's', 'e', 'c', 'r', 'e', 't' }); |
| 39 | + |
| 40 | + String greeting = examples.greetUser(console); |
| 41 | + |
| 42 | + assertThat(greeting) |
| 43 | + .as("command-line behavior can be tested once Console is isolated at the edge") |
| 44 | + .isEqualTo("hello, Rodolfo"); |
| 45 | + assertThat(console.output()) |
| 46 | + .as("Console supports formatted output alongside line input") |
| 47 | + .contains("hello, Rodolfo"); |
| 48 | + assertThat(console.lastLinePrompt()) |
| 49 | + .as("Console.readLine can include formatted prompts") |
| 50 | + .isEqualTo("name: "); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void passwordInputUsesCharArrayThatCanBeCleared() { |
| 55 | + char[] password = new char[] { 's', 'e', 'c', 'r', 'e', 't' }; |
| 56 | + FakeConsoleSession console = new FakeConsoleSession("ignored", password); |
| 57 | + |
| 58 | + int length = examples.readPasswordLengthAndClear(console); |
| 59 | + |
| 60 | + assertThat(length) |
| 61 | + .as("the example uses the password before clearing it") |
| 62 | + .isEqualTo(6); |
| 63 | + assertThat(password) |
| 64 | + .as("char[] secrets can be overwritten after use") |
| 65 | + .containsOnly('\0'); |
| 66 | + assertThat(console.lastPasswordPrompt()) |
| 67 | + .as("Console.readPassword supports a prompt and avoids echoing in a real terminal") |
| 68 | + .isEqualTo("password: "); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void passwordGuidanceMentionsTheJavaSixSecurityHabit() { |
| 73 | + assertThat(examples.passwordGuidance()) |
| 74 | + .as("the API lesson is to prefer readPassword char arrays over immutable String secrets") |
| 75 | + .contains("Console.readPassword") |
| 76 | + .contains("char[]") |
| 77 | + .contains("clear"); |
| 78 | + } |
| 79 | + |
| 80 | + private static final class FakeConsoleSession implements ConsoleApiExamples.ConsoleSession { |
| 81 | + |
| 82 | + private final String line; |
| 83 | + private final char[] password; |
| 84 | + private final StringBuilder output = new StringBuilder(); |
| 85 | + private String lastLinePrompt; |
| 86 | + private String lastPasswordPrompt; |
| 87 | + |
| 88 | + private FakeConsoleSession(String line, char[] password) { |
| 89 | + this.line = line; |
| 90 | + this.password = password; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String readLine(String prompt, Object... args) { |
| 95 | + lastLinePrompt = String.format(prompt, args); |
| 96 | + return line; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public char[] readPassword(String prompt, Object... args) { |
| 101 | + lastPasswordPrompt = String.format(prompt, args); |
| 102 | + return password; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void printf(String format, Object... args) { |
| 107 | + output.append(String.format(format, args)); |
| 108 | + } |
| 109 | + |
| 110 | + private String output() { |
| 111 | + return output.toString(); |
| 112 | + } |
| 113 | + |
| 114 | + private String lastLinePrompt() { |
| 115 | + return lastLinePrompt; |
| 116 | + } |
| 117 | + |
| 118 | + private String lastPasswordPrompt() { |
| 119 | + return lastPasswordPrompt; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments