Browse Source

Merge commit '143653c2bc39157a8b0a5f7f58c8cd364a56e3e7' into HEAD

feature-password-validator
jenkins 3 years ago
parent
commit
5504e883c7
  1. 79
      src/main/java/PasswordManager.java
  2. 3
      src/main/java/PasswordManagerInterface.java
  3. 59
      src/test/java/PasswordManagerTest.java

79
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<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");
}
}
}
}
}

3
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) {
};

59
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));
}
}
Loading…
Cancel
Save