PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/test_higher_javasupport.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 834 lines | 609 code | 121 blank | 104 comment | 19 complexity | 141fcc6dc106e680d4d993efd33d8315 MD5 | raw file
  1. require 'java'
  2. require 'rbconfig'
  3. require 'test/unit'
  4. TopLevelConstantExistsProc = Proc.new do
  5. java_import 'java.lang.String'
  6. end
  7. class TestHigherJavasupport < Test::Unit::TestCase
  8. TestHelper = org.jruby.test.TestHelper
  9. JArray = ArrayList = java.util.ArrayList
  10. FinalMethodBaseTest = org.jruby.test.FinalMethodBaseTest
  11. Annotation = java.lang.annotation.Annotation
  12. ClassWithPrimitive = org.jruby.test.ClassWithPrimitive
  13. def test_java_int_primitive_assignment
  14. assert_nothing_raised {
  15. cwp = ClassWithPrimitive.new
  16. cwp.an_int = nil
  17. assert_equal 0, cwp.an_int
  18. }
  19. end
  20. def test_java_passing_class
  21. assert_equal("java.util.ArrayList", TestHelper.getClassName(ArrayList))
  22. end
  23. @@include_java_lang = Proc.new {
  24. include_package "java.lang"
  25. java_alias :JavaInteger, :Integer
  26. }
  27. def test_java_class_loading_and_class_name_collisions
  28. assert_raises(NameError) { System }
  29. @@include_java_lang.call
  30. assert_nothing_raised { System }
  31. assert_equal(10, JavaInteger.new(10).intValue)
  32. assert_raises(NoMethodError) { Integer.new(10) }
  33. end
  34. Random = java.util.Random
  35. Double = java.lang.Double
  36. def test_constructors_and_instance_methods
  37. r = Random.new
  38. assert_equal(Random, r.class)
  39. r = Random.new(1001)
  40. assert_equal(10.0, Double.new(10).doubleValue())
  41. assert_equal(10.0, Double.new("10").doubleValue())
  42. assert_equal(Random, r.class)
  43. assert_equal(Fixnum, r.nextInt.class)
  44. assert_equal(Fixnum, r.nextInt(10).class)
  45. end
  46. Long = java.lang.Long
  47. def test_instance_methods_differing_only_on_argument_type
  48. l1 = Long.new(1234)
  49. l2 = Long.new(1000)
  50. assert(l1.compareTo(l2) > 0)
  51. end
  52. def test_dispatching_on_nil
  53. sb = TestHelper.getInterfacedInstance()
  54. assert_equal(nil , sb.dispatchObject(nil))
  55. end
  56. def test_class_methods
  57. result = java.lang.System.currentTimeMillis()
  58. assert_equal(Fixnum, result.class)
  59. end
  60. Boolean = java.lang.Boolean
  61. def test_class_methods_differing_only_on_argument_type
  62. assert_equal(true, Boolean.valueOf("true"))
  63. assert_equal(false, Boolean.valueOf(false))
  64. end
  65. Character = java.lang.Character
  66. def test_constants
  67. assert_equal(9223372036854775807, Long::MAX_VALUE)
  68. assert(! defined? Character::Y_DATA) # Known private field in Character
  69. # class definition with "_" constant causes error
  70. assert_nothing_raised { org.jruby.javasupport.test.ConstantHolder }
  71. end
  72. def test_using_arrays
  73. list = JArray.new
  74. list.add(10)
  75. list.add(20)
  76. array = list.toArray
  77. assert_equal(10, array[0])
  78. assert_equal(20, array[1])
  79. assert_equal(2, array.length)
  80. array[1] = 1234
  81. assert_equal(10, array[0])
  82. assert_equal(1234, array[1])
  83. assert_equal([10, 1234], array.entries)
  84. assert_equal(10, array.min)
  85. end
  86. def test_creating_arrays
  87. array = Double[3].new
  88. assert_equal(3, array.length)
  89. array[0] = 3.14
  90. array[2] = 17.0
  91. assert_equal(3.14, array[0])
  92. assert_equal(17.0, array[2])
  93. end
  94. Pipe = java.nio.channels.Pipe
  95. def test_inner_classes
  96. assert_equal("java.nio.channels.Pipe$SinkChannel",
  97. Pipe::SinkChannel.java_class.name)
  98. if RUBY_VERSION =~ /1\.9/
  99. assert(Pipe::SinkChannel.instance_methods.include?(:keyFor))
  100. else
  101. assert(Pipe::SinkChannel.instance_methods.include?("keyFor"))
  102. end
  103. end
  104. def test_subclasses_and_their_return_types
  105. l = ArrayList.new
  106. r = Random.new
  107. l.add(10)
  108. assert_equal(10, l.get(0))
  109. l.add(r)
  110. r_returned = l.get(1)
  111. # Since Random is a public class we should get the value casted as that
  112. assert_equal("java.util.Random", r_returned.java_class.name)
  113. assert(r_returned.nextInt.kind_of?(Fixnum))
  114. end
  115. HashMap = java.util.HashMap
  116. def test_private_classes_interfaces_and_return_types
  117. h = HashMap.new
  118. assert_equal(HashMap, h.class)
  119. h.put("a", 1)
  120. iter = h.entrySet.iterator
  121. inner_instance_entry = iter.next
  122. # The class implements a public interface, MapEntry, so the methods
  123. # on that should be available, even though the instance is of a
  124. # private class.
  125. assert_equal("a", inner_instance_entry.getKey)
  126. end
  127. def test_extending_java_interfaces
  128. if java.lang.Comparable.instance_of?(Module)
  129. anonymous = Class.new(Object)
  130. anonymous.send :include, java.lang.Comparable
  131. anonymous.send :include, java.lang.Runnable
  132. assert anonymous < java.lang.Comparable
  133. assert anonymous < java.lang.Runnable
  134. assert anonymous.new.kind_of?(java.lang.Runnable)
  135. assert anonymous.new.kind_of?(java.lang.Comparable)
  136. else
  137. assert Class.new(java.lang.Comparable)
  138. end
  139. end
  140. def test_support_of_other_class_loaders
  141. assert_helper_class = Java::JavaClass.for_name("org.jruby.test.TestHelper")
  142. assert_helper_class2 = Java::JavaClass.for_name("org.jruby.test.TestHelper")
  143. assert(assert_helper_class.java_class == assert_helper_class2.java_class, "Successive calls return the same class")
  144. method = assert_helper_class.java_method('loadAlternateClass')
  145. alt_assert_helper_class = method.invoke_static()
  146. constructor = alt_assert_helper_class.constructor();
  147. alt_assert_helper = constructor.new_instance();
  148. identityMethod = alt_assert_helper_class.java_method('identityTest')
  149. identity = identityMethod.invoke(alt_assert_helper)
  150. assert_equal("ABCDEFGH", identity)
  151. end
  152. module Foo
  153. java_import("java.util.ArrayList")
  154. end
  155. java_import("java.lang.String") {|package,name| "J#{name}" }
  156. java_import ["java.util.Hashtable", "java.util.Vector"]
  157. def test_class_constants_defined_under_correct_modules
  158. assert_equal(0, Foo::ArrayList.new.size)
  159. assert_equal("a", JString.new("a").to_s)
  160. assert_equal(0, Vector.new.size)
  161. assert_equal(0, Hashtable.new.size)
  162. end
  163. def test_high_level_java_should_only_deal_with_proxies_and_not_low_level_java_class
  164. a = JString.new
  165. assert(a.getClass().class != "Java::JavaClass")
  166. end
  167. # We had a problem with accessing singleton class versus class earlier. Sanity check
  168. # to make sure we are not writing class methods to the same place.
  169. java_import 'org.jruby.test.AlphaSingleton'
  170. java_import 'org.jruby.test.BetaSingleton'
  171. def test_make_sure_we_are_not_writing_class_methods_to_the_same_place
  172. assert_nothing_raised { AlphaSingleton.getInstance.alpha }
  173. end
  174. java_import 'org.jruby.javasupport.test.Color'
  175. def test_lazy_proxy_method_tests_for_alias_and_respond_to
  176. color = Color.new('green')
  177. assert_equal(true, color.respond_to?(:setColor))
  178. assert_equal(false, color.respond_to?(:setColorBogus))
  179. end
  180. class MyColor < Color
  181. alias_method :foo, :getColor
  182. def alias_test
  183. alias_method :foo2, :setColorReallyBogus
  184. end
  185. end
  186. def test_accessor_methods
  187. my_color = MyColor.new('blue')
  188. assert_equal('blue', my_color.foo)
  189. assert_raises(NoMethodError) { my_color.alias_test }
  190. my_color.color = 'red'
  191. assert_equal('red', my_color.color)
  192. my_color.setDark(true)
  193. assert_equal(true, my_color.dark?)
  194. my_color.dark = false
  195. assert_equal(false, my_color.dark?)
  196. end
  197. # No explicit test, but implicitly EMPTY_LIST.each should not blow up interpreter
  198. # Old error was EMPTY_LIST is a private class implementing a public interface with public methods
  199. java_import 'java.util.Collections'
  200. def test_empty_list_each_should_not_blow_up_interpreter
  201. assert_nothing_raised { Collections::EMPTY_LIST.each {|element| } }
  202. end
  203. def test_already_loaded_proxies_should_still_see_extend_proxy
  204. JavaUtilities.extend_proxy('java.util.List') do
  205. def foo
  206. true
  207. end
  208. end
  209. assert_equal(true, Foo::ArrayList.new.foo)
  210. end
  211. def test_same_proxy_does_not_raise
  212. # JString already included and it is the same proxy, so do not throw an error
  213. # (e.g. intent of java_import already satisfied)
  214. assert_nothing_raised do
  215. begin
  216. old_stream = $stderr.dup
  217. $stderr.reopen(RbConfig::CONFIG['target_os'] =~ /Windows|mswin/ ? 'NUL:' : '/dev/null')
  218. $stderr.sync = true
  219. class << self
  220. java_import("java.lang.String") {|package,name| "J#{name}" }
  221. end
  222. ensure
  223. $stderr.reopen(old_stream)
  224. end
  225. end
  226. end
  227. java_import 'java.util.Calendar'
  228. def test_date_time_conversion
  229. # Test java.util.Date <=> Time implicit conversion
  230. calendar = Calendar.getInstance
  231. calendar.setTime(Time.at(0))
  232. java_date = calendar.getTime
  233. assert_equal(java_date.getTime, Time.at(0).to_i)
  234. end
  235. def test_expected_java_string_methods_exist
  236. jstring_methods = %w[bytes charAt char_at compareTo compareToIgnoreCase compare_to
  237. compare_to_ignore_case concat contentEquals content_equals endsWith
  238. ends_with equals equalsIgnoreCase equals_ignore_case getBytes getChars
  239. getClass get_bytes get_chars get_class hashCode hash_code indexOf
  240. index_of intern java_class java_object java_object= lastIndexOf last_index_of
  241. length matches notify notifyAll notify_all regionMatches region_matches replace
  242. replaceAll replaceFirst replace_all replace_first split startsWith starts_with
  243. subSequence sub_sequence substring taint tainted? toCharArray toLowerCase
  244. toString toUpperCase to_char_array to_lower_case to_string
  245. to_upper_case trim wait]
  246. if RUBY_VERSION =~ /1\.9/
  247. jstring_methods = jstring_methods.map(&:to_sym)
  248. end
  249. jstring_methods.each { |method| assert(JString.public_instance_methods.include?(method), "#{method} is missing from JString") }
  250. end
  251. java_import 'java.math.BigDecimal'
  252. def test_big_decimal_interaction
  253. assert_equal(BigDecimal, BigDecimal.new("1.23").add(BigDecimal.new("2.34")).class)
  254. end
  255. def test_direct_package_access
  256. a = java.util.ArrayList.new
  257. assert_equal(0, a.size)
  258. end
  259. Properties = Java::java.util.Properties
  260. def test_declare_constant
  261. p = Properties.new
  262. p.setProperty("a", "b")
  263. assert_equal("b", p.getProperty("a"))
  264. end
  265. if java.awt.event.ActionListener.instance_of?(Module)
  266. class MyBadActionListener
  267. include java.awt.event.ActionListener
  268. end
  269. else
  270. class MyBadActionListener < java.awt.event.ActionListener
  271. end
  272. end
  273. def test_expected_missing_interface_method
  274. assert_raises(NoMethodError) { MyBadActionListener.new.actionPerformed }
  275. end
  276. def test_that_misspelt_fq_class_names_dont_stop_future_fq_class_names_with_same_inner_most_package
  277. assert_raises(NameError) { Java::java.til.zip.ZipFile }
  278. assert_nothing_raised { Java::java.util.zip.ZipFile }
  279. end
  280. def test_that_subpackages_havent_leaked_into_other_packages
  281. assert_equal(false, Java::java.respond_to?(:zip))
  282. assert_equal(false, Java::com.respond_to?(:util))
  283. end
  284. def test_that_sub_packages_called_java_javax_com_org_arent_short_circuited
  285. #to their top-level conterparts
  286. assert(!com.equal?(java.flirble.com))
  287. end
  288. def test_that_we_get_the_same_package_instance_on_subsequent_calls
  289. assert(com.flirble.equal?(com.flirble))
  290. end
  291. @@include_proc = Proc.new do
  292. Thread.stop
  293. java_import "java.lang.System"
  294. java_import "java.lang.Runtime"
  295. Thread.current[:time] = System.currentTimeMillis
  296. Thread.current[:mem] = Runtime.getRuntime.freeMemory
  297. end
  298. # Disabled temporarily...keeps failing for no obvious reason
  299. =begin
  300. def test_that_multiple_threads_including_classes_dont_step_on_each_other
  301. # we swallow the output to $stderr, so testers don't have to see the
  302. # warnings about redefining constants over and over again.
  303. threads = []
  304. begin
  305. old_stream = $stderr.dup
  306. $stderr.reopen(RbConfig::CONFIG['target_os'] =~ /Windows|mswin/ ? 'NUL:' : '/dev/null')
  307. $stderr.sync = true
  308. 50.times do
  309. threads << Thread.new(&@@include_proc)
  310. end
  311. # wait for threads to all stop, then wake them up
  312. threads.each {|t| Thread.pass until t.stop?}
  313. threads.each {|t| t.run}
  314. # join each to let them run
  315. threads.each {|t| t.join }
  316. # confirm they all successfully called currentTimeMillis and freeMemory
  317. ensure
  318. $stderr.reopen(old_stream)
  319. end
  320. threads.each do |t|
  321. assert(t[:time])
  322. assert(t[:mem])
  323. end
  324. end
  325. =end
  326. unless (java.lang.System.getProperty("java.specification.version") == "1.4")
  327. if javax.xml.namespace.NamespaceContext.instance_of?(Module)
  328. class NSCT
  329. include javax.xml.namespace.NamespaceContext
  330. # JRUBY-66: No super here...make sure we still work.
  331. def initialize(arg)
  332. end
  333. def getNamespaceURI(prefix)
  334. 'ape:sex'
  335. end
  336. end
  337. else
  338. class NSCT < javax.xml.namespace.NamespaceContext
  339. # JRUBY-66: No super here...make sure we still work.
  340. def initialize(arg)
  341. end
  342. def getNamespaceURI(prefix)
  343. 'ape:sex'
  344. end
  345. end
  346. end
  347. def test_no_need_to_call_super_in_initialize_when_implementing_java_interfaces
  348. # No error is a pass here for JRUBY-66
  349. assert_nothing_raised do
  350. javax.xml.xpath.XPathFactory.newInstance.newXPath.setNamespaceContext(NSCT.new(1))
  351. end
  352. end
  353. end
  354. def test_can_see_inner_class_constants_with_same_name_as_top_level
  355. # JRUBY-425: make sure we can reference inner class names that match
  356. # the names of toplevel constants
  357. ell = java.awt.geom.Ellipse2D
  358. assert_nothing_raised { ell::Float.new }
  359. end
  360. def test_that_class_methods_are_being_camel_cased
  361. assert(java.lang.System.respond_to?("current_time_millis"))
  362. end
  363. if Java::java.lang.Runnable.instance_of?(Module)
  364. class TestInitBlock
  365. include Java::java.lang.Runnable
  366. def initialize(&block)
  367. raise if !block
  368. @bar = block.call
  369. end
  370. def bar; @bar; end
  371. end
  372. else
  373. class TestInitBlock < Java::java.lang.Runnable
  374. def initialize(&block)
  375. raise if !block
  376. @bar = block.call
  377. end
  378. def bar; @bar; end
  379. end
  380. end
  381. def test_that_blocks_are_passed_through_to_the_constructor_for_an_interface_impl
  382. assert_nothing_raised {
  383. assert_equal("foo", TestInitBlock.new { "foo" }.bar)
  384. }
  385. end
  386. def test_no_collision_with_ruby_allocate_and_java_allocate
  387. # JRUBY-232
  388. assert_nothing_raised { java.nio.ByteBuffer.allocate(1) }
  389. end
  390. # JRUBY-636 and other "extending Java classes"-issues
  391. class BigInt < java.math.BigInteger
  392. def initialize(val)
  393. super(val)
  394. end
  395. def test
  396. "Bit count = #{bitCount}"
  397. end
  398. end
  399. def test_extend_java_class
  400. assert_equal 2, BigInt.new("10").bitCount
  401. assert_equal "Bit count = 2", BigInt.new("10").test
  402. end
  403. class TestOS < java.io.OutputStream
  404. attr_reader :written
  405. def write(p)
  406. @written = true
  407. end
  408. end
  409. def test_extend_output_stream
  410. _anos = TestOS.new
  411. bos = java.io.BufferedOutputStream.new _anos
  412. bos.write 32
  413. bos.flush
  414. assert _anos.written
  415. end
  416. def test_impl_shortcut
  417. $has_run = false
  418. java.lang.Runnable.impl do
  419. $has_run = true
  420. end.run
  421. assert $has_run
  422. end
  423. # JRUBY-674
  424. OuterClass = org.jruby.javasupport.test.OuterClass
  425. def test_inner_class_proxies
  426. assert defined?(OuterClass::PublicStaticInnerClass)
  427. if RUBY_VERSION =~ /1\.9/
  428. assert OuterClass::PublicStaticInnerClass.instance_methods.include?(:a)
  429. else
  430. assert OuterClass::PublicStaticInnerClass.instance_methods.include?("a")
  431. end
  432. assert !defined?(OuterClass::ProtectedStaticInnerClass)
  433. assert !defined?(OuterClass::DefaultStaticInnerClass)
  434. assert !defined?(OuterClass::PrivateStaticInnerClass)
  435. assert defined?(OuterClass::PublicInstanceInnerClass)
  436. if RUBY_VERSION =~ /1\.9/
  437. assert OuterClass::PublicInstanceInnerClass.instance_methods.include?(:a)
  438. else
  439. assert OuterClass::PublicInstanceInnerClass.instance_methods.include?("a")
  440. end
  441. assert !defined?(OuterClass::ProtectedInstanceInnerClass)
  442. assert !defined?(OuterClass::DefaultInstanceInnerClass)
  443. assert !defined?(OuterClass::PrivateInstanceInnerClass)
  444. end
  445. # Test the new "import" syntax
  446. def test_import
  447. assert_nothing_raised {
  448. import java.nio.ByteBuffer
  449. ByteBuffer.allocate(10)
  450. }
  451. end
  452. def test_java_exception_handling
  453. list = ArrayList.new
  454. begin
  455. list.get(5)
  456. assert(false)
  457. rescue java.lang.IndexOutOfBoundsException => e
  458. assert(e.message =~ /Index: 5, Size: 0$/)
  459. end
  460. end
  461. # test for JRUBY-698
  462. def test_java_method_returns_null
  463. java_import 'org.jruby.test.ReturnsNull'
  464. rn = ReturnsNull.new
  465. assert_equal("", rn.returnNull.to_s)
  466. end
  467. # test for JRUBY-664
  468. class FinalMethodChildClass < FinalMethodBaseTest
  469. end
  470. def test_calling_base_class_final_method
  471. assert_equal("In foo", FinalMethodBaseTest.new.foo)
  472. assert_equal("In foo", FinalMethodChildClass.new.foo)
  473. end
  474. # test case for JRUBY-679
  475. # class Weather < java.util.Observable
  476. # def initialize(temp)
  477. # super()
  478. # @temp = temp
  479. # end
  480. # end
  481. # class Meteorologist < java.util.Observer
  482. # attr_reader :updated
  483. # def initialize(weather)
  484. # weather.addObserver(self)
  485. # end
  486. # def update(obs, arg)
  487. # @updated = true
  488. # end
  489. # end
  490. # def test_should_be_able_to_have_different_ctor_arity_between_ruby_subclass_and_java_superclass
  491. # assert_nothing_raised do
  492. # w = Weather.new(32)
  493. # m = Meteorologist.new(w)
  494. # w.notifyObservers
  495. # assert(m.updated)
  496. # end
  497. # end
  498. class A < java.lang.Object
  499. include org.jruby.javasupport.test.Interface1
  500. def method1
  501. end
  502. end
  503. A.new
  504. class B < A
  505. include org.jruby.javasupport.test.Interface2
  506. def method2
  507. end
  508. end
  509. B.new
  510. class C < B
  511. end
  512. C.new
  513. def test_interface_methods_seen
  514. ci = org.jruby.javasupport.test.ConsumeInterfaces.new
  515. ci.addInterface1(A.new)
  516. ci.addInterface1(B.new)
  517. ci.addInterface2(B.new)
  518. ci.addInterface1(C.new)
  519. ci.addInterface2(C.new)
  520. end
  521. class LCTestA < java::lang::Object
  522. include org::jruby::javasupport::test::Interface1
  523. def method1
  524. end
  525. end
  526. LCTestA.new
  527. class LCTestB < LCTestA
  528. include org::jruby::javasupport::test::Interface2
  529. def method2
  530. end
  531. end
  532. LCTestB.new
  533. class java::lang::Object
  534. def boo
  535. 'boo!'
  536. end
  537. end
  538. def test_lowercase_colon_package_syntax
  539. assert_equal(java::lang::String, java.lang.String)
  540. assert_equal('boo!', java.lang.String.new('xxx').boo)
  541. ci = org::jruby::javasupport::test::ConsumeInterfaces.new
  542. assert_equal('boo!', ci.boo)
  543. assert_equal('boo!', LCTestA.new.boo)
  544. assert_equal('boo!', LCTestB.new.boo)
  545. ci.addInterface1(LCTestA.new)
  546. ci.addInterface1(LCTestB.new)
  547. ci.addInterface2(LCTestB.new)
  548. end
  549. def test_marsal_java_object_fails
  550. assert_raises(TypeError) { Marshal.dump(java::lang::Object.new) }
  551. end
  552. def test_string_from_bytes
  553. assert_equal('foo', String.from_java_bytes('foo'.to_java_bytes))
  554. end
  555. # JRUBY-2088
  556. def test_package_notation_with_arguments
  557. assert_raises(ArgumentError) do
  558. java.lang("ABC").String
  559. end
  560. assert_raises(ArgumentError) do
  561. java.lang.String(123)
  562. end
  563. assert_raises(ArgumentError) do
  564. Java::se("foobar").com.Foobar
  565. end
  566. end
  567. # JRUBY-1545
  568. def test_creating_subclass_to_java_interface_raises_type_error
  569. assert_raises(TypeError) do
  570. eval(<<CLASSDEF)
  571. class FooXBarBarBar < Java::JavaLang::Runnable
  572. end
  573. CLASSDEF
  574. end
  575. end
  576. # JRUBY-781
  577. def test_that_classes_beginning_with_small_letter_can_be_referenced
  578. assert_equal Module, org.jruby.test.smallLetterClazz.class
  579. assert_equal Class, org.jruby.test.smallLetterClass.class
  580. end
  581. # JRUBY-1076
  582. def test_package_module_aliased_methods
  583. assert java.lang.respond_to?(:__constants__)
  584. assert java.lang.respond_to?(:__methods__)
  585. java.lang.String # ensure java.lang.String has been loaded
  586. if RUBY_VERSION =~ /1\.9/
  587. assert java.lang.__constants__.include?(:String)
  588. else
  589. assert java.lang.__constants__.include?('String')
  590. end
  591. end
  592. # JRUBY-2106
  593. def test_package_load_doesnt_set_error
  594. $! = nil
  595. undo = javax.swing.undo
  596. assert_nil($!)
  597. end
  598. # JRUBY-2106
  599. def test_top_level_package_load_doesnt_set_error
  600. $! = nil
  601. Java::boom
  602. assert_nil($!)
  603. $! = nil
  604. Java::Boom
  605. assert_nil($!)
  606. end
  607. # JRUBY-2169
  608. def test_java_class_resource_methods
  609. $CLASSPATH << 'test/org/jruby/javasupport/test/'
  610. file = 'test_java_class_resource_methods.properties'
  611. jc = JRuby.runtime.jruby_class_loader
  612. # get resource as URL
  613. url = jc.resource_as_url(file)
  614. assert(java.net.URL === url)
  615. assert(/^foo=bar/ =~ java.io.DataInputStream.new(url.content).read_line)
  616. # get resource as stream
  617. is = jc.resource_as_stream(file)
  618. assert(java.io.InputStream === is)
  619. assert(/^foo=bar/ =~ java.io.DataInputStream.new(is).read_line)
  620. # get resource as string
  621. str = jc.resource_as_string(file)
  622. assert(/^foo=bar/ =~ str)
  623. end
  624. # JRUBY-2169
  625. def test_ji_extended_methods_for_java_1_5
  626. jc = java.lang.String.java_class
  627. ctor = jc.constructors[0]
  628. meth = jc.java_instance_methods[0]
  629. field = jc.fields[0]
  630. # annotations
  631. assert(Annotation[] === jc.annotations)
  632. assert(Annotation[] === ctor.annotations)
  633. assert(Annotation[] === meth.annotations)
  634. assert(Annotation[] === field.annotations)
  635. # TODO: more extended methods to test
  636. end
  637. # JRUBY-2169
  638. def test_java_class_ruby_class
  639. assert java.lang.Object.java_class.ruby_class == java.lang.Object
  640. assert java.lang.Runnable.java_class.ruby_class == java.lang.Runnable
  641. end
  642. def test_null_toString
  643. assert nil == org.jruby.javasupport.test.NullToString.new.to_s
  644. end
  645. # JRUBY-2277
  646. # kind of a strange place for this test, but the error manifested
  647. # when JI was enabled. the actual bug was a problem in alias_method,
  648. # and not related to JI; see related test in test_methods.rb
  649. def test_alias_method_with_JI_enabled_does_not_raise
  650. name = Object.new
  651. def name.to_str
  652. "new_name"
  653. end
  654. assert_nothing_raised { String.send("alias_method", name, "to_str") }
  655. end
  656. # JRUBY-2671
  657. def test_coerce_array_to_java_with_javaobject_inside
  658. x = nil
  659. assert_nothing_raised { x = java.util.ArrayList.new([java.lang.Integer.new(1)]) }
  660. assert_equal("[1]", x.to_string)
  661. end
  662. # JRUBY-2865
  663. def test_extend_default_package_class
  664. cls = Class.new(Java::DefaultPackageClass);
  665. assert_nothing_raised { cls.new }
  666. end
  667. # JRUBY-3046
  668. def test_java_methods_have_arity
  669. assert_nothing_raised do
  670. assert_equal(-1, java.lang.String.instance_method(:toString).arity)
  671. end
  672. end
  673. # JRUBY-3476
  674. def test_object_with_singleton_returns_java_class
  675. x = java.lang.Object.new
  676. def x.foo; end
  677. assert(x.java_class.kind_of?Java::JavaClass)
  678. end
  679. # JRUBY-4524
  680. class IncludePackageSuper
  681. def self.const_missing(a)
  682. a
  683. end
  684. end
  685. class IncludePackageSub < IncludePackageSuper
  686. include_package 'java.util'
  687. def arraylist
  688. ArrayList
  689. end
  690. def blahblah
  691. BlahBlah
  692. end
  693. end
  694. def test_include_package_chains_super
  695. assert_equal java.util.ArrayList, IncludePackageSub.new.arraylist
  696. assert_equal :BlahBlah, IncludePackageSub.new.blahblah
  697. end
  698. # JRUBY-4529
  699. def test_float_always_coerces_to_java_float
  700. assert_nothing_raised do
  701. a = 1.0
  702. loop do
  703. a /= 2
  704. a.to_java :float
  705. break if a == 0.0
  706. end
  707. end
  708. end
  709. end