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.
147 lines
4.8 KiB
147 lines
4.8 KiB
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.NoSuchElementException;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
class PasswordManagerTest {
|
|
|
|
static PasswordManager pm;
|
|
|
|
@BeforeAll
|
|
static void init() {
|
|
pm = new PasswordManager();
|
|
}
|
|
|
|
@BeforeEach
|
|
void reset() {
|
|
pm.outputStream = System.out;
|
|
pm.inputStream = System.in;
|
|
}
|
|
|
|
@Test
|
|
void constructor() {
|
|
assertInstanceOf(PasswordManager.class, pm);
|
|
}
|
|
|
|
@Test
|
|
void listVaults() {
|
|
pm.listVaults();
|
|
}
|
|
|
|
@Test
|
|
void openVault() {
|
|
pm.openVault();
|
|
}
|
|
|
|
@Test
|
|
void menuNavigationListVaults() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
pm.inputStream = new ByteArrayInputStream("l\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = outputStream;
|
|
pm.showMenu();
|
|
pm.createNewVault();
|
|
assertTrue(outputStream.toString().contains("Vaults:\n- vault id: 0\n"));
|
|
}
|
|
|
|
@Test
|
|
void menuNavigationCreateNewVault() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
pm.inputStream = new ByteArrayInputStream("v\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = outputStream;
|
|
int vaultCount = pm.vaults.size();
|
|
pm.showMenu();
|
|
assertEquals(vaultCount + 1, pm.vaults.size());
|
|
}
|
|
|
|
@Test
|
|
void menuNavigationOpenVault() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
pm.inputStream = new ByteArrayInputStream("v 0\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = outputStream;
|
|
pm.showMenu();
|
|
assertFalse(outputStream.toString().endsWith("-- vaults not implemented yet\n"));
|
|
}
|
|
|
|
@Test
|
|
void menuNavigationSelectVaultOperation() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
pm.inputStream = new ByteArrayInputStream("v\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = new ByteArrayOutputStream();
|
|
pm.showMenu();
|
|
|
|
pm.inputStream = new ByteArrayInputStream("v 0\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = new ByteArrayOutputStream();
|
|
pm.showMenu();
|
|
System.out.println(pm.outputStream.toString());
|
|
|
|
pm.inputStream = new ByteArrayInputStream("o 0\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = outputStream;
|
|
pm.vaults.get(0).inputS = new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.showMenu();
|
|
assertTrue(outputStream.toString().contains("Run selected operation"));
|
|
|
|
outputStream = new ByteArrayOutputStream();
|
|
|
|
pm.inputStream = new ByteArrayInputStream("o foobert\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = outputStream;
|
|
pm.showMenu();
|
|
assertTrue(outputStream.toString().endsWith("-- please enter a valid operation id\n"));
|
|
|
|
outputStream = new ByteArrayOutputStream();
|
|
|
|
pm.inputStream = new ByteArrayInputStream("o 1000\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.outputStream = outputStream;
|
|
pm.showMenu();
|
|
System.out.println(outputStream);
|
|
assertTrue(outputStream.toString().endsWith("-- please enter a valid operation id\n"));
|
|
}
|
|
|
|
@Test
|
|
void showMenu() {
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
pm.inputStream = getEmptyStringInputStream();
|
|
pm.outputStream = outputStream;
|
|
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu());
|
|
assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8"));
|
|
}
|
|
|
|
@Test
|
|
void exitMenu() {
|
|
pm.inputStream = new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.showMenu();
|
|
assertFalse(pm.running);
|
|
}
|
|
|
|
@Test
|
|
void doNotExitMenuAfterWrongInput() {
|
|
pm.inputStream = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
|
|
pm.showMenu();
|
|
assertTrue(pm.running);
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
pm.inputStream = getEmptyStringInputStream();
|
|
pm.outputStream = outputStream;
|
|
|
|
assertThrowsExactly(NoSuchElementException.class, () -> pm.showMenu());
|
|
assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8"));
|
|
}
|
|
|
|
@Test
|
|
void createNewVault() {
|
|
int vaultCount = pm.vaults.size();
|
|
pm.createNewVault();
|
|
assertEquals(vaultCount + 1, pm.vaults.size());
|
|
}
|
|
|
|
private ByteArrayInputStream getEmptyStringInputStream() {
|
|
return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
}
|