From 087e2d6b60446ba53dacf3f13ee0559e47e381ec Mon Sep 17 00:00:00 2001 From: fdai6352 Date: Tue, 15 Feb 2022 17:43:35 +0100 Subject: [PATCH] Set PWLength Functionality, proved against wrong input --- src/main/java/Vault.java | 8 +++++++- src/test/java/VaultTest.java | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/Vault.java b/src/main/java/Vault.java index 95e2c4a..772a844 100644 --- a/src/main/java/Vault.java +++ b/src/main/java/Vault.java @@ -45,7 +45,13 @@ public class Vault implements VaultInterface { Scanner scan = new Scanner(inputS); String input = scan.nextLine(); - PWLength = Integer.parseInt(input); + try { + PWLength = Integer.parseInt(input); + } catch (NumberFormatException e) { + println("Please Enter Valid Number."); + } + + println("\nNew PWlength is now: "); } diff --git a/src/test/java/VaultTest.java b/src/test/java/VaultTest.java index b64923a..fdbc0f9 100644 --- a/src/test/java/VaultTest.java +++ b/src/test/java/VaultTest.java @@ -89,6 +89,19 @@ public class VaultTest { 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")); + } + private ByteArrayInputStream getEmptyStringInputStream() { return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); }