1 require File.dirname(__FILE__) + "/../spec_helper"
3 import java.lang.reflect.Field
4 import "java_integration.fixtures.PrivateField"
5 import "java_integration.fixtures.ProtectedField"
6 import "java_integration.fixtures.PublicField"
7 import "java_integration.fixtures.PackageField"
9 describe "A JavaClass" do
10 it "should provide a look up for fields using a Java formatted name" do
11 PrivateField.java_class.declared_field(:strField).should_not == nil
12 ProtectedField.java_class.declared_field(:strField).should_not == nil
13 PublicField.java_class.declared_field(:strField).should_not == nil
14 PackageField.java_class.declared_field(:strField).should_not == nil
17 it "should provide a look up for a fields using a Ruby formatted name" do
18 PrivateField.java_class.declared_field(:str_field).should_not == nil
19 ProtectedField.java_class.declared_field(:str_field).should_not == nil
20 PublicField.java_class.declared_field(:str_field).should_not == nil
21 PackageField.java_class.declared_field(:str_field).should_not == nil
25 describe "A JavaField" do
26 describe "given a partially unwrapped Java object" do
27 describe "with a private field" do
29 @obj = PrivateField.new
30 @field = PrivateField.java_class.declared_field :strField
31 @field.accessible = true
34 it "should set Ruby values" do
35 lambda { @field.set_value @obj.java_object, "42" }.should_not raise_error
36 lambda { @field.set_value @obj.java_object, nil }.should_not raise_error
39 it "should set Java values" do
40 lambda { @field.set_value @obj.java_object, Java.ruby_to_java("42") }.should_not raise_error
41 lambda { @field.set_value @obj.java_object, Java.ruby_to_java(nil) }.should_not raise_error
44 it "should get Ruby values" do
45 @field.set_value @obj.java_object, "42"
46 @field.value(@obj.java_object).should == "42"
48 @field.set_value @obj.java_object, nil
49 @field.value(@obj.java_object).should be_nil
53 describe "with a protected field" do
55 @obj = ProtectedField.new
56 @field = ProtectedField.java_class.declared_field :strField
57 @field.accessible = true
60 it "should set Ruby values" do
61 lambda { @field.set_value @obj.java_object, "42" }.should_not raise_error
62 lambda { @field.set_value @obj.java_object, nil }.should_not raise_error
65 it "should set Java values" do
66 lambda { @field.set_value @obj.java_object, Java.ruby_to_java("42") }.should_not raise_error
67 lambda { @field.set_value @obj.java_object, Java.ruby_to_java(nil) }.should_not raise_error
70 it "should get Ruby values" do
71 @field.set_value @obj.java_object, "42"
72 @field.value(@obj.java_object).should == "42"
74 @field.set_value @obj.java_object, nil
75 @field.value(@obj.java_object).should be_nil
79 describe "with a public field" do
81 @obj = PublicField.new
82 @field = PublicField.java_class.declared_field :strField
85 it "should set Ruby values" do
86 lambda { @field.set_value @obj.java_object, "42" }.should_not raise_error
87 lambda { @field.set_value @obj.java_object, nil }.should_not raise_error
90 it "should set Java values" do
91 lambda { @field.set_value @obj.java_object, Java.ruby_to_java("42") }.should_not raise_error
92 lambda { @field.set_value @obj.java_object, Java.ruby_to_java(nil) }.should_not raise_error
96 it "should get Ruby values" do
97 @field.set_value @obj.java_object, "42"
98 @field.value(@obj.java_object).should == "42"
100 @field.set_value @obj.java_object, nil
101 @field.value(@obj.java_object).should be_nil
105 describe "with a package field" do
107 @obj = PackageField.new
108 @field = PackageField.java_class.declared_field :strField
109 @field.accessible = true
112 it "should set Ruby values" do
113 lambda { @field.set_value @obj.java_object, "42" }.should_not raise_error
114 lambda { @field.set_value @obj.java_object, nil }.should_not raise_error
117 it "should set Java values" do
118 lambda { @field.set_value @obj.java_object, Java.ruby_to_java("42") }.should_not raise_error
119 lambda { @field.set_value @obj.java_object, Java.ruby_to_java(nil) }.should_not raise_error
122 it "should get Ruby values" do
123 @field.set_value @obj.java_object, "42"
124 @field.value(@obj.java_object).should == "42"
126 @field.set_value @obj.java_object, nil
127 @field.value(@obj.java_object).should be_nil
132 describe "given a Ruby-wrapped Java object" do
133 describe "with a private field" do
135 @obj = PrivateField.new
136 @field = PrivateField.java_class.declared_field :strField
137 @field.accessible = true
140 it "should set Ruby values" do
141 lambda { @field.set_value @obj, "42" }.should_not raise_error
142 lambda { @field.set_value @obj, nil }.should_not raise_error
145 it "should set Java values" do
146 lambda { @field.set_value @obj, Java.ruby_to_java("42") }.should_not raise_error
147 lambda { @field.set_value @obj, Java.ruby_to_java(nil) }.should_not raise_error
150 it "should get Ruby values" do
151 @field.set_value @obj, "42"
152 @field.value(@obj).should == "42"
154 @field.set_value @obj, nil
155 @field.value(@obj).should be_nil
159 describe "with a protected field" do
161 @obj = ProtectedField.new
162 @field = ProtectedField.java_class.declared_field :strField
163 @field.accessible = true
166 it "should set Ruby values" do
167 lambda { @field.set_value @obj, "42" }.should_not raise_error
168 lambda { @field.set_value @obj, nil }.should_not raise_error
171 it "should set Java values" do
172 lambda { @field.set_value @obj, Java.ruby_to_java("42") }.should_not raise_error
173 lambda { @field.set_value @obj, Java.ruby_to_java(nil) }.should_not raise_error
176 it "should get Ruby values" do
177 @field.set_value @obj, "42"
178 @field.value(@obj).should == "42"
180 @field.set_value @obj, nil
181 @field.value(@obj).should be_nil
185 describe "with a public field" do
187 @obj = PublicField.new
188 @field = PublicField.java_class.declared_field :strField
191 it "should set Ruby values" do
192 lambda { @field.set_value @obj, "42" }.should_not raise_error
193 lambda { @field.set_value @obj, nil }.should_not raise_error
196 it "should set Java values" do
197 lambda { @field.set_value @obj, Java.ruby_to_java("42") }.should_not raise_error
198 lambda { @field.set_value @obj, Java.ruby_to_java(nil) }.should_not raise_error
201 it "should get Ruby values" do
202 @field.set_value @obj, "42"
203 @field.value(@obj).should == "42"
205 @field.set_value @obj, nil
206 @field.value(@obj).should be_nil
210 describe "with a package field" do
212 @obj = PackageField.new
213 @field = PackageField.java_class.declared_field :strField
214 @field.accessible = true
217 it "should set Ruby values" do
218 lambda { @field.set_value @obj, "42" }.should_not raise_error
219 lambda { @field.set_value @obj, nil }.should_not raise_error
222 it "should set Java values" do
223 lambda { @field.set_value @obj, Java.ruby_to_java("42") }.should_not raise_error
224 lambda { @field.set_value @obj, Java.ruby_to_java(nil) }.should_not raise_error
227 it "should get Ruby values" do
228 @field.set_value @obj, "42"
229 @field.value(@obj).should == "42"
231 @field.set_value @obj, nil
232 @field.value(@obj).should be_nil