public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) { checkFormatString(promptFormat); this.console = console; this.promptFormat = promptFormat; this.maxTries = maxTries; this.numTries = 0; }
@Test public void testShouldRetry() { Resource<String> resource = new PrivateKeyStringResource(""); ConsolePasswordFinder finder = new ConsolePasswordFinder(null, FORMAT, 1); Assert.assertTrue("Should allow a retry at first", finder.shouldRetry(resource)); finder.reqPassword(resource); Assert.assertFalse("Should stop allowing retries after one interaction", finder.shouldRetry(resource)); }
@Test public void testReqPasswordNullConsole() { Resource<?> resource = Mockito.mock(Resource.class); char[] password = new ConsolePasswordFinder(null, FORMAT, 1).reqPassword(resource); Assert.assertNull("Password should be null with null console", password); Mockito.verifyNoMoreInteractions(resource); }
@Test(expected = IllegalArgumentException.class) public void testPromptFormatTooManyMarkers() { new ConsolePasswordFinder(null, "%s%s", 1); }
@Test public void testReqPassword() { char[] expectedPassword = "password".toCharArray(); Console console = Mockito.mock(Console.class); Mockito.when(console.readPassword(Mockito.anyString(), Mockito.any())) .thenReturn(expectedPassword); Resource resource = Mockito.mock(Resource.class); char[] password = new ConsolePasswordFinder(console).reqPassword(resource); Assert.assertArrayEquals("Password should match mocked return value", expectedPassword, password); Mockito.verifyNoMoreInteractions(resource); }
@Test(expected = IllegalArgumentException.class) public void testPromptFormatWrongMarkerType() { new ConsolePasswordFinder(null, "%d", 1); }
public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) { checkFormatString(promptFormat); this.console = console; this.promptFormat = promptFormat; this.maxTries = maxTries; this.numTries = 0; }
@Test public void testPromptFormat() { Assert.assertNotNull( "Empty format should create valid ConsolePasswordFinder", new ConsolePasswordFinder(null, "", 1)); Assert.assertNotNull( "Single-string format should create valid ConsolePasswordFinder", new ConsolePasswordFinder(null, FORMAT, 1)); }