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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
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
expect(oome).to eq(exception)
end
it "can be rescued using Object" do
begin
raise OutOfMemoryError.new
rescue Object => e
expect(e).to be_kind_of(OutOfMemoryError)
end
end
it "can be rescued using Exception" do
exception = OutOfMemoryError.new
begin
raise exception
rescue Exception => e
end
expect(e).to eq(exception)
end
it "cannot be rescued using StandardError" do
exception = OutOfMemoryError.new
expect do
begin
raise exception
rescue => e
end
end.to raise_error(exception)
end
it "cannot be rescued inline" do
obj = Object.new
def obj.go
raise OutOfMemoryError.new
end
expect do
obj.go rescue 'foo'
end.to 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
expect(npe).to eq(exception)
end
it "can be rescued using Object" do
begin
raise java.lang.NullPointerException.new
rescue Object => e
expect(e).to be_kind_of(java.lang.NullPointerException)
end
end
it "can be rescued using Exception" do
begin
raise java.lang.NullPointerException.new
rescue Exception => e
expect(e).to be_kind_of(java.lang.NullPointerException)
end
end
it "can be rescued using StandardError" do
begin
raise java.lang.NullPointerException.new
rescue StandardError => e
expect(e).to 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
expect { obj.go rescue 'foo' }.not_to raise_error
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
expect(e).to 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| puts "=== called: #{exception.inspect}"; true }
end
begin
raise java.lang.NullPointerException.new
rescue cls => e
expect(e).to 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");
expect(java_ex.message).to eq("(RuntimeError) error message")
java_ex = JRuby.runtime.new_name_error("error message", "name");
expect(java_ex.message).to eq("(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
expect(e.message).to match(/lets cause an init exception$/)
end
end
it "can be re-raised" do
expect {
begin
ThrowExceptionInInitializer.new.test
rescue NativeException => e
raise e.exception("re-raised")
end
}.to 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
let(:exception) { @ex_class.new }
it "is rescuable with all Java superclasses" do
begin
raise exception
fail
rescue java.lang.Throwable
expect($!).to eq(exception)
end
begin
raise exception
fail
rescue java.lang.Exception
expect($!).to eq(exception)
end
begin
raise exception
fail
rescue java.lang.RuntimeException
expect($!).to eq(exception)
end
end
it "presents its Ruby nature when rescued" do
begin
raise exception
fail
rescue java.lang.Throwable => t
expect(t.class).to eq(@ex_class)
expect(t).to equal(exception)
end
end
class MyError1 < Java::JavaLang::RuntimeException; end
class MyError2 < Java::JavaLang::RuntimeException; end
it 'rescues correct type with multiple sub-classes' do
expect {
begin
raise MyError1.new('my_error_1')
rescue MyError2 => e
fail 'rescued MyError2 => ' + e.inspect
end
}.to raise_error(MyError1)
begin
raise MyError2.new('my_error_2')
rescue MyError1 => e
fail 'rescued MyError1: ' + e.inspect
rescue MyError2 => e
expect(e.message).to eql 'my_error_2'
rescue java.lang.RuntimeException => e
fail 'rescued java.lang.RuntimeException => ' + e.inspect
end
begin
raise MyError2.new('my_error_2')
rescue MyError1, RuntimeError => e
fail 'rescued MyError1, RuntimeError => ' + e.inspect
rescue LoadError, MyError2 => e
expect(e.message).to eql 'my_error_2'
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
expect(e.message).to eq("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
expect(e.message).to eq("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
|