about_java_interop.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. require File.expand_path(File.dirname(__FILE__) + '/neo')
  2. include Java
  3. # Concepts
  4. # * Pull in a java class
  5. # * calling a method, Camel vs snake
  6. # * Resolving module/class name conflicts
  7. # * Showing what gets returned
  8. # * Ruby Strings VS Java Strings
  9. # * Calling custom java class
  10. # * Calling Ruby from java???
  11. class AboutJavaInterop < Neo::Koan
  12. def test_using_a_java_library_class
  13. java_array = java.util.ArrayList.new
  14. assert_equal __(Java::JavaUtil::ArrayList), java_array.class
  15. end
  16. def test_java_class_can_be_referenced_using_both_ruby_and_java_like_syntax
  17. assert_equal __(true), Java::JavaUtil::ArrayList == java.util.ArrayList
  18. end
  19. def test_include_class_includes_class_in_module_scope
  20. assert_nil defined?(TreeSet) # __
  21. include_class "java.util.TreeSet"
  22. assert_equal __("constant"), defined?(TreeSet)
  23. end
  24. # THINK ABOUT IT:
  25. #
  26. # What if we use:
  27. #
  28. # include_class "java.lang.String"
  29. #
  30. # What would be the value of the String constant after this
  31. # include_class is run? Would it be useful to provide a way of
  32. # aliasing java classes to different names?
  33. JString = java.lang.String
  34. def test_also_java_class_can_be_given_ruby_aliases
  35. java_string = JString.new("A Java String")
  36. assert_equal __(java.lang.String), java_string.class
  37. assert_equal __(java.lang.String), JString
  38. end
  39. def test_can_directly_call_java_methods_on_java_objects
  40. java_string = JString.new("A Java String")
  41. assert_equal __("a java string"), java_string.toLowerCase
  42. end
  43. def test_jruby_provides_snake_case_versions_of_java_methods
  44. java_string = JString.new("A Java String")
  45. assert_equal __("a java string"), java_string.to_lower_case
  46. end
  47. def test_jruby_provides_question_mark_versions_of_boolean_methods
  48. java_string = JString.new("A Java String")
  49. assert_equal __(true), java_string.endsWith("String")
  50. assert_equal __(true), java_string.ends_with("String")
  51. assert_equal __(true), java_string.ends_with?("String")
  52. end
  53. def test_java_string_are_not_ruby_strings
  54. ruby_string = "A Java String"
  55. java_string = java.lang.String.new(ruby_string)
  56. assert_equal __(true), java_string.is_a?(java.lang.String)
  57. assert_equal __(false), java_string.is_a?(String)
  58. end
  59. def test_java_strings_can_be_compared_to_ruby_strings_maybe
  60. ruby_string = "A Java String"
  61. java_string = java.lang.String.new(ruby_string)
  62. assert_equal __(false), ruby_string == java_string
  63. assert_equal __(true), java_string == ruby_string
  64. # THINK ABOUT IT:
  65. #
  66. # Is there any possible way for this to be more wrong?
  67. #
  68. # SERIOUSLY, THINK ABOUT IT:
  69. #
  70. # Why do you suppose that Ruby and Java strings compare like that?
  71. #
  72. # ADVANCED THINK ABOUT IT:
  73. #
  74. # Is there a way to make Ruby/Java string comparisons commutative?
  75. # How would you do it?
  76. end
  77. def test_however_most_methods_returning_strings_return_ruby_strings
  78. java_array = java.util.ArrayList.new
  79. assert_equal __("[]"), java_array.toString
  80. assert_equal __(true), java_array.toString.is_a?(String)
  81. assert_equal __(false), java_array.toString.is_a?(java.lang.String)
  82. end
  83. def test_some_ruby_objects_can_be_coerced_to_java
  84. assert_equal __(Java::JavaLang::String), "ruby string".to_java.class
  85. assert_equal __(Java::JavaLang::Long), 1.to_java.class
  86. assert_equal __(Java::JavaLang::Double), 9.32.to_java.class
  87. assert_equal __(Java::JavaLang::Boolean), false.to_java.class
  88. end
  89. def test_some_ruby_objects_are_not_coerced_to_what_you_might_expect
  90. assert_equal __(false), [].to_java.class == Java::JavaUtil::ArrayList
  91. assert_equal __(false), {}.to_java.class == Java::JavaUtil::HashMap
  92. assert_equal __(false), Object.new.to_java.class == Java::JavaLang::Object
  93. end
  94. def test_java_collections_are_enumerable
  95. java_array = java.util.ArrayList.new
  96. java_array << "one" << "two" << "three"
  97. assert_equal __(["ONE", "TWO", "THREE"]), java_array.map { |item| item.upcase }
  98. end
  99. # ------------------------------------------------------------------
  100. # Open the Java ArrayList class and add a new method.
  101. class Java::JavaUtil::ArrayList
  102. def multiply_all
  103. result = 1
  104. each do |item|
  105. result *= item
  106. end
  107. result
  108. end
  109. end
  110. def test_java_class_are_open_from_ruby
  111. java_array = java.util.ArrayList.new
  112. java_array.add_all([1,2,3,4,5])
  113. assert_equal __(120), java_array.multiply_all
  114. end
  115. end