Browse Source

select vault in menu

feature-implement-menu-control
binsky 3 years ago
parent
commit
143653c2bc
  1. 54
      src/main/java/PasswordManager.java
  2. 13
      src/test/java/PasswordManagerTest.java

54
src/main/java/PasswordManager.java

@ -1,7 +1,10 @@
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 {
@ -20,17 +23,17 @@ public class PasswordManager implements PasswordManagerInterface {
}
public PasswordManager() {
try {
outputStream.write("Hello World\n".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
println("Hello World");
}
@Override
public void listVaults() {
println("Vaults:");
}
private void println(String output) {
try {
outputStream.write("Vaults:\n".getBytes(StandardCharsets.UTF_8));
outputStream.write((output + "\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
@ -47,17 +50,14 @@ public class PasswordManager implements PasswordManagerInterface {
Scanner scan = new Scanner(inputStream);
running = true;
sb.append("ciip Gruppe 8 - Password Manager\n\n");
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");
try {
outputStream.write((sb + "\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
println(sb.toString());
String input = scan.nextLine();
@ -65,6 +65,36 @@ public class PasswordManager implements PasswordManagerInterface {
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<String> 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");
}
}
}
}
}

13
src/test/java/PasswordManagerTest.java

@ -49,13 +49,22 @@ class PasswordManagerTest {
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("ciip Gruppe 8"));
assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8"));
}
@Test
@ -76,7 +85,7 @@ class PasswordManagerTest {
pm.outputStream = outputStream;
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu());
assertTrue(outputStream.toString().startsWith("ciip Gruppe 8"));
assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8"));
}
private ByteArrayInputStream getEmptyStringInputStream() {

Loading…
Cancel
Save