Browse Source

Merge commit 'a18ebb743871694e4c56ad5403a5b5894ed1495b' into HEAD

feature-refactor-password-validator-tests
jenkins 3 years ago
parent
commit
a059cec5c1
  1. 70
      src/main/java/Vault.java
  2. 11
      src/main/java/VaultInterface.java
  3. 85
      src/test/java/VaultTest.java

70
src/main/java/Vault.java

@ -8,7 +8,10 @@ public class Vault implements VaultInterface {
public boolean config; public boolean config;
public int PWLength; public int PWLength;
public boolean haveCapitals = false;
public boolean haveCapitals;
public boolean hasSpecialChars;
public boolean hasNumbers;
public String userName;
InputStream inputS = System.in; InputStream inputS = System.in;
OutputStream outputS = System.out; OutputStream outputS = System.out;
@ -27,7 +30,9 @@ public class Vault implements VaultInterface {
sbcm.append("Configure:\n"); sbcm.append("Configure:\n");
sbcm.append("- Password length: l\n"); sbcm.append("- Password length: l\n");
sbcm.append("- Have Capitals: h\n");
sbcm.append("- Have capitals: h\n");
sbcm.append("- Have special characters: s\n");
sbcm.append("- Have Numbers: n\n");
sbcm.append("- exit: e\n"); sbcm.append("- exit: e\n");
println(sbcm.toString()); println(sbcm.toString());
@ -36,25 +41,33 @@ public class Vault implements VaultInterface {
if (input.equals("e")) { if (input.equals("e")) {
config = false; config = false;
} else if (input.equals("l")) {
println("Set PW length:");
String inputPw = scan.nextLine();
setPWLength(inputPw);
} else if (input.equals("h")) {
println("Should your PW have capitals? Type in yes or no.");
String inputC = scan.nextLine();
setCapital(inputC);
} else if (input.equals("s")){
println("Should you PW have special characters? Type in yes or no.");
String inputS = scan.nextLine();
setSpecialChar(inputS);
} else if (input.equals("n")) {
println("Should you PW have numbers? Type in yes or no.");
String inputN = scan.nextLine();
setNumbers(input);
} }
} }
public void setPWLength(){
println("Set PW length:");
Scanner scan = new Scanner(inputS);
String input = scan.nextLine();
public void setPWLength(String pwLength){
try { try {
PWLength = Integer.parseInt(input);
PWLength = Integer.parseInt(pwLength);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
println("Please Enter Valid Number.");
println("Please enter valid number.");
} }
println("\nNew PWlength is now: ");
println("\nNew password length is now: ");
} }
@ -74,11 +87,7 @@ public class Vault implements VaultInterface {
} }
public void setCapital() {
println("Should you PW have Capitals? Type in yes or no.");
Scanner scan = new Scanner(inputS);
String input = scan.nextLine();
public void setCapital(String input) {
if(input.equals("yes")){ if(input.equals("yes")){
haveCapitals = true; haveCapitals = true;
@ -88,4 +97,27 @@ public class Vault implements VaultInterface {
println("Your PWs don´t have capitals anymore."); println("Your PWs don´t have capitals anymore.");
} }
} }
public void setSpecialChar(String input) {
if(input.equals("yes")){
hasSpecialChars = true;
println("Your PWs contain now special characters.");
}else if(input.equals("no")){
hasSpecialChars = false;
println("Your PWs don´t have special characters anymore.");
}
}
public void setNumbers(String input) {
if(input.equals("yes")){
hasNumbers = true;
println("Your PWs contains now numbers.");
}else if(input.equals("no")){
hasNumbers = false;
println("Your PWs don´t have numbers anymore.");
}
}
} }

11
src/main/java/VaultInterface.java

@ -2,10 +2,9 @@
public interface VaultInterface { public interface VaultInterface {
public void configure();
public void addCredential();
public void showCredential();
public void getAsJson();
public void loadFromJson();
void configure();
void addCredential();
void showCredential();
void getAsJson();
void loadFromJson();
} }

85
src/test/java/VaultTest.java

@ -28,7 +28,7 @@ public class VaultTest {
} }
@Test @Test
void addCredential() {vlt.addCredential();}
void addCredentialTest() { vlt.addCredential();}
@Test @Test
void showCredential() {vlt.showCredential();} void showCredential() {vlt.showCredential();}
@ -70,58 +70,99 @@ public class VaultTest {
} }
@Test @Test
void openSetPWLength() {
void openSetPWLengthMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 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.outputS = outputStream;
vlt.setPWLength();
assertTrue(outputStream.toString().startsWith("Set"));
vlt.configure();
assertEquals(7, vlt.PWLength);
assertTrue(outputStream.toString().contains("Set"));
} }
@Test @Test
void setPWLength() { void setPWLength() {
vlt.inputS = new ByteArrayInputStream("7\n".getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream; vlt.outputS = outputStream;
vlt.setPWLength();
vlt.setPWLength("7");
assertEquals(7, vlt.PWLength); assertEquals(7, vlt.PWLength);
assertTrue(outputStream.toString().contains("now:")); assertTrue(outputStream.toString().contains("now:"));
} }
@Test @Test
void setPWLengthWrongInput() {
vlt.inputS = new ByteArrayInputStream("T\n".getBytes(StandardCharsets.UTF_8));
vlt.setPWLength();
void openSetCapitalsMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = getEmptyStringInputStream();
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream; vlt.outputS = outputStream;
assertThrowsExactly(NoSuchElementException.class, () -> vlt.setPWLength());
assertTrue(outputStream.toString().startsWith("Set"));
vlt.configure();
assertTrue(vlt.haveCapitals = true);
} }
@Test @Test
void setCapital() {
vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));
void setCapitals() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream; vlt.outputS = outputStream;
vlt.setCapital();
vlt.setCapital("yes");
assertTrue(outputStream.toString().startsWith("Should"));
assertTrue(vlt.haveCapitals = true); assertTrue(vlt.haveCapitals = true);
assertTrue(outputStream.toString().contains("contain")); assertTrue(outputStream.toString().contains("contain"));
vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream; vlt.outputS = outputStream;
vlt.setCapital();
vlt.setCapital("no");
assertTrue(outputStream.toString().startsWith("Should"));
assertFalse(vlt.haveCapitals = false); assertFalse(vlt.haveCapitals = false);
assertTrue(outputStream.toString().contains("don´t")); assertTrue(outputStream.toString().contains("don´t"));
} }
@Test
void openSetSpecialCharsMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.configure();
assertTrue(vlt.hasSpecialChars = true);
}
@Test
void setSpecialCharTest() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
vlt.setSpecialChar("yes");
assertTrue(vlt.hasSpecialChars = true);
vlt.outputS = outputStream;
vlt.setSpecialChar("no");
assertFalse(vlt.hasSpecialChars = false);
assertTrue(outputStream.toString().contains("don´t"));
}
@Test
void openSetNumbersMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.configure();
assertTrue(vlt.hasNumbers = true);
}
@Test
void setNumbersTest() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
vlt.setNumbers("yes");
assertTrue(vlt.hasNumbers = true);
assertTrue(outputStream.toString().contains("contain"));
vlt.outputS = outputStream;
vlt.setNumbers("no");
assertFalse(vlt.hasNumbers = false);
assertTrue(outputStream.toString().contains("don´t"));
}
private ByteArrayInputStream getEmptyStringInputStream() { private ByteArrayInputStream getEmptyStringInputStream() {
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
} }

Loading…
Cancel
Save