rjb 1.1.0-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,142 @@
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 47 2007-12-28 06:39:43Z 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 RJB_RUBY_VERSION_CODE < 190
27
+ #if !defined(RHASH_TBL)
28
+ #define RHASH_TBL(x) RHASH((x))->tbl
29
+ #endif
30
+ #endif
31
+
32
+ #if !defined(COUNTOF)
33
+ #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
34
+ #endif
35
+
36
+ #if !defined(_I64_MIN)
37
+ #define _I64_MIN (-9223372036854775807i64 - 1)
38
+ #endif
39
+ #if !defined(_I64_MAX)
40
+ #define _I64_MAX 9223372036854775807i64
41
+ #endif
42
+
43
+
44
+ /* in load.c */
45
+ extern int rjb_create_jvm(JNIEnv** pjenv, JavaVMInitArgs*, char*, VALUE);
46
+
47
+ /* in rjb.c */
48
+ extern JavaVM* rjb_jvm;
49
+ extern jclass rjb_rbridge;
50
+ extern jmethodID rjb_register_bridge;
51
+ extern VALUE rjb_loaded_classes;
52
+ extern jmethodID rjb_class_getName;
53
+ extern jclass rjb_j_throwable;
54
+ extern jmethodID rjb_throwable_getMessage;
55
+ extern JNIEnv* rjb_attach_current_thread(void);
56
+ extern jclass rjb_find_class(JNIEnv* jenv, VALUE name);
57
+ extern void rjb_release_string(JNIEnv *jenv, jstring str, const char* chrs);
58
+
59
+ /* in rjbexception.c */
60
+ extern VALUE rjb_get_exception_class(JNIEnv* jenv, jstring str);
61
+ extern void rjb_check_exception(JNIEnv* jenv, int t);
62
+ extern VALUE rjb_s_throw(int, VALUE*, VALUE);
63
+
64
+ /* conversion functions */
65
+ typedef void (*R2J)(JNIEnv*, VALUE, jvalue*, const char*, int);
66
+ typedef VALUE (*J2R)(JNIEnv*, jvalue);
67
+ typedef jarray (*R2JARRAY)(JNIEnv*, VALUE, const char*);
68
+ typedef void (JNICALL *RELEASEARRAY)(JNIEnv*, jobject, void*, jint);
69
+ typedef jlong (JNICALL *INVOKEAL)(JNIEnv*, jobject, jmethodID, const jvalue*);
70
+ typedef jdouble (JNICALL *INVOKEAD)(JNIEnv*, jobject, jmethodID, const jvalue*);
71
+ typedef jfloat (JNICALL *INVOKEAF)(JNIEnv*, jobject, jmethodID, const jvalue*);
72
+ typedef jboolean (JNICALL *INVOKEAZ)(JNIEnv*, jobject, jmethodID, const jvalue*);
73
+ typedef jshort (JNICALL *INVOKEAS)(JNIEnv*, jobject, jmethodID, const jvalue*);
74
+ typedef jobject (JNICALL *INVOKEA)(JNIEnv*, jobject, jmethodID, const jvalue*);
75
+ typedef VALUE (*CONV)(JNIEnv*, void*);
76
+
77
+ /*
78
+ * internal method class
79
+ */
80
+ struct cls_constructor {
81
+ jmethodID id;
82
+ int arg_count;
83
+ R2J* arg_convert;
84
+ char* method_signature;
85
+ char result_signature;
86
+ char result_arraydepth;
87
+ };
88
+
89
+ struct cls_method {
90
+ struct cls_constructor basic;
91
+ ID name;
92
+ int static_method;
93
+ off_t method;
94
+ J2R result_convert;
95
+ /* overload only */
96
+ struct cls_method* next;
97
+ };
98
+
99
+ /*
100
+ * internal field class
101
+ */
102
+ struct cls_field {
103
+ ID name;
104
+ jfieldID id;
105
+ char* field_signature;
106
+ char result_signature;
107
+ char result_arraydepth;
108
+ R2J arg_convert;
109
+ J2R value_convert;
110
+ int readonly;
111
+ int static_field;
112
+ };
113
+
114
+ /*
115
+ * Object instance
116
+ */
117
+ struct jvi_data {
118
+ jclass klass; /* class */
119
+ jobject obj; /* instance */
120
+ st_table* methods;
121
+ st_table* fields;
122
+ };
123
+
124
+ /*
125
+ * Class instance
126
+ */
127
+ struct jv_data {
128
+ struct jvi_data idata;
129
+ st_table* static_methods;
130
+ struct cls_constructor** constructors;
131
+ };
132
+
133
+ /*
134
+ * Bridge instance
135
+ */
136
+ struct rj_bridge {
137
+ jobject bridge;
138
+ jobject proxy;
139
+ VALUE wrapped;
140
+ };
141
+
142
+ #endif
@@ -0,0 +1,135 @@
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 47 2007-12-28 06:39:43Z arton $
16
+ */
17
+
18
+ #include "ruby.h"
19
+ #include "extconf.h"
20
+ #if RJB_RUBY_VERSION_CODE < 190
21
+ #include "st.h"
22
+ #else
23
+ #include "ruby/st.h"
24
+ #endif
25
+ #include "jniwrap.h"
26
+ #include "riconv.h"
27
+ #include "rjb.h"
28
+
29
+ /*
30
+ * handle Java exception
31
+ * At this time, the Java exception is defined without the package name.
32
+ * This design may change in future release.
33
+ */
34
+ VALUE rjb_get_exception_class(JNIEnv* jenv, jstring str)
35
+ {
36
+ VALUE rexp;
37
+ char* pcls;
38
+ VALUE cname;
39
+ const char* p = (*jenv)->GetStringUTFChars(jenv, str, JNI_FALSE);
40
+ char* clsname = ALLOCA_N(char, strlen(p) + 1);
41
+ strcpy(clsname, p);
42
+ rjb_release_string(jenv, str, p);
43
+ pcls = strrchr(clsname, '.');
44
+ if (pcls)
45
+ {
46
+ pcls++;
47
+ }
48
+ else
49
+ {
50
+ pcls = clsname;
51
+ }
52
+ cname = rb_str_new2(pcls);
53
+ rexp = rb_hash_aref(rjb_loaded_classes, cname);
54
+ if (rexp == Qnil)
55
+ {
56
+ rexp = rb_define_class(pcls, rb_eStandardError);
57
+ st_insert(RHASH_TBL(rjb_loaded_classes), cname, rexp);
58
+ }
59
+ return rexp;
60
+ }
61
+
62
+ /*
63
+ * throw newly created exception with supplied message.
64
+ */
65
+ VALUE rjb_s_throw(int argc, VALUE* argv, VALUE self)
66
+ {
67
+ VALUE klass;
68
+ VALUE message;
69
+ JNIEnv* jenv = rjb_attach_current_thread();
70
+ if (rb_scan_args(argc, argv, "11", &klass, &message) == 2)
71
+ {
72
+ jclass excep = rjb_find_class(jenv, klass);
73
+ if (excep == NULL)
74
+ {
75
+ rb_raise(rb_eRuntimeError, "`%s' not found", StringValueCStr(klass));
76
+ }
77
+ (*jenv)->ThrowNew(jenv, excep, StringValueCStr(message));
78
+ }
79
+ else
80
+ {
81
+ struct jvi_data* ptr;
82
+ Data_Get_Struct(klass, struct jvi_data, ptr);
83
+ if (!(*jenv)->IsInstanceOf(jenv, ptr->obj, rjb_j_throwable))
84
+ {
85
+ rb_raise(rb_eRuntimeError, "arg1 must be a throwable");
86
+ }
87
+ else
88
+ {
89
+ (*jenv)->Throw(jenv, ptr->obj);
90
+ }
91
+ }
92
+ return Qnil;
93
+ }
94
+
95
+ void rjb_check_exception(JNIEnv* jenv, int t)
96
+ {
97
+ jthrowable exp = (*jenv)->ExceptionOccurred(jenv);
98
+ if (exp)
99
+ {
100
+ VALUE rexp = Qnil;
101
+ if (RTEST(ruby_verbose))
102
+ {
103
+ (*jenv)->ExceptionDescribe(jenv);
104
+ }
105
+ (*jenv)->ExceptionClear(jenv);
106
+ // if (t)
107
+ if(1)
108
+ {
109
+ char* msg = "unknown exception";
110
+ jclass cls = (*jenv)->GetObjectClass(jenv, exp);
111
+ jstring str = (*jenv)->CallObjectMethod(jenv, exp, rjb_throwable_getMessage);
112
+ if (str)
113
+ {
114
+ const char* p = (*jenv)->GetStringUTFChars(jenv, str, JNI_FALSE);
115
+ msg = ALLOCA_N(char, strlen(p) + 1);
116
+ strcpy(msg, p);
117
+ rjb_release_string(jenv, str, p);
118
+ }
119
+ str = (*jenv)->CallObjectMethod(jenv, cls, rjb_class_getName);
120
+ if (str)
121
+ {
122
+ rexp = rjb_get_exception_class(jenv, str);
123
+ }
124
+ if (rexp == Qnil)
125
+ {
126
+ rb_raise(rb_eRuntimeError, "%s", msg);
127
+ }
128
+ else
129
+ {
130
+ rb_raise(rexp, msg);
131
+ }
132
+ }
133
+ }
134
+ }
135
+
@@ -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,570 @@
1
+ #!/usr/local/env ruby
2
+ # $Id: test.rb 45 2007-12-26 20:03:21Z kuwa1 $
3
+
4
+ require 'test/unit'
5
+ require 'rjb'
6
+
7
+ puts "start RJB(#{Rjb::VERSION}) test"
8
+ class TestRjb < Test::Unit::TestCase
9
+ include Rjb
10
+ def setup
11
+ load('.')
12
+ Rjb::primitive_conversion = false
13
+
14
+ @jString = import('java.lang.String')
15
+ @jInteger = Rjb::import('java.lang.Integer')
16
+ @jShort = Rjb::import('java.lang.Short')
17
+ @jDouble = Rjb::import('java.lang.Double')
18
+ @jFloat = Rjb::import('java.lang.Float')
19
+ @jBoolean = Rjb::import('java.lang.Boolean')
20
+ @jByte = Rjb::import('java.lang.Byte')
21
+ @jLong = Rjb::import('java.lang.Long')
22
+ @jChar = Rjb::import('java.lang.Character')
23
+ end
24
+
25
+ def tearDown
26
+ unload
27
+ end
28
+
29
+ def test_metaclass
30
+ cls = import('java.lang.Class')
31
+ assert_equal('java.lang.Class', cls._classname)
32
+ assert_equal('java.lang.Class', cls.getName)
33
+ assert_equal(17, cls.getModifiers)
34
+ end
35
+
36
+ def test_scalar
37
+ assert_equal('java.lang.Class', @jString._classname)
38
+ assert_equal('class java.lang.String', @jString.toString)
39
+ str = @jString.new
40
+ assert_equal('java.lang.String', str._classname)
41
+ assert_equal(0, str.length)
42
+ assert_equal('', str.toString)
43
+ str = @jString.new_with_sig('Ljava.lang.String;', "abcde")
44
+ # result integer
45
+ assert_equal(5, str.length)
46
+ # result string
47
+ assert_equal('abcde', str.toString)
48
+ # argument test
49
+ # char
50
+ assert_equal('abxde', str.replace("c".sum, "x".sum))
51
+ # string
52
+ assert_equal('abdxe', str.replaceAll('cd', 'dx'))
53
+ # int
54
+ assert_equal('bc', str.substring(1, 3))
55
+ assert_equal('e', str.substring(4))
56
+ # float with static
57
+ assert_equal('5.23', @jString._invoke('valueOf', 'F', 5.23))
58
+ assert_equal('25.233', @jString._invoke('valueOf', 'D', 25.233))
59
+ # rjb object (String)
60
+ str2 = @jString.new_with_sig('Ljava.lang.String;', 'fghijk')
61
+ assert_equal('abcdefghijk', str.concat(str2))
62
+ # rjb object other (implicit toString call is Rjb feature)
63
+ i = @jInteger.new_with_sig('I', 35901)
64
+ assert_equal('abcde35901', str.concat(i))
65
+ # result boolean and argument is rjb object
66
+ assert_equal(false, i.equals(str))
67
+ assert_equal(false, str.equals(i))
68
+ assert_equal(true, str.equals("abcde"))
69
+ assert_equal(true, str.equals(str))
70
+ # long
71
+ l = @jLong.new_with_sig('J', -9223372036854775808)
72
+ assert_equal(-9223372036854775808, l.longValue)
73
+ l = @jLong.new_with_sig('J', 9223372036854775807)
74
+ assert_equal(9223372036854775807, l.longValue)
75
+ # double
76
+ d = @jDouble.new_with_sig('D', 1234.5678901234567890)
77
+ assert_equal(1234.5678901234567890, d.doubleValue)
78
+ # byte
79
+ b = @jByte.new_with_sig('B', 13)
80
+ assert_equal(13, b.byteValue)
81
+ # float
82
+ f = @jFloat.new_with_sig('F', 13.5)
83
+ assert_equal(13.5, f.floatValue)
84
+ # short
85
+ s = @jShort.new_with_sig('S', 1532)
86
+ assert_equal(1532, s.shortValue)
87
+ c = @jChar.new_with_sig('C', "A".sum)
88
+ assert_equal("A".sum, c.charValue)
89
+ end
90
+
91
+ def test_array
92
+ str = @jString.new_with_sig('[C', ["a".sum, "b".sum, "c".sum, "d".sum, "e".sum, "c".sum, "f".sum, "c".sum, "g".sum])
93
+ assert_equal('abcdecfcg', str.toString)
94
+ # conv string array
95
+ splt = str.split('c')
96
+ assert(Array === splt)
97
+ assert_equal(4, splt.size)
98
+ assert_equal('ab', splt[0])
99
+ assert_equal('g', splt[3])
100
+ # conv byte array to (ruby)string
101
+ ba = str.getBytes
102
+ assert_equal('abcdecfcg', ba)
103
+ # conv char array to array(int)
104
+ ca = str.toCharArray
105
+ assert_equal(["a".sum, "b".sum, "c".sum, "d".sum, "e".sum, "c".sum, "f".sum, "c".sum, "g".sum], ca)
106
+ end
107
+
108
+ def test_importobj()
109
+ sys = import('java.lang.System')
110
+ props = sys.getProperties
111
+ assert_equal('java.util.Properties', props._classname)
112
+ if /cygwin/ =~ RUBY_PLATFORM # patch for dirty environment
113
+ assert_equal(Dir::pwd, %x[cygpath -u #{props.getProperty('user.dir').gsub('\\', '/')}].chop)
114
+ else
115
+ assert_equal(Dir::pwd, props.getProperty('user.dir').gsub('\\', '/'))
116
+ end
117
+ assert_equal(@jBoolean.valueOf(true).booleanValue(), true)
118
+ assert_equal(@jBoolean.valueOf(false).booleanValue(), false)
119
+ assert_equal(@jBoolean.valueOf('true').booleanValue(), true)
120
+ assert_equal(@jBoolean.valueOf('false').booleanValue(), false)
121
+ end
122
+
123
+ def test_importobjarray()
124
+ jarray = import('java.util.ArrayList')
125
+ a = jarray.new()
126
+ a.add(@jInteger.new_with_sig('I', 1))
127
+ a.add(@jInteger.new_with_sig('I', 2))
128
+ a.add(@jInteger.new_with_sig('I', 3))
129
+ oa = a.toArray
130
+ assert_equal(3, oa.size)
131
+ assert_equal(1, oa[0].intValue)
132
+ assert_equal(2, oa[1].intValue)
133
+ assert_equal(3, oa[2].intValue)
134
+ end
135
+
136
+ def test_kjconv()
137
+ if Object::const_defined?(:Encoding)
138
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8".force_encoding "euc-jp"
139
+ s = @jString.new(euc_kj)
140
+ assert_equal(s.toString().encoding, Encoding::UTF_8)
141
+ assert_equal(s.toString().encode("euc-jp"), euc_kj)
142
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67".force_encoding "shift_jis"
143
+ s = @jString.new(sjis_kj)
144
+ assert_equal(s.toString().encode("shift_jis"), sjis_kj)
145
+ utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88".force_encoding "utf-8"
146
+ s = @jString.new(utf8_kj.encode("utf-8"))
147
+ assert_equal(s.toString(), utf8_kj)
148
+ if /mswin(?!ce)|mingw|cygwin|bccwin/ =~ RUBY_PLATFORM
149
+ #expecting shift_jis on windows
150
+ none_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67" #.force_encoding "shift_jis"
151
+ s = @jString.new(none_kj)
152
+ assert_equal(s.toString().encode("shift_jis"), none_kj)
153
+ else
154
+ #expecting utf-8 unless windows
155
+ none_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88".force_encoding "utf-8"
156
+ s = @jString.new(none_kj)
157
+ assert_equal(s.toString(), none_kj)
158
+ end
159
+ else
160
+ $KCODE = 'euc'
161
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
162
+ s = @jString.new(euc_kj)
163
+ assert_equal(s.toString(), euc_kj)
164
+ $KCODE = 'sjis'
165
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
166
+ s = @jString.new(sjis_kj)
167
+ assert_equal(s.toString(), sjis_kj)
168
+ $KCODE = 'utf8'
169
+ utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
170
+ s = @jString.new(utf8_kj)
171
+ assert_equal(s.toString(), utf8_kj)
172
+ $KCODE = 'none'
173
+ if /mswin(?!ce)|mingw|cygwin|bccwin/ =~ RUBY_PLATFORM
174
+ #expecting shift_jis on windows
175
+ none_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
176
+ else
177
+ #expecting utf-8 unless windows
178
+ none_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
179
+ end
180
+ s = @jString.new(none_kj)
181
+ assert_equal(s.toString(), none_kj)
182
+ $KCODE = 'utf8'
183
+ utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
184
+ s = @jString.new(utf8_kj)
185
+ assert_equal(s.toString(), utf8_kj)
186
+ $KCODE = 'sjis'
187
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
188
+ s = @jString.new(sjis_kj)
189
+ assert_equal(s.toString(), sjis_kj)
190
+ $KCODE = 'euc'
191
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
192
+ s = @jString.new(euc_kj)
193
+ assert_equal(s.toString(), euc_kj)
194
+ end
195
+ end
196
+
197
+ def test_constants()
198
+ assert_equal(0x7fffffffffffffff, @jLong.MAX_VALUE)
199
+ assert_equal(-9223372036854775808, @jLong.MIN_VALUE)
200
+ end
201
+
202
+ class TestIter
203
+ def initialize()
204
+ @i = 5
205
+ end
206
+ def hasNext()
207
+ @i > 0
208
+ end
209
+ def next()
210
+ @i -= 1
211
+ @i.to_s
212
+ end
213
+ end
214
+
215
+ def test_newobject()
216
+ it = TestIter.new
217
+ it = bind(it, 'java.util.Iterator')
218
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
219
+ a = test.new
220
+ assert("43210", a.concat(it))
221
+ end
222
+
223
+ class TestComparator
224
+ def compare(o1, o2)
225
+ o1.to_i - o2.to_i
226
+ end
227
+ def equals(o)
228
+ o == self
229
+ end
230
+ end
231
+
232
+ def test_comparator
233
+ cp = TestComparator.new
234
+ cp = bind(cp, 'java.util.Comparator')
235
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
236
+ a = test.new
237
+ assert(0, a.check(cp, 123, 123))
238
+ assert(5, a.check(cp, 81, 76))
239
+ assert(-5, a.check(cp, 76, 81))
240
+ end
241
+
242
+ # assert_raise is useless in this test, because NumberFormatException may be defined in
243
+ # its block.
244
+ def test_exception()
245
+ begin
246
+ @jInteger.parseInt('blabla')
247
+ flunk('no exception')
248
+ rescue NumberFormatException => e
249
+ # OK
250
+ end
251
+ end
252
+
253
+ class TestIterator
254
+ def initialize(tp)
255
+ @type = tp
256
+ end
257
+ def hasNext()
258
+ true
259
+ end
260
+ def next()
261
+ if @type == 0
262
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
263
+ elsif @type == 1
264
+ Rjb::throw(Rjb::import('java.util.NoSuchElementException').new('instance test'))
265
+ end
266
+ end
267
+ end
268
+
269
+ def test_throw()
270
+ it = TestIterator.new(0)
271
+ it = bind(it, 'java.util.Iterator')
272
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
273
+ a = test.new
274
+ begin
275
+ a.concat(it)
276
+ flunk('no exception')
277
+ rescue NoSuchElementException => e
278
+ assert_equal('test exception', e.message)
279
+ end
280
+ end
281
+
282
+ def test_instance_throw()
283
+ it = TestIterator.new(1)
284
+ it = bind(it, 'java.util.Iterator')
285
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
286
+ a = test.new
287
+ begin
288
+ a.concat(it)
289
+ flunk('no exception')
290
+ rescue NoSuchElementException => e
291
+ assert_equal('instance test', e.message)
292
+ end
293
+ end
294
+
295
+ def test_null_string()
296
+ sys = import('java.lang.System')
297
+ begin
298
+ sys.getProperty(nil)
299
+ flunk('no exception')
300
+ rescue NullPointerException => e
301
+ assert(true)
302
+ rescue RuntimeError => e
303
+ flunk(e.message)
304
+ end
305
+ end
306
+
307
+ def test_throw_error()
308
+ begin
309
+ throw(self)
310
+ flunk('no exception')
311
+ rescue TypeError => e
312
+ end
313
+ begin
314
+ throw(@jString.new('a'))
315
+ flunk('no exception')
316
+ rescue RuntimeError => e
317
+ assert_equal('arg1 must be a throwable', e.message)
318
+ end
319
+ begin
320
+ throw('java.lang.NoSuchException', 'test')
321
+ flunk('no excpetion')
322
+ rescue RuntimeError => e
323
+ assert_equal("`java.lang.NoSuchException' not found", e.message)
324
+ end
325
+ end
326
+
327
+ def test_field()
328
+ point = import('java.awt.Point')
329
+ pnt = point.new(11, 13)
330
+ assert_equal(11, pnt.x)
331
+ assert_equal(13, pnt.y)
332
+ pnt.x = 32
333
+ assert_equal(32, pnt.x)
334
+ end
335
+
336
+ def test_instancemethod_from_class()
337
+ begin
338
+ assert_equal('true', @jString.valueOf(true))
339
+ @jString.length
340
+ flunk('no exception')
341
+ rescue RuntimeError => e
342
+ assert_equal('instance method `length\' for class', e.message)
343
+ end
344
+ end
345
+
346
+ def test_instancefield_from_class()
347
+ point = import('java.awt.Point')
348
+ begin
349
+ point.x
350
+ flunk('no exception')
351
+ rescue RuntimeError => e
352
+ assert_equal('instance field `x\' for class', e.message)
353
+ end
354
+ begin
355
+ point.x = 30
356
+ rescue RuntimeError => e
357
+ assert_equal('instance field `x\' for class', e.message)
358
+ end
359
+ end
360
+
361
+ def test_static_derived_method()
362
+ ext = import('jp.co.infoseek.hp.arton.rjb.ExtBase')
363
+ assert_equal("sVal", ext.getSVal)
364
+ end
365
+
366
+ def test_capitalized_method()
367
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
368
+ assert_equal("val", bs.val)
369
+ assert_equal("Val", bs.Val)
370
+ end
371
+
372
+ def test_underscored_constant()
373
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
374
+ assert_equal(5, bs._NUMBER_FIVE)
375
+ end
376
+
377
+ def test_passingclass()
378
+ ibs = import('jp.co.infoseek.hp.arton.rjb.IBase')
379
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
380
+ assert_equal('interface jp.co.infoseek.hp.arton.rjb.IBase', bs.intf(ibs))
381
+ end
382
+
383
+ def test_fornamehook()
384
+ # j2se class
385
+ cls = import('java.lang.Class')
386
+ c = cls.forName('java.lang.Class')
387
+ assert_equal(cls, c)
388
+ # user class
389
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
390
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.Base')
391
+ assert_equal(bs, b)
392
+ # class java's Class#forName and convert result to imported class
393
+ loader = Rjb::import('java.lang.ClassLoader')
394
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.Base', true, loader.getSystemClassLoader)
395
+ assert_equal(bs, b)
396
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.getSystemClassLoader)
397
+ assert(b.isInterface)
398
+ end
399
+
400
+ def test_send_array_of_arrays()
401
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
402
+ a = test.joinStringArray([['ab', 'cd'], ['ef', 'gh']])
403
+ assert_equal(['ab', 'cd', 'ef', 'gh'], a)
404
+ a = test.joinIntArray([[1, 2, 3], [4, 5, 6]])
405
+ a.collect! {|e| e.intValue }
406
+ assert_equal([1, 2, 3, 4, 5, 6], a)
407
+ r = [[[ 1, 2], [2, 3] ], [[ 3, 4], [5, 6]], [[7, 8], [1, 3]]]
408
+ a = test.throughIntArray(r)
409
+ assert_equal(a, r)
410
+ end
411
+
412
+ def test_import_and_instanciate()
413
+ b = import('jp.co.infoseek.hp.arton.rjb.Base')
414
+ assert_equal('hello', b.new.getInstanceVar())
415
+ end
416
+
417
+ def test_array_of_arrays()
418
+ jversion = import('java.lang.System').getProperty('java.version')
419
+ if /^1\.5/ =~ jversion
420
+ method = import('java.lang.reflect.Method')
421
+ end
422
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
423
+ a = test.getStringArrayOfArrays()
424
+ assert_equal("abc", a[0][0])
425
+ assert_equal("def", a[0][1])
426
+ assert_equal("123", a[1][0])
427
+ assert_equal("456", a[1][1])
428
+
429
+ ints = test.getIntArrayOfArrays()
430
+ assert_equal(2, ints.size )
431
+ assert_equal([1,2,3], ints[0] )
432
+ assert_equal([[1,2,3],[4,5,6]], ints )
433
+
434
+ sized = test.getSizedArray()
435
+ assert_equal("find me",sized[0][1][2][3])
436
+
437
+ mixed = test.getMixedArray()
438
+ assert_equal(12,mixed[0][0][0].intValue)
439
+ assert_equal("another string",mixed[1][0][1].toString)
440
+ assert_equal([],mixed[2])
441
+ end
442
+
443
+ def test_CastObjectArray()
444
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
445
+ a = test.getObjectArray()
446
+ assert_equal(1, a[0].intValue)
447
+ assert_equal('Hello World !', a[1].toString)
448
+ a = test.getObjectArrayOfArray()
449
+ assert_equal(1, a[0][0].intValue)
450
+ assert_equal('Hello World !', a[0][1].toString)
451
+ assert_equal(2, a[1][0].intValue)
452
+ assert_equal('Hello World !!', a[1][1].toString)
453
+ end
454
+
455
+ def test_CallByNullForArraies()
456
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
457
+ assert_equal(nil, test.callWithArraies(nil, nil, nil, nil, nil, nil,
458
+ nil, nil))
459
+ end
460
+
461
+ def test_failed_constructor_call()
462
+ begin
463
+ s = @jString.new('a', 'b', 'c')
464
+ flunk('no exception')
465
+ rescue RuntimeError => e
466
+ assert(e)
467
+ end
468
+ end
469
+
470
+ def test_rubyize
471
+ loader = Rjb::import('java.lang.ClassLoader')
472
+ cls = import('java.lang.Class')
473
+ b = cls.for_name('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.system_class_loader)
474
+ assert(b.interface?)
475
+ stringbuffer = Rjb::import('java.lang.StringBuffer')
476
+ sb = stringbuffer.new('abc')
477
+ assert_equal(1, sb.index_of('bc'))
478
+ sb.set_char_at(1, "B".sum)
479
+ assert_equal('aBc', sb.to_string)
480
+ sb.length = 2
481
+ assert_equal('aB', sb.to_string)
482
+ end
483
+
484
+ def test_auto_conv
485
+ assert_equal(false, Rjb::primitive_conversion)
486
+ Rjb::primitive_conversion = true
487
+ assert_equal(true, Rjb::primitive_conversion)
488
+ assert_equal(1, @jInteger.valueOf('1'))
489
+ assert_equal(-1, @jInteger.valueOf('-1'))
490
+ assert_equal(2, @jShort.valueOf('2'))
491
+ assert_equal(-2, @jShort.valueOf('-2'))
492
+ assert_equal(3.1, @jDouble.valueOf('3.1'))
493
+ assert_equal(4.5, @jFloat.valueOf('4.5'))
494
+ assert(@jBoolean.TRUE)
495
+ assert_equal(5, @jByte.valueOf('5'))
496
+ assert_equal(-6, @jByte.valueOf('-6'))
497
+ assert_equal(0x7000000000000000, @jLong.valueOf('8070450532247928832'))
498
+ assert_equal(-9223372036854775807, @jLong.valueOf('-9223372036854775807'))
499
+ assert_equal("A".sum, @jChar.valueOf("A".sum))
500
+ end
501
+
502
+ def test_obj_to_primitive
503
+ ar = Rjb::import('java.util.ArrayList')
504
+ a = ar.new
505
+ a.add @jString.new('abcdef')
506
+ a.add @jInteger.valueOf('1')
507
+ a.add @jShort.valueOf('2')
508
+ a.add @jDouble.valueOf('3.1')
509
+ a.add @jFloat.valueOf('4.5')
510
+ a.add @jBoolean.TRUE
511
+ a.add @jByte.valueOf('5')
512
+ a.add @jLong.valueOf('8070450532247928832')
513
+ a.add @jChar.valueOf("A".sum)
514
+
515
+ Rjb::primitive_conversion = true
516
+
517
+ assert_equal 'abcdef', a.get(0)
518
+ assert_equal 1, a.get(1)
519
+ assert_equal 2, a.get(2)
520
+ assert_equal 3.1, a.get(3)
521
+ assert_equal 4.5, a.get(4)
522
+ assert a.get(5)
523
+ assert_equal 5, a.get(6)
524
+ assert_equal 8070450532247928832, a.get(7)
525
+ assert_equal "A".sum, a.get(8)
526
+ end
527
+
528
+ def test_primitive_to_obj
529
+ Rjb::primitive_conversion = true
530
+
531
+ ar = Rjb::import('java.util.ArrayList')
532
+ a = ar.new
533
+ a.add @jString.new('abcdef')
534
+ a.add @jInteger.valueOf('1')
535
+ a.add @jShort.valueOf('2')
536
+ a.add @jDouble.valueOf('3.1')
537
+ a.add @jFloat.valueOf('4.5')
538
+ a.add @jBoolean.TRUE
539
+ a.add @jByte.valueOf('5')
540
+ a.add @jLong.valueOf('8070450532247928832')
541
+ a.add @jChar.valueOf("A".sum)
542
+ assert_equal 'abcdef', a.get(0)
543
+ assert_equal 1, a.get(1)
544
+ assert_equal 2, a.get(2)
545
+ assert_equal 3.1, a.get(3)
546
+ assert_equal 4.5, a.get(4)
547
+ assert a.get(5)
548
+ assert_equal 5, a.get(6)
549
+ assert_equal 8070450532247928832, a.get(7)
550
+ assert_equal "A".sum, a.get(8)
551
+ end
552
+
553
+ def test_enum
554
+ t = Rjb::import('jp.co.infoseek.hp.arton.rjb.Test$TestTypes')
555
+ assert t.ONE.equals(t.values()[0])
556
+ assert_equal 3, t.values().size
557
+ assert_equal 2, t.THREE.ordinal
558
+ assert_equal "TWO", t.TWO.name
559
+ assert_equal "THREE", t.THREE.toString
560
+ end
561
+
562
+ #rjb-bugs-15430 rebported by Bryan Duxbury
563
+ def test_generics_map
564
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
565
+ map = test.sorted_map
566
+ assert_equal "\0\x1\x2\x3\x4", map.get('abc')
567
+ assert_equal "\x5\x6\x7\x8\x9", map.get('def')
568
+ end
569
+ end
570
+