diff --git a/src/main/java/PasswordManager.java b/src/main/java/PasswordManager.java index ddc68c9..a8cec33 100644 --- a/src/main/java/PasswordManager.java +++ b/src/main/java/PasswordManager.java @@ -1,4 +1,7 @@ +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Scanner; public class PasswordManager implements PasswordManagerInterface { @@ -7,10 +10,11 @@ public class PasswordManager implements PasswordManagerInterface { public static void main(String[] args) { PasswordManager pm = new PasswordManager(); + InputStream inputStream = System.in; + OutputStream outputStream = System.out; while (pm.running) { - InputStream inputStream = System.in; - pm.showMenu(inputStream); + pm.showMenu(inputStream, outputStream); } System.exit(0); } @@ -21,7 +25,6 @@ public class PasswordManager implements PasswordManagerInterface { @Override public void listVaults() { - } @Override @@ -30,16 +33,21 @@ public class PasswordManager implements PasswordManagerInterface { } @Override - public void showMenu(InputStream inputStream) { + public void showMenu(InputStream inputStream, OutputStream outputStream) { StringBuilder sb = new StringBuilder(); Scanner scan = new Scanner(inputStream); + running = true; sb.append("ciip Gruppe 8 - Password Manager\n\n"); sb.append("Menu:\n"); sb.append("- exit: e\n"); - System.out.println(sb + "\n"); + try { + outputStream.write((sb + "\n").getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + e.printStackTrace(); + } String input = scan.nextLine(); diff --git a/src/main/java/PasswordManagerInterface.java b/src/main/java/PasswordManagerInterface.java index bd8421a..da179e3 100644 --- a/src/main/java/PasswordManagerInterface.java +++ b/src/main/java/PasswordManagerInterface.java @@ -1,9 +1,10 @@ import java.io.InputStream; +import java.io.OutputStream; public interface PasswordManagerInterface { public static void main(String[] args) { }; public void listVaults(); public void openVault(); - public void showMenu(InputStream inputStream); + public void showMenu(InputStream inputStream, OutputStream outputStream); } diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index 81e4427..8125195 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -1,8 +1,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; +import java.io.*; import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException; @@ -35,13 +34,28 @@ class PasswordManagerTest { @Test void showMenu() { - ByteArrayInputStream virtualConsoleInput = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); - assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(virtualConsoleInput)); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(getEmptyStringInputStream(), outputStream)); + assertTrue(outputStream.toString().startsWith("ciip Gruppe 8")); } @Test void exitMenu() { - pm.showMenu(new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8))); + pm.showMenu(new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8)), System.out); assertFalse(pm.running); } + + @Test + void doNotExitMenuAfterWrongInput() { + pm.showMenu(new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)), System.out); + assertTrue(pm.running); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(getEmptyStringInputStream(), outputStream)); + assertTrue(outputStream.toString().startsWith("ciip Gruppe 8")); + } + + private ByteArrayInputStream getEmptyStringInputStream() { + return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); + } }