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
|
require File.dirname(__FILE__) + "/../spec_helper"
require 'java'
describe "A Java Throwable" do
it "implements backtrace" do
ex = java.lang.Exception.new
trace = nil
expect {trace = ex.backtrace}.not_to raise_error
expect(trace).to eq(ex.stack_trace.map(&:to_s))
end
it "implements backtrace= as a no-op" do
ex = java.lang.IllegalStateException.new
backtrace = ex.backtrace
ex.set_backtrace ['blah']
expect(ex.backtrace).to eq backtrace
end
it "implements to_s as message" do
ex = java.lang.Exception.new
expect(ex.to_s).to eq ''
expect(ex.to_s).to eq ex.message
ex = java.lang.RuntimeException.new('hello')
expect(ex.to_s).to eq 'hello'
expect(ex.to_s).to eq ex.message
end
it "implements inspect to be Ruby-like" do
ex = java.lang.Exception.new('hello')
expect(ex.inspect).to eq '#<Java::JavaLang::Exception: hello>'
ex = java.lang.AssertionError.new
expect(ex.inspect).to eq '#<Java::JavaLang::AssertionError: >'
end
it "implements full_message" do
ex = java.lang.Exception.new('hello')
expect(ex.full_message).to match /hello \(Java::JavaLang::Exception\)/
expect(ex.full_message(:highlight => true, :order => :top)).to match /hello \(Java::JavaLang::Exception\)/
end
it "can be rescued by rescue Exception" do
begin
raise ex = java.lang.Exception.new
rescue Exception => e
expect(e).to eq(ex)
end
end
it "can be rescued by rescue java.lang.Throwable" do
begin
raise ex = java.lang.Exception.new
rescue java.lang.Exception => e
expect(e).to eq(ex)
end
end
it "can be rescued by rescue Object" do
begin
raise ex = java.lang.Exception.new
rescue Object => e
expect(e).to eq(ex)
end
end
end
describe "Rescuing a Java exception using Exception" do
it "does not prevent non-local return from working" do
x = Object.new
def x.foo
loop do
begin
return 1
rescue Exception
2
end
end
end
expect(x.foo).to eq 1
end
it "does not prevent non-local break from working" do
expect(loop do
begin
break 1
rescue Exception
2
end
end).to eq 1
end
it "does not prevent non-local redo from working" do
i = 0
loop do
begin
i += 1
redo if i < 2
break
rescue Exception
i = 3
end
end
expect(i).to eq 2
end
it "does not prevent catch/throw from working" do
expect do
catch :blah do
begin
throw :blah
rescue Exception
raise
end
end
end.not_to raise_error
end
it "does not prevent retry from working" do
i = 0
begin
i += 1
raise StandardError if i < 2
rescue StandardError
begin
retry
rescue Exception
i = 3
end
end
expect(i).to eq 2
end
it "synchronizes causes" do
ex = Class.new(StandardError)
begin
e = ex.new
expect(e.cause).to be_nil
raise e
rescue
begin
e2 = ex.new
expect(e2.cause).to be_nil
t = JRuby.ref(e2)
expect(t.getCause).to be_nil
raise e2
rescue
expect(e2.cause).to equal(e)
expect(t.getCause).to equal(e)
end
end
begin
e = java.lang.NullPointerException.new
raise e
rescue java.lang.NullPointerException
begin
e2 = ex.new
raise e2
rescue
expect(e2.cause).to equal(e)
expect(JRuby.ref(e2).toThrowable.getCause).to equal(e)
end
end
end
describe 'Ruby sub-class' do
class RubyThrowable < java.lang.Exception
def initialize(msg) super(); @msg = msg end
def getMessage; @msg end
end
it 'has Throwable extensions' do
throwable = RubyThrowable.new 'foo'
expect( throwable.backtrace ).to_not be_empty
expect( throwable.message ).to eql 'foo'
expect( throwable.inspect ).to eql '#<RubyThrowable: foo>'
end
end
end
|