6666import com .oracle .truffle .api .nodes .Node ;
6767
6868public class JavaInteropTest extends PythonTests {
69- private ByteArrayOutputStream os ;
69+ private ByteArrayOutputStream out ;
7070 private Context context ;
7171 private ByteArrayOutputStream err ;
7272
7373 @ Before
7474 public void setUpTest () {
75- os = new ByteArrayOutputStream ();
75+ out = new ByteArrayOutputStream ();
7676 err = new ByteArrayOutputStream ();
7777 Builder builder = Context .newBuilder ();
7878 builder .allowAllAccess (true );
79- builder .out (os );
79+ builder .out (out );
8080 builder .err (err );
8181 context = builder .build ();
8282 }
@@ -128,7 +128,7 @@ public void testPassingFloats() throws Exception {
128128 context .eval (script );
129129 Value main = context .getPolyglotBindings ().getMember ("foo" );
130130 main .execute ((float ) 1.0 , (float ) 2.0 );
131- assertEquals ("2.0\n " , os .toString ("UTF-8" ));
131+ assertEquals ("2.0\n " , out .toString ("UTF-8" ));
132132 }
133133
134134 @ Test
@@ -141,7 +141,7 @@ public void testAsFunction() throws Exception {
141141 context .eval (script );
142142 Value main = context .getPolyglotBindings ().getMember ("foo" );
143143 main .execute ();
144- assertEquals ("Called\n " , os .toString ("UTF-8" ));
144+ assertEquals ("Called\n " , out .toString ("UTF-8" ));
145145 }
146146
147147 @ Test
@@ -154,7 +154,7 @@ public void testAsFunctionVarArgs() throws Exception {
154154 context .eval (script );
155155 Value main = context .getPolyglotBindings ().getMember ("foo" );
156156 main .execute ("Hello" , "World" );
157- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
157+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
158158 }
159159
160160 @ Test
@@ -165,7 +165,7 @@ public void mainFunctionsAreImplicitlyImporteable() throws Exception {
165165 context .eval (script );
166166 Value main = context .getBindings ("python" ).getMember ("foo" );
167167 main .execute ("Hello" , "World" );
168- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
168+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
169169 }
170170
171171 @ Test
@@ -175,7 +175,7 @@ public void builtinFunctionsAreImporteable() throws Exception {
175175 context .eval (script );
176176 Value main = context .getBindings ("python" ).getMember ("__builtins__" ).getMember ("print" );
177177 main .execute ("Hello" , "World" );
178- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
178+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
179179 }
180180
181181 @ Test
@@ -186,15 +186,15 @@ public void testMultipleInvocationsAreInSameScope() throws Exception {
186186 Source script = Source .create ("python" , source );
187187 Value foo = context .eval (script );
188188 foo .execute ("Hello" , "World" );
189- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
189+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
190190
191191 source = "def bar(a, b):\n " +
192192 " foo(a, b)\n " +
193193 "bar" ;
194194 script = Source .create ("python" , source );
195195 Value bar = context .eval (script );
196196 bar .execute ("Hello" , "World" );
197- assertEquals ("Hello World\n Hello World\n " , os .toString ("UTF-8" ));
197+ assertEquals ("Hello World\n Hello World\n " , out .toString ("UTF-8" ));
198198
199199 source = "invalid syntax" ;
200200 script = Source .create ("python" , source );
@@ -203,9 +203,9 @@ public void testMultipleInvocationsAreInSameScope() throws Exception {
203203 } catch (Throwable t ) {
204204 }
205205 bar .execute ("Hello" , "World" );
206- assertEquals ("Hello World\n Hello World\n Hello World\n " , os .toString ("UTF-8" ));
206+ assertEquals ("Hello World\n Hello World\n Hello World\n " , out .toString ("UTF-8" ));
207207 foo .execute ("Hello" , "World" );
208- assertEquals ("Hello World\n Hello World\n Hello World\n Hello World\n " , os .toString ("UTF-8" ));
208+ assertEquals ("Hello World\n Hello World\n Hello World\n Hello World\n " , out .toString ("UTF-8" ));
209209 }
210210
211211 @ Test
@@ -335,4 +335,101 @@ public void tryInvokeThenReadExecute() {
335335 assert result [0 ].equals (ForeignObjectWithOOInvoke .class .getName ());
336336 assert result [1 ].equals (ForeignObjectWithoutOOInvoke .class .getName ());
337337 }
338+
339+ public class JavaObject {
340+ public byte byteValue = 1 ;
341+ public short shortValue = 2 ;
342+ public int intValue = 3 ;
343+ public long longValue = 4 ;
344+ public float floatValue = 5 ;
345+ public double doubleValue = 6 ;
346+ public boolean booleanValue = true ;
347+ public char charValue = 'c' ;
348+
349+ public byte getByteValue () {
350+ return byteValue ;
351+ }
352+
353+ public short getShortValue () {
354+ return shortValue ;
355+ }
356+
357+ public int getIntValue () {
358+ return intValue ;
359+ }
360+
361+ public long getLongValue () {
362+ return longValue ;
363+ }
364+
365+ public float getFloatValue () {
366+ return floatValue ;
367+ }
368+
369+ public double getDoubleValue () {
370+ return doubleValue ;
371+ }
372+
373+ public boolean getBooleanValue () {
374+ return booleanValue ;
375+ }
376+
377+ public char getCharValue () {
378+ return charValue ;
379+ }
380+ }
381+
382+ @ Test
383+ public void accessJavaObjectFields () throws IOException {
384+ Source suitePy = Source .newBuilder ("python" , "" +
385+ "def foo(obj):\n " +
386+ " print(obj.byteValue, type(obj.byteValue))\n " +
387+ " print(obj.shortValue, type(obj.shortValue))\n " +
388+ " print(obj.intValue, type(obj.intValue))\n " +
389+ " print(obj.longValue, type(obj.longValue))\n " +
390+ " print(obj.floatValue, type(obj.floatValue))\n " +
391+ " print(obj.doubleValue, type(obj.doubleValue))\n " +
392+ " print(obj.booleanValue, type(obj.booleanValue))\n " +
393+ " print(obj.charValue, type(obj.charValue))\n " +
394+ "foo" ,
395+ "suite.py" ).build ();
396+ Value foo = context .eval (suitePy );
397+ foo .execute (new JavaObject ());
398+ assertEquals ("" +
399+ "1 <class 'int'>\n " +
400+ "2 <class 'int'>\n " +
401+ "3 <class 'int'>\n " +
402+ "4 <class 'int'>\n " +
403+ "5.0 <class 'float'>\n " +
404+ "6.0 <class 'float'>\n " +
405+ "True <class 'bool'>\n " +
406+ "c <class 'str'>\n " , out .toString ("UTF-8" ));
407+ }
408+
409+ @ Test
410+ public void accessJavaObjectGetters () throws IOException {
411+ Source suitePy = Source .newBuilder ("python" , "" +
412+ "def foo(obj):\n " +
413+ " print(obj.getByteValue(), type(obj.getByteValue()))\n " +
414+ " print(obj.getShortValue(), type(obj.getShortValue()))\n " +
415+ " print(obj.getIntValue(), type(obj.getIntValue()))\n " +
416+ " print(obj.getLongValue(), type(obj.getLongValue()))\n " +
417+ " print(obj.getFloatValue(), type(obj.getFloatValue()))\n " +
418+ " print(obj.getDoubleValue(), type(obj.getDoubleValue()))\n " +
419+ " print(obj.getBooleanValue(), type(obj.getBooleanValue()))\n " +
420+ " print(obj.getCharValue(), type(obj.getCharValue()))\n " +
421+ "foo" ,
422+ "suite.py" ).build ();
423+ Value foo = context .eval (suitePy );
424+ foo .execute (new JavaObject ());
425+ assertEquals ("" +
426+ "1 <class 'int'>\n " +
427+ "2 <class 'int'>\n " +
428+ "3 <class 'int'>\n " +
429+ "4 <class 'int'>\n " +
430+ "5.0 <class 'float'>\n " +
431+ "6.0 <class 'float'>\n " +
432+ "True <class 'bool'>\n " +
433+ "c <class 'str'>\n " , out .toString ("UTF-8" ));
434+ }
338435}
0 commit comments