Browse Source

Merge commit 'df02d33f556a100e23ac92a56e444797fd193db2' into HEAD

feature-refactor-password-validator-tests
jenkins 3 years ago
parent
commit
87da40c570
  1. 68
      src/main/java/Vault.java
  2. 102
      src/test/java/VaultTest.java

68
src/main/java/Vault.java

@ -1,9 +1,63 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Vault implements VaultInterface { public class Vault implements VaultInterface {
public boolean config;
public int PWLength;
public boolean haveCapitals = false;
InputStream inputS = System.in;
OutputStream outputS = System.out;
private void println(String output) {
try {
outputS.write((output + "\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public void configure(){ public void configure(){
StringBuilder sbcm = new StringBuilder();
Scanner scan = new Scanner(inputS);
config = true;
sbcm.append("Configure:\n");
sbcm.append("- Password length: l\n");
sbcm.append("- Have Capitals: h\n");
sbcm.append("- exit: e\n");
println(sbcm.toString());
String input = scan.nextLine();
if (input.equals("e")) {
config = false;
}
} }
public void setPWLength(){
println("Set PW length:");
Scanner scan = new Scanner(inputS);
String input = scan.nextLine();
try {
PWLength = Integer.parseInt(input);
} catch (NumberFormatException e) {
println("Please Enter Valid Number.");
}
println("\nNew PWlength is now: ");
}
public void addCredential(){ public void addCredential(){
} }
@ -20,4 +74,18 @@ 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();
if(input.equals("yes")){
haveCapitals = true;
println("Your PWs contain now capitals.");
}else if(input.equals("no")){
haveCapitals = false;
println("Your PWs don´t have capitals anymore.");
}
}
} }

102
src/test/java/VaultTest.java

@ -1,14 +1,19 @@
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.*;
public class VaultTest { public class VaultTest {
//Vault vlt = new Vault();
static Vault vlt; static Vault vlt;
@BeforeAll @BeforeAll
@ -16,14 +21,11 @@ public class VaultTest {
vlt = new Vault(); vlt = new Vault();
} }
/* @BeforeEach
@BeforeEach
void reset() { void reset() {
pm.outputStream = System.out;
pm.inputStream = System.in;
}*/
@Test
void configure() {vlt.configure();}
vlt.outputS = System.out;
vlt.inputS = System.in;
}
@Test @Test
void addCredential() {vlt.addCredential();} void addCredential() {vlt.addCredential();}
@ -37,4 +39,90 @@ public class VaultTest {
@Test @Test
void loadFromJson() {vlt.loadFromJson();} void loadFromJson() {vlt.loadFromJson();}
@Test
void openConfigureMenu() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.configure();
assertTrue(outputStream.toString().startsWith("Configure:"));
}
@Test
void exitConfigurationMenu(){
vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
vlt.configure();
assertFalse(vlt.config);
}
@Test
void doNotExitConfigAfterWrongInput() {
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
vlt.configure();
assertTrue(vlt.config);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = getEmptyStringInputStream();
vlt.outputS = outputStream;
assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure());
assertTrue(outputStream.toString().startsWith("Configure:"));
}
@Test
void openSetPWLength() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("12".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.setPWLength();
assertTrue(outputStream.toString().startsWith("Set"));
}
@Test
void setPWLength() {
vlt.inputS = new ByteArrayInputStream("7\n".getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
vlt.setPWLength();
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));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
vlt.setCapital();
assertTrue(outputStream.toString().startsWith("Should"));
assertTrue(vlt.haveCapitals = true);
assertTrue(outputStream.toString().contains("contain"));
vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.setCapital();
assertTrue(outputStream.toString().startsWith("Should"));
assertFalse(vlt.haveCapitals = false);
assertTrue(outputStream.toString().contains("don´t"));
}
private ByteArrayInputStream getEmptyStringInputStream() {
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
}
} }
Loading…
Cancel
Save