You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito;
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.*;
public class VaultTest {
//Vault vlt = new Vault();
static Vault vlt;
@BeforeAll static void init() { vlt = new Vault(); }
@BeforeEach void reset() { vlt.outputS = System.out; vlt.inputS = System.in; }
@Test void addCredential() {vlt.addCredential();}
@Test void showCredential() {vlt.showCredential();}
@Test void getAsJson() {vlt.getAsJson();}
@Test 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")); }
@Test void setSpecialCharTest() { vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); vlt.outputS = outputStream; vlt.setSpecialChar();
assertTrue(outputStream.toString().startsWith("Should")); assertTrue(vlt.hasSpecialChars = true); assertTrue(outputStream.toString().contains("contain"));
vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8)); vlt.outputS = outputStream; vlt.setSpecialChar();
assertTrue(outputStream.toString().startsWith("Should")); assertFalse(vlt.hasSpecialChars = false); assertTrue(outputStream.toString().contains("don´t")); }
private ByteArrayInputStream getEmptyStringInputStream() { return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); } }
|