-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from triceo/one-nine-five
Never prompt for password if it was passed as an argument (fixes #195)
- Loading branch information
Showing
3 changed files
with
226 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.beust.jcommander; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class PasswordTest { | ||
|
||
@DataProvider(name = "args") | ||
public Object[][] createArgs() { | ||
return new Object[][] { | ||
{ new PasswordTestingArgs() }, | ||
{ new OptionalPasswordTestingArgs() }, | ||
}; | ||
} | ||
|
||
public interface Args { | ||
|
||
String getPassword(); | ||
|
||
int getPort(); | ||
|
||
} | ||
|
||
public static class PasswordTestingArgs implements PasswordTest.Args { | ||
@Parameter(names = {"--password", "-p"}, description = "Private key password", | ||
password = true, required = true) | ||
public String password; | ||
|
||
@Parameter(names = {"--port", "-o"}, description = "Port to bind server to", | ||
required = true) | ||
public int port; | ||
|
||
@Override | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public int getPort() { | ||
return port; | ||
} | ||
} | ||
|
||
@Test(dataProvider = "args") | ||
public void passwordNotAsked(Args a) { | ||
String expectedPassword = "somepassword"; | ||
int expectedPort = 7; | ||
new JCommander(a, "--password", expectedPassword, "--port", String.valueOf(7)); | ||
Assert.assertEquals(a.getPort(), expectedPort); | ||
Assert.assertEquals(a.getPassword(), expectedPassword); | ||
} | ||
|
||
@Test(dataProvider = "args", expectedExceptions = ParameterException.class) | ||
public void passwordWithExcessiveArity(Args a) { | ||
new JCommander(a, "--password", "somepassword", "someotherarg", "--port", String.valueOf(7)); | ||
} | ||
|
||
@Test(dataProvider = "args") | ||
public void passwordAsked(Args a) { | ||
InputStream stdin = System.in; | ||
String password = "password"; | ||
int port = 7; | ||
try { | ||
System.setIn(new ByteArrayInputStream(password.getBytes())); | ||
new JCommander(a, "--port", String.valueOf(port), "--password"); | ||
Assert.assertEquals(a.getPort(), port); | ||
Assert.assertEquals(a.getPassword(), password); | ||
} finally { | ||
System.setIn(stdin); | ||
} | ||
} | ||
|
||
public static class OptionalPasswordTestingArgs implements PasswordTest.Args { | ||
@Parameter(names = {"--password", "-p"}, description = "Private key password", | ||
password = true) | ||
public String password; | ||
|
||
@Parameter(names = {"--port", "-o"}, description = "Port to bind server to", | ||
required = true) | ||
public int port; | ||
|
||
@Override | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public int getPort() { | ||
return port; | ||
} | ||
} | ||
|
||
@Test | ||
public void passwordOptionalNotProvided() { | ||
Args a = new OptionalPasswordTestingArgs(); | ||
new JCommander(a, "--port", "7"); | ||
Assert.assertEquals(a.getPort(), 7); | ||
Assert.assertEquals(a.getPassword(), null); | ||
} | ||
|
||
@Test(expectedExceptions = ParameterException.class) | ||
public void passwordRequredNotProvided() { | ||
Args a = new PasswordTestingArgs(); | ||
new JCommander(a, "--port", "7"); | ||
} | ||
|
||
} |