ReadOfflineModelCommand extends AbstractCommand { public static ReadOfflineModelCommand parse(String cmd, ExecutionContext ctx) throws ODPSConsoleException { if (cmd == null || ctx == null) { return null; } Matcher m = PATTERN.matcher(cmd); if (!m.matches()) { return null; } String input = m.group(1); String[] inputs = new AntlrObject(input).getTokenStringArray(); CommandLine commandLine = getCommandLine(inputs); String projectName = null; if (commandLine.hasOption("p")) { projectName = commandLine.getOptionValue("p"); } int argSize = commandLine.getArgList().size(); if (argSize != 1 && argSize != 3) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Model name not found."); } String modelName = null; modelName = commandLine.getArgs()[0]; if (!modelName.matches("[.\\w]+")) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Invalid model name."); } if (modelName.contains(".")) { String[] result = modelName.split("\\.", 2); if (projectName != null && (!result[0].equals(projectName))) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Project name conflict."); } projectName = result[0]; modelName = result[1]; } String volumeName = null; String partitionName = null; if (commandLine.getArgList().size() == 3) { if (commandLine.getArgs()[1].toUpperCase().compareTo("AS") != 0) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Invalid command."); } volumeName = commandLine.getArgs()[2]; if (volumeName.contains(".")) { String[] result = volumeName.split("\\.", 2); volumeName = result[0]; partitionName = result[1]; } } if (projectName == null) { projectName = ctx.getProjectName(); } return new ReadOfflineModelCommand(projectName, modelName, volumeName, partitionName, cmd, ctx); } ReadOfflineModelCommand(String projectName, String modelName,
                                 String volumeName, String partitionName,
                                 String cmd, ExecutionContext ctx); static void printUsage(PrintStream stream); void SubmitWriteVolumeJob(); @Override void run(); static ReadOfflineModelCommand parse(String cmd, ExecutionContext ctx); static final String[] HELP_TAGS; }
@Test public void testReadOffLineModelMatchNone() throws ODPSConsoleException, OdpsException { ExecutionContext context = ExecutionContext.init(); String cmd = "read offlinemodel"; ReadOfflineModelCommand command = ReadOfflineModelCommand.parse(cmd, context); assertNull(command); }
@Test public void testReadOffLineModelConflitPrjName() throws ODPSConsoleException, OdpsException { ExecutionContext context = ExecutionContext.init(); String cmd = "read offlinemodel -p prj1 prj2.model"; expectEx.expect(ODPSConsoleException.class); expectEx.expectMessage(ODPSConsoleConstants.BAD_COMMAND + "Project name conflict."); ReadOfflineModelCommand.parse(cmd, context); }
@Test public void testReadOffLineModelNoModel() throws ODPSConsoleException, OdpsException { ExecutionContext context = ExecutionContext.init(); String cmd = "read offlinemodel -p prj"; expectEx.expect(ODPSConsoleException.class); expectEx.expectMessage(ODPSConsoleConstants.BAD_COMMAND + "Model name not found."); ReadOfflineModelCommand.parse(cmd, context); }
@Test public void testReadOffLineModelInvalidModelName() throws ODPSConsoleException, OdpsException { ExecutionContext context = ExecutionContext.init(); String cmd = "read offlinemodel model-error"; expectEx.expect(ODPSConsoleException.class); expectEx.expectMessage(ODPSConsoleConstants.BAD_COMMAND + "Invalid model name."); ReadOfflineModelCommand.parse(cmd, context); }
@Test public void testReadOffLineModelWithAsStatementError() throws ODPSConsoleException, OdpsException { ExecutionContext context = ExecutionContext.init(); String cmd = "read offlinemodel model as_error volume"; expectEx.expect(ODPSConsoleException.class); expectEx.expectMessage(ODPSConsoleConstants.BAD_COMMAND + "Invalid command."); ReadOfflineModelCommand.parse(cmd, context); }