TopInstanceCommand extends AbstractCommand { public static TopInstanceCommand parse(String commandString, ExecutionContext sessionContext) throws ODPSConsoleException { Matcher matcher = PATTERN.matcher(commandString); String projectName = null; Integer delay = null; Boolean onlyOwner = null; Integer number = null; Instance.Status status = null; if (matcher.matches()) { if (!StringUtils.isNullOrEmpty(matcher.group(2))) { CommandLine commandLine = getCommandLine(matcher.group(2)); if (!commandLine.getArgList().isEmpty()) { throw new ODPSConsoleException( ODPSConsoleConstants.BAD_COMMAND + "[too much parameters.]"); } if (commandLine.hasOption(PROJECT_TAG)) { projectName = commandLine.getOptionValue(PROJECT_TAG); } if (commandLine.hasOption(STATUS_TAG)) { try { status = Instance.Status.valueOf(commandLine.getOptionValue(STATUS_TAG).toUpperCase()); } catch (IllegalArgumentException e) { throw new ODPSConsoleException( ODPSConsoleConstants.BAD_COMMAND + "[bad status value.]"); } } if (commandLine.hasOption(LIMIT_TAG)) { String val = commandLine.getOptionValue(LIMIT_TAG); if (org.apache.commons.lang.StringUtils.isNumeric(val)) { number = Integer.parseInt(val); if (number <= 0) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "[bad limit value, limit number should bigger than zero.]"); } } else { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "[bad limit value.]"); } } if (commandLine.hasOption(DELAY_TAG)) { String val = commandLine.getOptionValue(DELAY_TAG); if (StringUtils.isNullOrEmpty(val)) { delay = DEFAULT_DELAY; } else if (org.apache.commons.lang.StringUtils.isNumeric(val) && Integer.parseInt(val) > 0) { delay = Integer.parseInt(val); } else { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "[bad delay value.]"); } } if (commandLine.hasOption(ALL_TAG)) { onlyOwner = false; } } TopInstanceCommand command = new TopInstanceCommand(projectName, commandString, sessionContext); command.setOnlyOwner(onlyOwner); command.setStatus(status); command.setDelay(delay); command.setNubmer(number); return command; } return null; } TopInstanceCommand(String projectName, String commandText, ExecutionContext context); static void printUsage(PrintStream stream); void setNubmer(Integer nubmer); void setDelay(Integer delay); void setOnlyOwner(Boolean onlyOwner); void setStatus(Instance.Status status); void run(); static TopInstanceCommand parse(String commandString, ExecutionContext sessionContext); static final String[] HELP_TAGS; }
@Test public void testSyncPositive() throws ODPSConsoleException, OdpsException { ExecutionContext ctx = ExecutionContext.init(); for (String str : positive) { TopInstanceCommand test = TopInstanceCommand.parse(str, ctx); assertNotNull(test); } }
@Test public void testSyncNegative() throws ODPSConsoleException, OdpsException { ExecutionContext ctx = ExecutionContext.init(); for (String str : negative) { TopInstanceCommand test = TopInstanceCommand.parse(str, ctx); assertNull(test); } }
@Test public void testError() throws OdpsException, ODPSConsoleException { ExecutionContext ctx = ExecutionContext.init(); int count = 0; for (String str : error) { try { TopInstanceCommand test = TopInstanceCommand.parse(str, ctx); } catch (ODPSConsoleException e) { count++; } } assertEquals(error.length, count); }