1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
use strict ;
use Test ;
use Inline Config =>
DIRECTORY => './_Inline_test';
use Inline (
Java => 'DATA',
STUDY => ['org.perl.inline.java.InlineJavaPerlCaller'],
STARTUP_DELAY => 20,
) ;
use Inline::Java qw(caught) ;
BEGIN {
my $cnt = 37 ;
plan(tests => $cnt) ;
}
my $mtc_cnt = 0 ;
my $mtc_mode = 0 ;
my $t = new t15() ;
{
eval {
ok($t->add(5, 6), 11) ;
ok($t->add_via_perl(5, 6), 11) ;
my $a = $t->incr_via_perl([7, 6, 5]) ;
ok($a->[1], 7) ;
$a = $t->incr_via_perl_ctx($a) ;
ok($a->[1], 8) ;
ok($t->mul(5, 6), 30) ;
ok($t->mul_via_perl(5, 6), 30) ;
ok($t->silly_mul(3, 2), 6) ;
ok($t->silly_mul_via_perl(3, 2), 6) ;
ok(add_via_java(3, 4), 7) ;
ok($t->add_via_perl_via_java(3, 4), 7) ;
ok($t->silly_mul_via_perl_via_java(10, 9), 90) ;
ok(t15->add_via_perl_via_java_t($t, 6, 9), 15) ;
ok($t->cat_via_perl("Inline", "Java"), "InlineJava") ;
ok($t->perl_static(), 'main->static') ;
ok(twister(20, 0, 0), "return perl twister") ;
ok($t->twister(20, 0, 0), "return java twister") ;
eval {twister(20, 0, 1)} ; ok($@, qr/^throw perl twister/) ;
my $msg = '' ;
eval {$t->twister(20, 0, 1)} ;
if ($@) {
if (caught('t15$OwnException')){
$msg = $@->getMessage() ;
}
else{
die $@ ;
}
}
ok($msg, "throw java twister") ;
eval {$t->bug()} ; ok($@, qr/^bug/) ;
ok($t->perlt()->add(5, 6), 11) ;
eval {$t->perldummy()} ; ok($@, qr/Can't propagate non-/) ; #'
$t->mtc_callbacks(20) ;
$t->StartCallbackLoop() ;
ok($mtc_cnt, 20) ;
$mtc_cnt = -30 ;
$t->mtc_callbacks2(50) ;
$t->StartCallbackLoop() ;
ok($mtc_cnt, 20) ;
$mtc_cnt = 0 ;
$mtc_mode = 1 ;
$t->mtc_callbacks2(20) ;
$t->StartCallbackLoop() ;
ok($mtc_cnt, 20) ;
$mtc_cnt = 0 ;
$mtc_mode = 2 ;
$t->mtc_callbacks2(20) ;
$t->OpenCallbackStream() ;
while (($mtc_cnt < 20)&&($t->WaitForCallback(-1) > 0)){
$t->ProcessNextCallback() ;
}
ok($mtc_cnt, 20) ;
$mtc_cnt = 0 ;
$mtc_mode = 2 ;
$t->mtc_callbacks2(10) ;
while ($t->WaitForCallback(3.1416) > 0){
ok($t->WaitForCallback(0) >= 1) ;
$t->ProcessNextCallback() ;
}
ok($mtc_cnt, 10) ;
# Unfortunately we can't test this because the Thread.run method doesn't allow us
# to throw any exceptions...
# $t->mtc_callbacks_error() ;
} ;
if ($@){
if (caught("java.lang.Throwable")){
$@->printStackTrace() ;
die("Caught Java Exception") ;
}
else{
die $@ ;
}
}
}
ok($t->__get_private()->{proto}->ObjectCount(), 1) ;
sub add {
my $i = shift ;
my $j = shift ;
return $i + $j ;
}
sub incr {
my $ija = shift ;
for (my $i = 0 ; $i < $ija->length() ; $i++){
$ija->[$i]++ ;
}
return wantarray ? @{$ija} : $ija ;
}
sub mul {
my $i = shift ;
my $j = shift ;
return $i * $j ;
}
sub cat {
my $i = shift ;
my $j = shift ;
return $i . $j ;
}
sub add_via_java {
my $i = shift ;
my $j = shift ;
return $t->add($i, $j) ;
}
sub add_via_java_t {
my $_t = shift ;
my $i = shift ;
my $j = shift ;
return $_t->add($i, $j) ;
}
sub twister {
my $max = shift ;
my $cnt = shift ;
my $explode = shift ;
if ($cnt == $max){
if ($explode){
die("throw perl twister") ;
}
else{
return "return perl twister" ;
}
}
else{
return $t->twister($max, $cnt+1, $explode) ;
}
}
sub t {
return $t ;
}
sub dummy {
die(bless({}, "Inline::Java::dummy")) ;
}
sub mt_callback {
my $pc = shift ;
$mtc_cnt++ ;
if ($mtc_cnt >= 20){
if ($mtc_mode == 0){
$pc->StopCallbackLoop() ;
}
elsif ($mtc_mode == 1){
my $o = new org::perl::inline::java::InlineJavaPerlCaller() ;
$o->StopCallbackLoop() ;
}
}
}
sub static_method {
my $class = shift ;
return 'main->static' ;
}
__END__
__Java__
import java.io.* ;
import org.perl.inline.java.* ;
class t15 extends InlineJavaPerlCaller {
class OwnException extends Exception {
OwnException(String msg){
super(msg) ;
}
}
class OwnThread extends Thread {
InlineJavaPerlCaller pc = null ;
boolean error = false ;
OwnThread(InlineJavaPerlCaller _pc, int nb, boolean err){
super("CALLBACK-TEST-THREAD-#" + nb) ;
pc = _pc ;
error = err ;
}
public void run(){
try {
if (! error){
pc.CallPerlSub("main::mt_callback", new Object [] {pc}) ;
}
else {
new InlineJavaPerlCaller() ;
}
}
catch (InlineJavaException ie){
ie.printStackTrace() ;
}
catch (InlineJavaPerlException ipe){
ipe.printStackTrace() ;
}
}
}
public t15() throws InlineJavaException {
}
public int add(int a, int b){
return a + b ;
}
public int mul(int a, int b){
return a * b ;
}
public int silly_mul(int a, int b){
int ret = 0 ;
for (int i = 0 ; i < b ; i++){
ret = add(ret, a) ;
}
return a * b ;
}
public int silly_mul_via_perl(int a, int b) throws InlineJavaException, InlineJavaPerlException {
int ret = 0 ;
for (int i = 0 ; i < b ; i++){
ret = add_via_perl(ret, a) ;
}
return ret ;
}
public int add_via_perl(int a, int b) throws InlineJavaException, InlineJavaPerlException {
String val = (String)CallPerlSub("main::add",
new Object [] {new Integer(a), new Integer(b)}) ;
return new Integer(val).intValue() ;
}
public int [] incr_via_perl(int a[]) throws InlineJavaException, InlineJavaPerlException {
int [] r = (int [])CallPerlSub("main::incr",
new Object [] {a}, a.getClass()) ;
return r ;
}
public int [] incr_via_perl_ctx(int a[]) throws InlineJavaException, InlineJavaPerlException {
int [] r = (int [])CallPerlSub("@main::incr",
new Object [] {a}, a.getClass()) ;
return r ;
}
public void death_via_perl() throws InlineJavaException, InlineJavaPerlException {
InlineJavaPerlCaller c = new InlineJavaPerlCaller() ;
c.CallPerlSub("main::death", null) ;
}
public void except() throws InlineJavaException, InlineJavaPerlException {
throw new InlineJavaPerlException("test") ;
}
public int mul_via_perl(int a, int b) throws InlineJavaException, InlineJavaPerlException {
String val = (String)CallPerlSub("main::mul",
new Object [] {new Integer(a), new Integer(b)}) ;
return new Integer(val).intValue() ;
}
public int add_via_perl_via_java(int a, int b) throws InlineJavaException, InlineJavaPerlException {
String val = (String)CallPerlSub("main::add_via_java",
new Object [] {new Integer(a), new Integer(b)}) ;
return new Integer(val).intValue() ;
}
static public int add_via_perl_via_java_t(t15 t, int a, int b) throws InlineJavaException, InlineJavaPerlException {
InlineJavaPerlCaller c = new InlineJavaPerlCaller() ;
String val = (String)c.CallPerlSub("main::add_via_java_t",
new Object [] {t, new Integer(a), new Integer(b)}) ;
return new Integer(val).intValue() ;
}
public int silly_mul_via_perl_via_java(int a, int b) throws InlineJavaException, InlineJavaPerlException {
int ret = 0 ;
for (int i = 0 ; i < b ; i++){
String val = (String)CallPerlSub("add_via_java",
new Object [] {new Integer(ret), new Integer(a)}) ;
ret = new Integer(val).intValue() ;
}
return ret ;
}
public String cat_via_perl(String a, String b) throws InlineJavaException, InlineJavaPerlException {
String val = (String)CallPerlSub("cat",
new Object [] {a, b}) ;
return val ;
}
public String twister(int max, int cnt, int explode) throws InlineJavaException, InlineJavaPerlException, OwnException {
if (cnt == max){
if (explode > 0){
throw new OwnException("throw java twister") ;
}
else{
return "return java twister" ;
}
}
else{
return (String)CallPerlSub("twister",
new Object [] {new Integer(max), new Integer(cnt+1), new Integer(explode)}) ;
}
}
public void bug() throws InlineJavaException {
throw new InlineJavaException("bug") ;
}
public Object perlt() throws InlineJavaException, InlineJavaPerlException, OwnException {
return CallPerlSub("t", null) ;
}
public Object perl_static() throws InlineJavaException, InlineJavaPerlException, OwnException {
return CallPerlStaticMethod("main", "static_method", null) ;
}
public Object perldummy() throws InlineJavaException, InlineJavaPerlException, OwnException {
return CallPerlSub("dummy", null) ;
}
public void mtc_callbacks(int n){
for (int i = 0 ; i < n ; i++){
OwnThread t = new OwnThread(this, i, false) ;
t.start() ;
}
}
public void mtc_callbacks2(int n) throws InlineJavaException, InlineJavaPerlException {
for (int i = 0 ; i < n ; i++){
InlineJavaPerlCaller pc = new InlineJavaPerlCaller() ;
OwnThread t = new OwnThread(pc, i, false) ;
t.start() ;
}
}
public void mtc_callbacks_error(){
OwnThread t = new OwnThread(this, 0, true) ;
t.start() ;
}
}
|