File: rescue_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (252 lines) | stat: -rw-r--r-- 5,868 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require File.dirname(__FILE__) + "/../spec_helper"

java_import java.lang.OutOfMemoryError
java_import "java_integration.fixtures.ThrowExceptionInInitializer"
java_import "java_integration.fixtures.ExceptionRunner"

describe "A non-wrapped Java error" do
  it "can be rescued using the Java type" do
    exception = OutOfMemoryError.new
    begin
      raise exception
    rescue OutOfMemoryError => oome
    end

    oome.should == exception
  end

  it "can be rescued using Object" do
    begin
      raise OutOfMemoryError.new
    rescue Object => e
      e.should be_kind_of(OutOfMemoryError)
    end
  end

  it "can be rescued using Exception" do
    exception = OutOfMemoryError.new
    begin
      raise exception
    rescue Exception => e
    end

    e.should == exception
  end

  it "cannot be rescued using StandardError" do
    exception = OutOfMemoryError.new
    lambda do
      begin
        raise exception
      rescue => e
      end
    end.should raise_error(exception)
  end

  it "cannot be rescued inline" do
    obj = Object.new
    def obj.go
      raise OutOfMemoryError.new
    end

    lambda do
      obj.go rescue 'foo'
    end.should raise_error(OutOfMemoryError)
  end
end

describe "A non-wrapped Java exception" do
  it "can be rescued using the Java type" do
    exception = java.lang.NullPointerException.new
    begin
      raise exception
    rescue java.lang.NullPointerException => npe
    end

    npe.should == exception
  end

  it "can be rescued using Object" do
    begin
      raise java.lang.NullPointerException.new
    rescue Object => e
      e.should be_kind_of(java.lang.NullPointerException)
    end
  end

  it "can be rescued using Exception" do
    begin
      raise java.lang.NullPointerException.new
    rescue Exception => e
      e.should be_kind_of(java.lang.NullPointerException)
    end
  end

  it "can be rescued using StandardError" do
    begin
      raise java.lang.NullPointerException.new
    rescue StandardError => e
      e.should be_kind_of(java.lang.NullPointerException)
    end
  end

  it "can be rescued inline" do
    obj = Object.new
    def obj.go
      raise java.lang.NullPointerException.new
    end

    (obj.go rescue 'foo').should == 'foo'
  end

  it "can be rescued dynamically using Module" do
    mod = Module.new
    (class << mod; self; end).instance_eval do
      define_method(:===) { |exception| true }
    end
    begin
      raise java.lang.NullPointerException.new
    rescue mod => e
      e.should be_kind_of(java.lang.NullPointerException)
    end
  end

  it "can be rescued dynamically using Class" do
    cls = Class.new
    (class << cls; self; end).instance_eval do
      define_method(:===) { |exception| true }
    end
    begin
      raise java.lang.NullPointerException.new
    rescue cls => e
      e.should be_kind_of(java.lang.NullPointerException)
    end
  end
end

describe "A Ruby-level exception" do
  it "carries its message along to the Java exception" do
    java_ex = JRuby.runtime.new_runtime_error("error message");
    java_ex.message.should == "(RuntimeError) error message"

    java_ex = JRuby.runtime.new_name_error("error message", "name");
    java_ex.message.should == "(NameError) error message"
  end
end

describe "A native exception wrapped by another" do
  it "gets the first available message from the causes' chain" do
    begin
      ThrowExceptionInInitializer.new.test
    rescue NativeException => e
      e.message.should =~ /lets cause an init exception$/
    end
  end

  it "can be re-raised" do
    lambda {
      begin
        ThrowExceptionInInitializer.new.test
      rescue NativeException => e
        raise e.exception("re-raised")
      end
    }.should raise_error(NativeException)
  end
end

describe "A Ruby subclass of a Java exception" do
  before :all do
    @ex_class = Class.new(java.lang.RuntimeException)
  end

  it "is rescuable with all Java superclasses" do
    exception = @ex_class.new

    begin
      raise exception
      fail
    rescue java.lang.Throwable
      $!.should == exception
    end

    begin
      raise exception
      fail
    rescue java.lang.Exception
      $!.should == exception
    end

    begin
      raise exception
      fail
    rescue java.lang.RuntimeException
      $!.should == exception
    end
  end

  it "presents its Ruby nature when rescued" do
    exception = @ex_class.new

    begin
      raise exception
      fail
    rescue java.lang.Throwable => t
      t.class.should == @ex_class
      t.should equal(exception)
    end
  end
end

describe "Ruby exception raised through Java and back to Ruby" do

  it "preserves its class and message" do
    begin
      ExceptionRunner.new.do_it_now do
        raise "it comes from ruby"
      end
      fail
    rescue RuntimeError => e
      e.message.should == "it comes from ruby"
    end
  end

  context "(via a different thread)"  do

    it "preserves its class and message" do
      begin
        ExceptionRunner.new.do_it_threaded do
          raise "it comes from ruby"
        end
        fail
      rescue RuntimeError => e
        e.message.should == "it comes from ruby"
      end
    end

  end

  SampleTask = Struct.new(:time) do
    include Comparable

    def <=>(that)
      raise ArgumentError.new("unexpected #{self.inspect}") unless self.time
      raise ArgumentError.new("unexpected #{that.inspect}") unless that.time
      self.time <=> that.time
    end
  end

  it 'does not swallow Ruby errors on compareTo' do
    queue = java.util.PriorityQueue.new(10)
    queue.add t2 = SampleTask.new(2)
    queue.add t1 = SampleTask.new(1)
    begin
      queue.add SampleTask.new(nil)
    rescue ArgumentError => e
      expect( e.message ).to start_with 'unexpected #<struct SampleTask'
    else
      fail 'compareTo did not raise'
    end
    expect( queue.first ).to be t1
  end

end