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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
require 'java'
require 'test/unit'
# Unit and regression tests for backtraces.
# Note: The tests follow MRI 1.8.6 behavior.
# Behavior of MRI 1.9 is different:
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/15589
class TestBacktraces < Test::Unit::TestCase
def test_simple_exception
@offset = __LINE__
raise RuntimeError.new("Test")
rescue Exception => ex
expectation = "+1:in `test_simple_exception'"
assert_exception_backtrace(expectation, ex)
end
def test_native_java_backtrace
org.jruby.test.TestHelper.throwTestHelperException
fail 'did no raise exception'
rescue NativeException => ex
assert_equal '#<NativeException: org.jruby.test.TestHelper$TestHelperException: null>', ex.inspect
assert_equal 'org.jruby.test.TestHelper$TestHelperException: null', ex.message
assert_not_nil ex.cause
assert_instance_of org.jruby.test.TestHelper::TestHelperException, ex.cause
# starts with Java stack trace part :
_throwTestHelperException = /org.jruby.test.TestHelper\.java:\d+:in `throwTestHelperException'/
assert_match _throwTestHelperException, ex.backtrace[0]
java_trace = ex.backtrace.find_all do |trace|
trace =~ _throwTestHelperException
end
assert_equal 1, java_trace.length # only once!
_test_native_java_backtrace = /test\/jruby\/test_backtraces.rb:\d+:in `test_native_java_backtrace'/
ruby_trace = ex.backtrace.find_all do |trace|
trace =~ _test_native_java_backtrace
end
assert_equal 1, ruby_trace.length # only once!
backtrace = ex.backtrace.join("\r\n")
unless backtrace.include?('test_native_java_backtrace')
flunk("test_java_backtrace not in backtrace")
end
end
def test_native_java_backtrace2
sample_class = java.lang.Class.for_name('org.jruby.javasupport.test.name.Sample')
constructor = sample_class.constructor(Java::int)
constructor.new_instance 0
begin
constructor.new_instance(-1)
fail 'did no raise exception'
rescue NativeException => ex
assert_equal 'java.lang.reflect.InvocationTargetException: param == -1', ex.message
assert_equal '#<NativeException: java.lang.reflect.InvocationTargetException: param == -1>', ex.inspect
assert_instance_of java.lang.reflect.InvocationTargetException, ex.cause
# NOTE: backtrace will be messed in this case as long as there's filtering
# clases org.jruby.javasupport.test.name.Sample "org.jruby.javasupport" prefix is considered internal
# ex.backtrace.each { |b| puts " #{b.inspect}" }
end
end
def test_java_backtrace
org.jruby.test.TestHelper.throwTestHelperException
fail 'did no raise exception'
rescue java.lang.Exception => ex
# TestHelperException extends RuntimeException
# assert_nil ex.message
_throwTestHelperException = /org.jruby.test.TestHelper.throwTestHelperException/
assert_match _throwTestHelperException, ex.backtrace[0]
java_trace = ex.backtrace.find_all do |trace|
trace =~ _throwTestHelperException
end
assert_equal 1, java_trace.length # only once!
ruby_trace = ex.backtrace.find_all do |trace|
trace.index('test_java_backtrace') && trace =~ /test\/jruby\/test_backtraces.rb:\d+/
end
assert_equal 1, ruby_trace.length # only once!
end
def test_simple_exception_recursive
@offset = __LINE__
def meth(n)
raise RuntimeError.new("Test") if n == 10
n += 1
meth(n)
end
meth(0)
rescue Exception => ex
expectation = %q{
+2:in `meth'
+4:in `meth'
+6:in `test_simple_exception_recursive'
}
assert_exception_backtrace(expectation, ex)
end
def test_native_exception_recursive
@offset = __LINE__
def meth(n)
raise "hello".sub(/l/, 5) if n == 10
n += 1
meth(n)
end
meth(0)
rescue Exception => ex
expectation = %q{
+2:in `sub'
+2:in `meth'
+4:in `meth'
+6:in `test_native_exception_recursive'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_block
@offset = __LINE__
def foo
yield
end
def bar
yield
end
foo { bar { raise TypeError.new("HEH") } }
rescue Exception => ex
expectation = %q{
+7:in `test_exception_from_block'
+5:in `bar'
+7:in `test_exception_from_block'
+2:in `foo'
+7:in `test_exception_from_block'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_for
array = [1,2,3,4,5]
@offset = __LINE__
for e in array
raise RuntimeError if e
end
rescue Exception => ex
expectation = %q{
+2:in `test_exception_from_for'
+1:in `each'
+1:in `test_exception_from_for'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_proc
p = Proc.new {
@offset = __LINE__
raise StandardError.new
}
p.call
rescue Exception => ex
expectation = %q{
+1:in `test_exception_from_proc'
+3:in `call'
+3:in `test_exception_from_proc'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_lambda
l = lambda {
@offset = __LINE__
raise StandardError.new
}
l.call
rescue Exception => ex
expectation = %q{
+1:in `test_exception_from_lambda'
+3:in `call'
+3:in `test_exception_from_lambda'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_array_plus
@offset = __LINE__
[1,2,3] + 5
rescue Exception => ex
expectation = %q{
+1:in `+'
+1:in `test_exception_from_array_plus'
}
assert_exception_backtrace(expectation, ex)
end
# JRUBY-2138
def test_exception_from_string_plus
@offset = __LINE__
"hello" + nil
rescue Exception => ex
expectation = %q{
+1:in `+'
+1:in `test_exception_from_string_plus'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_string_sub
@offset = __LINE__
"hello".sub(/l/, 5)
rescue Exception => ex
expectation = %q{
+1:in `sub'
+1:in `test_exception_from_string_sub'
}
assert_exception_backtrace(expectation, ex)
end
def test_zero_devision_exception
@offset = __LINE__
1/0
rescue Exception => ex
expectation = %q{
+1:in `/'
+1:in `test_zero_devision_exception'
}
assert_exception_backtrace(expectation, ex)
end
def test_exeption_from_object_send
@offset = __LINE__
"hello".__send__(:sub, /l/, 5)
rescue Exception => ex
expectation = %q{
+1:in `sub'
+1:in `__send__'
+1:in `test_exeption_from_object_send'
}
assert_exception_backtrace(expectation, ex)
end
def test_arity_exception
@offset = __LINE__
"hello".sub
rescue Exception => ex
expectation = "+1:in `sub'"
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_eval
ex = get_exception {
@offset = __LINE__
eval("raise RuntimeError.new")
}
expectation = %Q{
+1:in `test_exception_from_eval'
#{__FILE__}:#{@get_exception_yield_line}:in `eval'
+1:in `test_exception_from_eval'
#{__FILE__}:#{@get_exception_yield_line}:in `get_exception'
}
assert_exception_backtrace(expectation, ex)
end
def test_exception_from_block_inside_eval
ex = get_exception {
@offset = __LINE__
eval("def foo; yield; end; foo { raise RuntimeError.new }")
}
expectation = %Q{
+1:in `test_exception_from_block_inside_eval'
(eval):1:in `foo'
(eval):1:in `test_exception_from_block_inside_eval'
#{__FILE__}:#{@get_exception_yield_line}:in `eval'
+1:in `test_exception_from_block_inside_eval'
#{__FILE__}:#{@get_exception_yield_line}:in `get_exception'
}
assert_exception_backtrace(expectation, ex)
end
# JRUBY-2695
def test_exception_from_thread_with_abort_on_exception_true
require 'stringio'
$stderr = StringIO.new
Thread.abort_on_exception = true
ex = get_exception {
@offset = __LINE__
t = Thread.new do
raise RuntimeError.new "DUMMY_MSG"
end
sleep 3
t.join
}
assert_equal(RuntimeError, ex.class)
# This check is not fully MRI-compatible (MRI reports more frames),
# but at list this is something.
expectation = %Q{
+2:in `test_exception_from_thread_with_abort_on_exception_true'
}
assert_exception_backtrace(expectation, ex)
ensure
Thread.abort_on_exception = false
$stderr = STDERR
end
def test_throwing_runnable_backtrace # GH-3177
fixnum_times_ = 'org.jruby.RubyFixnum.times(org/jruby/RubyFixnum.java:'
backtrace = nil
i = 0
throwing = org.jruby.javasupport.test.ThrowingRunnable.new do
1.times {
begin
throwing.doRun( (i += 1) > 0 )
rescue java.lang.Exception
assert e = $!.backtrace.find { |e2| e2.index('org.jruby.RubyFixnum.times') }
assert_equal fixnum_times_, e[ 0...fixnum_times_.size ]
backtrace = $!.backtrace.dup
raise
end
}
end
begin
throwing.doRun(false)
rescue java.lang.Exception
# puts $!.backtrace
# second rewriting of the same exception :
assert e = $!.backtrace.find { |e2| e2.index('org.jruby.RubyFixnum.times') }
assert_equal fixnum_times_, e[ 0...fixnum_times_.size ]
# NOTE back-trace gets duplicate .rb calls - seems not necessary to fix?!
# assert_equal backtrace, $!.backtrace # expect the same back-trace
else
fail 'expected to throw a java.lang.Exception'
end
end
private
# Convenience method to obtain the exception,
# and to print the stack trace, if needed.
def get_exception(verbose = false)
begin
@get_exception_yield_line = __LINE__ + 1
yield
rescue Exception => ex
puts ex.backtrace.join("\n") if verbose
ex
end
end
# Main verification method that performs actual checks
# on the stacktraces.
def assert_exception_backtrace(expectations, exception)
backtrace = []
expectations.strip.split("\n").each { |line|
line.strip!
# if line starts with +nnn, we prepend the current file and offset
if line.match(/^\+(\d+)(:.*)/)
flunk("@offset is not defined in the test case") unless @offset ||= nil
# For JRuby, we soften this requirement, since native calls will
# show their actual .java file and line, rather than the caller.
#line = "#{__FILE__}:#{$1.to_i + @offset}#{$2}"
line = /.*:#{$1.to_i + @offset}#{$2}/
end
}
backtrace.each_with_index { |expected, idx|
# Soften, per above comment
#assert_equal(expected, exception.backtrace[idx])
assert expected =~ exception.backtrace[idx]
}
end
end
|