diff --git a/src/builtin/javasupport/core_ext/module.rb b/src/builtin/javasupport/core_ext/module.rb index 98bb30c5a12..4297ca12d3b 100644 --- a/src/builtin/javasupport/core_ext/module.rb +++ b/src/builtin/javasupport/core_ext/module.rb @@ -33,6 +33,9 @@ def self.const_missing(constant) # for the file in question, and failing that by adding a const_missing hook # to try that package when constants are missing. def import(package_name) + return super(package_name) if package_name.respond_to?(:java_class) || package_name.split(/\./).last =~ /^[A-Z]/ + + package_name = package_name._name if package_name.respond_to?(:_name) warn "importing full package name is *highly* experimental...proceed with caution" class_list = org.jruby.util.PackageSearch.findClassesInPackage(package_name) diff --git a/test/org/jruby/javasupport/test/SimpleExecutor.java b/test/org/jruby/javasupport/test/SimpleExecutor.java new file mode 100644 index 00000000000..f78f0e8f411 --- /dev/null +++ b/test/org/jruby/javasupport/test/SimpleExecutor.java @@ -0,0 +1,7 @@ +package org.jruby.javasupport.test; + +public class SimpleExecutor { + public void execute(Runnable r) { + r.run(); + } +} \ No newline at end of file diff --git a/test/testJavaExtensions.rb b/test/testJavaExtensions.rb deleted file mode 100644 index 53b8b081bbe..00000000000 --- a/test/testJavaExtensions.rb +++ /dev/null @@ -1,116 +0,0 @@ -require 'test/minirunit' -test_check "Java Extensions Support" - -if defined? Java - - require 'java' - - module JavaCollections - include_class 'java.util.HashMap' - include_class "java.util.ArrayList" - include_class "java.util.HashSet" - include_class "java.lang.Short" - - - def self.testSet - set = HashSet.new - set.add(1) - set.add(2) - - newSet = [] - set.each {|x| newSet << x } - - test_ok newSet.include?(1) - test_ok newSet.include?(2) - end - - def self.testComparable - one = Short.new(1) - two = Short.new(2) - three = Short.new(3) - list = [ three, two, one] - list = list.sort - test_equal([one, two, three], list) - end - - def self.testMap - map = HashMap.new - map.put('A','1'); - map.put('C','3'); - - hash = Hash.new - map.each {|key, value| - hash[key] = value - } - test_equal('1', hash['A']) - test_equal('3', hash['C']) - end - - def self.testList - a = ArrayList.new - - a << 3 - a << 1 - a << 2 - - test_ok([1, 2, 3], a.sort) - test_ok([1], a.select {|e| e >= 1 }) - end - - testSet - testMap - testList - testComparable - end - - module JavaExceptions - include_class 'org.jruby.test.TestHelper' - include_class 'java.lang.RuntimeException' - include_class 'java.lang.NullPointerException' - include_class 'org.jruby.test.TestHelper$TestHelperException' do |p,c| "THException"; end - begin - TestHelper.throwTestHelperException - rescue THException => e - end - - begin - TestHelper.throwTestHelperException - rescue NullPointerException => e - test_fail("Should not rescue") - rescue THException => e - end - - begin - TestHelper.throwTestHelperException - rescue RuntimeException => e - end - - begin - TestHelper.throwTestHelperException - rescue NativeException => e - test_equal e.backtrace.size, e.backtrace.size - end - end - - module JavaBeans - BLUE = "blue" - GREEN = "green" - - include_class 'org.jruby.javasupport.test.Color' - # Java bean convention properties as attributes - color = Color.new(GREEN) - - test_ok !color.isDark - color.dark = true - test_ok color.dark - test_ok color.dark? - - - test_equal GREEN, color.color - - color.color = BLUE - test_equal BLUE, color.color - - - end -end diff --git a/test/testJavaProxy.rb b/test/testJavaProxy.rb deleted file mode 100644 index 5303b85af66..00000000000 --- a/test/testJavaProxy.rb +++ /dev/null @@ -1,60 +0,0 @@ -require 'java' -require 'test/minirunit' -test_check "Test JavaProxy:" - -class RoomTest - include_package 'org.jruby.javasupport.test' - include_package 'java.lang' - - java_alias :JString, :String - - def testObject - room1 = Room.new("Bedroom") - room2 = Room.new("Bedroom") - room3 = Room.new("Bathroom") - - test_ok(room1 == room2); - test_ok(room1 == room2.java_object); - test_ok(room1.java_object == room2.java_object) - test_ok(room1.java_object == room2) - - test_ok(room1 != room3) - test_ok(room1 != room3.java_object) - test_ok(room1.java_object != room3.java_object) - test_ok(room1.java_object != room3) - test_ok(room1.java_object != "Bedroom") - - test_ok("Bedroom" == room1.to_s) - test_ok(room1.to_s == "Bedroom") - - test_ok(room1.equal?(room1)) - test_ok(!room1.equal?(room2)) - - test_ok(JString.new("Bedroom").hashCode() == room1.hash()) - test_ok(JString.new("Bathroom").hashCode() == room3.hash()) - test_ok(room1.hash() != room3.hash()) - - roomArray = Room[1].new - roomArray[0] = room1 - test_equal(room1, roomArray[0]) - test_equal(1, roomArray.length) - end -end - -RoomTest.new.testObject - -###### test synchronized method - -# FIXME: this doesn't actually test that we're successfully synchronizing -obj = java.lang.Object.new -result = nil -test_no_exception { - result = obj.synchronized { "foo" } -} -test_equal("foo", result) -test_exception { - obj.wait 1 -} -test_no_exception { - obj.synchronized { obj.wait 1 } -} diff --git a/test/test_index b/test/test_index index 70b6ac0aca9..72a7d274f16 100644 --- a/test/test_index +++ b/test/test_index @@ -48,8 +48,6 @@ testInstantiatingInterfaces.rb testInteger.rb testIO.rb testJavaArraySupport.rb -testJavaExtensions.rb -testJavaProxy.rb testKernel.rb testLine.rb testLine_block_comment.rb diff --git a/test/test_java_extension.rb b/test/test_java_extension.rb index 76c5b46ee91..439be19605c 100644 --- a/test/test_java_extension.rb +++ b/test/test_java_extension.rb @@ -1,8 +1,9 @@ require 'java' - -include_class 'org.jruby.test.Worker' +require 'test/unit' class TestJavaExtension < Test::Unit::TestCase + import org.jruby.test.Worker + class TestParent < org.jruby.test.Parent attr_accessor :result def run(a) @@ -10,10 +11,174 @@ def run(a) end end - def test_overriding_method + def test_overriding_method_in_java_superclass w = Worker.new p = TestParent.new w.run_parent(p) assert_equal "TEST PARENT: WORKER", p.result end + + import java.util.HashMap + import java.util.ArrayList + import java.util.HashSet + import java.lang.Short + + def test_set + set = HashSet.new + set.add(1) + set.add(2) + + newSet = [] + set.each {|x| newSet << x } + + assert newSet.include?(1) + assert newSet.include?(2) + end + + def test_comparable + one = Short.new(1) + two = Short.new(2) + three = Short.new(3) + list = [three, two, one] + list = list.sort + assert_equal([one, two, three], list) + end + + def test_map + map = HashMap.new + map.put('A','1') + map.put('C','3') + + hash = Hash.new + map.each {|key, value| hash[key] = value } + assert_equal('1', hash['A']) + assert_equal('3', hash['C']) + end + + def test_list + a = ArrayList.new + + a << 3 + a << 1 + a << 2 + + assert([1, 2, 3], a.sort) + assert([1], a.select {|e| e >= 1 }) + end + + import org.jruby.test.TestHelper + import java.lang.RuntimeException + import java.lang.NullPointerException + import("org.jruby.test.TestHelper$TestHelperException") {"THException"} + + def test_catch_java_exception_with_rescue + begin + TestHelper.throwTestHelperException + rescue THException => e + end + end + + def test_catch_multiple_java_exceptions_picks_correct_rescue + begin + TestHelper.throwTestHelperException + rescue NullPointerException => e + flunk("Should not rescue") + rescue THException => e + end + end + + def test_catch_java_exception_by_superclass + begin + TestHelper.throwTestHelperException + rescue RuntimeException => e + end + end + + def test_catch_java_exception_by_ruby_native_exception + begin + TestHelper.throwTestHelperException + rescue NativeException => e + end + end + + BLUE = "blue" + GREEN = "green" + + import org.jruby.javasupport.test.Color + + def test_java_bean_conventions_in_ruby + # Java bean convention properties as attributes + color = Color.new(GREEN) + + assert !color.isDark + color.dark = true + assert color.dark + assert color.dark? + + assert_equal GREEN, color.color + + color.color = BLUE + assert_equal BLUE, color.color + end + + include_package 'org.jruby.javasupport.test' + include_package 'java.lang' + + java_alias :JString, :String + + def test_java_proxy_object_equivalence + room1 = Room.new("Bedroom") + room2 = Room.new("Bedroom") + room3 = Room.new("Bathroom") + + assert(room1 == room2); + assert(room1 == room2.java_object); + assert(room1.java_object == room2.java_object) + assert(room1.java_object == room2) + + assert(room1 != room3) + assert(room1 != room3.java_object) + assert(room1.java_object != room3.java_object) + assert(room1.java_object != room3) + assert(room1.java_object != "Bedroom") + + assert("Bedroom" == room1.to_s) + assert(room1.to_s == "Bedroom") + + assert(room1.equal?(room1)) + assert(!room1.equal?(room2)) + + assert(JString.new("Bedroom").hashCode() == room1.hash()) + assert(JString.new("Bathroom").hashCode() == room3.hash()) + assert(room1.hash() != room3.hash()) + + roomArray = Room[1].new + roomArray[0] = room1 + assert_equal(room1, roomArray[0]) + assert_equal(1, roomArray.length) + end + + def test_synchronized_method_available + # FIXME: this doesn't actually test that we're successfully synchronizing + obj = java.lang.Object.new + result = nil + assert_nothing_raised { + result = obj.synchronized { "foo" } + } + assert_equal("foo", result) + assert_raises(NativeException) { + obj.wait 1 + } + assert_nothing_raised { + obj.synchronized { obj.wait 1 } + } + end + + + def test_java_interface_impl_with_block + ran = false + executor = SimpleExecutor.new + executor.execute(Runnable.impl {ran = true}) + assert ran + end end