diff --git a/src/main/java/PasswordManager.java b/src/main/java/PasswordManager.java index 97c4711..29960fd 100644 --- a/src/main/java/PasswordManager.java +++ b/src/main/java/PasswordManager.java @@ -1,16 +1,42 @@ +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Scanner; + public class PasswordManager implements PasswordManagerInterface { + + boolean running = true; + InputStream inputStream = System.in; + OutputStream outputStream = System.out; + public static void main(String[] args) { PasswordManager pm = new PasswordManager(); - pm.showMenu(); + + while (pm.running) { + pm.showMenu(); + } + System.exit(0); } public PasswordManager() { - System.out.println("Hello World"); + println("Hello World"); } @Override public void listVaults() { + println("Vaults:"); + } + private void println(String output) { + try { + outputStream.write((output + "\n").getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + e.printStackTrace(); + } } @Override @@ -20,6 +46,55 @@ public class PasswordManager implements PasswordManagerInterface { @Override public void showMenu() { + StringBuilder sb = new StringBuilder(); + Scanner scan = new Scanner(inputStream); + running = true; + + sb.append("\nciip Gruppe 8 - Password Manager\n\n"); + + sb.append("Menu:\n"); + sb.append("- list vaults: l:\n"); + sb.append("- select vault: v x (replace x with vault id)\n"); + sb.append("- exit: e\n"); + + println(sb.toString()); + + String input = scan.nextLine(); + + if (input.equals("e")) { + running = false; + } else if (input.equals("l")) { + listVaults(); + } else if (input.startsWith("v")) { + String vaultAction = (input.replace('v', ' ')).trim(); + int vaultId = -1; + + try { + vaultId = Integer.parseInt(vaultAction); + } catch (NumberFormatException e) { + println("-- please enter a correct vault id"); + } + + if (vaultId >= 0) { + try { + int internalOptionCounter = 0; + ArrayList options = new ArrayList<>(); + for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected + try { + if (Modifier.isPublic(this.getClass().getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod) + options.add(m.getName()); + println(internalOptionCounter + " - " + m.getName()); + internalOptionCounter++; + } + } catch (NoSuchMethodException ignore) { + // println("No method with name " + m.getName() + " found"); + } + } + } catch (ClassNotFoundException e) { + println("-- vaults not implemented yet"); + } + } + } } } diff --git a/src/main/java/PasswordManagerInterface.java b/src/main/java/PasswordManagerInterface.java index 385532f..b7a7616 100644 --- a/src/main/java/PasswordManagerInterface.java +++ b/src/main/java/PasswordManagerInterface.java @@ -1,3 +1,6 @@ +import java.io.InputStream; +import java.io.OutputStream; + public interface PasswordManagerInterface { public static void main(String[] args) { }; diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index b18cf0d..149e12f 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -1,6 +1,12 @@ import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.NoSuchElementException; + import static org.junit.jupiter.api.Assertions.*; @@ -13,6 +19,12 @@ class PasswordManagerTest { pm = new PasswordManager(); } + @BeforeEach + void reset() { + pm.outputStream = System.out; + pm.inputStream = System.in; + } + @Test void constructor() { assertInstanceOf(PasswordManager.class, pm); @@ -28,8 +40,55 @@ class PasswordManagerTest { pm.openVault(); } + @Test + void menuNavigationListVaults() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + pm.inputStream = new ByteArrayInputStream("l\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = outputStream; + pm.showMenu(); + assertTrue(outputStream.toString().endsWith("Vaults:\n")); + } + + @Test + void menuNavigationOpenVault() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + pm.inputStream = new ByteArrayInputStream("v 0\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = outputStream; + pm.showMenu(); + assertTrue(outputStream.toString().endsWith("-- vaults not implemented yet\n")); + } + @Test void showMenu() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + pm.inputStream = getEmptyStringInputStream(); + pm.outputStream = outputStream; + assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu()); + assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8")); + } + + @Test + void exitMenu() { + pm.inputStream = new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8)); pm.showMenu(); + assertFalse(pm.running); + } + + @Test + void doNotExitMenuAfterWrongInput() { + pm.inputStream = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)); + pm.showMenu(); + assertTrue(pm.running); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + pm.inputStream = getEmptyStringInputStream(); + pm.outputStream = outputStream; + + assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu()); + assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8")); + } + + private ByteArrayInputStream getEmptyStringInputStream() { + return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); } }