CallCommand extends AbstractCommand { @Override public void execute(String commandName, BufferedReader reader, BufferedWriter writer) throws Py4JException, IOException { String targetObjectId = reader.readLine(); String methodName = reader.readLine(); List<Object> arguments = getArguments(reader); ReturnObject returnObject = invokeMethod(methodName, targetObjectId, arguments); String returnCommand = Protocol.getOutputCommand(returnObject); logger.finest("Returning command: " + returnCommand); writer.write(returnCommand); writer.flush(); } CallCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); final static String CALL_COMMAND_NAME; }
@Test public void testStatic() { String inputCommand = "z:java.lang.String\nvalueOf\ni123\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ys123\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testReflectionException() { String inputCommand = "z:java.lang.String\nvalueOf2\ni123\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertTrue(sWriter.toString().startsWith("!xspy4j.Py4JException: ")); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testReflectionException2() { String inputCommand = target + "\nmethod1aa\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertTrue(sWriter.toString().startsWith("!xspy4j.Py4JException: ")); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testClientCodeException() { String inputCommand = "z:java.lang.Integer\nvalueOf\nsallo\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!xro1\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testNoParam() { String inputCommand = target + "\nmethod1\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yi1\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testVoidMethod() { String inputCommand = target + "\nmethod2\nsThis is a\tString\\n\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMethodWithNull() { String inputCommand = target + "\nmethod2\nn\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); inputCommand = target + "\nmethod4\nn\ne\n"; command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n!yro1\n", sWriter.toString()); assertEquals(((ExampleClass) gateway.getObject("o1")).getField1(), 3); inputCommand = target + "\nmethod7\nn\ne\n"; command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n!yro1\n!yi2\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMethodWithParams() { String inputCommand = target + "\nmethod3\ni1\nbtrue\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!ysHello World\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testCharMethod() { String inputCommand = target + "\nmethod4\nsc\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yro1\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testCharMethod2() { String inputCommand = target + "\nmethod6\nsc\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yro1\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testStringMethod() { String inputCommand = target + "\nmethod4\nsc\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yro1\n", sWriter.toString()); assertEquals(3, ((ExampleClass) gateway.getObject("o1")).getField1()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testStringMethodWithNull() { String inputCommand = target + "\nmethod4\nn\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yro1\n", sWriter.toString()); assertEquals(3, ((ExampleClass) gateway.getObject("o1")).getField1()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testUsingMethodReturn() { String inputCommand = target + "\nmethod4\nsc\ne\n"; String inputCommand2 = target + "\nmethod5\nro1\ne\n"; try { command.execute("c", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yro1\n", sWriter.toString()); command.execute("c", new BufferedReader(new StringReader(inputCommand2)), writer); assertEquals("!yro1\n!yi2\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }