ListCommand extends AbstractCommand { @Override public void execute(String commandName, BufferedReader reader, BufferedWriter writer) throws Py4JException, IOException { char subCommand = safeReadLine(reader).charAt(0); String returnCommand = null; if (subCommand == LIST_SLICE_SUB_COMMAND_NAME) { returnCommand = slice_list(reader); } else if (subCommand == LIST_CONCAT_SUB_COMMAND_NAME) { returnCommand = concat_list(reader); } else if (subCommand == LIST_MULT_SUB_COMMAND_NAME) { returnCommand = mult_list(reader); } else if (subCommand == LIST_IMULT_SUB_COMMAND_NAME) { returnCommand = imult_list(reader); } else if (subCommand == LIST_COUNT_SUB_COMMAND_NAME) { returnCommand = count_list(reader); } else { returnCommand = call_collections_method(reader, subCommand); } logger.finest("Returning command: " + returnCommand); writer.write(returnCommand); writer.flush(); } ListCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); static final String LIST_COMMAND_NAME; static final char LIST_SORT_SUB_COMMAND_NAME; static final char LIST_REVERSE_SUB_COMMAND_NAME; static final char LIST_MAX_SUB_COMMAND_NAME; static final char LIST_MIN_SUB_COMMAND_NAME; static final char LIST_SLICE_SUB_COMMAND_NAME; static final char LIST_CONCAT_SUB_COMMAND_NAME; static final char LIST_MULT_SUB_COMMAND_NAME; static final char LIST_IMULT_SUB_COMMAND_NAME; static final char LIST_COUNT_SUB_COMMAND_NAME; static final String RETURN_VOID; }
@Test public void testSort() { String inputCommand = ListCommand.LIST_SORT_SUB_COMMAND_NAME + "\n" + target + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertEquals(list.get(0), "1"); assertEquals(list.get(3), "9"); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSortException() { String inputCommand = ListCommand.LIST_SORT_SUB_COMMAND_NAME + "\n" + target2 + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!x\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testReverse() { String inputCommand = ListCommand.LIST_REVERSE_SUB_COMMAND_NAME + "\n" + target + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertEquals(list.get(0), "2"); assertEquals(list.get(1), "3"); assertEquals(list.get(2), "9"); assertEquals(list.get(3), "1"); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testReverseException() { String inputCommand = ListCommand.LIST_REVERSE_SUB_COMMAND_NAME + "\n" + target2 + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMin() { String inputCommand = ListCommand.LIST_MIN_SUB_COMMAND_NAME + "\n" + target + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ys1\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMinException() { String inputCommand = ListCommand.LIST_MIN_SUB_COMMAND_NAME + "\n" + target2 + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!x\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMax() { String inputCommand = ListCommand.LIST_MAX_SUB_COMMAND_NAME + "\n" + target + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ys9\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMaxException() { String inputCommand = ListCommand.LIST_MAX_SUB_COMMAND_NAME + "\n" + target2 + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!x\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testCount() { String inputCommand = ListCommand.LIST_COUNT_SUB_COMMAND_NAME + "\n" + target + "\ns1\ne\n"; list.add("1"); try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yi2\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@SuppressWarnings("rawtypes") @Test public void testConcat() { String inputCommand = ListCommand.LIST_CONCAT_SUB_COMMAND_NAME + "\n" + target + "\n" + target2 + "\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ylo2\n", sWriter.toString()); List newList = (List) gateway.getObject("o2"); assertEquals(7, newList.size()); assertEquals(4, list.size()); assertEquals(3, list2.size()); assertEquals(newList.get(4), list2.get(0)); inputCommand = ListCommand.LIST_CONCAT_SUB_COMMAND_NAME + "\n" + target + "\n" + target + "\ne\n"; command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ylo2\n!ylo3\n", sWriter.toString()); newList = (List) gateway.getObject("o3"); assertEquals(8, newList.size()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@SuppressWarnings("rawtypes") @Test public void testMult() { String inputCommand = ListCommand.LIST_MULT_SUB_COMMAND_NAME + "\n" + target + "\ni3\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ylo2\n", sWriter.toString()); List newList = (List) gateway.getObject("o2"); assertEquals(12, newList.size()); assertEquals(4, list.size()); inputCommand = ListCommand.LIST_MULT_SUB_COMMAND_NAME + "\n" + target + "\ni-1\ne\n"; command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ylo2\n!ylo3\n", sWriter.toString()); newList = (List) gateway.getObject("o3"); assertEquals(0, newList.size()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testiMult() { String inputCommand = ListCommand.LIST_IMULT_SUB_COMMAND_NAME + "\n" + target + "\ni3\ne\n"; try { assertEquals(4, list.size()); command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertEquals(12, list.size()); inputCommand = ListCommand.LIST_IMULT_SUB_COMMAND_NAME + "\n" + target + "\ni-1\ne\n"; command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n!yv\n", sWriter.toString()); assertEquals(0, list.size()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@SuppressWarnings("rawtypes") @Test public void testSlice() { String inputCommand = ListCommand.LIST_SLICE_SUB_COMMAND_NAME + "\n" + target + "\ni1\ni2\ne\n"; try { command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ylo2\n", sWriter.toString()); List newList = (List) gateway.getObject("o2"); assertEquals(2, newList.size()); assertEquals("9", newList.get(0)); assertEquals("3", newList.get(1)); assertEquals(4, list.size()); inputCommand = ListCommand.LIST_SLICE_SUB_COMMAND_NAME + "\n" + target + "\ne\n"; command.execute("l", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ylo2\n!ylo3\n", sWriter.toString()); newList = (List) gateway.getObject("o3"); assertEquals(0, newList.size()); } catch (Exception e) { e.printStackTrace(); fail(); } }