66
66
import com .oracle .truffle .api .nodes .Node ;
67
67
68
68
public class JavaInteropTest extends PythonTests {
69
- private ByteArrayOutputStream os ;
69
+ private ByteArrayOutputStream out ;
70
70
private Context context ;
71
71
private ByteArrayOutputStream err ;
72
72
73
73
@ Before
74
74
public void setUpTest () {
75
- os = new ByteArrayOutputStream ();
75
+ out = new ByteArrayOutputStream ();
76
76
err = new ByteArrayOutputStream ();
77
77
Builder builder = Context .newBuilder ();
78
78
builder .allowAllAccess (true );
79
- builder .out (os );
79
+ builder .out (out );
80
80
builder .err (err );
81
81
context = builder .build ();
82
82
}
@@ -128,7 +128,7 @@ public void testPassingFloats() throws Exception {
128
128
context .eval (script );
129
129
Value main = context .getPolyglotBindings ().getMember ("foo" );
130
130
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" ));
132
132
}
133
133
134
134
@ Test
@@ -141,7 +141,7 @@ public void testAsFunction() throws Exception {
141
141
context .eval (script );
142
142
Value main = context .getPolyglotBindings ().getMember ("foo" );
143
143
main .execute ();
144
- assertEquals ("Called\n " , os .toString ("UTF-8" ));
144
+ assertEquals ("Called\n " , out .toString ("UTF-8" ));
145
145
}
146
146
147
147
@ Test
@@ -154,7 +154,7 @@ public void testAsFunctionVarArgs() throws Exception {
154
154
context .eval (script );
155
155
Value main = context .getPolyglotBindings ().getMember ("foo" );
156
156
main .execute ("Hello" , "World" );
157
- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
157
+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
158
158
}
159
159
160
160
@ Test
@@ -165,7 +165,7 @@ public void mainFunctionsAreImplicitlyImporteable() throws Exception {
165
165
context .eval (script );
166
166
Value main = context .getBindings ("python" ).getMember ("foo" );
167
167
main .execute ("Hello" , "World" );
168
- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
168
+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
169
169
}
170
170
171
171
@ Test
@@ -175,7 +175,7 @@ public void builtinFunctionsAreImporteable() throws Exception {
175
175
context .eval (script );
176
176
Value main = context .getBindings ("python" ).getMember ("__builtins__" ).getMember ("print" );
177
177
main .execute ("Hello" , "World" );
178
- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
178
+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
179
179
}
180
180
181
181
@ Test
@@ -186,15 +186,15 @@ public void testMultipleInvocationsAreInSameScope() throws Exception {
186
186
Source script = Source .create ("python" , source );
187
187
Value foo = context .eval (script );
188
188
foo .execute ("Hello" , "World" );
189
- assertEquals ("Hello World\n " , os .toString ("UTF-8" ));
189
+ assertEquals ("Hello World\n " , out .toString ("UTF-8" ));
190
190
191
191
source = "def bar(a, b):\n " +
192
192
" foo(a, b)\n " +
193
193
"bar" ;
194
194
script = Source .create ("python" , source );
195
195
Value bar = context .eval (script );
196
196
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" ));
198
198
199
199
source = "invalid syntax" ;
200
200
script = Source .create ("python" , source );
@@ -203,9 +203,9 @@ public void testMultipleInvocationsAreInSameScope() throws Exception {
203
203
} catch (Throwable t ) {
204
204
}
205
205
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" ));
207
207
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" ));
209
209
}
210
210
211
211
@ Test
@@ -335,4 +335,101 @@ public void tryInvokeThenReadExecute() {
335
335
assert result [0 ].equals (ForeignObjectWithOOInvoke .class .getName ());
336
336
assert result [1 ].equals (ForeignObjectWithoutOOInvoke .class .getName ());
337
337
}
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
+ }
338
435
}
0 commit comments