public void testRubyExceptionBacktraceIncludesJavaOrigin() throws Exception { String script = "require 'java'\n" + "hash = Hash.new { org.jruby.test.TestRaiseException::ThrowFromJava.new.throwIt }\n" + "begin; hash['missing']; rescue java.lang.Exception => e; $ex_trace = e.backtrace end \n" + "$ex_trace"; RubyArray trace = (RubyArray) runtime.evalScriptlet(script); String fullTrace = trace.join(runtime.getCurrentContext(), runtime.newString("\n")).toString(); // System.out.println(fullTrace); // NOTE: 'unknown' JRuby packages e.g. "org.jruby.test" should not be filtered // ... if they are that hurts stack-traces from extensions such as jruby-rack and jruby-openssl assertTrue( trace.get(0).toString(), trace .get(0) .toString() .startsWith("org.jruby.test.TestRaiseException$ThrowFromJava.throwIt")); boolean hash_default = false; for (Object element : trace) { if (element.toString().contains("org.jruby.RubyHash.default")) { if (hash_default) fail("duplicate " + element + " in : \n" + fullTrace); hash_default = true; } } assertTrue("missing org.jruby.RubyHash.default ... in : \n" + fullTrace, hash_default); }
public void eventHandler( ThreadContext context, String event, String file, int line, String name, IRubyObject type) { // Line numbers are 1s based. Arrays are zero based. We need to compensate for that. line -= 1; // Make sure that we have SCRIPT_LINES__ and it's a hash RubyHash scriptLines = getScriptLines(context.getRuntime()); if (scriptLines == null || !scriptLines.containsKey(file)) { return; } // make sure the file's source lines are in SCRIPT_LINES__ cover = getCover(context.getRuntime()); RubyArray lines = (RubyArray) scriptLines.get(file); if (lines == null || cover == null) { return; } // make sure file's counts are in COVER and set to zero RubyArray counts = (RubyArray) cover.get(file); if (counts == null) { counts = context.getRuntime().newArray(); for (int i = 0; i < lines.size(); i++) { counts.add(Long.valueOf(0)); } cover.put(file, counts); } // in the case of code generation (one example is instance_eval for routes optimization) // We could get here and see that we are not matched up with what we expect if (counts.size() <= line) { for (int i = counts.size(); i <= line; i++) { counts.add(Long.valueOf(0)); } } if (!context.isWithinTrace()) { try { context.setWithinTrace(true); // update counts in COVER Long count = (Long) counts.get(line); if (count == null) { count = Long.valueOf(0); } count = Long.valueOf(count.longValue() + 1); counts.set(line, count); } finally { context.setWithinTrace(false); } } }
@Override public void accept(ThreadContext context, SaveContextVisitor visitor) { visitor.enter((Element) node); XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context); if (xmlNodeSet.length() > 0) { RubyArray array = (RubyArray) xmlNodeSet.to_a(context); for (int i = 0; i < array.getLength(); i++) { Object item = array.get(i); if (item instanceof XmlNode) { XmlNode cur = (XmlNode) item; cur.accept(context, visitor); } else if (item instanceof XmlNamespace) { XmlNamespace cur = (XmlNamespace) item; cur.accept(context, visitor); } } } visitor.leave((Element) node); }