CreateResourceCommand extends AbstractCommand { public static AddResourceCommand parse(String commandString, ExecutionContext sessionContext) throws ODPSConsoleException { String[] tokens = new AntlrObject(commandString).getTokenStringArray(); if (tokens != null && tokens.length >= 2 && tokens[0].toUpperCase().equals("CREATE") && tokens[1].toUpperCase().equals("RESOURCE")) { GnuParser parser = new GnuParser(); Options options = new Options(); options.addOption("p", "project", true, null); options.addOption("c", "comment", true, null); options.addOption("f", "force", false, null); try { CommandLine cl = parser.parse(options, tokens); String refName = null; String alias = ""; String comment = null; String type = null; String partitionSpec = ""; boolean isUpdate = false; List<String> argList = cl.getArgList(); int size = argList.size(); if (size < 4) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Missing parameters"); } ListIterator<String> iter = argList.listIterator(); iter.next(); iter.next(); type = iter.next(); refName = iter.next(); if (iter.hasNext()) { String item = iter.next(); if (item.equals("(")) { boolean isParenPaired = false; while (iter.hasNext()) { String s = iter.next(); if (s.equals(")")) { isParenPaired = true; break; } partitionSpec += s; } if (!isParenPaired) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Unpaired parenthesis"); } if (!iter.hasNext()) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Missing parameter: alias"); } item = iter.next(); } alias = item; } if (iter.hasNext()) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Illegal parameter: " + iter.next()); } String projectName = null; Option[] opts = cl.getOptions(); for (Option opt : opts) { if ("f".equals(opt.getOpt())) { isUpdate = true; } else if ("c".equals(opt.getOpt())) { comment = opt.getValue(); } else if ("p".equals(opt.getOpt())) { projectName = opt.getValue(); } else { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Illegal option: " + opt.getOpt()); } } return new AddResourceCommand(commandString, sessionContext, refName, alias, comment, type, partitionSpec, isUpdate, projectName); } catch (ParseException e) { throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "Invalid parameters"); } } else { return null; } } CreateResourceCommand(String commandText, ExecutionContext context); static void printUsage(PrintStream stream); @Override void run(); static AddResourceCommand parse(String commandString, ExecutionContext sessionContext); static final String[] HELP_TAGS; }
@Test public void testCreateResourceCommandParser() throws OdpsException, ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "create resource -f jar my_jar.jar"; command = CreateResourceCommand.parse(commandText, context); assertEquals(command.getRefName(), "my_jar.jar"); assertEquals(command.getAlias(), ""); assertEquals(command.getComment(), null); assertEquals(command.getType(), "jar"); assertEquals(command.getPartitionSpec(), ""); assertEquals(command.isUpdate, true); commandText = "create resource table my_project2.my_table_2"; command = CreateResourceCommand.parse(commandText, context); assertEquals(command.getRefName(), "my_project2.my_table_2"); assertEquals(command.getAlias(), ""); assertEquals(command.getComment(), null); assertEquals(command.getType(), "table"); assertEquals(command.getPartitionSpec(), ""); assertEquals(command.isUpdate, false); commandText = "create resource table my_part_table(pt='1') my_part_res"; command = CreateResourceCommand.parse(commandText, context); assertEquals(command.getRefName(), "my_part_table"); assertEquals(command.getAlias(), "my_part_res"); assertEquals(command.getComment(), null); assertEquals(command.getType(), "table"); assertEquals(command.getPartitionSpec(), "pt='1'"); assertEquals(command.isUpdate, false); commandText = "create resource file -p my_project my_file.txt my_file_name"; command = CreateResourceCommand.parse(commandText, context); assertEquals(command.getRefName(), "my_file.txt"); assertEquals(command.getAlias(), "my_file_name"); assertEquals(command.getComment(), null); assertEquals(command.getType(), "file"); assertEquals(command.getPartitionSpec(), ""); assertEquals(command.isUpdate, false); commandText = "create resource table my_part_table() my_part_res"; command = CreateResourceCommand.parse(commandText, context); assertEquals(command.getRefName(), "my_part_table"); assertEquals(command.getAlias(), "my_part_res"); assertEquals(command.getComment(), null); assertEquals(command.getType(), "table"); assertEquals(command.getPartitionSpec(), ""); assertEquals(command.isUpdate, false); }
@Test(expected = ODPSConsoleException.class) public void testCreateResourceCommandParserWithException() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "create resource file -wrong"; command = CreateResourceCommand.parse(commandText, context); }
@Test(expected = ODPSConsoleException.class) public void testCreateResourceCommandParserWithException2() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "create resource file aaa bbb ccc ddd eee"; command = CreateResourceCommand.parse(commandText, context); }
@Test(expected = ODPSConsoleException.class) public void testCreateResourceCommandParserWithParenUnmatched() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "create resource table my_part_table(pt='1' my_part_res"; command = CreateResourceCommand.parse(commandText, context); }
@Test(expected = ODPSConsoleException.class) public void testCreateResourceCommandParserWithParenWithoutAlias() throws ODPSConsoleException { ExecutionContext context = ExecutionContext.init(); String commandText = null; AddResourceCommand command; commandText = "create resource table my_part_table(pt='1')"; command = CreateResourceCommand.parse(commandText, context); }