PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_java_extension.rb

https://github.com/moses/jruby
Ruby | 260 lines | 210 code | 48 blank | 2 comment | 14 complexity | 7763c4ef0aec6b298bfc5def90ec4189 MD5 | raw file
  1. require 'java'
  2. require 'test/unit'
  3. class TestJavaExtension < Test::Unit::TestCase
  4. import org.jruby.test.Worker
  5. import org.jruby.test.Abstract
  6. class TestParent < org.jruby.test.Parent
  7. attr_accessor :result
  8. def run(a)
  9. @result = "TEST PARENT: #{a}"
  10. end
  11. end
  12. def test_overriding_method_in_java_superclass
  13. w = Worker.new
  14. p = TestParent.new
  15. w.run_parent(p)
  16. assert_equal "TEST PARENT: WORKER", p.result
  17. end
  18. import java.util.HashMap
  19. import java.util.ArrayList
  20. import java.util.HashSet
  21. import java.lang.Short
  22. def test_set
  23. set = HashSet.new
  24. set.add(1)
  25. set.add(2)
  26. newSet = []
  27. set.each {|x| newSet << x }
  28. assert newSet.include?(1)
  29. assert newSet.include?(2)
  30. end
  31. def test_comparable
  32. one = Short.new(1)
  33. two = Short.new(2)
  34. three = Short.new(3)
  35. list = [three, two, one]
  36. list = list.sort
  37. assert_equal([one, two, three], list)
  38. end
  39. def test_map
  40. map = HashMap.new
  41. map.put('A','1')
  42. map.put('C','3')
  43. hash = Hash.new
  44. map.each {|key, value| hash[key] = value }
  45. assert_equal('1', hash['A'])
  46. assert_equal('3', hash['C'])
  47. end
  48. def test_list
  49. a = ArrayList.new
  50. a << 3
  51. a << 1
  52. a << 2
  53. assert([1, 2, 3], a.sort)
  54. assert([1, 2], a[2...3])
  55. assert([3, 1], a[0, 2])
  56. assert([1], a.select {|e| e >= 1 })
  57. end
  58. import org.jruby.test.TestHelper
  59. import java.lang.RuntimeException
  60. import java.lang.NullPointerException
  61. import("org.jruby.test.TestHelper$TestHelperException") {"THException"}
  62. def test_catch_java_exception_with_rescue
  63. begin
  64. TestHelper.throwTestHelperException
  65. rescue THException => e
  66. end
  67. end
  68. def test_catch_multiple_java_exceptions_picks_correct_rescue
  69. begin
  70. TestHelper.throwTestHelperException
  71. rescue NullPointerException => e
  72. flunk("Should not rescue")
  73. rescue THException => e
  74. end
  75. end
  76. def test_catch_java_exception_by_superclass
  77. begin
  78. TestHelper.throwTestHelperException
  79. rescue RuntimeException => e
  80. end
  81. end
  82. def test_catch_java_exception_by_ruby_native_exception
  83. begin
  84. TestHelper.throwTestHelperException
  85. rescue NativeException => e
  86. end
  87. end
  88. BLUE = "blue"
  89. GREEN = "green"
  90. import org.jruby.javasupport.test.Color
  91. def test_java_bean_conventions_in_ruby
  92. # Java bean convention properties as attributes
  93. color = Color.new(GREEN)
  94. assert !color.isDark
  95. color.dark = true
  96. assert color.dark
  97. assert color.dark?
  98. assert_equal GREEN, color.color
  99. color.color = BLUE
  100. assert_equal BLUE, color.color
  101. end
  102. include_package 'org.jruby.javasupport.test'
  103. include_package 'java.lang'
  104. java_alias :JString, :String
  105. def test_java_proxy_object_equivalence
  106. room1 = Room.new("Bedroom")
  107. room2 = Room.new("Bedroom")
  108. room3 = Room.new("Bathroom")
  109. assert(room1 == room2);
  110. assert(room1 == room2.java_object);
  111. assert(room1.java_object == room2.java_object)
  112. assert(room1.java_object == room2)
  113. assert(room1 != room3)
  114. assert(room1 != room3.java_object)
  115. assert(room1.java_object != room3.java_object)
  116. assert(room1.java_object != room3)
  117. assert(room1.java_object != "Bedroom")
  118. assert("Bedroom" == room1.to_s)
  119. assert(room1.to_s == "Bedroom")
  120. assert(room1.equal?(room1))
  121. assert(!room1.equal?(room2))
  122. assert(JString.new("Bedroom").hashCode() == room1.hash())
  123. assert(JString.new("Bathroom").hashCode() == room3.hash())
  124. assert(room1.hash() != room3.hash())
  125. roomArray = Room[1].new
  126. roomArray[0] = room1
  127. assert_equal(room1, roomArray[0])
  128. assert_equal(1, roomArray.length)
  129. end
  130. def test_synchronized_method_available
  131. # FIXME: this doesn't actually test that we're successfully synchronizing
  132. obj = java.lang.Object.new
  133. result = nil
  134. assert_nothing_raised {
  135. result = obj.synchronized { "foo" }
  136. }
  137. assert_equal("foo", result)
  138. assert_raises(NativeException) {
  139. obj.wait 1
  140. }
  141. assert_nothing_raised {
  142. obj.synchronized { obj.wait 1 }
  143. }
  144. end
  145. def test_java_interface_impl_with_block
  146. ran = false
  147. SimpleExecutor::WrappedByMethodCall.new.execute(Runnable.impl {ran = true})
  148. assert ran
  149. end
  150. def test_ruby_object_duck_typed_as_java_interface_when_passed_to_method
  151. runnable = Object.new
  152. def runnable.run; @ran ||= true; end
  153. def runnable.ran; @ran; end
  154. SimpleExecutor::WrappedByMethodCall.new.execute(runnable)
  155. assert runnable.ran
  156. end
  157. def test_ruby_object_duck_typed_as_java_interface_when_passed_to_ctor
  158. runnable = Object.new
  159. def runnable.run; @ran ||= true; end
  160. def runnable.ran; @ran; end
  161. SimpleExecutor::WrappedByConstructor.new(runnable).execute
  162. assert runnable.ran
  163. end
  164. def test_ruby_proc_passed_to_ctor_as_last_argument
  165. ran = false
  166. SimpleExecutor::WrappedByConstructor.new(proc {ran = true}).execute
  167. assert ran
  168. end
  169. def test_ruby_proc_duck_typed_as_runnable
  170. ran = false
  171. SimpleExecutor::WrappedByMethodCall.new.execute(proc {ran = true})
  172. assert ran
  173. end
  174. def test_ruby_proc_duck_typed_as_runnable_last_argument
  175. ran = 0
  176. SimpleExecutor::MultipleArguments.new.execute(2, proc {ran += 1})
  177. assert_equal 2, ran
  178. end
  179. def test_ruby_block_duck_typed_as_runnable
  180. ran = false
  181. SimpleExecutor::WrappedByMethodCall.new.execute { ran = true }
  182. assert ran
  183. end
  184. def test_ruby_block_duck_typed_as_runnable_last_argument
  185. ran = 0
  186. SimpleExecutor::MultipleArguments.new.execute(2) {ran += 1}
  187. assert_equal 2, ran
  188. end
  189. def test_ruby_block_with_args_as_interface
  190. file = java.io.File.new(".")
  191. listing = file.list {|file,str| !!(str =~ /\./) }
  192. assert listing.size >= 0
  193. end
  194. class ExtendedClass < org.jruby.test.Abstract
  195. def protected_method
  196. "Ruby overrides java!"
  197. end
  198. end
  199. def test_overriding_protected_method
  200. a = ExtendedClass.new
  201. begin
  202. assert_equal "Ruby overrides java!", a.call_protected
  203. rescue Exception => e
  204. flunk "Exception raised: #{$!}"
  205. end
  206. end
  207. def test_map_interface_to_array
  208. hash = {"one"=>"two","three"=>"four"}
  209. map = java.util.HashMap.new(hash)
  210. assert_equal hash.to_a.sort, map.to_a.sort
  211. end
  212. end