ArrayCommand 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 == ARRAY_GET_SUB_COMMAND_NAME) { returnCommand = getArray(reader); } else if (subCommand == ARRAY_SET_SUB_COMMAND_NAME) { returnCommand = setArray(reader); } else if (subCommand == ARRAY_SLICE_SUB_COMMAND_NAME) { returnCommand = sliceArray(reader); } else if (subCommand == ARRAY_LEN_SUB_COMMAND_NAME) { returnCommand = lenArray(reader); } else if (subCommand == ARRAY_CREATE_SUB_COMMAND_NAME) { returnCommand = createArray(reader); } else { returnCommand = Protocol.getOutputErrorCommand("Unknown Array SubCommand Name: " + subCommand); } logger.finest("Returning command: " + returnCommand); writer.write(returnCommand); writer.flush(); } ArrayCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); static final String ARRAY_COMMAND_NAME; static final char ARRAY_GET_SUB_COMMAND_NAME; static final char ARRAY_SET_SUB_COMMAND_NAME; static final char ARRAY_SLICE_SUB_COMMAND_NAME; static final char ARRAY_LEN_SUB_COMMAND_NAME; static final char ARRAY_CREATE_SUB_COMMAND_NAME; static final String RETURN_VOID; }
@Test public void testGet() { String inputCommand = ArrayCommand.ARRAY_GET_SUB_COMMAND_NAME + "\n" + target + "\ni1\ne\n"; try { command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ys111\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSet() { String inputCommand = ArrayCommand.ARRAY_SET_SUB_COMMAND_NAME + "\n" + target2 + "\ni1\ni555\ne\n"; try { command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertEquals(Array.getInt(array2, 1), 555); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSetNoneInPrimitiveArray() { String inputCommand = ArrayCommand.ARRAY_SET_SUB_COMMAND_NAME + "\n" + target2 + "\ni1\nn\ne\n"; try { command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertNull(Array.get(array2, 1)); fail(); } catch (Py4JException pe) { assertTrue(true); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSetNoneInArray() { String inputCommand = ArrayCommand.ARRAY_SET_SUB_COMMAND_NAME + "\n" + target + "\ni1\nn\ne\n"; try { command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertNull(Array.get(array1, 1)); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testLength() { String inputCommand = ArrayCommand.ARRAY_LEN_SUB_COMMAND_NAME + "\n" + target + "\ne\n"; try { command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yi2\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSlice() { int[] array3 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; String[][] array4 = new String[][] { { "111", "222" }, { "aaa", "bbb" }, { "88", "99" } }; gateway.putNewObject(array3); gateway.putNewObject(array4); String inputCommand = ArrayCommand.ARRAY_SLICE_SUB_COMMAND_NAME + "\n" + "o2" + "\ni1\ni5\ne\n"; try { command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yto4\n", sWriter.toString()); int[] intarray = (int[]) gateway.getObject("o4"); assertEquals(2, intarray.length); assertEquals(6, intarray[1]); inputCommand = ArrayCommand.ARRAY_SLICE_SUB_COMMAND_NAME + "\n" + "o3" + "\ni2\ne\n"; command.execute("a", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yto4\n!yto5\n", sWriter.toString()); String[][] stringarray = (String[][]) gateway.getObject("o5"); assertEquals(1, stringarray.length); assertEquals("99", stringarray[0][1]); } catch (Exception e) { e.printStackTrace(); fail(); } }