FieldCommand extends AbstractCommand { @Override public void execute(String commandName, BufferedReader reader, BufferedWriter writer) throws Py4JException, IOException { String returnCommand = null; String subCommand = safeReadLine(reader, false); if (subCommand.equals(FIELD_GET_SUB_COMMAND_NAME)) { returnCommand = getField(reader); } else if (subCommand.equals(FIELD_SET_SUB_COMMAND_NAME)) { returnCommand = setField(reader); } else { returnCommand = Protocol.getOutputErrorCommand("Unknown Field SubCommand Name: " + subCommand); } logger.finest("Returning command: " + returnCommand); writer.write(returnCommand); writer.flush(); } FieldCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); @Override void init(Gateway gateway, Py4JServerConnection connection); final static String FIELD_COMMAND_NAME; final static String FIELD_GET_SUB_COMMAND_NAME; final static String FIELD_SET_SUB_COMMAND_NAME; }
@Test public void testPrivateMember() { String inputCommand = "g\n" + target + "\nfield1\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yo\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testNoMember() { String inputCommand = "g\n" + target + "\nfield2\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yo\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testPrimitive() { String inputCommand = "g\n" + target + "\nfield10\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yi10\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testObject() { String inputCommand = "g\n" + target + "\nfield20\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yro1\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testNull() { String inputCommand = "g\n" + target + "\nfield21\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yn\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSetNoField() { String inputCommand = "s\n" + target + "\nfield1\ni123\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yo\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testSetFieldObject() { String objectId = gateway.putNewObject(new StringBuffer("Hello")); String inputCommand = "s\n" + target + "\nfield20\nr" + objectId + "\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertEquals(((ExampleClass) gateway.getObject(target)).field20, gateway.getObject(objectId)); } catch (Exception e) { e.printStackTrace(); fail(); } }