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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
|
#!/usr/local/env ruby -Ku
# encoding: utf-8
# $Id: test.rb 199 2012-12-17 13:31:18Z arton $
begin
require 'rjb'
rescue LoadError
require 'rubygems'
require 'rjb'
end
require 'test/unit'
require 'fileutils'
FileUtils.rm_f 'jp/co/infoseek/hp/arton/rjb/Base.class'
FileUtils.rm_f 'jp/co/infoseek/hp/arton/rjb/ExtBase.class'
puts "start RJB(#{Rjb::VERSION}) test"
class TestRjb < Test::Unit::TestCase
include Rjb
def setup
Rjb::load('.')
Rjb::add_jar(File.expand_path('rjbtest.jar'))
Rjb::primitive_conversion = false
@jString = import('java.lang.String')
@jInteger = import('java.lang.Integer')
@jShort = import('java.lang.Short')
@jDouble = import('java.lang.Double')
@jFloat = import('java.lang.Float')
@jBoolean = import('java.lang.Boolean')
@jByte = import('java.lang.Byte')
@jLong = import('java.lang.Long')
@jChar = import('java.lang.Character')
end
def teardown
end
def test_metaclass
cls = import('java.lang.Class')
assert_equal('java.lang.Class', cls._classname)
assert_equal('java.lang.Class', cls.getName)
assert_equal(17, cls.getModifiers)
end
def test_scalar
assert_equal('java.lang.Class', @jString._classname)
assert_equal('class java.lang.String', @jString.toString)
str = @jString.new
assert_equal('java.lang.String', str._classname)
assert_equal(0, str.length)
assert_equal('', str.toString)
str = @jString.new_with_sig('Ljava.lang.String;', "abcde")
# result integer
assert_equal(5, str.length)
# result string
assert_equal('abcde', str.toString)
# argument test
# char
assert_equal('abxde', str.replace("c".sum, "x".sum))
# string
assert_equal('abdxe', str.replaceAll('cd', 'dx'))
# int
assert_equal('bc', str.substring(1, 3))
assert_equal('e', str.substring(4))
# float with static
assert_equal('5.23', @jString._invoke('valueOf', 'F', 5.23))
assert_equal('25.233', @jString._invoke('valueOf', 'D', 25.233))
# rjb object (String)
str2 = @jString.new_with_sig('Ljava.lang.String;', 'fghijk')
assert_equal('abcdefghijk', str.concat(str2))
# rjb object other (implicit toString call is Rjb feature)
i = @jInteger.new_with_sig('I', 35901)
assert_equal('abcde35901', str.concat(i))
# result boolean and argument is rjb object
assert_equal(false, i.equals(str))
assert_equal(false, str.equals(i))
assert_equal(true, str.equals("abcde"))
assert_equal(true, str.equals(str))
# long
l = @jLong.new_with_sig('J', -9223372036854775808)
assert_equal(-9223372036854775808, l.longValue)
l = @jLong.new_with_sig('J', 9223372036854775807)
assert_equal(9223372036854775807, l.longValue)
# double
d = @jDouble.new_with_sig('D', 1234.5678901234567890)
assert_equal(1234.5678901234567890, d.doubleValue)
# byte
b = @jByte.new_with_sig('B', 13)
assert_equal(13, b.byteValue)
# float
f = @jFloat.new_with_sig('F', 13.5)
assert_equal(13.5, f.floatValue)
# short
s = @jShort.new_with_sig('S', 1532)
assert_equal(1532, s.shortValue)
c = @jChar.new_with_sig('C', "A".sum)
assert_equal("A".sum, c.charValue)
end
def test_array
str = @jString.new_with_sig('[C', ["a".sum, "b".sum, "c".sum, "d".sum, "e".sum, "c".sum, "f".sum, "c".sum, "g".sum])
assert_equal('abcdecfcg', str.toString)
# conv string array
splt = str.split('c')
assert(Array === splt)
assert_equal(4, splt.size)
assert_equal('ab', splt[0])
assert_equal('g', splt[3])
# conv byte array to (ruby)string
ba = str.getBytes
assert_equal('abcdecfcg', ba)
# conv char array to array(int)
ca = str.toCharArray
assert_equal(["a".sum, "b".sum, "c".sum, "d".sum, "e".sum, "c".sum, "f".sum, "c".sum, "g".sum], ca)
end
def test_importobj()
sys = import('java.lang.System')
props = sys.getProperties
assert_equal('java.util.Properties', props._classname)
if /cygwin/ =~ RUBY_PLATFORM # patch for dirty environment
assert_equal(Dir::pwd, %x[cygpath -u #{props.getProperty('user.dir').gsub('\\', '/')}].chop)
else
assert_equal(Dir::pwd, props.getProperty('user.dir').gsub('\\', '/'))
end
assert_equal(@jBoolean.valueOf(true).booleanValue(), true)
assert_equal(@jBoolean.valueOf(false).booleanValue(), false)
assert_equal(@jBoolean.valueOf('true').booleanValue(), true)
assert_equal(@jBoolean.valueOf('false').booleanValue(), false)
end
def test_importobjarray()
jarray = import('java.util.ArrayList')
a = jarray.new()
a.add(@jInteger.new_with_sig('I', 1))
a.add(@jInteger.new_with_sig('I', 2))
a.add(@jInteger.new_with_sig('I', 3))
oa = a.toArray
assert_equal(3, oa.size)
assert_equal(1, oa[0].intValue)
assert_equal(2, oa[1].intValue)
assert_equal(3, oa[2].intValue)
end
def test_kjconv()
if Object::const_defined?(:Encoding)
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8".force_encoding Encoding::EUC_JP
s = @jString.new_with_sig('Ljava.lang.String;', euc_kj)
assert_equal(s.toString().encoding, Encoding::UTF_8)
assert(test.isSameString(s))
assert(test.isSameString(euc_kj))
assert_equal(s.toString().encode(Encoding::EUC_JP), euc_kj)
sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67".force_encoding Encoding::SHIFT_JIS
s = @jString.new_with_sig('Ljava.lang.String;', sjis_kj)
assert_equal(s.toString().encoding, Encoding::UTF_8)
assert(test.isSameString(s))
assert(test.isSameString(sjis_kj))
assert_equal(s.toString().encode(Encoding::SHIFT_JIS), sjis_kj)
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
s = @jString.new_with_sig('Ljava.lang.String;', utf8_kj)
assert_equal(s.toString().encoding, Encoding::UTF_8)
assert(test.isSameString(s))
assert(test.isSameString(utf8_kj))
assert_equal(s.toString().encode(Encoding::UTF_8), utf8_kj)
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
s = @jString.new_with_sig('Ljava.lang.String;', iso2022jp_kj)
assert_equal(s.toString().encoding, Encoding::UTF_8)
assert(test.isSameString(s))
assert(test.isSameString(iso2022jp_kj))
assert_equal(s.toString().encode(Encoding::ISO_2022_JP), iso2022jp_kj)
assert_equal(@jString.new("abcdef".force_encoding(Encoding::ASCII_8BIT)).toString(), "abcdef")
assert_equal(@jString.new("abcdef".force_encoding(Encoding::find("us-ascii"))).toString(), "abcdef")
else
default_kcode = $KCODE
begin
$KCODE = 'euc'
euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
s = @jString.new(euc_kj)
assert_equal(s.toString(), euc_kj)
$KCODE = 'sjis'
sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
s = @jString.new(sjis_kj)
assert_equal(s.toString(), sjis_kj)
$KCODE = 'utf8'
utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
s = @jString.new(utf8_kj)
assert_equal(s.toString(), utf8_kj)
$KCODE = 'none'
if /mswin(?!ce)|mingw|cygwin|bccwin/ =~ RUBY_PLATFORM
#expecting shift_jis on windows
none_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
else
#expecting utf-8 unless windows
none_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
end
s = @jString.new(none_kj)
assert_equal(s.toString(), none_kj)
$KCODE = 'utf8'
utf8_kj = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
s = @jString.new(utf8_kj)
assert_equal(s.toString(), utf8_kj)
$KCODE = 'sjis'
sjis_kj = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
s = @jString.new(sjis_kj)
assert_equal(s.toString(), sjis_kj)
$KCODE = 'euc'
euc_kj = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
s = @jString.new(euc_kj)
assert_equal(s.toString(), euc_kj)
ensure
$KCODE = default_kcode
end
end
end
def test_combination_charcters
teststr = "\xc7\x96\xc3\xbc\xcc\x84\x75\xcc\x88\xcc\x84\xed\xa1\xa9\xed\xba\xb2\xe3\x81\x8b\xe3\x82\x9a"
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
s = test.getUmlaut()
if Object::const_defined?(:Encoding) #>=1.9
teststr = teststr.force_encoding(Encoding::UTF_8)
assert_equal(s, teststr)
else
default_kcode = $KCODE
begin
$KCODE = "utf8"
assert_equal(s, teststr)
ensure
$KCODE = default_kcode
end
end
end
def test_constants()
assert_equal(0x7fffffffffffffff, @jLong.MAX_VALUE)
assert_equal(-9223372036854775808, @jLong.MIN_VALUE)
end
class TestIter
def initialize()
@i = 5
end
def hasNext()
@i > 0
end
def next()
@i -= 1
@i.to_s
end
end
def test_newobject()
it = TestIter.new
it = bind(it, 'java.util.Iterator')
test = import('jp.co.infoseek.hp.arton.rjb.Test')
a = test.new
assert_equal("43210", a.concat(it))
end
def test_unbind()
it = TestIter.new
it = bind(it, 'java.util.Iterator')
assert_equal(it, unbind(it))
end
class TestComparator
def compare(o1, o2)
o1.to_i - o2.to_i
end
def equals(o)
o == self
end
end
def test_comparator
cp = TestComparator.new
cp = bind(cp, 'java.util.Comparator')
test = import('jp.co.infoseek.hp.arton.rjb.Test')
a = test.new
assert_equal(0, a.check(cp, 123, 123))
assert_equal(5, a.check(cp, 81, 76))
assert_equal(-5, a.check(cp, 76, 81))
end
# assert_raise is useless in this test, because NumberFormatException may be defined in
# its block.
def test_exception()
begin
@jInteger.parseInt('blabla')
flunk('no exception')
rescue NumberFormatException => e
assert_nil(e.cause)
# OK
end
end
class TestIterator
def initialize(tp)
@type = tp
end
def hasNext()
true
end
def next()
if @type == 0
Rjb::throw('java.util.NoSuchElementException', 'test exception')
elsif @type == 1
Rjb::throw(Rjb::import('java.util.NoSuchElementException').new('instance test'))
end
end
end
def test_throw()
it = TestIterator.new(0)
it = bind(it, 'java.util.Iterator')
test = import('jp.co.infoseek.hp.arton.rjb.Test')
a = test.new
begin
a.concat(it)
flunk('no exception')
rescue NoSuchElementException => e
assert_equal('test exception', e.message)
end
end
def test_instance_throw()
it = TestIterator.new(1)
it = bind(it, 'java.util.Iterator')
test = import('jp.co.infoseek.hp.arton.rjb.Test')
a = test.new
begin
a.concat(it)
flunk('no exception')
rescue NoSuchElementException => e
assert_equal('instance test', e.message)
end
end
def test_null_string()
sys = import('java.lang.System')
begin
sys.getProperty(nil)
flunk('no exception')
rescue NullPointerException => e
assert(true)
rescue RuntimeError => e
flunk(e.message)
end
end
def test_throw_error()
begin
throw(self)
flunk('no exception')
rescue TypeError => e
end
begin
throw(@jString.new('a'))
flunk('no exception')
rescue RuntimeError => e
assert_equal('arg1 must be a throwable', e.message)
end
begin
throw('java.lang.NoSuchException', 'test')
flunk('no excpetion')
rescue RuntimeError => e
assert_equal("`java.lang.NoSuchException' not found", e.message)
end
end
def test_throw_clear()
assert_nothing_raised {
begin
Rjb::throw('java.util.NoSuchElementException', 'test exception')
rescue #drop ruby exception
end
test = import('jp.co.infoseek.hp.arton.rjb.Test')
begin
Rjb::throw('java.util.NoSuchElementException', 'test exception')
rescue #drop ruby exception
end
test.new
begin
Rjb::throw('java.util.NoSuchElementException', 'test exception')
rescue #drop ruby exception
end
@jString.new_with_sig('Ljava.lang.String;', "abcde")
begin
Rjb::throw('java.util.NoSuchElementException', 'test exception')
rescue #drop ruby exception
end
it = TestIterator.new(0)
it = bind(it, 'java.util.Iterator')
begin
Rjb::throw('java.util.NoSuchElementException', 'test exception')
rescue NoSuchElementException
end
begin
Rjb::throw('java.lang.IllegalAccessException', 'test exception')
rescue IllegalAccessException
end
unbind(it)
}
end
def test_field()
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
assert_equal('Hello World !!', test.helloData)
test.helloData = 'Goodby World !'
assert_equal('Goodby World !', test.helloData)
end
def test_instancemethod_from_class()
begin
assert_equal('true', @jString.valueOf(true))
@jString.length
flunk('no exception')
rescue RuntimeError => e
assert_equal('instance method `length\' for class', e.message)
end
end
def test_instancefield_from_class()
point = import('java.awt.Point')
begin
point.x
flunk('no exception')
rescue RuntimeError => e
assert_equal('instance field `x\' for class', e.message)
end
begin
point.x = 30
rescue RuntimeError => e
assert_equal('instance field `x\' for class', e.message)
end
end
def test_static_derived_method()
ext = import('jp.co.infoseek.hp.arton.rjb.ExtBase')
assert_equal("sVal", ext.getSVal)
end
def test_capitalized_method()
bs = import('jp.co.infoseek.hp.arton.rjb.Base')
assert_equal("val", bs.val)
assert_equal("Val", bs.Val)
end
def test_underscored_constant()
bs = import('jp.co.infoseek.hp.arton.rjb.Base')
assert_equal(5, bs._NUMBER_FIVE)
end
def test_passingclass()
ibs = import('jp.co.infoseek.hp.arton.rjb.IBase')
bs = import('jp.co.infoseek.hp.arton.rjb.Base')
assert_equal('interface jp.co.infoseek.hp.arton.rjb.IBase', bs.intf(ibs))
end
def test_fornamehook()
# j2se class
cls = import('java.lang.Class')
c = cls.forName('java.lang.Class')
assert_equal(cls, c)
# user class
bs = import('jp.co.infoseek.hp.arton.rjb.Base')
b = cls.forName('jp.co.infoseek.hp.arton.rjb.Base')
assert_equal(bs, b)
# check class that was loaded from classpath
loader = Rjb::import('java.lang.ClassLoader')
b = cls.forName('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.getSystemClassLoader)
assert(b.isInterface)
end
def test_send_array_of_arrays()
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
a = test.joinStringArray([['ab', 'cd'], ['ef', 'gh']])
assert_equal(['ab', 'cd', 'ef', 'gh'], a)
a = test.joinIntArray([[1, 2, 3], [4, 5, 6]])
a.collect! {|e| e.intValue }
assert_equal([1, 2, 3, 4, 5, 6], a)
r = [[[ 1, 2], [2, 3] ], [[ 3, 4], [5, 6]], [[7, 8], [1, 3]]]
a = test.throughIntArray(r)
assert_equal(a, r)
end
def test_import_and_instanciate()
b = import('jp.co.infoseek.hp.arton.rjb.Base')
assert_equal('hello', b.new.getInstanceVar())
end
def test_array_of_arrays()
jversion = import('java.lang.System').getProperty('java.version')
if /^1\.5/ =~ jversion
method = import('java.lang.reflect.Method')
end
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
a = test.getStringArrayOfArrays()
assert_equal("abc", a[0][0])
assert_equal("def", a[0][1])
assert_equal("123", a[1][0])
assert_equal("456", a[1][1])
ints = test.getIntArrayOfArrays()
assert_equal(2, ints.size )
assert_equal([1,2,3], ints[0] )
assert_equal([[1,2,3],[4,5,6]], ints )
sized = test.getSizedArray()
assert_equal("find me",sized[0][1][2][3])
mixed = test.getMixedArray()
assert_equal(12,mixed[0][0][0].intValue)
assert_equal("another string",mixed[1][0][1].toString)
assert_equal([],mixed[2])
end
def test_CastObjectArray()
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
a = test.getObjectArray()
assert_equal(1, a[0].intValue)
assert_equal('Hello World !', a[1].toString)
a = test.getObjectArrayOfArray()
assert_equal(1, a[0][0].intValue)
assert_equal('Hello World !', a[0][1].toString)
assert_equal(2, a[1][0].intValue)
assert_equal('Hello World !!', a[1][1].toString)
end
def test_CallByNullForArrays()
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
assert_equal(nil, test.callWithArrays(nil, nil, nil, nil, nil, nil,
nil, nil))
end
def test_failed_constructor_call()
begin
s = @jString.new('a', 'b', 'c')
flunk('no exception')
rescue RuntimeError => e
assert(e)
end
end
def test_rubyize
loader = Rjb::import('java.lang.ClassLoader')
cls = import('java.lang.Class')
b = cls.for_name('jp.co.infoseek.hp.arton.rjb.IBase', true, loader.system_class_loader)
assert(b.interface?)
stringbuffer = Rjb::import('java.lang.StringBuffer')
sb = stringbuffer.new('abc')
assert_equal(1, sb.index_of('bc'))
sb.set_char_at(1, "B".sum)
assert_equal('aBc', sb.to_string)
sb.length = 2
assert_equal('aB', sb.to_string)
end
def test_auto_conv
assert_equal(false, Rjb::primitive_conversion)
Rjb::primitive_conversion = true
assert_equal(true, Rjb::primitive_conversion)
assert_equal(1, @jInteger.valueOf('1'))
assert_equal(-1, @jInteger.valueOf('-1'))
assert_equal(2, @jShort.valueOf('2'))
assert_equal(-2, @jShort.valueOf('-2'))
assert_equal(3.1, @jDouble.valueOf('3.1'))
assert_equal(4.5, @jFloat.valueOf('4.5'))
assert(@jBoolean.TRUE)
assert_equal(5, @jByte.valueOf('5'))
assert_equal(-6, @jByte.valueOf('-6'))
assert_equal(0x7000000000000000, @jLong.valueOf('8070450532247928832'))
assert_equal(-9223372036854775807, @jLong.valueOf('-9223372036854775807'))
assert_equal("A".sum, @jChar.valueOf("A".sum))
end
def test_obj_to_primitive
ar = Rjb::import('java.util.ArrayList')
a = ar.new
a.add @jString.new('abcdef')
a.add @jInteger.valueOf('1')
a.add @jShort.valueOf('2')
a.add @jDouble.valueOf('3.1')
a.add @jFloat.valueOf('4.5')
a.add @jBoolean.TRUE
a.add @jByte.valueOf('5')
a.add @jLong.valueOf('8070450532247928832')
a.add @jChar.valueOf("A".sum)
Rjb::primitive_conversion = true
assert_equal 'abcdef', a.get(0)
assert_equal 1, a.get(1)
assert_equal 2, a.get(2)
assert_equal 3.1, a.get(3)
assert_equal 4.5, a.get(4)
assert a.get(5)
assert_equal 5, a.get(6)
assert_equal 8070450532247928832, a.get(7)
assert_equal "A".sum, a.get(8)
end
def test_primitive_to_obj
Rjb::primitive_conversion = true
ar = Rjb::import('java.util.ArrayList')
a = ar.new
a.add @jString.new('abcdef')
a.add @jInteger.valueOf('1')
a.add @jShort.valueOf('2')
a.add @jDouble.valueOf('3.1')
a.add @jFloat.valueOf('4.5')
a.add @jBoolean.TRUE
a.add @jByte.valueOf('5')
a.add @jLong.valueOf('8070450532247928832')
a.add @jChar.valueOf("A".sum)
assert_equal 'abcdef', a.get(0)
assert_equal 1, a.get(1)
assert_equal 2, a.get(2)
assert_equal 3.1, a.get(3)
assert_equal 4.5, a.get(4)
assert a.get(5)
assert_equal 5, a.get(6)
assert_equal 8070450532247928832, a.get(7)
assert_equal "A".sum, a.get(8)
end
def test_enum
t = Rjb::import('jp.co.infoseek.hp.arton.rjb.Test$TestTypes')
assert t.ONE.equals(t.values()[0])
assert_equal 3, t.values().size
assert_equal 2, t.THREE.ordinal
assert_equal "TWO", t.TWO.name
assert_equal "THREE", t.THREE.toString
end
#rjb-bugs-15430 rebported by Bryan Duxbury
def test_generics_map
ctest = import('jp.co.infoseek.hp.arton.rjb.Test')
test = ctest.new
map = test.sorted_map
assert_equal "\0\x1\x2\x3\x4", map.get('abc')
assert_equal "\x5\x6\x7\x8\x9", map.get('def')
cmap = import('java.util.TreeMap')
map = cmap.new
map.put('abc', @jString.new('abc').bytes)
map.put('012', @jString.new('012').bytes)
Rjb::primitive_conversion = true
map2 = test.throughSortedMap(map)
assert_equal '012', map2.get('012')
assert_equal 'abc', map2.get('abc')
end
def x_test_zzunload
# this test should run at the last
unload
begin
load('.')
fail 'no exception'
rescue
assert_equal "can't create Java VM", $!.message
end
end
module TestMixin
def test_hello(s)
'hello ' + s
end
end
def test_extend
@jString.class_eval do
include TestRjb::TestMixin
end
s = @jString.new
assert_equal('hello world', s.test_hello('world'))
end
def test_extend_with_factory
point = import('java.awt.Point')
point.class_eval do
include TestRjb::TestMixin
end
p = point.new(11, 12)
assert_equal(11, p.x)
assert_equal(12, p.y)
assert_equal('hello world', p.test_hello('world'))
p = p.location
assert_equal(11, p.x)
assert_equal(12, p.y)
assert_equal('hello world', p.test_hello('world'))
end
def test_fetch_method_signature
expected = ['I', 'II', 'Ljava.lang.String;', 'Ljava.lang.String;I']
sig = @jString.sigs('indexOf').sort
assert_equal(expected, sig)
end
def test_fetch_method_without_signature
sig =
assert_equal([nil], @jString.sigs('toString'))
end
def test_fetch_static_method_signature
expected = ['Ljava.lang.String;[Ljava.lang.Object;',
'Ljava.util.Locale;Ljava.lang.String;[Ljava.lang.Object;']
sig = @jString.static_sigs('format').sort
assert_equal(expected, sig)
end
def test_fetch_ctor_signature
expected = ['I', 'Ljava.lang.String;']
sig = @jInteger.ctor_sigs.sort
assert_equal(expected, sig)
end
def test_methods_extension
m = @jString.new('').methods
assert m.include?('indexOf')
end
def test_class_methods_extension
m = @jString.methods
assert m.include?('format')
end
def test_pmethods_extension
m = @jString.new('').public_methods
assert m.include?('indexOf')
end
def test_class_pmethods_extension
m = @jString.public_methods
assert m.include?('format')
end
def test_java_methods
indexof = @jString.new('').java_methods.find do |m|
m =~ /^indexOf/
end
args = indexof.match(/\[([^\]]+)\]/)[1]
assert_equal('Ljava.lang.String;I, II, I, Ljava.lang.String;'.split(/,\s*/).sort,
args.split(/,\s*/).sort)
end
def test_java_class_methods
format = @jString.java_methods.find do |m|
m =~ /^format/
end
args = format.match(/\[([^\]]+)\]/)[1]
assert_equal('Ljava.lang.String;[Ljava.lang.Object;, Ljava.util.Locale;Ljava.lang.String;[Ljava.lang.Object;'.split(/,\s*/).sort, args.split(/,\s*/).sort)
end
def test_64fixnum
big = @jLong.new_with_sig('J', 1230918239495)
assert_equal 1230918239495, big.long_value
end
def test_add_jar
add_jar(File.expand_path('../jartest.jar', __FILE__))
jt = import('jp.co.infoseek.hp.arton.rjb.JarTest')
assert jt
assert_equal 'abcd', jt.new.add('ab', 'cd')
end
def test_add_jars
arg = ['../jartest.jar', '../jartest.jar'].map do |e|
File.expand_path(e, __FILE__)
end
add_jar(arg)
jt = import('jp.co.infoseek.hp.arton.rjb.JarTest')
assert_equal 'abcd', jt.new.add('ab', 'cd')
end
def test_bothdirection_buffer
org = "abcdefghijklmn"
baip = import('java.io.ByteArrayInputStream')
ba = baip.new(org)
buff = "\0" * org.size
assert_equal org.size, ba.read(buff)
assert_equal -1, ba.read(buff)
ba.close
assert_equal org, buff
end
def test_anoninterface
arrays = import('java.util.Arrays')
a = [3, -4, 5, -6, 8, -10, -14]
index = arrays.binary_search(a, 6) do |m, o1, o2|
o1.abs - o2.abs
end
assert_equal 3, index
index = arrays.binary_search(a, 7) do |m, o1, o2|
o1.abs - o2.abs
end
assert_equal -5, index
end
def test_impl
two = import('jp.co.infoseek.hp.arton.rjb.Two')
t = two.impl { |m| m.to_s }
a = import('jp.co.infoseek.hp.arton.rjb.TwoCaller').new
ret = a.foo(t)
assert_equal 'method1', ret[0]
assert_equal 'method2', ret[1]
end
def cause_exception
begin
@jInteger.parseInt('blabla')
rescue NumberFormatException => e
raise
end
end
def test_reraise_exception()
skip('1.8 does not support reraise') if /^1\.8/ =~ RUBY_VERSION
begin
cause_exception
rescue
assert($!.inspect =~ /NumberFormatException/)
end
end
def test_inner_exception
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
begin
test.cause_exception
flunk("no exception")
rescue IllegalStateException => e
ia = e.cause
assert_equal('bad argument', ia.message)
assert_equal('java.lang.IllegalArgumentException', ia._classname)
end
end
class CbTest
def method(l, s, i, d, str)
"test_ok:#{l}-#{s}-#{i}-#{d}-#{str}"
end
end
def test_longcallback()
cb = bind(CbTest.new, 'jp.co.infoseek.hp.arton.rjb.CallbackTest$Callback')
test = import('jp.co.infoseek.hp.arton.rjb.CallbackTest')
assert_equal 'test_ok:1234-1234-1234-1234.5-1234', test.callCallback(cb)
end
class TestIterEx < TestIter
def initialize()
super
@strattr = 'strattr'
@numattr = 32
end
attr_accessor :strattr, :numattr
def multargs(a, b)
a + b
end
end
def test_method_otherthan_bound()
it = TestIterEx.new
it = bind(it, 'java.util.Iterator')
test = import('jp.co.infoseek.hp.arton.rjb.Test')
a = test.new
assert_equal("43210", a.concat(it))
assert(it.respond_to?(:numattr))
assert(it.respond_to?(:multargs))
assert_equal(32, it.numattr)
assert_equal('strattr', it.strattr)
it.numattr += 1
assert_equal(33, it.numattr)
assert_equal(5, it.multargs(3, 2))
end
def test_noarg_invoke()
str = @jString.new('abc')
assert_equal('abc', str._invoke('toString', ''))
assert_equal('abc', str._invoke('toString', nil))
assert_equal('abc', str._invoke('toString'))
end
def test_noarg_sinvoke()
loader= import('java.lang.ClassLoader')
sloader = loader.system_class_loader
assert_equal(sloader._classname, loader._invoke('getSystemClassLoader', '')._classname)
assert_equal(sloader._classname, loader._invoke('getSystemClassLoader', nil)._classname)
assert_equal(sloader._classname, loader._invoke('getSystemClassLoader')._classname)
end
def test_longarg
skip('rbx can handle 64bits long') if RUBY_ENGINE == 'rbx'
assert_equal(597899502607411822, @jLong.reverse(0x7654321076543210))
begin
@jLong.reverse(0x76543210765432101)
fail 'no exception for bigbnum it doesn\'t convert Java long'
rescue RangeError
assert true
end
end
def test_bytearg
b = @jByte.new(32)
assert_equal(32, b.int_value)
assert b.compareTo(@jByte.new(32))
assert b.compareTo(@jByte.value_of(32))
b = @jByte.new_with_sig('B', 32)
assert_equal(32, b.int_value)
assert b.compareTo(@jByte._invoke(:valueOf, 'B', 32))
end
def test_typedarray
test = import('jp.co.infoseek.hp.arton.rjb.Test').new
uri = import('java.net.URI')
ret = test.java_typed_array(['a', 'b', 'c'], [1, 2, 3], [uri.new('http://www.artonx.org')])
assert_equal '[Ljava.lang.String;', ret[0]
assert_equal '[Ljava.lang.Integer;', ret[1]
assert_equal '[Ljava.net.URI;', ret[2]
end
SJIS_STR = "\x8a\xbf\x8e\x9a\x83\x65\x83\x4c\x83\x58\x83\x67"
EUCJP_STR = "\xb4\xc1\xbb\xfa\xa5\xc6\xa5\xad\xa5\xb9\xa5\xc8"
UTF8_STR = "\xE6\xBC\xA2\xE5\xAD\x97\xE3\x83\x86\xE3\x82\xAD\xE3\x82\xB9\xE3\x83\x88"
def test_auto_constructor_selection
skip 'no encoding' unless Object::const_defined?(:Encoding)
sys = import('java.lang.System')
encoding = sys.property('file.encoding')
s = @jString.new(SJIS_STR.force_encoding Encoding::SHIFT_JIS)
e = @jString.new(EUCJP_STR.force_encoding Encoding::EUC_JP)
u = @jString.new(UTF8_STR.force_encoding Encoding::UTF_8)
if encoding == 'MS932'
s1 = @jString.new(SJIS_STR.bytes)
elsif encoding.upcase == 'EUC-JP'
s1 = @jString.new(EUCJP_STR.bytes)
elsif encoding.upcase == 'UTF-8'
s1 = @jString.new(UTF8_STR.bytes)
else
skip 'no checkable encoding'
end
assert_equal s1.toString, s.toString
assert_equal s1.toString, e.toString
assert_equal s1.toString, u.toString
end
def test_bothdirection_chararray
charArrayReader = import('java.io.CharArrayReader')
org = [48, 49, 50, 51, 52, 53]
reader = charArrayReader.new(org)
buffer = Array.new(32, 0)
len = reader.read(buffer, 0, buffer.size)
assert_equal org.size, len
assert_equal org, buffer[0...len]
end
end
|