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
|
require File.dirname(__FILE__) + "/../spec_helper"
describe "A Java object's java_method method" do
before :each do
@list = java.util.ArrayList.new
@integer = java.lang.Integer.new(1)
end
it "works with name only for no-arg methods" do
m = @list.java_method :toString
m.call.should == "[]"
end
it "works with name plus empty array for no-arg methods" do
m = @list.java_method :toString, []
m.call.should == "[]"
end
it "works with a signature" do
m = @list.java_method :add, [Java::int, java.lang.Object]
m.call(0, 'foo')
@list.to_s.should == "[foo]"
end
it "produces Method for static methods against an instance" do
m = @integer.java_method :valueOf, [Java::int]
# JRUBY-4107
#m.call(1).should == @integer
m.call(1).should == 1
end
it "produces UnboundMethod for instance methods against a class" do
m = java.util.ArrayList.java_method :add, [Java::int, java.lang.Object]
m.bind(@list).call(0, 'foo')
@list.to_s.should == "[foo]"
end
it "produces Method for static methods against a class" do
m = java.lang.Integer.java_method :valueOf, [Java::int]
# JRUBY-4107
#m.call(1).should == @integer
m.call(1).should == 1
end
it "raises NameError if the method can't be found" do
lambda do
@list.java_method :foobar
end.should raise_error(NameError)
lambda do
@list.java_method :add, [Java::long, java.lang.Object]
end.should raise_error(NameError)
lambda do
java.lang.Integer.java_method :foobar
end.should raise_error(NameError)
lambda do
java.lang.Integer.java_method :valueOf, [Java::long]
end.should raise_error(NameError)
end
it "calls static methods" do
lambda {
import 'java_integration.fixtures.PackageStaticMethod'
method = PackageStaticMethod.java_class.declared_method_smart :thePackageScopeMethod
method.accessible = true
method.invoke nil.to_java
}.should_not raise_error
end
end
|