File: 02_perl_interpreter.t

package info (click to toggle)
libinline-java-perl 0.58~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 964 kB
  • ctags: 684
  • sloc: perl: 4,717; java: 2,844; makefile: 35
file content (155 lines) | stat: -rw-r--r-- 3,672 bytes parent folder | download
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
use strict ;

use Test ;
use File::Spec ;
use Config ;

BEGIN {
    if ($^O eq 'cygwin'){
		# Stand-alone Java interpreter cannot load Cygwin DLL directly
        plan(tests => 0) ;
        exit ;
	}

    plan(tests => 13) ;
}


use Inline Config =>
           DIRECTORY => './_Inline_test' ;

use Inline (
	Java => 'DATA',
	NAME => 'Tests'
) ;
use Inline::Java::Portable ;
ok(1) ;


my $inline = $org::perl::inline::java::InlineJavaPerlInterpreterTests::INLINE ;
$inline = $org::perl::inline::java::InlineJavaPerlInterpreterTests::INLINE ; # stupid warning...

my $install_dir = File::Spec->catdir($inline->get_api('install_lib'),
        'auto', $inline->get_api('modpname')) ;

require Inline::Java->find_default_j2sdk() ;
my $server_jar = Inline::Java::Portable::get_server_jar() ;

run_java($install_dir, $server_jar) ;


#################################################


sub run_java {
	my @cps = @_ ;

	$ENV{CLASSPATH} = Inline::Java::Portable::make_classpath(@cps) ;
	Inline::Java::debug(1, "CLASSPATH is $ENV{CLASSPATH}\n") ;

	my $java = File::Spec->catfile(
		Inline::Java::get_default_j2sdk(),
		Inline::Java::Portable::portable("J2SDK_BIN"), 
		'java' . Inline::Java::Portable::portable("EXE_EXTENSION")) ;

	my $debug = $ENV{PERL_INLINE_JAVA_DEBUG} || 0 ;
	my $cmd = Inline::Java::Portable::portable("SUB_FIX_CMD_QUOTES", "\"$java\" " . 
		"org.perl.inline.java.InlineJavaPerlInterpreterTests $debug") ;
	Inline::Java::debug(1, "Command is $cmd\n") ;
	open(CMD, "$cmd|") or die("Can't execute $cmd: $!") ;
	while (<CMD>){
		print $_ ;
	}
}


__END__

__Java__
package org.perl.inline.java ;

import java.util.* ;

class InlineJavaPerlInterpreterTests implements Runnable {
	private static int cnt = 2 ;
	private static InlineJavaPerlInterpreter pi = null ;
	private static int nb_callbacks_to_run = 5 ;
	private static int nb_callbacks_run = 0 ;

	private InlineJavaPerlInterpreterTests() throws InlineJavaException, InlineJavaPerlException {
	}

	private synchronized static void ok(Object o1, Object o2){
		if (o1.equals(o2)){
			String comment = " # " + o1 + " == " + o2 ;
			System.out.println("ok " + cnt + comment) ;
		}
		else {
			String comment = " # " + o1 + " != " + o2 ;
			System.out.println("nok " + cnt + comment) ;
		}
		cnt++ ;
	}


	public void run(){
		try {
			String name = (String)pi.CallPerlSub("whats_your_name", null, String.class) ;
			ok(name, "perl") ;
			nb_callbacks_run++ ;

			if (nb_callbacks_run == nb_callbacks_to_run){
				pi.StopCallbackLoop() ;
			}
		}
		catch (Exception e){
			e.printStackTrace() ;
			System.exit(1) ;
		}
	}


	public static void main(String args[]){
		try {
			int debug = 0 ;
			if (args.length > 0){
				debug = Integer.parseInt(args[0]) ;
				InlineJavaUtils.set_debug(debug) ;
			}

			InlineJavaPerlInterpreter.init("test") ;
			pi = InlineJavaPerlInterpreter.create() ; 

			pi.require("t/Tests.pl") ;
			ok("1", "1") ;
			pi.require("Carp") ;
			ok("1", "1") ;
			Integer sum = (Integer)pi.eval("34 + 56", Integer.class) ;
			ok(sum, new Integer(90)) ;
			String name = (String)pi.CallPerlSub("whats_your_name", null, String.class) ;
			ok(name, "perl") ;
	
			for (int i = 1 ; i <= nb_callbacks_to_run ; i++){
				Thread t = new Thread(new InlineJavaPerlInterpreterTests()) ;
				t.start() ;
			}

			pi.StartCallbackLoop();

			ArrayList a = new ArrayList() ;
			for (int i = 0 ; i < 100 ; i++){
				a.add(new Integer(i * 2)) ;
			}
			sum = (Integer)pi.CallPerlSub("sum_array_list", new Object [] {a}, Integer.class) ;
			ok(sum, new Integer(9900)) ;

			pi.destroy() ;
			ok("1", "1") ;
		}
		catch (Exception e){
			e.printStackTrace() ;
			System.exit(1) ;
		}
		ok("1", "1") ;
	}
}