| 1 | |
package org.jbehave.core.reporters; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayOutputStream; |
| 4 | |
import java.io.PrintStream; |
| 5 | |
import java.util.regex.Pattern; |
| 6 | |
|
| 7 | |
import org.jbehave.core.failures.UUIDExceptionWrapper; |
| 8 | |
|
| 9 | |
public class StackTraceFormatter { |
| 10 | |
|
| 11 | |
private boolean compressFailureTrace; |
| 12 | |
|
| 13 | 15 | public StackTraceFormatter(boolean compressFailureTrace) { |
| 14 | 15 | this.compressFailureTrace = compressFailureTrace; |
| 15 | 15 | } |
| 16 | |
|
| 17 | |
public String stackTrace(Throwable cause) { |
| 18 | 15 | if (cause.getClass().getName().equals(UUIDExceptionWrapper.class.getName())) { |
| 19 | 5 | cause = cause.getCause(); |
| 20 | |
} |
| 21 | 15 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 22 | 15 | cause.printStackTrace(new PrintStream(out)); |
| 23 | 15 | return stackTrace(out.toString().replaceAll("\r","")); |
| 24 | |
} |
| 25 | |
|
| 26 | |
protected String stackTrace(String stackTrace) { |
| 27 | 16 | if (!compressFailureTrace) { |
| 28 | 6 | return stackTrace; |
| 29 | |
} |
| 30 | |
|
| 31 | |
|
| 32 | 10 | stackTrace = cutOff(stackTrace, "org.jbehave.core.embedder."); |
| 33 | 10 | stackTrace = cutOff(stackTrace, "org.junit.runners."); |
| 34 | 10 | stackTrace = cutOff(stackTrace, "org.apache.maven.surefire."); |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 90 | for (Replacement replacement : REPLACEMENTS) { |
| 41 | 80 | stackTrace = replacement.from.matcher(stackTrace).replaceAll(replacement.to); |
| 42 | |
} |
| 43 | 10 | return stackTrace; |
| 44 | |
} |
| 45 | |
|
| 46 | |
private String cutOff(String stackTrace, String at) { |
| 47 | 30 | if (stackTrace.indexOf(at) > -1) { |
| 48 | 10 | int ix = stackTrace.indexOf(at); |
| 49 | 10 | ix = stackTrace.indexOf("\n", ix); |
| 50 | 10 | if (ix != -1) { |
| 51 | 10 | stackTrace = stackTrace.substring(0, ix) + "\n..."; |
| 52 | |
} |
| 53 | |
} |
| 54 | 30 | return stackTrace; |
| 55 | |
} |
| 56 | |
|
| 57 | 168 | private static class Replacement { |
| 58 | |
private final Pattern from; |
| 59 | |
private final String to; |
| 60 | |
|
| 61 | 8 | private Replacement(Pattern from, String to) { |
| 62 | 8 | this.from = from; |
| 63 | 8 | this.to = to; |
| 64 | 8 | } |
| 65 | |
} |
| 66 | |
|
| 67 | 1 | private static Replacement[] REPLACEMENTS = new Replacement[] { |
| 68 | |
new Replacement( |
| 69 | |
Pattern.compile("\\tat sun.reflect.NativeMethodAccessorImpl.invoke0\\(Native Method\\)\\n" |
| 70 | |
+ "\\tat sun.reflect.NativeMethodAccessorImpl.invoke\\(NativeMethodAccessorImpl.java:\\d+\\)\\n" |
| 71 | |
+ "\\tat sun.reflect.DelegatingMethodAccessorImpl.invoke\\(DelegatingMethodAccessorImpl.java:\\d+\\)\\n" |
| 72 | |
+ "\\tat java.lang.reflect.Method.invoke\\(Method.java:\\d+\\)"), |
| 73 | |
"\t(reflection-invoke)"), |
| 74 | |
new Replacement( |
| 75 | |
Pattern.compile("\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\\n" |
| 76 | |
+ "\\tat org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke\\(ClosureMetaMethod.java:\\d+\\)\\n" |
| 77 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite\\$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke\\(PojoMetaMethodSite.java:\\d+\\)\\n" |
| 78 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call\\(PojoMetaMethodSite.java:\\d+\\)\\n" |
| 79 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall\\(CallSiteArray.java:\\d+\\)\\n" |
| 80 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call\\(AbstractCallSite.java:\\d+\\)\\n" |
| 81 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call\\(AbstractCallSite.java:\\d+\\)"), |
| 82 | |
"\t(groovy-closure-invoke)"), |
| 83 | |
new Replacement( |
| 84 | |
Pattern.compile("\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\\n" |
| 85 | |
+ "\\tat groovy.lang.MetaMethod.doMethodInvoke\\(MetaMethod.java:\\d+\\)\\n" |
| 86 | |
+ "\\tat org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod\\(ClosureMetaClass.java:\\d+\\)\\n" |
| 87 | |
+ "\\tat org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN\\(ScriptBytecodeAdapter.java:\\d+\\)"), |
| 88 | |
"\t(groovy-instance-method-invoke)"), |
| 89 | |
new Replacement( |
| 90 | |
Pattern.compile("\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\n" |
| 91 | |
+ "\\tat org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke\\(ClosureMetaMethod.java:\\d+\\)\n" |
| 92 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite\\$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke\\(PojoMetaMethodSite.java:\\d+\\)\n" |
| 93 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call\\(PojoMetaMethodSite.java:\\d+\\)\n" |
| 94 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call\\(AbstractCallSite.java:\\d+\\)"), |
| 95 | |
"\t(groovy-abstract-method-invoke)"), |
| 96 | |
new Replacement( |
| 97 | |
Pattern.compile("\\tat org.codehaus.groovy.reflection.CachedMethod.invoke\\(CachedMethod.java:\\d+\\)\\n" |
| 98 | |
+ "\\tat groovy.lang.MetaMethod.doMethodInvoke\\(MetaMethod.java:\\d+\\)\\n" |
| 99 | |
+ "\\tat groovy.lang.MetaClassImpl.invokeStaticMethod\\(MetaClassImpl.java:\\d+\\)\\n" |
| 100 | |
+ "\\tat org.codehaus.groovy.runtime.InvokerHelper.invokeStaticMethod\\(InvokerHelper.java:\\d+\\)\\n" |
| 101 | |
+ "\\tat org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeStaticMethodN\\(ScriptBytecodeAdapter.java:\\d+\\)"), |
| 102 | |
"\t(groovy-static-method-invoke)"), |
| 103 | |
new Replacement( |
| 104 | |
Pattern.compile("\\tat sun.reflect.NativeConstructorAccessorImpl.newInstance0\\(Native Method\\)\\n" |
| 105 | |
+ "\\tat sun.reflect.NativeConstructorAccessorImpl.newInstance\\(NativeConstructorAccessorImpl.java:\\d+\\)\\n" |
| 106 | |
+ "\\tat sun.reflect.DelegatingConstructorAccessorImpl.newInstance\\(DelegatingConstructorAccessorImpl.java:\\d+\\)\\n" |
| 107 | |
+ "\\tat java.lang.reflect.Constructor.newInstance\\(Constructor.java:\\d+\\)"), |
| 108 | |
"\t(reflection-construct)"), |
| 109 | |
new Replacement( |
| 110 | |
Pattern.compile("\\tat org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(Current|)\\(CallSiteArray.java:\\d+\\)\\n" |
| 111 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(Current|)\\(AbstractCallSite.java:\\d+\\)\\n" |
| 112 | |
+ "\\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(Current|)\\(AbstractCallSite.java:\\d+\\)" |
| 113 | |
|
| 114 | |
), "\t(groovy-call)"), |
| 115 | |
|
| 116 | |
new Replacement(Pattern.compile("\\t\\(reflection\\-invoke\\)\\n" + "\\t\\(groovy\\-"), "\t(groovy-") }; |
| 117 | |
|
| 118 | |
} |