Removed Builder, and fixed call to checkFormatString

This commit is contained in:
Matt Dailey
2017-07-07 20:27:30 -04:00
committed by Jeroen van Erp
parent 3729119e23
commit e5084ed8db
2 changed files with 24 additions and 71 deletions

View File

@@ -21,17 +21,24 @@ import java.util.IllegalFormatException;
/** A PasswordFinder that reads a password from a console */ /** A PasswordFinder that reads a password from a console */
public class ConsolePasswordFinder implements PasswordFinder { public class ConsolePasswordFinder implements PasswordFinder {
public static final String DEFAULT_FORMAT = "Enter passphrase for %s:";
private final Console console; private final Console console;
private final String promptFormat; private final String promptFormat;
private final int maxTries; private final int maxTries;
private int numTries; private int numTries;
public static ConsolePasswordFinderBuilder builder() { public ConsolePasswordFinder() {
return new ConsolePasswordFinderBuilder(); this(System.console());
}
public ConsolePasswordFinder(Console console) {
this(console, DEFAULT_FORMAT, 3);
} }
public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) { public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) {
checkFormatString(promptFormat);
this.console = console; this.console = console;
this.promptFormat = promptFormat; this.promptFormat = promptFormat;
this.maxTries = maxTries; this.maxTries = maxTries;
@@ -53,59 +60,12 @@ public class ConsolePasswordFinder implements PasswordFinder {
return numTries < maxTries; return numTries < maxTries;
} }
public static class ConsolePasswordFinderBuilder { private static void checkFormatString(String promptFormat) {
private Console console; try {
private String promptFormat; String.format(promptFormat, "");
private int maxTries; } catch (IllegalFormatException e) {
throw new IllegalArgumentException("promptFormat must have no more than one %s and no other markers", e);
/** Builder constructor should only be called from parent class */
private ConsolePasswordFinderBuilder() {
console = System.console();
promptFormat = "Enter passphrase for %s:";
maxTries = 3;
}
public ConsolePasswordFinder build() {
return new ConsolePasswordFinder(console, promptFormat, maxTries);
}
public ConsolePasswordFinderBuilder setConsole(Console console) {
this.console = console;
return this;
}
public Console getConsole() {
return console;
}
/**
* @param promptFormat a StringFormatter string that may contain up to one "%s"
*/
public ConsolePasswordFinderBuilder setPromptFormat(String promptFormat) {
checkFormatString(promptFormat);
this.promptFormat = promptFormat;
return this;
}
public String getPromptFormat() {
return promptFormat;
}
public ConsolePasswordFinderBuilder setMaxTries(int maxTries) {
this.maxTries = maxTries;
return this;
}
public int getMaxTries() {
return maxTries;
}
private static void checkFormatString(String promptFormat) {
try {
String.format(promptFormat, "");
} catch (IllegalFormatException e) {
throw new IllegalArgumentException("promptFormat must have no more than one %s and no other markers", e);
}
} }
} }
} }

View File

@@ -23,6 +23,8 @@ import java.io.Console;
public class TestConsolePasswordFinder { public class TestConsolePasswordFinder {
private static final String FORMAT = "%s";
@Test @Test
public void testReqPassword() { public void testReqPassword() {
char[] expectedPassword = "password".toCharArray(); char[] expectedPassword = "password".toCharArray();
@@ -32,10 +34,7 @@ public class TestConsolePasswordFinder {
.thenReturn(expectedPassword); .thenReturn(expectedPassword);
Resource resource = Mockito.mock(Resource.class); Resource resource = Mockito.mock(Resource.class);
char[] password = ConsolePasswordFinder.builder() char[] password = new ConsolePasswordFinder(console).reqPassword(resource);
.setConsole(console)
.build()
.reqPassword(resource);
Assert.assertArrayEquals("Password should match mocked return value", Assert.assertArrayEquals("Password should match mocked return value",
expectedPassword, password); expectedPassword, password);
@@ -45,10 +44,7 @@ public class TestConsolePasswordFinder {
@Test @Test
public void testReqPasswordNullConsole() { public void testReqPasswordNullConsole() {
Resource<?> resource = Mockito.mock(Resource.class); Resource<?> resource = Mockito.mock(Resource.class);
char[] password = ConsolePasswordFinder.builder() char[] password = new ConsolePasswordFinder(null, FORMAT, 1).reqPassword(resource);
.setConsole(null)
.build()
.reqPassword(resource);
Assert.assertNull("Password should be null with null console", password); Assert.assertNull("Password should be null with null console", password);
Mockito.verifyNoMoreInteractions(resource); Mockito.verifyNoMoreInteractions(resource);
@@ -57,10 +53,7 @@ public class TestConsolePasswordFinder {
@Test @Test
public void testShouldRetry() { public void testShouldRetry() {
Resource<String> resource = new PrivateKeyStringResource(""); Resource<String> resource = new PrivateKeyStringResource("");
ConsolePasswordFinder finder = ConsolePasswordFinder.builder() ConsolePasswordFinder finder = new ConsolePasswordFinder(null, FORMAT, 1);
.setConsole(null)
.setMaxTries(1)
.build();
Assert.assertTrue("Should allow a retry at first", finder.shouldRetry(resource)); Assert.assertTrue("Should allow a retry at first", finder.shouldRetry(resource));
finder.reqPassword(resource); finder.reqPassword(resource);
@@ -71,20 +64,20 @@ public class TestConsolePasswordFinder {
public void testPromptFormat() { public void testPromptFormat() {
Assert.assertNotNull( Assert.assertNotNull(
"Empty format should create valid ConsolePasswordFinder", "Empty format should create valid ConsolePasswordFinder",
ConsolePasswordFinder.builder().setPromptFormat("").build()); new ConsolePasswordFinder(null, "", 1));
Assert.assertNotNull( Assert.assertNotNull(
"Single-string format should create valid ConsolePasswordFinder", "Single-string format should create valid ConsolePasswordFinder",
ConsolePasswordFinder.builder().setPromptFormat("%s").build()); new ConsolePasswordFinder(null, FORMAT, 1));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testPromptFormatTooManyMarkers() { public void testPromptFormatTooManyMarkers() {
ConsolePasswordFinder.builder().setPromptFormat("%s%s"); new ConsolePasswordFinder(null, "%s%s", 1);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testPromptFormatWrongMarkerType() { public void testPromptFormatWrongMarkerType() {
ConsolePasswordFinder.builder().setPromptFormat("%d"); new ConsolePasswordFinder(null, "%d", 1);
} }
} }