rjb 1.0.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ /*
2
+ * Rjb - Ruby <-> Java Bridge
3
+ * Copyright(c) 2004,2005 arton
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * $Id: rjb.h 2 2006-04-11 19:04:40Z arton $
16
+ * $Log: rjb.h,v $
17
+ * Revision 1.1 2005/01/16 17:36:10 arton
18
+ * Initial revision
19
+ *
20
+ *
21
+ */
22
+
23
+ #ifndef RJB_H
24
+ #define RJB_H
25
+
26
+ #if !defined(COUNTOF)
27
+ #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
28
+ #endif
29
+
30
+ #if !defined(_I64_MIN)
31
+ #define _I64_MIN (-9223372036854775807i64 - 1)
32
+ #endif
33
+ #if !defined(_I64_MAX)
34
+ #define _I64_MAX 9223372036854775807i64
35
+ #endif
36
+
37
+ /* in rjb.c */
38
+ extern VALUE loaded_classes;
39
+ extern jmethodID class_getName;
40
+ extern jclass j_throwable;
41
+ extern jmethodID throwable_getMessage;
42
+ extern JNIEnv* attach_current_thread(void);
43
+ extern jclass find_class(JNIEnv* jenv, VALUE name);
44
+ extern void release_string(JNIEnv *jenv, jstring str, const char* chrs);
45
+
46
+ /* in rjbexception.c */
47
+ extern VALUE get_exception_class(JNIEnv* jenv, jstring str);
48
+ extern void check_exception(JNIEnv* jenv, int t);
49
+ extern VALUE rjb_s_throw(int, VALUE*, VALUE);
50
+
51
+ /* conversion functions */
52
+ typedef void (*R2J)(JNIEnv*, VALUE, jvalue*, const char*, int);
53
+ typedef VALUE (*J2R)(JNIEnv*, jvalue);
54
+ typedef jarray (*R2JARRAY)(JNIEnv*, VALUE, const char*);
55
+ typedef void (JNICALL *RELEASEARRAY)(JNIEnv*, jobject, void*, jint);
56
+ typedef jlong (JNICALL *INVOKEAL)(JNIEnv*, jobject, jmethodID, const jvalue*);
57
+ typedef jdouble (JNICALL *INVOKEAD)(JNIEnv*, jobject, jmethodID, const jvalue*);
58
+ typedef jfloat (JNICALL *INVOKEAF)(JNIEnv*, jobject, jmethodID, const jvalue*);
59
+ typedef jboolean (JNICALL *INVOKEAZ)(JNIEnv*, jobject, jmethodID, const jvalue*);
60
+ typedef jshort (JNICALL *INVOKEAS)(JNIEnv*, jobject, jmethodID, const jvalue*);
61
+ typedef jobject (JNICALL *INVOKEA)(JNIEnv*, jobject, jmethodID, const jvalue*);
62
+ typedef VALUE (*CONV)(JNIEnv*, void*);
63
+
64
+ /*
65
+ * internal method class
66
+ */
67
+ struct cls_constructor {
68
+ jmethodID id;
69
+ int arg_count;
70
+ R2J* arg_convert;
71
+ char* method_signature;
72
+ char result_signature;
73
+ char result_arraydepth;
74
+ };
75
+
76
+ struct cls_method {
77
+ struct cls_constructor basic;
78
+ ID name;
79
+ int static_method;
80
+ off_t method;
81
+ J2R result_convert;
82
+ /* overload only */
83
+ struct cls_method* next;
84
+ };
85
+
86
+ /*
87
+ * internal field class
88
+ */
89
+ struct cls_field {
90
+ ID name;
91
+ jfieldID id;
92
+ char* field_signature;
93
+ char result_signature;
94
+ char result_arraydepth;
95
+ R2J arg_convert;
96
+ J2R value_convert;
97
+ int readonly;
98
+ int static_field;
99
+ };
100
+
101
+ /*
102
+ * Object instance
103
+ */
104
+ struct jvi_data {
105
+ jclass klass; /* class */
106
+ jobject obj; /* instance */
107
+ st_table* methods;
108
+ st_table* fields;
109
+ };
110
+
111
+ /*
112
+ * Class instance
113
+ */
114
+ struct jv_data {
115
+ struct jvi_data idata;
116
+ st_table* static_methods;
117
+ struct cls_constructor** constructors;
118
+ };
119
+
120
+ /*
121
+ * Bridge instance
122
+ */
123
+ struct rj_bridge {
124
+ jobject bridge;
125
+ jobject proxy;
126
+ VALUE wrapped;
127
+ };
128
+
129
+ #endif
@@ -0,0 +1,130 @@
1
+ /*
2
+ * Rjb - Ruby <-> Java Bridge
3
+ * Copyright(c) 2004,2005,2006 arton
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * This library is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * $Id: rjbexception.c 10 2006-07-16 04:15:47Z arton $
16
+ */
17
+
18
+ #include "ruby.h"
19
+ #include "st.h"
20
+ #include "jniwrap.h"
21
+ #include "riconv.h"
22
+ #include "rjb.h"
23
+
24
+ /*
25
+ * handle Java exception
26
+ * At this time, the Java exception is defined without the package name.
27
+ * This design may change in future release.
28
+ */
29
+ VALUE get_exception_class(JNIEnv* jenv, jstring str)
30
+ {
31
+ VALUE rexp;
32
+ char* pcls;
33
+ VALUE cname;
34
+ const char* p = (*jenv)->GetStringUTFChars(jenv, str, JNI_FALSE);
35
+ char* clsname = ALLOCA_N(char, strlen(p) + 1);
36
+ strcpy(clsname, p);
37
+ release_string(jenv, str, p);
38
+ pcls = strrchr(clsname, '.');
39
+ if (pcls)
40
+ {
41
+ pcls++;
42
+ }
43
+ else
44
+ {
45
+ pcls = clsname;
46
+ }
47
+ cname = rb_str_new2(pcls);
48
+ rexp = rb_hash_aref(loaded_classes, cname);
49
+ if (rexp == Qnil)
50
+ {
51
+ rexp = rb_define_class(pcls, rb_eStandardError);
52
+ st_insert(RHASH(loaded_classes)->tbl, cname, rexp);
53
+ }
54
+ return rexp;
55
+ }
56
+
57
+ /*
58
+ * throw newly created exception with supplied message.
59
+ */
60
+ VALUE rjb_s_throw(int argc, VALUE* argv, VALUE self)
61
+ {
62
+ VALUE klass;
63
+ VALUE message;
64
+ JNIEnv* jenv = attach_current_thread();
65
+ if (rb_scan_args(argc, argv, "11", &klass, &message) == 2)
66
+ {
67
+ jclass excep = find_class(jenv, klass);
68
+ if (excep == NULL)
69
+ {
70
+ rb_raise(rb_eRuntimeError, "`%s' not found", StringValueCStr(klass));
71
+ }
72
+ (*jenv)->ThrowNew(jenv, excep, StringValueCStr(message));
73
+ }
74
+ else
75
+ {
76
+ struct jvi_data* ptr;
77
+ Data_Get_Struct(klass, struct jvi_data, ptr);
78
+ if (!(*jenv)->IsInstanceOf(jenv, ptr->obj, j_throwable))
79
+ {
80
+ rb_raise(rb_eRuntimeError, "arg1 must be a throwable");
81
+ }
82
+ else
83
+ {
84
+ (*jenv)->Throw(jenv, ptr->obj);
85
+ }
86
+ }
87
+ return Qnil;
88
+ }
89
+
90
+ void check_exception(JNIEnv* jenv, int t)
91
+ {
92
+ jthrowable exp = (*jenv)->ExceptionOccurred(jenv);
93
+ if (exp)
94
+ {
95
+ VALUE rexp = Qnil;
96
+ if (RTEST(ruby_verbose))
97
+ {
98
+ (*jenv)->ExceptionDescribe(jenv);
99
+ }
100
+ (*jenv)->ExceptionClear(jenv);
101
+ // if (t)
102
+ if(1)
103
+ {
104
+ char* msg = "unknown exception";
105
+ jclass cls = (*jenv)->GetObjectClass(jenv, exp);
106
+ jstring str = (*jenv)->CallObjectMethod(jenv, exp, throwable_getMessage);
107
+ if (str)
108
+ {
109
+ const char* p = (*jenv)->GetStringUTFChars(jenv, str, JNI_FALSE);
110
+ msg = ALLOCA_N(char, strlen(p) + 1);
111
+ strcpy(msg, p);
112
+ release_string(jenv, str, p);
113
+ }
114
+ str = (*jenv)->CallObjectMethod(jenv, cls, class_getName);
115
+ if (str)
116
+ {
117
+ rexp = get_exception_class(jenv, str);
118
+ }
119
+ if (rexp == Qnil)
120
+ {
121
+ rb_raise(rb_eRuntimeError, "%s", msg);
122
+ }
123
+ else
124
+ {
125
+ rb_raise(rexp, msg);
126
+ }
127
+ }
128
+ }
129
+ }
130
+
@@ -0,0 +1,22 @@
1
+ =begin
2
+ Copyright(c) 2006 arton
3
+ =end
4
+
5
+ require 'rbconfig'
6
+
7
+ module RjbConf
8
+ dir = File.join(File.dirname(File.dirname(__FILE__)), 'data')
9
+ if File.exist?(dir)
10
+ datadir = dir
11
+ else
12
+ datadir = Config::CONFIG['datadir']
13
+ end
14
+ BRIDGE_FILE = File.join(datadir, 'rjb', 'jp', 'co', 'infoseek', 'hp',
15
+ 'arton', 'rjb', 'RBridge.class')
16
+ unless File.exist?(BRIDGE_FILE)
17
+ raise 'bridge file not found'
18
+ end
19
+ end
20
+
21
+ require 'rjbcore'
22
+
Binary file
@@ -0,0 +1,27 @@
1
+ ����
2
+ �E���炩���ߊ‹��ϐ���JAVA_HOME��ݒ肵�Ă����Ă��������B
3
+ �E���̏ꍇ�AJAVA_HOME�́AJ2SDK�̃C���X�g�[���f�B���N�g���̕K�v������܂��B
4
+ �E���炩���ߊ‹��ϐ�PATH��$JAVA_HOME/bin��ݒ肵�Ă����Ă��������B
5
+ �EWindows�̏ꍇ�APATH�ɂ�%PATH%;%JAVA_HOME%bin��ݒ肷�邱�ƂɂȂ�܂��B
6
+ �Eruby1.8�ȍ~�����s�ł���悤��PATH��ݒ肵�Ă����Ă��������B
7
+
8
+ �C���X�g�[�����@
9
+ 1. unzip rjb-*
10
+ 2. cd rjb-*
11
+ 3. ruby setup.rb config
12
+ 4. ruby setup.rb setup
13
+ 5. sudo ruby setup.rb install
14
+ Windows�ł́A�قƂ�ǂ̏ꍇ�ŏ���sudo�͕s�v�ł��B�u�قƂ�ǂ̏ꍇ�v�ɊY�����Ȃ��ꍇ�͉����K�v���͂킩���Ă���͂��ł��̂Ő����͏ȗ����܂��B
15
+
16
+ ���s��
17
+ �E���炩���ߊ‹��ϐ���JAVA_HOME��ݒ肵�Ă����Ă��������B
18
+ �E���̏ꍇ�AJAVA_HOME�́AJ2SDK�̃C���X�g�[���f�B���N�g���̕K�v������܂��B
19
+ �ELinux�Ɋւ��Ă�LD_LIBRARY_PATH�ɁAjava2�̋��L�I�u�W�F�N�g�f�B���N�g����ݒ肵�Ă����K�v������܂��B
20
+
21
+ �e�X�g�����‹�
22
+ Windows2000 SP4-ruby1.8.2-j2se1.5.0, Solaris9-ruby1.8.0-j2se1.4.2, Linux 2.4.26-ruby-1.8.1-j2se1.4.2
23
+
24
+ �A����
25
+ artonx@yahoo.co.jp
26
+ http://arton.no-ip.info/collabo/backyard/?RjbQandA (�L�����ɂ�diary�փc�b�R�~�����Ă��������j
27
+
@@ -0,0 +1,28 @@
1
+ Rjb is Ruby-Java bridge using Java Native Interface.
2
+
3
+ How to install
4
+
5
+ you need to install Java2 sdk, and setup JAVA_HOME enviromental varible except for OS X.
6
+ I assume that OS X's JAVA_HOME is fixed as '/System/Library/Frameworks/JavaVM.framework'.
7
+
8
+ then,
9
+
10
+ ruby setup.rb config
11
+ ruby setup.rb setup
12
+
13
+ (in Unix)
14
+ sudo ruby setup.rb install
15
+ or
16
+ (in win32)
17
+ ruby setup.rb install
18
+
19
+ How to test
20
+ in Win32
21
+ cd test
22
+ ruby test.rb
23
+
24
+ in Unix
25
+ see test/readme.unix
26
+ you must set LD_LIBRARY_PATH environmental variable to run rjb.
27
+
28
+ artonx@yahoo.co.jp
@@ -0,0 +1,46 @@
1
+ require 'rjb'
2
+
3
+ Rjb::load
4
+
5
+ SwingUtilities = Rjb::import('javax.swing.SwingUtilities')
6
+ JThread = Rjb::import('java.lang.Thread')
7
+ JFileChooser = Rjb::import('javax.swing.JFileChooser')
8
+ class Run
9
+ def initialize(&block)
10
+ @block = block
11
+ end
12
+ def run
13
+ puts 'go-hello'
14
+ @block.call
15
+ puts 'ret-hello'
16
+ end
17
+ end
18
+
19
+ class FileChooser
20
+ @@klass = JFileChooser
21
+ def initialize(ext = '*', desc = 'any files')
22
+ @selected = nil
23
+ end
24
+
25
+ def show()
26
+ run = Rjb::bind(Run.new do
27
+ puts 'hello'
28
+ @selected = nil
29
+ chooser = @@klass.new()
30
+ puts 'hello'
31
+ ret = chooser.showOpenDialog(nil)
32
+ puts 'hello'
33
+ if ret == @@klass.APPROVE_OPTION
34
+ @selected = chooser.getSelectedFile
35
+ end
36
+ end, 'java.lang.Runnable')
37
+ SwingUtilities.invokeAndWait(run)
38
+ end
39
+ attr_reader :selected
40
+ end
41
+
42
+ f = FileChooser.new
43
+ if f.show == 0
44
+ puts f.selected.getAbsolutePath
45
+ end
46
+ puts 'bye'
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ require 'rjb'
3
+
4
+ class TestRjbGC < Test::Unit::TestCase
5
+ include Rjb
6
+ def setup
7
+ load(nil, ['-verbose:gc'])
8
+ end
9
+
10
+ def tearDown
11
+ unload
12
+ end
13
+
14
+ def test_gc
15
+ stringBuffer = import('java.lang.StringBuffer')
16
+ (0..1000).each do |i|
17
+ sb = stringBuffer.new
18
+ (0..1000).each do |j|
19
+ sb.append(' ')
20
+ end
21
+ GC.start
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,394 @@
1
+ require 'test/unit'
2
+ require 'rjb'
3
+
4
+ puts "start RJB(#{Rjb::VERSION}) test"
5
+ class TestRjb < Test::Unit::TestCase
6
+ include Rjb
7
+ def setup
8
+ load('.')
9
+ end
10
+
11
+ def tearDown
12
+ unload
13
+ end
14
+
15
+ def test_metaclass
16
+ cls = import('java.lang.Class')
17
+ assert_equal('java.lang.Class', cls._classname)
18
+ assert_equal('java.lang.Class', cls.getName)
19
+ assert_equal(17, cls.getModifiers)
20
+ end
21
+
22
+ def test_scalar
23
+ cls = import('java.lang.String')
24
+ assert_equal('java.lang.Class', cls._classname)
25
+ assert_equal('class java.lang.String', cls.toString)
26
+ str = cls.new
27
+ assert_equal('java.lang.String', str._classname)
28
+ assert_equal(0, str.length)
29
+ assert_equal('', str.toString)
30
+ str = cls.new_with_sig('Ljava.lang.String;', "abcde")
31
+ # result integer
32
+ assert_equal(5, str.length)
33
+ # result string
34
+ assert_equal('abcde', str.toString)
35
+ # argument test
36
+ # char
37
+ assert_equal('abxde', str.replace(?c, ?x))
38
+ # string
39
+ assert_equal('abdxe', str.replaceAll('cd', 'dx'))
40
+ # int
41
+ assert_equal('bc', str.substring(1, 3))
42
+ assert_equal('e', str.substring(4))
43
+ # float with static
44
+ assert_equal('5.23', cls._invoke('valueOf', 'F', 5.23))
45
+ assert_equal('25.233', cls._invoke('valueOf', 'D', 25.233))
46
+ # rjb object (String)
47
+ str2 = cls.new_with_sig('Ljava.lang.String;', 'fghijk')
48
+ assert_equal('abcdefghijk', str.concat(str2))
49
+ # rjb object other (implicit toString call is Rjb feature)
50
+ jint = import('java.lang.Integer')
51
+ i = jint.new_with_sig('I', 35901)
52
+ assert_equal('abcde35901', str.concat(i))
53
+ # result boolean and argument is rjb object
54
+ assert_equal(false, i.equals(str))
55
+ assert_equal(false, str.equals(i))
56
+ assert_equal(true, str.equals("abcde"))
57
+ assert_equal(true, str.equals(str))
58
+ # long
59
+ lng = import('java.lang.Long')
60
+ l = lng.new_with_sig('J', -9223372036854775808)
61
+ assert_equal(-9223372036854775808, l.longValue)
62
+ l = lng.new_with_sig('J', 9223372036854775807)
63
+ assert_equal(9223372036854775807, l.longValue)
64
+ # double
65
+ dbl = import('java.lang.Double')
66
+ d = dbl.new_with_sig('D', 1234.5678901234567890)
67
+ assert_equal(1234.5678901234567890, d.doubleValue)
68
+ # byte
69
+ byt = import('java.lang.Byte')
70
+ b = byt.new_with_sig('B', 13)
71
+ assert_equal(13, b.byteValue)
72
+ # float
73
+ flt = import('java.lang.Float')
74
+ f = flt.new_with_sig('F', 13.5)
75
+ assert_equal(13.5, f.floatValue)
76
+ # short
77
+ sht = import('java.lang.Short')
78
+ s = sht.new_with_sig('S', 1532)
79
+ assert_equal(1532, s.shortValue)
80
+ chr = import('java.lang.Character')
81
+ c = chr.new_with_sig('C', ?A)
82
+ assert_equal(?A, c.charValue)
83
+ end
84
+
85
+ def test_array
86
+ cls = import('java.lang.String')
87
+ str = cls.new_with_sig('[C', [?a, ?b, ?c, ?d, ?e, ?c, ?f, ?c, ?g])
88
+ assert_equal('abcdecfcg', str.toString)
89
+ # conv string array
90
+ splt = str.split('c')
91
+ assert(Array === splt)
92
+ assert_equal(4, splt.size)
93
+ assert_equal('ab', splt[0])
94
+ assert_equal('g', splt[3])
95
+ # conv byte array to (ruby)string
96
+ ba = str.getBytes
97
+ assert_equal('abcdecfcg', ba)
98
+ # conv char array to array(int)
99
+ ca = str.toCharArray
100
+ assert_equal([?a, ?b, ?c, ?d, ?e, ?c, ?f, ?c, ?g], ca)
101
+ end
102
+
103
+ def test_importobj()
104
+ sys = import('java.lang.System')
105
+ props = sys.getProperties
106
+ assert_equal('java.util.Properties', props._classname)
107
+ if /cygwin/ =~ RUBY_PLATFORM # patch for dirty environment
108
+ assert_equal(Dir::pwd, %x[cygpath -u #{props.getProperty('user.dir').gsub('\\', '/')}].chop)
109
+ else
110
+ assert_equal(Dir::pwd, props.getProperty('user.dir').gsub('\\', '/'))
111
+ end
112
+ boolean = import('java.lang.Boolean')
113
+ assert_equal(boolean.valueOf(true).booleanValue(), true)
114
+ assert_equal(boolean.valueOf(false).booleanValue(), false)
115
+ assert_equal(boolean.valueOf('true').booleanValue(), true)
116
+ assert_equal(boolean.valueOf('false').booleanValue(), false)
117
+ end
118
+
119
+ def test_importobjarray()
120
+ jarray = import('java.util.ArrayList')
121
+ a = jarray.new()
122
+ jint = import('java.lang.Integer')
123
+ a.add(jint.new_with_sig('I', 1))
124
+ a.add(jint.new_with_sig('I', 2))
125
+ a.add(jint.new_with_sig('I', 3))
126
+ oa = a.toArray
127
+ assert_equal(3, oa.size)
128
+ assert_equal(1, oa[0].intValue)
129
+ assert_equal(2, oa[1].intValue)
130
+ assert_equal(3, oa[2].intValue)
131
+ end
132
+
133
+ def test_kjconv()
134
+ $KCODE = 'euc'
135
+ cls = import('java.lang.String')
136
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
137
+ s = cls.new(euc_kj)
138
+ assert_equal(s.toString(), euc_kj)
139
+ $KCODE = 'sjis'
140
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
141
+ s = cls.new(sjis_kj)
142
+ assert_equal(s.toString(), sjis_kj)
143
+ end
144
+
145
+ def test_constants()
146
+ lng = import('java.lang.Long')
147
+ assert_equal(0x7fffffffffffffff, lng.MAX_VALUE)
148
+ assert_equal(-9223372036854775808, lng.MIN_VALUE)
149
+ end
150
+
151
+ class TestIter
152
+ def initialize()
153
+ @i = 5
154
+ end
155
+ def hasNext()
156
+ @i > 0
157
+ end
158
+ def next()
159
+ @i -= 1
160
+ @i.to_s
161
+ end
162
+ end
163
+
164
+ def test_newobject()
165
+ it = TestIter.new
166
+ it = bind(it, 'java.util.Iterator')
167
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
168
+ a = test.new
169
+ assert("43210", a.concat(it))
170
+ end
171
+
172
+ class TestComparator
173
+ def compare(o1, o2)
174
+ o1.to_i - o2.to_i
175
+ end
176
+ def equals(o)
177
+ o == self
178
+ end
179
+ end
180
+
181
+ def test_comparator
182
+ cp = TestComparator.new
183
+ cp = bind(cp, 'java.util.Comparator')
184
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
185
+ a = test.new
186
+ assert(0, a.check(cp, 123, 123))
187
+ assert(5, a.check(cp, 81, 76))
188
+ assert(-5, a.check(cp, 76, 81))
189
+ end
190
+
191
+ # assert_raise is useless in this test, because NumberFormatException may be defined in
192
+ # its block.
193
+ def test_exception()
194
+ it = import('java.lang.Integer')
195
+ begin
196
+ it.parseInt('blabla')
197
+ flunk('no exception')
198
+ rescue NumberFormatException => e
199
+ # OK
200
+ end
201
+ end
202
+
203
+ class TestIterator
204
+ def initialize(tp)
205
+ @type = tp
206
+ end
207
+ def hasNext()
208
+ true
209
+ end
210
+ def next()
211
+ if @type == 0
212
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
213
+ elsif @type == 1
214
+ Rjb::throw(Rjb::import('java.util.NoSuchElementException').new('instance test'))
215
+ end
216
+ end
217
+ end
218
+
219
+ def test_throw()
220
+ it = TestIterator.new(0)
221
+ it = bind(it, 'java.util.Iterator')
222
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
223
+ a = test.new
224
+ begin
225
+ a.concat(it)
226
+ flunk('no exception')
227
+ rescue NoSuchElementException => e
228
+ assert_equal('test exception', e.message)
229
+ end
230
+ end
231
+
232
+ def test_instance_throw()
233
+ it = TestIterator.new(1)
234
+ it = bind(it, 'java.util.Iterator')
235
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
236
+ a = test.new
237
+ begin
238
+ a.concat(it)
239
+ flunk('no exception')
240
+ rescue NoSuchElementException => e
241
+ assert_equal('instance test', e.message)
242
+ end
243
+ end
244
+
245
+ def test_null_string()
246
+ sys = import('java.lang.System')
247
+ begin
248
+ sys.getProperty(nil)
249
+ flunk('no exception')
250
+ rescue NullPointerException => e
251
+ assert(true)
252
+ rescue RuntimeError => e
253
+ flunk(e.message)
254
+ end
255
+ end
256
+
257
+ def test_throw_error()
258
+ begin
259
+ throw(self)
260
+ flunk('no exception')
261
+ rescue TypeError => e
262
+ end
263
+ begin
264
+ throw(import('java.lang.String').new('a'))
265
+ flunk('no exception')
266
+ rescue RuntimeError => e
267
+ assert_equal('arg1 must be a throwable', e.message)
268
+ end
269
+ begin
270
+ throw('java.lang.NoSuchException', 'test')
271
+ flunk('no excpetion')
272
+ rescue RuntimeError => e
273
+ assert_equal("`java.lang.NoSuchException' not found", e.message)
274
+ end
275
+ end
276
+
277
+ def test_field()
278
+ point = import('java.awt.Point')
279
+ pnt = point.new(11, 13)
280
+ assert_equal(11, pnt.x)
281
+ assert_equal(13, pnt.y)
282
+ pnt.x = 32
283
+ assert_equal(32, pnt.x)
284
+ end
285
+
286
+ def test_instancemethod_from_class()
287
+ begin
288
+ cls = import('java.lang.String')
289
+ assert_equal('true', cls.valueOf(true))
290
+ cls.length
291
+ flunk('no exception')
292
+ rescue RuntimeError => e
293
+ assert_equal('instance method `length\' for class', e.message)
294
+ end
295
+ end
296
+
297
+ def test_instancefield_from_class()
298
+ point = import('java.awt.Point')
299
+ begin
300
+ point.x
301
+ flunk('no exception')
302
+ rescue RuntimeError => e
303
+ assert_equal('instance field `x\' for class', e.message)
304
+ end
305
+ begin
306
+ point.x = 30
307
+ rescue RuntimeError => e
308
+ assert_equal('instance field `x\' for class', e.message)
309
+ end
310
+ end
311
+
312
+ def test_static_derived_method()
313
+ ext = import('jp.co.infoseek.hp.arton.rjb.ExtBase')
314
+ assert_equal("sVal", ext.getSVal)
315
+ end
316
+
317
+ def test_capitalized_method()
318
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
319
+ assert_equal("val", bs.val)
320
+ assert_equal("Val", bs.Val)
321
+ end
322
+
323
+ def test_underscored_constant()
324
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
325
+ assert_equal(5, bs._NUMBER_FIVE)
326
+ end
327
+
328
+ def test_passingclass()
329
+ ibs = import('jp.co.infoseek.hp.arton.rjb.IBase')
330
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
331
+ assert_equal('interface jp.co.infoseek.hp.arton.rjb.IBase', bs.intf(ibs))
332
+ end
333
+
334
+ def test_fornamehook()
335
+ # j2se class
336
+ cls = import('java.lang.Class')
337
+ c = cls.forName('java.lang.Class')
338
+ assert_equal(cls, c)
339
+ # user class
340
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
341
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.Base')
342
+ assert_equal(bs, b)
343
+ # class java's Class#forName and convert result to imported class
344
+ loader = Rjb::import('java.lang.ClassLoader')
345
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.Base', true, loader.getSystemClassLoader)
346
+ assert_equal(bs, b)
347
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.getSystemClassLoader)
348
+ assert(b.isInterface)
349
+ end
350
+
351
+ def test_send_array_of_arrays()
352
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
353
+ a = test.joinStringArray([['ab', 'cd'], ['ef', 'gh']])
354
+ assert_equal(['ab', 'cd', 'ef', 'gh'], a)
355
+ a = test.joinIntArray([[1, 2, 3], [4, 5, 6]])
356
+ a.collect! {|e| e.intValue }
357
+ assert_equal([1, 2, 3, 4, 5, 6], a)
358
+ r = [[[ 1, 2], [2, 3] ], [[ 3, 4], [5, 6]], [[7, 8], [1, 3]]]
359
+ a = test.throughIntArray(r)
360
+ assert_equal(a, r)
361
+ end
362
+
363
+ def test_import_and_instanciate()
364
+ b = import('jp.co.infoseek.hp.arton.rjb.Base')
365
+ assert_equal('hello', b.new.getInstanceVar())
366
+ end
367
+
368
+ def test_array_of_arrays()
369
+ jversion = import('java.lang.System').getProperty('java.version')
370
+ if /^1\.5/ =~ jversion
371
+ method = import('java.lang.reflect.Method')
372
+ end
373
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
374
+ a = test.getStringArrayOfArrays()
375
+ assert_equal("abc", a[0][0])
376
+ assert_equal("def", a[0][1])
377
+ assert_equal("123", a[1][0])
378
+ assert_equal("456", a[1][1])
379
+
380
+ ints = test.getIntArrayOfArrays()
381
+ assert_equal(2, ints.size )
382
+ assert_equal([1,2,3], ints[0] )
383
+ assert_equal([[1,2,3],[4,5,6]], ints )
384
+
385
+ sized = test.getSizedArray()
386
+ assert_equal("find me",sized[0][1][2][3])
387
+
388
+ mixed = test.getMixedArray()
389
+ assert_equal(12,mixed[0][0][0].intValue)
390
+ assert_equal("another string",mixed[1][0][1].toString)
391
+ assert_equal([],mixed[2])
392
+ end
393
+ end
394
+