CredentialUtil extends Configured implements Tool { @Override public int run(String[] args) throws Exception { int exitCode = 1; for (int i = 0; i < args.length; ++i) { if (args[i].equals("create")) { if (i == args.length - 1) { return 1; } command = new CreateCommand(); alias = args[++i]; if (alias.equals("-h") || alias.equals("-help")) { printUsage(); return 0; } } else if (args[i].equals("get")) { if (i == args.length - 1) { return 1; } command = new GetCommand(); alias = args[++i]; if (alias.equals("-h") || alias.equals("-help")) { printUsage(); return 0; } } else if (args[i].equals("delete")) { if (i == args.length - 1) { printUsage(); return 1; } command = new DeleteCommand(); alias = args[++i]; if (alias.equals("-help")) { printUsage(); return 0; } } else if (args[i].equals("list")) { if (i < args.length - 1) { alias = args[i + 1]; } command = new ListCommand(); if (alias.equals("-h") || alias.equals("-help")) { printUsage(); return 0; } alias = "not required"; } else if (args[i].equals("-provider")) { if (i == args.length - 1) { return 1; } String providerPath = getNormalizedPath(args[++i]); getConf().set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, providerPath); provider = getCredentialProvider(); } else if (args[i].equals("-f") || args[i].equals("-force")) { interactive = false; overwrite = true; } else if (args[i].equals("-n")) { interactive = false; overwrite = false; } else if (args[i].equals("-v") || args[i].equals("-value")) { value = args[++i]; } else if (args[i].equals("-h") || args[i].equals("-help")) { printUsage(); return 0; } else { printUsage(); ToolRunner.printGenericCommandUsage(System.err); return 1; } } if (command == null) { printUsage(); } else if (command.validate()) { exitCode = command.execute(); } return exitCode; }  static void main(String[] args); @Override int run(String[] args); static final String jceksPrefix; static final String localJceksPrefix; }
@Test public void testCreateCommandOverwriteExisting() throws Exception { String alias = "javax.jdo.option.ConnectionPassword"; String credential = "MyTopSecretPassword"; String providerPath = getProviderPath("CreateCommandTest.jceks"); String[] args; int exitCode; args = getCreateArgs(alias, credential, providerPath); exitCode = ToolRunner.run(new Configuration(), new CredentialUtil(), args); Assert.assertEquals(exitCode, 0); credential = "MyUpdatedTopSecretPassword"; args = getUpdateArgs(alias, credential, providerPath); exitCode = executeCommand(args); Assert.assertEquals(exitCode, 0); }
@Test public void testSafeCreateCommandExisting() throws Exception { String alias = "javax.jdo.option.ConnectionPassword"; String credential = "MyTopSecretPassword"; String providerPath = getProviderPath("CreateCommandTest.jceks"); String[] args; int exitCode; args = getCreateArgs(alias, credential, providerPath); exitCode = ToolRunner.run(new Configuration(), new CredentialUtil(), args); Assert.assertEquals(exitCode, 0); credential = "AnotherTopSecretPassword"; args = getSafeCreateArgs(alias, credential, providerPath); exitCode = executeCommand(args); Assert.assertEquals(exitCode, 0); }