File: access_spec.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (104 lines) | stat: -rw-r--r-- 2,461 bytes parent folder | download | duplicates (4)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require File.dirname(__FILE__) + "/../spec_helper"

import "java_integration.fixtures.PrivateField"
import "java_integration.fixtures.ProtectedField"
import "java_integration.fixtures.PublicField"
import "java_integration.fixtures.PackageField"

class PrivateField
  field_accessor :strField => :field
end

class ProtectedField
  field_reader :strField => :field
end

class PackageField
  field_accessor :strField => :field

  def existing_method
    "meth"
  end

  field_reader :strField => :existing_method
end

describe "JRuby-wrapped Java Objects" do
  it "should expose private Java fields when field_accessor used" do
    lambda {
      PrivateField.new.field.should == "1764"
    }.should_not raise_error
    
    lambda {
      obj = PrivateField.new
      obj.field = "foo"
      obj.field.should == "foo"
    }.should_not raise_error
  end
  
  it "should expose protected Java fields when field_accessor used" do
    lambda {
      ProtectedField.new.field.should == "1765" 
   }.should_not raise_error
  end
  
  it "should expose public-visible fields" do
    lambda {
      PublicField.new.strField.should == "1767"
    }.should_not raise_error

    year = java.util.Date.new.year

    lambda {
      PublicField.new.dateField.year.should == year
    }.should_not raise_error
  end

  it "should expose package-visible fields" do
    lambda {
      PackageField.new.field.should == "1766"
    }.should_not raise_error
    
    lambda {
      obj = PackageField.new
      obj.field = "foo"
      obj.field.should == "foo"
    }.should_not raise_error
  end

  it "should throw an error for a field which does not exist" do
    lambda {
      class PackageField
        field_accessor(:totallyBogus).should raise_error
      end
    }
  end

  it "should throw an error for one field which does not exist of two" do
    lambda {
      class PackageField
        field_accessor(:strField, :totallyBogus).should raise_error
      end
    }
  end

  it "should not allow field_accessor to work on final field" do
    lambda {
      class PrivateField
        field_accessor(:finalStrField).should raise_error
      end
    }
  end

  it "should access to static fields" do
    lambda {
      expected = ["/"].to_java(:string)

      field = java.lang.ClassLoader.java_class.declared_field("sys_paths")
      field.accessible = true

      field.set_value(nil, expected)
      field.value(nil).should == expected
    }.should_not raise_error
  end
end