RenameUser extends Tool { @Override protected ToolContext processArgs(String[] args) throws ToolArgumentException { context = new RenameUserToolContext(); context.setLogin(login); int i = 0; while (i < args.length) { if (args[i].equals("-h") || args[i].equals("-help")) { context.setHelp(true); break; } else if (args[i].equals("-user")) { i++; if (i == args.length || args[i].length() == 0) { throw new ToolArgumentException("The value <user> for the option -user must be specified"); } context.setUsername(args[i]); } else if (args[i].equals("-pwd")) { i++; if (i == args.length || args[i].length() == 0) { throw new ToolArgumentException("The value <password> for the option -pwd must be specified"); } context.setPassword(args[i]); } else if (args[i].equals("-encoding")) { i++; if (i == args.length || args[i].length() == 0) { throw new ToolArgumentException("The value <encoding> for the option -encoding must be specified"); } try { context.encoding = Charset.forName(args[i]); } catch (IllegalCharsetNameException e) { throw new ToolArgumentException("The value <encoding> is not recognised"); } catch (UnsupportedCharsetException e) { throw new ToolArgumentException("The value <encoding> is unsupported"); } } else if (args[i].equals("-quiet")) { context.setQuiet(true); } else if (args[i].equals("-verbose")) { context.setVerbose(true); } else if (args[i].equals("-f") || args[i].equals("-file")) { i++; if (i == args.length || args[i].length() == 0) { throw new ToolArgumentException("The value <filename> for the option -file must be specified"); } context.setFilename(args[i]); } else if (!args[i].startsWith("-")) { i++; if (i == args.length || args[i-1].trim().length() == 0 || args[i].trim().length() == 0) { throw new ToolArgumentException("Both <oldUsername> <newUsername> must be specified"); } if (context.userCount() > 0) { throw new ToolArgumentException("Only one <oldUsername> <newUsername> pair may be " + "specified on the command line. See the -file option"); } String oldUsername = args[i-1].trim(); String newUsername = args[i].trim(); String error = context.add(-1, null, oldUsername, newUsername); if (error != null) { throw new ToolArgumentException(error); } } else { throw new ToolArgumentException("Unknown option " + args[i]); } i++; } return context; }  void setLogin(boolean login); static void main(String[] args);  }
@Test public void testLoginUsernameAndPassword() throws Exception { args[3] = "password"; renameUser.processArgs(args); assertEquals("admin", renameUser.context.getUsername()); assertEquals("password", renameUser.context.getPassword()); }
@Test public void testCmdLineUsernames() throws Exception { renameUser.processArgs(args); assertUsers("oldUsername", "newUsername"); }