01: package org.testng.reporters.util;
02:
03: import org.testng.ITestNGMethod;
04:
05: /**
06: * Functionality to allow tools to analyse and subdivide stack traces.
07: *
08: * @author Paul Mendelson
09: * @since 5.3
10: * @version $Revision: 173 $
11: */
12: public class StackTraceTools {
13: // ~ Methods --------------------------------------------------------------
14:
15: /** Finds topmost position of the test method in the stack, or top of stack if <code>method</code> is not in it. */
16: public static int getTestRoot(StackTraceElement[] stack,
17: ITestNGMethod method) {
18: if (stack != null) {
19: String cname = method.getTestClass().getName();
20: for (int x = stack.length - 1; x >= 0; x--) {
21: if (cname.equals(stack[x].getClassName())
22: && method.getMethodName().equals(
23: stack[x].getMethodName())) {
24: return x;
25: }
26: }
27: return stack.length - 1;
28: } else {
29: return -1;
30: }
31: }
32:
33: /** Finds topmost position of the test method in the stack, or top of stack if <code>method</code> is not in it. */
34: public static StackTraceElement[] getTestNGInstrastructure(
35: StackTraceElement[] stack, ITestNGMethod method) {
36: int slot = StackTraceTools.getTestRoot(stack, method);
37: if (slot >= 0) {
38: StackTraceElement[] r = new StackTraceElement[stack.length
39: - slot];
40: for (int x = 0; x < r.length; x++) {
41: r[x] = stack[x + slot];
42: }
43: return r;
44: } else {
45: return new StackTraceElement[0];
46: }
47: }
48: }
|