From 94555a4510b8bd3f7e19dd3a91ce943fed092c29 Mon Sep 17 00:00:00 2001 From: TJ Date: Sun, 26 Jul 2020 09:29:54 +0530 Subject: [PATCH] BAEL-4403 (#9740) * Hexagonal architecture in Java * BAEL-4403 * Revert "Hexagonal architecture in Java" This reverts commit 7ba4c9dcbd99ca418c070a73bd9ab9f6c956d185. * 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 --- .../baeldung/consoleout/ConsoleAndOut.java | 31 +++++++++++++++++++ .../consoleout/ConsoleAndOutUnitTest.java | 27 ++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 core-java-modules/core-java-console/src/main/java/com/baeldung/consoleout/ConsoleAndOut.java create mode 100644 core-java-modules/core-java-console/src/test/java/com/baeldung/consoleout/ConsoleAndOutUnitTest.java diff --git a/core-java-modules/core-java-console/src/main/java/com/baeldung/consoleout/ConsoleAndOut.java b/core-java-modules/core-java-console/src/main/java/com/baeldung/consoleout/ConsoleAndOut.java new file mode 100644 index 0000000000..082a5219c9 --- /dev/null +++ b/core-java-modules/core-java-console/src/main/java/com/baeldung/consoleout/ConsoleAndOut.java @@ -0,0 +1,31 @@ +package com.baeldung.consoleout; + +import java.io.Console; + +public class ConsoleAndOut { + public static void main(String[] args) { + try { + printConsoleObject(); + readPasswordFromConsole(); + } catch (Exception ex) { + // Eating NullPointerExcpetion which will occur when this + // program will be run from mediums other than console + } + printSysOut(); + } + + static void printConsoleObject() { + Console console = System.console(); + console.writer().print(console); + } + + static void readPasswordFromConsole() { + Console console = System.console(); + char[] password = console.readPassword("Enter password: "); + console.printf(String.valueOf(password)); + } + + static void printSysOut() { + System.out.println(System.out); + } +} diff --git a/core-java-modules/core-java-console/src/test/java/com/baeldung/consoleout/ConsoleAndOutUnitTest.java b/core-java-modules/core-java-console/src/test/java/com/baeldung/consoleout/ConsoleAndOutUnitTest.java new file mode 100644 index 0000000000..619a65a1fc --- /dev/null +++ b/core-java-modules/core-java-console/src/test/java/com/baeldung/consoleout/ConsoleAndOutUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.consoleout; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +class ConsoleAndOutUnitTest { + + @Test + void whenRetreivingConsole_thenPrintConsoleObject() { + assertThrows(NullPointerException.class, () -> { + ConsoleAndOut.printConsoleObject(); + }); + } + + @Test + void whenReadingPassword_thenReadPassword() { + assertThrows(NullPointerException.class, () -> { + ConsoleAndOut.readPasswordFromConsole(); + }); + } + + @Test + void whenRetrievingSysOut_thenPrintSysOutObject() { + ConsoleAndOut.printSysOut(); + } +}