Skip to content

Commit 1b7d00d

Browse files
tarunjain01Tarun Jain
andauthored
BAEL-4403 (eugenp#9740)
* Hexagonal architecture in Java * BAEL-4403 * Revert "Hexagonal architecture in Java" This reverts commit 7ba4c9d. * Updated code to use only Console * Added System.out * Created seaprate method for each functionality * Added Unit Test Case for ConsoleAndOut class Co-authored-by: Tarun Jain <[email protected]>
1 parent bc5f22d commit 1b7d00d

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.consoleout;
2+
3+
import java.io.Console;
4+
5+
public class ConsoleAndOut {
6+
public static void main(String[] args) {
7+
try {
8+
printConsoleObject();
9+
readPasswordFromConsole();
10+
} catch (Exception ex) {
11+
// Eating NullPointerExcpetion which will occur when this
12+
// program will be run from mediums other than console
13+
}
14+
printSysOut();
15+
}
16+
17+
static void printConsoleObject() {
18+
Console console = System.console();
19+
console.writer().print(console);
20+
}
21+
22+
static void readPasswordFromConsole() {
23+
Console console = System.console();
24+
char[] password = console.readPassword("Enter password: ");
25+
console.printf(String.valueOf(password));
26+
}
27+
28+
static void printSysOut() {
29+
System.out.println(System.out);
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.consoleout;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ConsoleAndOutUnitTest {
8+
9+
@Test
10+
void whenRetreivingConsole_thenPrintConsoleObject() {
11+
assertThrows(NullPointerException.class, () -> {
12+
ConsoleAndOut.printConsoleObject();
13+
});
14+
}
15+
16+
@Test
17+
void whenReadingPassword_thenReadPassword() {
18+
assertThrows(NullPointerException.class, () -> {
19+
ConsoleAndOut.readPasswordFromConsole();
20+
});
21+
}
22+
23+
@Test
24+
void whenRetrievingSysOut_thenPrintSysOutObject() {
25+
ConsoleAndOut.printSysOut();
26+
}
27+
}

0 commit comments

Comments
 (0)