Browse Source

navigate to list vaults in menu

feature-implement-menu-control
binsky 3 years ago
parent
commit
97e48e530f
  1. 20
      src/main/java/PasswordManager.java
  2. 2
      src/main/java/PasswordManagerInterface.java
  3. 34
      src/test/java/PasswordManagerTest.java

20
src/main/java/PasswordManager.java

@ -14,7 +14,7 @@ public class PasswordManager implements PasswordManagerInterface {
PasswordManager pm = new PasswordManager();
while (pm.running) {
pm.showMenu(null, null);
pm.showMenu();
}
System.exit(0);
}
@ -29,7 +29,11 @@ public class PasswordManager implements PasswordManagerInterface {
@Override
public void listVaults() {
try {
outputStream.write("Vaults:\n".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
@ -38,14 +42,7 @@ public class PasswordManager implements PasswordManagerInterface {
}
@Override
public void showMenu(InputStream inputStream, OutputStream outputStream) {
if (inputStream == null) {
inputStream = this.inputStream;
}
if (outputStream == null) {
outputStream = this.outputStream;
}
public void showMenu() {
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(inputStream);
running = true;
@ -53,6 +50,7 @@ public class PasswordManager implements PasswordManagerInterface {
sb.append("ciip Gruppe 8 - Password Manager\n\n");
sb.append("Menu:\n");
sb.append("- list vaults: l:\n");
sb.append("- exit: e\n");
try {
@ -65,6 +63,8 @@ public class PasswordManager implements PasswordManagerInterface {
if (input.equals("e")) {
running = false;
} else if (input.equals("l")) {
listVaults();
}
}
}

2
src/main/java/PasswordManagerInterface.java

@ -6,5 +6,5 @@ public interface PasswordManagerInterface {
};
public void listVaults();
public void openVault();
public void showMenu(InputStream inputStream, OutputStream outputStream);
public void showMenu();
}

34
src/test/java/PasswordManagerTest.java

@ -1,7 +1,9 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException;
@ -17,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);
@ -32,26 +40,42 @@ 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 showMenu() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(getEmptyStringInputStream(), outputStream));
pm.inputStream = getEmptyStringInputStream();
pm.outputStream = outputStream;
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu());
assertTrue(outputStream.toString().startsWith("ciip Gruppe 8"));
}
@Test
void exitMenu() {
pm.showMenu(new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8)), System.out);
pm.inputStream = new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8));
pm.showMenu();
assertFalse(pm.running);
}
@Test
void doNotExitMenuAfterWrongInput() {
pm.showMenu(new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8)), System.out);
pm.inputStream = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
pm.showMenu();
assertTrue(pm.running);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu(getEmptyStringInputStream(), outputStream));
pm.inputStream = getEmptyStringInputStream();
pm.outputStream = outputStream;
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu());
assertTrue(outputStream.toString().startsWith("ciip Gruppe 8"));
}

Loading…
Cancel
Save