JavaObject { public String produceRCode(boolean useEquals) throws IllegalAccessException { return (JavaObject.ConvertToRCode(name, object, true, useEquals)); } JavaObject(String name, Object o); Object getObject(); void setObject(Object object); String getName(); void setName(String name); static String ConvertToRCode(String name, Object javaobject, boolean useList, boolean useEquals); String produceRCode(boolean useEquals);  }
@Test public void simpleJavaObjectTest() throws IllegalAccessException { TestClass tc = new TestClass(); JavaObject t = new JavaObject("myObj", tc); assertEquals("myObj <- list(i=9, f=10.0, d=3.14, b=TRUE, l=567, s=\"test\")\n", t.produceRCode(false)); }
@Test public void passJavaObjectToR() throws IllegalAccessException { TestClass tc = new TestClass(); tc.i = 999; tc.f = 2.71828f; tc.d = Math.PI; JavaObject jo = new JavaObject("myObj", tc); RCaller rcaller = RCaller.create(); RCode code = RCode.create(); code.addRCode(jo.produceRCode(false)); code.addRCode("myObj$i <- myObj$i + 1"); rcaller.setRCode(code); rcaller.runAndReturnResult("myObj"); int[] result = rcaller.getParser().getAsIntArray("i"); assertEquals(1000, result[0]); rcaller.deleteTempFiles(); }
@Test public void TestClassWithArrays() throws IllegalAccessException, IOException { TestClassWithArrays tcwa = new TestClassWithArrays(); JavaObject jo = new JavaObject("tcwa", tcwa); RCaller rcaller = RCaller.create(); RCode code = RCode.create(); code.addRCode(jo.produceRCode(false)); rcaller.setRCode(code); rcaller.runAndReturnResult("tcwa"); int[] expectedIntArray = rcaller.getParser().getAsIntArray("ia"); for (int i = 0; i < tcwa.ia.length; i++) { assertEquals(expectedIntArray[i], tcwa.ia[i]); } double[] expectedDoubleArray = rcaller.getParser().getAsDoubleArray("da"); for (int i = 0; i < tcwa.da.length; i++) { assertEquals(expectedDoubleArray[i], tcwa.da[i], delta); } String[] expectedStringArray = rcaller.getParser().getAsStringArray("sa"); for (int i = 0; i < tcwa.sa.length; i++) { assertEquals(expectedStringArray[i], tcwa.sa[i]); } rcaller.deleteTempFiles(); }
@Test public void TestClassWithArraysCalculations() throws IllegalAccessException, IOException { TestClassWithArrays tcwa = new TestClassWithArrays(); JavaObject jo = new JavaObject("tcwa", tcwa); RCaller rcaller = RCaller.create(); RCode code = RCode.create(); code.addRCode(jo.produceRCode(false)); code.addRCode("result <- quantile(tcwa$da, 0.95)"); rcaller.setRCode(code); rcaller.runAndReturnResult("result"); double mean = rcaller.getParser().getAsDoubleArray("result")[0]; assertEquals(mean, 10.05, delta); rcaller.deleteTempFiles(); }