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.

188 lines
6.1 KiB

  1. import org.junit.jupiter.api.BeforeAll;
  2. import org.junit.jupiter.api.BeforeEach;
  3. import org.junit.jupiter.api.Test;
  4. import org.mockito.Mockito;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.ByteArrayOutputStream;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.NoSuchElementException;
  9. import static org.junit.jupiter.api.Assertions.*;
  10. public class VaultTest {
  11. //Vault vlt = new Vault();
  12. static Vault vlt;
  13. @BeforeAll
  14. static void init() {
  15. vlt = new Vault();
  16. }
  17. @BeforeEach
  18. void reset() {
  19. vlt.outputS = System.out;
  20. vlt.inputS = System.in;
  21. }
  22. @Test
  23. void addCredentialTest() {
  24. String expected = "user";
  25. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  26. vlt.inputS = new ByteArrayInputStream(expected.getBytes(StandardCharsets.UTF_8));
  27. vlt.outputS = outputStream;
  28. vlt.addCredential();
  29. assertTrue(outputStream.toString().startsWith("Add"));
  30. assertEquals(expected, vlt.userName);
  31. }
  32. @Test
  33. void showCredential() {vlt.showCredential();}
  34. @Test
  35. void getAsJson() {vlt.getAsJson();}
  36. @Test
  37. void loadFromJson() {vlt.loadFromJson();}
  38. @Test
  39. void openConfigureMenu() {
  40. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  41. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  42. vlt.outputS = outputStream;
  43. vlt.configure();
  44. assertTrue(outputStream.toString().startsWith("Configure:"));
  45. }
  46. @Test
  47. void exitConfigurationMenu(){
  48. vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
  49. vlt.configure();
  50. assertFalse(vlt.config);
  51. }
  52. @Test
  53. void doNotExitConfigAfterWrongInput() {
  54. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  55. vlt.configure();
  56. assertTrue(vlt.config);
  57. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  58. vlt.inputS = getEmptyStringInputStream();
  59. vlt.outputS = outputStream;
  60. assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure());
  61. assertTrue(outputStream.toString().startsWith("Configure:"));
  62. }
  63. @Test
  64. void configureMenuWrongInputMessage() {
  65. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  66. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  67. vlt.outputS = outputStream;
  68. vlt.configure();
  69. assertTrue(outputStream.toString().contains("Please"));
  70. }
  71. @Test
  72. void openSetPWLength() {
  73. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  74. vlt.inputS = new ByteArrayInputStream("12".getBytes(StandardCharsets.UTF_8));
  75. vlt.outputS = outputStream;
  76. vlt.setPWLength();
  77. assertTrue(outputStream.toString().startsWith("Set"));
  78. }
  79. @Test
  80. void setPWLength() {
  81. vlt.inputS = new ByteArrayInputStream("7\n".getBytes(StandardCharsets.UTF_8));
  82. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  83. vlt.outputS = outputStream;
  84. vlt.setPWLength();
  85. assertEquals(7, vlt.PWLength);
  86. assertTrue(outputStream.toString().contains("now:"));
  87. }
  88. @Test
  89. void setPWLengthWrongInput() {
  90. vlt.inputS = new ByteArrayInputStream("T\n".getBytes(StandardCharsets.UTF_8));
  91. vlt.setPWLength();
  92. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  93. vlt.inputS = getEmptyStringInputStream();
  94. vlt.outputS = outputStream;
  95. assertThrowsExactly(NoSuchElementException.class, () -> vlt.setPWLength());
  96. assertTrue(outputStream.toString().startsWith("Set"));
  97. }
  98. @Test
  99. void setCapital() {
  100. vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));
  101. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  102. vlt.outputS = outputStream;
  103. vlt.setCapital();
  104. assertTrue(outputStream.toString().startsWith("Should"));
  105. assertTrue(vlt.haveCapitals = true);
  106. assertTrue(outputStream.toString().contains("contain"));
  107. vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
  108. vlt.outputS = outputStream;
  109. vlt.setCapital();
  110. assertTrue(outputStream.toString().startsWith("Should"));
  111. assertFalse(vlt.haveCapitals = false);
  112. assertTrue(outputStream.toString().contains("don´t"));
  113. }
  114. @Test
  115. void setSpecialCharTest() {
  116. vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));
  117. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  118. vlt.outputS = outputStream;
  119. vlt.setSpecialChar();
  120. assertTrue(outputStream.toString().startsWith("Should"));
  121. assertTrue(vlt.hasSpecialChars = true);
  122. assertTrue(outputStream.toString().contains("contain"));
  123. vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
  124. vlt.outputS = outputStream;
  125. vlt.setSpecialChar();
  126. assertTrue(outputStream.toString().startsWith("Should"));
  127. assertFalse(vlt.hasSpecialChars = false);
  128. assertTrue(outputStream.toString().contains("don´t"));
  129. }
  130. @Test
  131. void setNumbersTest() {
  132. vlt.inputS = new ByteArrayInputStream("yes\n".getBytes(StandardCharsets.UTF_8));
  133. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  134. vlt.outputS = outputStream;
  135. vlt.SetNumbers();
  136. assertTrue(outputStream.toString().startsWith("Should"));
  137. assertTrue(vlt.hasNumbers = true);
  138. assertTrue(outputStream.toString().contains("contain"));
  139. vlt.inputS = new ByteArrayInputStream("no\n".getBytes(StandardCharsets.UTF_8));
  140. vlt.outputS = outputStream;
  141. vlt.SetNumbers();
  142. assertTrue(outputStream.toString().startsWith("Should"));
  143. assertFalse(vlt.hasNumbers = false);
  144. assertTrue(outputStream.toString().contains("don´t"));
  145. }
  146. private ByteArrayInputStream getEmptyStringInputStream() {
  147. return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
  148. }
  149. }