diff --git a/src/main/java/Vault.java b/src/main/java/Vault.java index 4c3de46..e80e2f5 100644 --- a/src/main/java/Vault.java +++ b/src/main/java/Vault.java @@ -39,20 +39,20 @@ public class Vault implements VaultInterface { if (input.equals("e")) { config = false; - }else if (!input.equals("l")|!input.equals("l")|!input.equals("l")) { + }else if (input.equals("l")) { + String inputpw = scan.nextLine(); + setPWLength(inputpw); + }else if (!input.equals("l")|!input.equals("e")|!input.equals("h")) { println("Please enter valid key."); } } - public void setPWLength(){ + public void setPWLength(String pwLength){ println("Set PW length:"); - Scanner scan = new Scanner(inputS); - String input = scan.nextLine(); - try { - PWLength = Integer.parseInt(input); + PWLength = Integer.parseInt(pwLength); } catch (NumberFormatException e) { println("Please Enter Valid Number."); } diff --git a/src/test/java/VaultTest.java b/src/test/java/VaultTest.java index 00642ab..f13a60b 100644 --- a/src/test/java/VaultTest.java +++ b/src/test/java/VaultTest.java @@ -90,38 +90,24 @@ public class VaultTest { @Test - void openSetPWLength() { + void openSetPWLengthMenuItem() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - vlt.inputS = new ByteArrayInputStream("12".getBytes(StandardCharsets.UTF_8)); + vlt.inputS = new ByteArrayInputStream("l\n7".getBytes(StandardCharsets.UTF_8)); vlt.outputS = outputStream; - vlt.setPWLength(); - assertTrue(outputStream.toString().startsWith("Set")); + vlt.configure(); + assertEquals(7, vlt.PWLength); } @Test void setPWLength() { - vlt.inputS = new ByteArrayInputStream("7\n".getBytes(StandardCharsets.UTF_8)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); vlt.outputS = outputStream; - vlt.setPWLength(); + vlt.setPWLength("7"); assertEquals(7, vlt.PWLength); assertTrue(outputStream.toString().contains("now:")); } - @Test - void setPWLengthWrongInput() { - vlt.inputS = new ByteArrayInputStream("T\n".getBytes(StandardCharsets.UTF_8)); - vlt.setPWLength(); - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - vlt.inputS = getEmptyStringInputStream(); - vlt.outputS = outputStream; - - assertThrowsExactly(NoSuchElementException.class, () -> vlt.setPWLength()); - assertTrue(outputStream.toString().startsWith("Set")); - } - @Test void setCapital() { vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));