rjb 1.3.0-universal-darwin-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/readme.sj ADDED
@@ -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
+
data/readme.txt ADDED
@@ -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,35 @@
1
+ #!/usr/local/bin/ruby -Ks
2
+ #coding: cp932
3
+
4
+ require 'rjb'
5
+
6
+ Rjb::load
7
+
8
+ unless RUBY_VERSION =~ /^1\.9/
9
+ class String
10
+ def encode(s)
11
+ self
12
+ end
13
+ end
14
+ end
15
+
16
+ class FileChooser
17
+ @@klass = Rjb::import('javax.swing.JFileChooser')
18
+ def initialize(ext = '*', desc = 'any files')
19
+ @selected = nil
20
+ end
21
+
22
+ def show()
23
+ chooser = @@klass.new
24
+ if chooser.showOpenDialog(nil) == @@klass.APPROVE_OPTION
25
+ @selected = chooser.getSelectedFile
26
+ end
27
+ end
28
+ attr_reader :selected
29
+ end
30
+
31
+ f = FileChooser.new
32
+ if f.show
33
+ puts f.selected.getAbsolutePath.encode('cp932')
34
+ end
35
+ puts 'bye'
data/test/exttest.rb ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/local/env ruby -Ku
2
+ # encoding: utf-8
3
+ # $Id:$
4
+
5
+ begin
6
+ require 'rjb'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'rjb'
10
+ end
11
+
12
+ if Rjb::VERSION < '1.2.2'
13
+ puts "Rjb #{Rjb::VERSION} does not support rjbextension. exit"
14
+ exit 0
15
+ end
16
+
17
+ require 'rjbextension'
18
+ require 'test/unit'
19
+ require 'fileutils'
20
+
21
+ FileUtils.rm_f 'jp/co/infoseek/hp/arton/rjb/Base.class'
22
+
23
+ puts "start RJB(#{Rjb::VERSION}) test"
24
+ class ExtTestRjb < Test::Unit::TestCase
25
+
26
+ def jp
27
+ JavaPackage.new('jp')
28
+ end
29
+
30
+ def test_require_extension
31
+ assert !Rjb::loaded?
32
+ $LOAD_PATH << '.'
33
+ require 'rjbtest.jar'
34
+ Rjb::load
35
+ assert Rjb::loaded?
36
+ base = jp.co.infoseek.hp.arton.rjb.Base.new
37
+ assert_equal('hello', base.instance_var)
38
+ end
39
+ end
data/test/gctest.rb ADDED
@@ -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
data/test/jartest.jar ADDED
Binary file
data/test/rjbtest.jar ADDED
Binary file
data/test/test.rb ADDED
@@ -0,0 +1,754 @@
1
+ #!/usr/local/env ruby -Ku
2
+ # encoding: utf-8
3
+ # $Id: test.rb 147 2010-10-23 05:10:33Z arton $
4
+
5
+ begin
6
+ require 'rjb'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'rjb'
10
+ end
11
+ require 'test/unit'
12
+ require 'fileutils'
13
+
14
+ FileUtils.rm_f 'jp/co/infoseek/hp/arton/rjb/Base.class'
15
+ FileUtils.rm_f 'jp/co/infoseek/hp/arton/rjb/ExtBase.class'
16
+
17
+ puts "start RJB(#{Rjb::VERSION}) test"
18
+ class TestRjb < Test::Unit::TestCase
19
+ include Rjb
20
+ def setup
21
+ Rjb::load('.')
22
+ Rjb::add_jar(File.expand_path('rjbtest.jar'))
23
+ Rjb::primitive_conversion = false
24
+
25
+ @jString = import('java.lang.String')
26
+ @jInteger = import('java.lang.Integer')
27
+ @jShort = import('java.lang.Short')
28
+ @jDouble = import('java.lang.Double')
29
+ @jFloat = import('java.lang.Float')
30
+ @jBoolean = import('java.lang.Boolean')
31
+ @jByte = import('java.lang.Byte')
32
+ @jLong = import('java.lang.Long')
33
+ @jChar = import('java.lang.Character')
34
+ end
35
+
36
+ def teardown
37
+ end
38
+
39
+ def test_metaclass
40
+ cls = import('java.lang.Class')
41
+ assert_equal('java.lang.Class', cls._classname)
42
+ assert_equal('java.lang.Class', cls.getName)
43
+ assert_equal(17, cls.getModifiers)
44
+ end
45
+
46
+ def test_scalar
47
+ assert_equal('java.lang.Class', @jString._classname)
48
+ assert_equal('class java.lang.String', @jString.toString)
49
+ str = @jString.new
50
+ assert_equal('java.lang.String', str._classname)
51
+ assert_equal(0, str.length)
52
+ assert_equal('', str.toString)
53
+ str = @jString.new_with_sig('Ljava.lang.String;', "abcde")
54
+ # result integer
55
+ assert_equal(5, str.length)
56
+ # result string
57
+ assert_equal('abcde', str.toString)
58
+ # argument test
59
+ # char
60
+ assert_equal('abxde', str.replace("c".sum, "x".sum))
61
+ # string
62
+ assert_equal('abdxe', str.replaceAll('cd', 'dx'))
63
+ # int
64
+ assert_equal('bc', str.substring(1, 3))
65
+ assert_equal('e', str.substring(4))
66
+ # float with static
67
+ assert_equal('5.23', @jString._invoke('valueOf', 'F', 5.23))
68
+ assert_equal('25.233', @jString._invoke('valueOf', 'D', 25.233))
69
+ # rjb object (String)
70
+ str2 = @jString.new_with_sig('Ljava.lang.String;', 'fghijk')
71
+ assert_equal('abcdefghijk', str.concat(str2))
72
+ # rjb object other (implicit toString call is Rjb feature)
73
+ i = @jInteger.new_with_sig('I', 35901)
74
+ assert_equal('abcde35901', str.concat(i))
75
+ # result boolean and argument is rjb object
76
+ assert_equal(false, i.equals(str))
77
+ assert_equal(false, str.equals(i))
78
+ assert_equal(true, str.equals("abcde"))
79
+ assert_equal(true, str.equals(str))
80
+ # long
81
+ l = @jLong.new_with_sig('J', -9223372036854775808)
82
+ assert_equal(-9223372036854775808, l.longValue)
83
+ l = @jLong.new_with_sig('J', 9223372036854775807)
84
+ assert_equal(9223372036854775807, l.longValue)
85
+ # double
86
+ d = @jDouble.new_with_sig('D', 1234.5678901234567890)
87
+ assert_equal(1234.5678901234567890, d.doubleValue)
88
+ # byte
89
+ b = @jByte.new_with_sig('B', 13)
90
+ assert_equal(13, b.byteValue)
91
+ # float
92
+ f = @jFloat.new_with_sig('F', 13.5)
93
+ assert_equal(13.5, f.floatValue)
94
+ # short
95
+ s = @jShort.new_with_sig('S', 1532)
96
+ assert_equal(1532, s.shortValue)
97
+ c = @jChar.new_with_sig('C', "A".sum)
98
+ assert_equal("A".sum, c.charValue)
99
+ end
100
+
101
+ def test_array
102
+ str = @jString.new_with_sig('[C', ["a".sum, "b".sum, "c".sum, "d".sum, "e".sum, "c".sum, "f".sum, "c".sum, "g".sum])
103
+ assert_equal('abcdecfcg', str.toString)
104
+ # conv string array
105
+ splt = str.split('c')
106
+ assert(Array === splt)
107
+ assert_equal(4, splt.size)
108
+ assert_equal('ab', splt[0])
109
+ assert_equal('g', splt[3])
110
+ # conv byte array to (ruby)string
111
+ ba = str.getBytes
112
+ assert_equal('abcdecfcg', ba)
113
+ # conv char array to array(int)
114
+ ca = str.toCharArray
115
+ assert_equal(["a".sum, "b".sum, "c".sum, "d".sum, "e".sum, "c".sum, "f".sum, "c".sum, "g".sum], ca)
116
+ end
117
+
118
+ def test_importobj()
119
+ sys = import('java.lang.System')
120
+ props = sys.getProperties
121
+ assert_equal('java.util.Properties', props._classname)
122
+ if /cygwin/ =~ RUBY_PLATFORM # patch for dirty environment
123
+ assert_equal(Dir::pwd, %x[cygpath -u #{props.getProperty('user.dir').gsub('\\', '/')}].chop)
124
+ else
125
+ assert_equal(Dir::pwd, props.getProperty('user.dir').gsub('\\', '/'))
126
+ end
127
+ assert_equal(@jBoolean.valueOf(true).booleanValue(), true)
128
+ assert_equal(@jBoolean.valueOf(false).booleanValue(), false)
129
+ assert_equal(@jBoolean.valueOf('true').booleanValue(), true)
130
+ assert_equal(@jBoolean.valueOf('false').booleanValue(), false)
131
+ end
132
+
133
+ def test_importobjarray()
134
+ jarray = import('java.util.ArrayList')
135
+ a = jarray.new()
136
+ a.add(@jInteger.new_with_sig('I', 1))
137
+ a.add(@jInteger.new_with_sig('I', 2))
138
+ a.add(@jInteger.new_with_sig('I', 3))
139
+ oa = a.toArray
140
+ assert_equal(3, oa.size)
141
+ assert_equal(1, oa[0].intValue)
142
+ assert_equal(2, oa[1].intValue)
143
+ assert_equal(3, oa[2].intValue)
144
+ end
145
+
146
+ def test_kjconv()
147
+ if Object::const_defined?(:Encoding)
148
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
149
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8".force_encoding Encoding::EUC_JP
150
+ s = @jString.new(euc_kj)
151
+ assert_equal(s.toString().encoding, Encoding::UTF_8)
152
+ assert(test.isSameString(s))
153
+ assert(test.isSameString(euc_kj))
154
+ assert_equal(s.toString().encode(Encoding::EUC_JP), euc_kj)
155
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67".force_encoding Encoding::SHIFT_JIS
156
+ s = @jString.new(sjis_kj)
157
+ assert_equal(s.toString().encoding, Encoding::UTF_8)
158
+ assert(test.isSameString(s))
159
+ assert(test.isSameString(sjis_kj))
160
+ assert_equal(s.toString().encode(Encoding::SHIFT_JIS), sjis_kj)
161
+ utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88".force_encoding Encoding::UTF_8
162
+ s = @jString.new(utf8_kj)
163
+ assert_equal(s.toString().encoding, Encoding::UTF_8)
164
+ assert(test.isSameString(s))
165
+ assert(test.isSameString(utf8_kj))
166
+ assert_equal(s.toString().encode(Encoding::UTF_8), utf8_kj)
167
+ iso2022jp_kj = "\x1b\x24\x42\x34\x41\x3b\x7a\x25\x46\x25\x2d\x25\x39\x25\x48\x1b\x28\x42".force_encoding Encoding::ISO_2022_JP
168
+ s = @jString.new(iso2022jp_kj)
169
+ assert_equal(s.toString().encoding, Encoding::UTF_8)
170
+ assert(test.isSameString(s))
171
+ assert(test.isSameString(iso2022jp_kj))
172
+ assert_equal(s.toString().encode(Encoding::ISO_2022_JP), iso2022jp_kj)
173
+ assert_equal(@jString.new("abcdef".force_encoding(Encoding::ASCII_8BIT)).toString(), "abcdef")
174
+ assert_equal(@jString.new("abcdef".force_encoding(Encoding::find("us-ascii"))).toString(), "abcdef")
175
+ else
176
+ default_kcode = $KCODE
177
+ begin
178
+ $KCODE = 'euc'
179
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
180
+ s = @jString.new(euc_kj)
181
+ assert_equal(s.toString(), euc_kj)
182
+ $KCODE = 'sjis'
183
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
184
+ s = @jString.new(sjis_kj)
185
+ assert_equal(s.toString(), sjis_kj)
186
+ $KCODE = 'utf8'
187
+ utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
188
+ s = @jString.new(utf8_kj)
189
+ assert_equal(s.toString(), utf8_kj)
190
+ $KCODE = 'none'
191
+ if /mswin(?!ce)|mingw|cygwin|bccwin/ =~ RUBY_PLATFORM
192
+ #expecting shift_jis on windows
193
+ none_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
194
+ else
195
+ #expecting utf-8 unless windows
196
+ none_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
197
+ end
198
+ s = @jString.new(none_kj)
199
+ assert_equal(s.toString(), none_kj)
200
+ $KCODE = 'utf8'
201
+ utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
202
+ s = @jString.new(utf8_kj)
203
+ assert_equal(s.toString(), utf8_kj)
204
+ $KCODE = 'sjis'
205
+ sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
206
+ s = @jString.new(sjis_kj)
207
+ assert_equal(s.toString(), sjis_kj)
208
+ $KCODE = 'euc'
209
+ euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
210
+ s = @jString.new(euc_kj)
211
+ assert_equal(s.toString(), euc_kj)
212
+ ensure
213
+ $KCODE = default_kcode
214
+ end
215
+ end
216
+ end
217
+
218
+ def test_combination_charcters
219
+ teststr = "\xc7\x96\xc3\xbc\xcc\x84\x75\xcc\x88\xcc\x84\xed\xa1\xa9\xed\xba\xb2\xe3\x81\x8b\xe3\x82\x9a"
220
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
221
+ s = test.getUmlaut()
222
+ if Object::const_defined?(:Encoding) #>=1.9
223
+ teststr = teststr.force_encoding(Encoding::UTF_8)
224
+ assert_equal(s, teststr)
225
+ else
226
+ default_kcode = $KCODE
227
+ begin
228
+ $KCODE = "utf8"
229
+ assert_equal(s, teststr)
230
+ ensure
231
+ $KCODE = default_kcode
232
+ end
233
+ end
234
+ end
235
+
236
+ def test_constants()
237
+ assert_equal(0x7fffffffffffffff, @jLong.MAX_VALUE)
238
+ assert_equal(-9223372036854775808, @jLong.MIN_VALUE)
239
+ end
240
+
241
+ class TestIter
242
+ def initialize()
243
+ @i = 5
244
+ end
245
+ def hasNext()
246
+ @i > 0
247
+ end
248
+ def next()
249
+ @i -= 1
250
+ @i.to_s
251
+ end
252
+ end
253
+
254
+ def test_newobject()
255
+ it = TestIter.new
256
+ it = bind(it, 'java.util.Iterator')
257
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
258
+ a = test.new
259
+ assert("43210", a.concat(it))
260
+ end
261
+
262
+ def test_unbind()
263
+ it = TestIter.new
264
+ it = bind(it, 'java.util.Iterator')
265
+ assert_equal(it, unbind(it))
266
+ end
267
+
268
+ class TestComparator
269
+ def compare(o1, o2)
270
+ o1.to_i - o2.to_i
271
+ end
272
+ def equals(o)
273
+ o == self
274
+ end
275
+ end
276
+
277
+ def test_comparator
278
+ cp = TestComparator.new
279
+ cp = bind(cp, 'java.util.Comparator')
280
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
281
+ a = test.new
282
+ assert_equal(0, a.check(cp, 123, 123))
283
+ assert_equal(5, a.check(cp, 81, 76))
284
+ assert_equal(-5, a.check(cp, 76, 81))
285
+ end
286
+
287
+ # assert_raise is useless in this test, because NumberFormatException may be defined in
288
+ # its block.
289
+ def test_exception()
290
+ begin
291
+ @jInteger.parseInt('blabla')
292
+ flunk('no exception')
293
+ rescue NumberFormatException => e
294
+ assert_nil(e.cause)
295
+ # OK
296
+ end
297
+ end
298
+
299
+ class TestIterator
300
+ def initialize(tp)
301
+ @type = tp
302
+ end
303
+ def hasNext()
304
+ true
305
+ end
306
+ def next()
307
+ if @type == 0
308
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
309
+ elsif @type == 1
310
+ Rjb::throw(Rjb::import('java.util.NoSuchElementException').new('instance test'))
311
+ end
312
+ end
313
+ end
314
+
315
+ def test_throw()
316
+ it = TestIterator.new(0)
317
+ it = bind(it, 'java.util.Iterator')
318
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
319
+ a = test.new
320
+ begin
321
+ a.concat(it)
322
+ flunk('no exception')
323
+ rescue NoSuchElementException => e
324
+ assert_equal('test exception', e.message)
325
+ end
326
+ end
327
+
328
+ def test_instance_throw()
329
+ it = TestIterator.new(1)
330
+ it = bind(it, 'java.util.Iterator')
331
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
332
+ a = test.new
333
+ begin
334
+ a.concat(it)
335
+ flunk('no exception')
336
+ rescue NoSuchElementException => e
337
+ assert_equal('instance test', e.message)
338
+ end
339
+ end
340
+
341
+ def test_null_string()
342
+ sys = import('java.lang.System')
343
+ begin
344
+ sys.getProperty(nil)
345
+ flunk('no exception')
346
+ rescue NullPointerException => e
347
+ assert(true)
348
+ rescue RuntimeError => e
349
+ flunk(e.message)
350
+ end
351
+ end
352
+
353
+ def test_throw_error()
354
+ begin
355
+ throw(self)
356
+ flunk('no exception')
357
+ rescue TypeError => e
358
+ end
359
+ begin
360
+ throw(@jString.new('a'))
361
+ flunk('no exception')
362
+ rescue RuntimeError => e
363
+ assert_equal('arg1 must be a throwable', e.message)
364
+ end
365
+ begin
366
+ throw('java.lang.NoSuchException', 'test')
367
+ flunk('no excpetion')
368
+ rescue RuntimeError => e
369
+ assert_equal("`java.lang.NoSuchException' not found", e.message)
370
+ end
371
+ end
372
+
373
+ def test_throw_clear()
374
+ assert_nothing_raised {
375
+ begin
376
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
377
+ rescue #drop ruby exception
378
+ end
379
+ test = import('jp.co.infoseek.hp.arton.rjb.Test')
380
+ begin
381
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
382
+ rescue #drop ruby exception
383
+ end
384
+ test.new
385
+ begin
386
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
387
+ rescue #drop ruby exception
388
+ end
389
+ @jString.new_with_sig('Ljava.lang.String;', "abcde")
390
+ begin
391
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
392
+ rescue #drop ruby exception
393
+ end
394
+ it = TestIterator.new(0)
395
+ it = bind(it, 'java.util.Iterator')
396
+ begin
397
+ Rjb::throw('java.util.NoSuchElementException', 'test exception')
398
+ rescue NoSuchElementException
399
+ end
400
+ begin
401
+ Rjb::throw('java.lang.IllegalAccessException', 'test exception')
402
+ rescue IllegalAccessException
403
+ end
404
+ unbind(it)
405
+ }
406
+ end
407
+
408
+ def test_field()
409
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
410
+ assert_equal('Hello World !!', test.helloData)
411
+ test.helloData = 'Goodby World !'
412
+ assert_equal('Goodby World !', test.helloData)
413
+ end
414
+
415
+ def test_instancemethod_from_class()
416
+ begin
417
+ assert_equal('true', @jString.valueOf(true))
418
+ @jString.length
419
+ flunk('no exception')
420
+ rescue RuntimeError => e
421
+ assert_equal('instance method `length\' for class', e.message)
422
+ end
423
+ end
424
+
425
+ def test_instancefield_from_class()
426
+ point = import('java.awt.Point')
427
+ begin
428
+ point.x
429
+ flunk('no exception')
430
+ rescue RuntimeError => e
431
+ assert_equal('instance field `x\' for class', e.message)
432
+ end
433
+ begin
434
+ point.x = 30
435
+ rescue RuntimeError => e
436
+ assert_equal('instance field `x\' for class', e.message)
437
+ end
438
+ end
439
+
440
+ def test_static_derived_method()
441
+ ext = import('jp.co.infoseek.hp.arton.rjb.ExtBase')
442
+ assert_equal("sVal", ext.getSVal)
443
+ end
444
+
445
+ def test_capitalized_method()
446
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
447
+ assert_equal("val", bs.val)
448
+ assert_equal("Val", bs.Val)
449
+ end
450
+
451
+ def test_underscored_constant()
452
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
453
+ assert_equal(5, bs._NUMBER_FIVE)
454
+ end
455
+
456
+ def test_passingclass()
457
+ ibs = import('jp.co.infoseek.hp.arton.rjb.IBase')
458
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
459
+ assert_equal('interface jp.co.infoseek.hp.arton.rjb.IBase', bs.intf(ibs))
460
+ end
461
+
462
+ def test_fornamehook()
463
+ # j2se class
464
+ cls = import('java.lang.Class')
465
+ c = cls.forName('java.lang.Class')
466
+ assert_equal(cls, c)
467
+ # user class
468
+ bs = import('jp.co.infoseek.hp.arton.rjb.Base')
469
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.Base')
470
+ assert_equal(bs, b)
471
+ # check class that was loaded from classpath
472
+ loader = Rjb::import('java.lang.ClassLoader')
473
+ b = cls.forName('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.getSystemClassLoader)
474
+ assert(b.isInterface)
475
+ end
476
+
477
+ def test_send_array_of_arrays()
478
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
479
+ a = test.joinStringArray([['ab', 'cd'], ['ef', 'gh']])
480
+ assert_equal(['ab', 'cd', 'ef', 'gh'], a)
481
+ a = test.joinIntArray([[1, 2, 3], [4, 5, 6]])
482
+ a.collect! {|e| e.intValue }
483
+ assert_equal([1, 2, 3, 4, 5, 6], a)
484
+ r = [[[ 1, 2], [2, 3] ], [[ 3, 4], [5, 6]], [[7, 8], [1, 3]]]
485
+ a = test.throughIntArray(r)
486
+ assert_equal(a, r)
487
+ end
488
+
489
+ def test_import_and_instanciate()
490
+ b = import('jp.co.infoseek.hp.arton.rjb.Base')
491
+ assert_equal('hello', b.new.getInstanceVar())
492
+ end
493
+
494
+ def test_array_of_arrays()
495
+ jversion = import('java.lang.System').getProperty('java.version')
496
+ if /^1\.5/ =~ jversion
497
+ method = import('java.lang.reflect.Method')
498
+ end
499
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
500
+ a = test.getStringArrayOfArrays()
501
+ assert_equal("abc", a[0][0])
502
+ assert_equal("def", a[0][1])
503
+ assert_equal("123", a[1][0])
504
+ assert_equal("456", a[1][1])
505
+
506
+ ints = test.getIntArrayOfArrays()
507
+ assert_equal(2, ints.size )
508
+ assert_equal([1,2,3], ints[0] )
509
+ assert_equal([[1,2,3],[4,5,6]], ints )
510
+
511
+ sized = test.getSizedArray()
512
+ assert_equal("find me",sized[0][1][2][3])
513
+
514
+ mixed = test.getMixedArray()
515
+ assert_equal(12,mixed[0][0][0].intValue)
516
+ assert_equal("another string",mixed[1][0][1].toString)
517
+ assert_equal([],mixed[2])
518
+ end
519
+
520
+ def test_CastObjectArray()
521
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
522
+ a = test.getObjectArray()
523
+ assert_equal(1, a[0].intValue)
524
+ assert_equal('Hello World !', a[1].toString)
525
+ a = test.getObjectArrayOfArray()
526
+ assert_equal(1, a[0][0].intValue)
527
+ assert_equal('Hello World !', a[0][1].toString)
528
+ assert_equal(2, a[1][0].intValue)
529
+ assert_equal('Hello World !!', a[1][1].toString)
530
+ end
531
+
532
+ def test_CallByNullForArrays()
533
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
534
+ assert_equal(nil, test.callWithArrays(nil, nil, nil, nil, nil, nil,
535
+ nil, nil))
536
+ end
537
+
538
+ def test_failed_constructor_call()
539
+ begin
540
+ s = @jString.new('a', 'b', 'c')
541
+ flunk('no exception')
542
+ rescue RuntimeError => e
543
+ assert(e)
544
+ end
545
+ end
546
+
547
+ def test_rubyize
548
+ loader = Rjb::import('java.lang.ClassLoader')
549
+ cls = import('java.lang.Class')
550
+ b = cls.for_name('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.system_class_loader)
551
+ assert(b.interface?)
552
+ stringbuffer = Rjb::import('java.lang.StringBuffer')
553
+ sb = stringbuffer.new('abc')
554
+ assert_equal(1, sb.index_of('bc'))
555
+ sb.set_char_at(1, "B".sum)
556
+ assert_equal('aBc', sb.to_string)
557
+ sb.length = 2
558
+ assert_equal('aB', sb.to_string)
559
+ end
560
+
561
+ def test_auto_conv
562
+ assert_equal(false, Rjb::primitive_conversion)
563
+ Rjb::primitive_conversion = true
564
+ assert_equal(true, Rjb::primitive_conversion)
565
+ assert_equal(1, @jInteger.valueOf('1'))
566
+ assert_equal(-1, @jInteger.valueOf('-1'))
567
+ assert_equal(2, @jShort.valueOf('2'))
568
+ assert_equal(-2, @jShort.valueOf('-2'))
569
+ assert_equal(3.1, @jDouble.valueOf('3.1'))
570
+ assert_equal(4.5, @jFloat.valueOf('4.5'))
571
+ assert(@jBoolean.TRUE)
572
+ assert_equal(5, @jByte.valueOf('5'))
573
+ assert_equal(-6, @jByte.valueOf('-6'))
574
+ assert_equal(0x7000000000000000, @jLong.valueOf('8070450532247928832'))
575
+ assert_equal(-9223372036854775807, @jLong.valueOf('-9223372036854775807'))
576
+ assert_equal("A".sum, @jChar.valueOf("A".sum))
577
+ end
578
+
579
+ def test_obj_to_primitive
580
+ ar = Rjb::import('java.util.ArrayList')
581
+ a = ar.new
582
+ a.add @jString.new('abcdef')
583
+ a.add @jInteger.valueOf('1')
584
+ a.add @jShort.valueOf('2')
585
+ a.add @jDouble.valueOf('3.1')
586
+ a.add @jFloat.valueOf('4.5')
587
+ a.add @jBoolean.TRUE
588
+ a.add @jByte.valueOf('5')
589
+ a.add @jLong.valueOf('8070450532247928832')
590
+ a.add @jChar.valueOf("A".sum)
591
+
592
+ Rjb::primitive_conversion = true
593
+
594
+ assert_equal 'abcdef', a.get(0)
595
+ assert_equal 1, a.get(1)
596
+ assert_equal 2, a.get(2)
597
+ assert_equal 3.1, a.get(3)
598
+ assert_equal 4.5, a.get(4)
599
+ assert a.get(5)
600
+ assert_equal 5, a.get(6)
601
+ assert_equal 8070450532247928832, a.get(7)
602
+ assert_equal "A".sum, a.get(8)
603
+ end
604
+
605
+ def test_primitive_to_obj
606
+ Rjb::primitive_conversion = true
607
+
608
+ ar = Rjb::import('java.util.ArrayList')
609
+ a = ar.new
610
+ a.add @jString.new('abcdef')
611
+ a.add @jInteger.valueOf('1')
612
+ a.add @jShort.valueOf('2')
613
+ a.add @jDouble.valueOf('3.1')
614
+ a.add @jFloat.valueOf('4.5')
615
+ a.add @jBoolean.TRUE
616
+ a.add @jByte.valueOf('5')
617
+ a.add @jLong.valueOf('8070450532247928832')
618
+ a.add @jChar.valueOf("A".sum)
619
+ assert_equal 'abcdef', a.get(0)
620
+ assert_equal 1, a.get(1)
621
+ assert_equal 2, a.get(2)
622
+ assert_equal 3.1, a.get(3)
623
+ assert_equal 4.5, a.get(4)
624
+ assert a.get(5)
625
+ assert_equal 5, a.get(6)
626
+ assert_equal 8070450532247928832, a.get(7)
627
+ assert_equal "A".sum, a.get(8)
628
+ end
629
+
630
+ def test_enum
631
+ t = Rjb::import('jp.co.infoseek.hp.arton.rjb.Test$TestTypes')
632
+ assert t.ONE.equals(t.values()[0])
633
+ assert_equal 3, t.values().size
634
+ assert_equal 2, t.THREE.ordinal
635
+ assert_equal "TWO", t.TWO.name
636
+ assert_equal "THREE", t.THREE.toString
637
+ end
638
+
639
+ #rjb-bugs-15430 rebported by Bryan Duxbury
640
+ def test_generics_map
641
+ test = import('jp.co.infoseek.hp.arton.rjb.Test').new
642
+ map = test.sorted_map
643
+ assert_equal "\0\x1\x2\x3\x4", map.get('abc')
644
+ assert_equal "\x5\x6\x7\x8\x9", map.get('def')
645
+ end
646
+
647
+ def x_test_zzunload
648
+ # this test should run at the last
649
+ unload
650
+ begin
651
+ load('.')
652
+ fail 'no exception'
653
+ rescue
654
+ assert_equal "can't create Java VM", $!.message
655
+ end
656
+ end
657
+
658
+ module TestMixin
659
+ def test_hello(s)
660
+ 'hello ' + s
661
+ end
662
+ end
663
+ def test_extend
664
+ @jString.class_eval do
665
+ include TestRjb::TestMixin
666
+ end
667
+ s = @jString.new
668
+ assert_equal('hello world', s.test_hello('world'))
669
+ end
670
+ def test_extend_with_factory
671
+ point = import('java.awt.Point')
672
+ point.class_eval do
673
+ include TestRjb::TestMixin
674
+ end
675
+ p = point.new(11, 12)
676
+ assert_equal(11, p.x)
677
+ assert_equal(12, p.y)
678
+ assert_equal('hello world', p.test_hello('world'))
679
+ p = p.location
680
+ assert_equal(11, p.x)
681
+ assert_equal(12, p.y)
682
+ assert_equal('hello world', p.test_hello('world'))
683
+ end
684
+ def test_fetch_method_signature
685
+ expected = ['I', 'II', 'Ljava.lang.String;', 'Ljava.lang.String;I']
686
+ sig = @jString.sigs('indexOf').sort
687
+ assert_equal(expected, sig)
688
+ end
689
+ def test_fetch_method_without_signature
690
+ sig =
691
+ assert_equal([nil], @jString.sigs('toString'))
692
+ end
693
+ def test_fetch_static_method_signature
694
+ expected = ['Ljava.lang.String;[Ljava.lang.Object;',
695
+ 'Ljava.util.Locale;Ljava.lang.String;[Ljava.lang.Object;']
696
+ sig = @jString.static_sigs('format').sort
697
+ assert_equal(expected, sig)
698
+ end
699
+ def test_fetch_ctor_signature
700
+ expected = ['I', 'Ljava.lang.String;']
701
+ sig = @jInteger.ctor_sigs.sort
702
+ assert_equal(expected, sig)
703
+ end
704
+ def test_methods_extension
705
+ m = @jString.new('').methods
706
+ assert m.include?('indexOf')
707
+ end
708
+ def test_class_methods_extension
709
+ m = @jString.methods
710
+ assert m.include?('format')
711
+ end
712
+ def test_pmethods_extension
713
+ m = @jString.new('').public_methods
714
+ assert m.include?('indexOf')
715
+ end
716
+ def test_class_pmethods_extension
717
+ m = @jString.public_methods
718
+ assert m.include?('format')
719
+ end
720
+ def test_java_methods
721
+ indexof = @jString.new('').java_methods.find do |m|
722
+ m =~ /^indexOf/
723
+ end
724
+ args = indexof.match(/\[([^\]]+)\]/)[1]
725
+ assert_equal('Ljava.lang.String;I, II, I, Ljava.lang.String;'.split(/,\s*/).sort,
726
+ args.split(/,\s*/).sort)
727
+ end
728
+ def test_java_class_methods
729
+ format = @jString.java_methods.find do |m|
730
+ m =~ /^format/
731
+ end
732
+ args = format.match(/\[([^\]]+)\]/)[1]
733
+ assert_equal('Ljava.lang.String;[Ljava.lang.Object;, Ljava.util.Locale;Ljava.lang.String;[Ljava.lang.Object;'.split(/,\s*/).sort, args.split(/,\s*/).sort)
734
+ end
735
+ def test_64fixnum
736
+ big = @jLong.new_with_sig('J', 1230918239495)
737
+ assert_equal 1230918239495, big.long_value
738
+ end
739
+ def test_add_jar
740
+ add_jar(File.expand_path('./jartest.jar'))
741
+ jt = import('jp.co.infoseek.hp.arton.rjb.JarTest')
742
+ assert jt
743
+ assert_equal 'abcd', jt.new.add('ab', 'cd')
744
+ end
745
+ def test_add_jars
746
+ arg = ['./jartest.jar', './jartest.jar'].map do |e|
747
+ File.expand_path(e)
748
+ end
749
+ add_jar(arg)
750
+ jt = import('jp.co.infoseek.hp.arton.rjb.JarTest')
751
+ assert_equal 'abcd', jt.new.add('ab', 'cd')
752
+ end
753
+ end
754
+