1 28 package org.jruby.test; 29 30 import org.jruby.exceptions.RaiseException; 31 import org.jruby.Ruby; 32 33 import java.io.ByteArrayOutputStream ; 34 import java.io.PrintStream ; 35 36 public class TestRaiseException extends TestRubyBase { 37 public static class ThrowFromJava { 38 public void throwIt() { 39 throw new RuntimeException ("here"); 40 } 41 } 42 43 protected void setUp() throws Exception { 44 super.setUp(); 45 runtime = Ruby.getDefaultInstance(); 46 } 47 48 public void testJavaExceptionTraceIncludesRubys() throws Exception { 49 String script = 50 "def one\n" + 51 " two\n" + 52 "end\n" + 53 "def two\n" + 54 " raise 'here'\n" + 55 "end\n" + 56 "one\n"; 57 try { 58 eval(script); 59 fail("should have raised an exception"); 60 } catch (RaiseException re) { 61 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 62 re.printStackTrace(new PrintStream (baos)); 63 String trace = baos.toString(); 64 assertTrue(trace.indexOf("here") >= 0); 66 assertTrue(trace.indexOf("one") >= 0); 67 assertTrue(trace.indexOf("two") >= 0); 68 assertTrue(trace.indexOf("evaluator") == -1); 69 } 70 } 71 72 public void testRubyExceptionTraceIncludesJavas() throws Exception { 73 String script = 74 "require 'java'\n" + 75 "include_class('org.jruby.test.TestRaiseException$ThrowFromJava') {|p,c| 'ThrowFromJava' }\n" + 76 "def throw_it\n" + 77 "tfj = ThrowFromJava.new\n" + 78 "tfj.throwIt\n" + 79 "end\n" + 80 "throw_it\n"; 81 try { 82 eval(script); 83 fail("should have raised an exception"); 84 } catch (RaiseException re) { 85 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 86 re.printStackTrace(new PrintStream (baos)); 87 } 90 } 91 } | Popular Tags |