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.

239 lines
7.6 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 openCredentialMenu() {
  24. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  25. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  26. vlt.outputS = outputStream;
  27. vlt.credentialMenu();
  28. assertTrue(vlt.credentialM);
  29. assertTrue(outputStream.toString().startsWith("Configure"));
  30. }
  31. @Test
  32. void exitConfigureMenu(){
  33. vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
  34. vlt.credentialMenu();
  35. assertFalse(vlt.credentialM);
  36. }
  37. @Test
  38. void openAddCredentialMenuItem() {
  39. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  40. vlt.inputS = new ByteArrayInputStream("a".getBytes(StandardCharsets.UTF_8));
  41. vlt.outputS = outputStream;
  42. vlt.credentialMenu();
  43. assertTrue(outputStream.toString().contains("Type"));
  44. }
  45. @Test
  46. void openShowCredentialMenuItem() {
  47. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  48. vlt.inputS = new ByteArrayInputStream("c".getBytes(StandardCharsets.UTF_8));
  49. vlt.outputS = outputStream;
  50. vlt.credentialMenu();
  51. assertTrue(outputStream.toString().contains("Type"));
  52. }
  53. @Test
  54. void openEditCredentialMenuItem() {
  55. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  56. vlt.inputS = new ByteArrayInputStream("l".getBytes(StandardCharsets.UTF_8));
  57. vlt.outputS = outputStream;
  58. vlt.credentialMenu();
  59. assertTrue(outputStream.toString().contains("Type"));
  60. }
  61. @Test
  62. void addCredentialTest() {
  63. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  64. vlt.outputS = outputStream;
  65. vlt.inputS = new ByteArrayInputStream("peter\npassword".getBytes(StandardCharsets.UTF_8));
  66. vlt.addCredential();
  67. assertTrue(outputStream.toString().contains("username"));
  68. assertEquals("peter", vlt.userName);
  69. assertTrue(outputStream.toString().contains("password"));
  70. assertEquals("password",vlt.password);
  71. }
  72. @Test
  73. void showCredentialTest() {
  74. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  75. vlt.outputS = outputStream;
  76. vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8));
  77. vlt.showCredential();
  78. assertTrue(outputStream.toString().contains("Type"));
  79. assertTrue(vlt.isInt);
  80. vlt.outputS = outputStream;
  81. vlt.inputS = new ByteArrayInputStream("peter".getBytes(StandardCharsets.UTF_8));
  82. vlt.showCredential();
  83. assertTrue(outputStream.toString().contains("Type"));
  84. assertFalse(vlt.isInt);
  85. }*/
  86. @Test
  87. void getAsJson() {vlt.getAsJson();}
  88. @Test
  89. void loadFromJson() {vlt.loadFromJson();}
  90. /* @Test
  91. void openConfigureMenu() {
  92. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  93. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  94. vlt.outputS = outputStream;
  95. vlt.configure();
  96. assertTrue(outputStream.toString().startsWith("Configure:"));
  97. }
  98. @Test
  99. void exitConfigurationMenu(){
  100. vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
  101. vlt.configure();
  102. assertFalse(vlt.config);
  103. }*/
  104. /* @Test
  105. void doNotExitConfigAfterWrongInput() {
  106. vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
  107. vlt.configure();
  108. assertTrue(vlt.config);
  109. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  110. vlt.inputS = getEmptyStringInputStream();
  111. vlt.outputS = outputStream;
  112. assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure());
  113. assertTrue(outputStream.toString().startsWith("Configure:"));
  114. }*/
  115. /* @Test
  116. void openSetPWLengthMenuItem() {
  117. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  118. vlt.inputS = new ByteArrayInputStream("l\n7".getBytes(StandardCharsets.UTF_8));
  119. vlt.outputS = outputStream;
  120. vlt.configure();
  121. assertEquals(7, vlt.PWLength);
  122. assertTrue(outputStream.toString().contains("Set"));
  123. }*/
  124. @Test
  125. void setPWLength() {
  126. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  127. vlt.outputS = outputStream;
  128. vlt.setPWLength("7");
  129. assertEquals(7, vlt.PWLength);
  130. assertTrue(outputStream.toString().contains("now:"));
  131. }
  132. /*@Test
  133. void openSetCapitalsMenuItem() {
  134. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  135. vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
  136. vlt.outputS = outputStream;
  137. vlt.configure();
  138. assertTrue(vlt.haveCapitals = true);
  139. }*/
  140. @Test
  141. void setCapitals() {
  142. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  143. vlt.outputS = outputStream;
  144. vlt.setCapital("yes");
  145. assertTrue(vlt.haveCapitals = true);
  146. assertTrue(outputStream.toString().contains("contain"));
  147. vlt.outputS = outputStream;
  148. vlt.setCapital("no");
  149. assertFalse(vlt.haveCapitals = false);
  150. assertTrue(outputStream.toString().contains("don't"));
  151. System.out.println(outputStream.toString());
  152. }
  153. /*@Test
  154. void openSetSpecialCharsMenuItem() {
  155. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  156. vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
  157. vlt.outputS = outputStream;
  158. vlt.configure();
  159. assertTrue(vlt.hasSpecialChars = true);
  160. }*/
  161. @Test
  162. void setSpecialCharTest() {
  163. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  164. vlt.outputS = outputStream;
  165. vlt.setSpecialChar("yes");
  166. assertTrue(vlt.hasSpecialChars = true);
  167. vlt.outputS = outputStream;
  168. vlt.setSpecialChar("no");
  169. assertFalse(vlt.hasSpecialChars = false);
  170. assertTrue(outputStream.toString().contains("don't"));
  171. }
  172. /*@Test
  173. void openSetNumbersMenuItem() {
  174. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  175. vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
  176. vlt.outputS = outputStream;
  177. vlt.configure();
  178. assertTrue(vlt.hasNumbers = true);
  179. }*/
  180. @Test
  181. void setNumbersTest() {
  182. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  183. vlt.outputS = outputStream;
  184. vlt.setNumbers("yes");
  185. assertTrue(vlt.hasNumbers = true);
  186. assertTrue(outputStream.toString().contains("contain"));
  187. vlt.outputS = outputStream;
  188. vlt.setNumbers("no");
  189. assertFalse(vlt.hasNumbers = false);
  190. assertTrue(outputStream.toString().contains("don't"));
  191. }
  192. private ByteArrayInputStream getEmptyStringInputStream() {
  193. return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
  194. }
  195. }