|
1 | 1 | package org.junit.internal;
|
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
3 | 5 | import java.io.PrintWriter;
|
| 6 | +import java.io.StringReader; |
4 | 7 | import java.io.StringWriter;
|
| 8 | +import java.util.AbstractList; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
5 | 13 |
|
6 | 14 | /**
|
7 | 15 | * Miscellaneous functions dealing with {@code Throwable}.
|
@@ -54,4 +62,185 @@ public static String getStacktrace(Throwable exception) {
|
54 | 62 | exception.printStackTrace(writer);
|
55 | 63 | return stringWriter.toString();
|
56 | 64 | }
|
| 65 | + |
| 66 | + /** |
| 67 | + * Gets a trimmed version of the stack trace of the given exception. Stack trace |
| 68 | + * elements that are below the test method are filtered out. |
| 69 | + * |
| 70 | + * @return a trimmed stack trace, or the original trace if trimming wasn't possible |
| 71 | + */ |
| 72 | + public static String getTrimmedStackTrace(Throwable exception) { |
| 73 | + List<String> trimmedStackTraceLines = getTrimmedStackTraceLines(exception); |
| 74 | + if (trimmedStackTraceLines.isEmpty()) { |
| 75 | + return getFullStackTrace(exception); |
| 76 | + } |
| 77 | + |
| 78 | + StringBuilder result = new StringBuilder(exception.toString()); |
| 79 | + appendStackTraceLines(trimmedStackTraceLines, result); |
| 80 | + appendStackTraceLines(getCauseStackTraceLines(exception), result); |
| 81 | + return result.toString(); |
| 82 | + } |
| 83 | + |
| 84 | + private static List<String> getTrimmedStackTraceLines(Throwable exception) { |
| 85 | + List<StackTraceElement> stackTraceElements = Arrays.asList(exception.getStackTrace()); |
| 86 | + int linesToInclude = stackTraceElements.size(); |
| 87 | + |
| 88 | + State state = State.PROCESSING_OTHER_CODE; |
| 89 | + for (StackTraceElement stackTraceElement : asReversedList(stackTraceElements)) { |
| 90 | + state = state.processStackTraceElement(stackTraceElement); |
| 91 | + if (state == State.DONE) { |
| 92 | + List<String> trimmedLines = new ArrayList<String>(linesToInclude + 2); |
| 93 | + trimmedLines.add(""); |
| 94 | + for (StackTraceElement each : stackTraceElements.subList(0, linesToInclude)) { |
| 95 | + trimmedLines.add("\tat " + each); |
| 96 | + } |
| 97 | + if (exception.getCause() != null) { |
| 98 | + trimmedLines.add("\t... " + (stackTraceElements.size() - trimmedLines.size()) + " trimmed"); |
| 99 | + } |
| 100 | + return trimmedLines; |
| 101 | + } |
| 102 | + linesToInclude--; |
| 103 | + } |
| 104 | + return Collections.emptyList(); |
| 105 | + } |
| 106 | + |
| 107 | + private static List<String> getCauseStackTraceLines(Throwable exception) { |
| 108 | + if (exception.getCause() != null) { |
| 109 | + String fullTrace = getFullStackTrace(exception); |
| 110 | + BufferedReader reader = new BufferedReader( |
| 111 | + new StringReader(fullTrace.substring(exception.toString().length()))); |
| 112 | + List<String> causedByLines = new ArrayList<String>(); |
| 113 | + |
| 114 | + try { |
| 115 | + String line; |
| 116 | + while ((line = reader.readLine()) != null) { |
| 117 | + if (line.startsWith("Caused by: ")) { |
| 118 | + causedByLines.add(line); |
| 119 | + while ((line = reader.readLine()) != null) { |
| 120 | + causedByLines.add(line); |
| 121 | + } |
| 122 | + return causedByLines; |
| 123 | + } |
| 124 | + } |
| 125 | + } catch (IOException e) { |
| 126 | + // We should never get here, because we are reading from a StringReader |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return Collections.emptyList(); |
| 131 | + } |
| 132 | + |
| 133 | + private static String getFullStackTrace(Throwable exception) { |
| 134 | + StringWriter stringWriter = new StringWriter(); |
| 135 | + PrintWriter writer = new PrintWriter(stringWriter); |
| 136 | + exception.printStackTrace(writer); |
| 137 | + return stringWriter.toString(); |
| 138 | + } |
| 139 | + |
| 140 | + private static void appendStackTraceLines( |
| 141 | + List<String> stackTraceLines, StringBuilder destBuilder) { |
| 142 | + for (String stackTraceLine : stackTraceLines) { |
| 143 | + destBuilder.append(String.format("%s%n", stackTraceLine)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static <T> List<T> asReversedList(final List<T> list) { |
| 148 | + return new AbstractList<T>() { |
| 149 | + |
| 150 | + @Override |
| 151 | + public T get(int index) { |
| 152 | + return list.get(list.size() - index - 1); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public int size() { |
| 157 | + return list.size(); |
| 158 | + } |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + private enum State { |
| 163 | + PROCESSING_OTHER_CODE { |
| 164 | + @Override public State processLine(String methodName) { |
| 165 | + if (isTestFrameworkMethod(methodName)) { |
| 166 | + return PROCESSING_TEST_FRAMEWORK_CODE; |
| 167 | + } |
| 168 | + return this; |
| 169 | + } |
| 170 | + }, |
| 171 | + PROCESSING_TEST_FRAMEWORK_CODE { |
| 172 | + @Override public State processLine(String methodName) { |
| 173 | + if (isReflectionMethod(methodName)) { |
| 174 | + return PROCESSING_REFLECTION_CODE; |
| 175 | + } else if (isTestFrameworkMethod(methodName)) { |
| 176 | + return this; |
| 177 | + } |
| 178 | + return PROCESSING_OTHER_CODE; |
| 179 | + } |
| 180 | + }, |
| 181 | + PROCESSING_REFLECTION_CODE { |
| 182 | + @Override public State processLine(String methodName) { |
| 183 | + if (isReflectionMethod(methodName)) { |
| 184 | + return this; |
| 185 | + } else if (isTestFrameworkMethod(methodName)) { |
| 186 | + // This is here to handle TestCase.runBare() calling TestCase.runTest(). |
| 187 | + return PROCESSING_TEST_FRAMEWORK_CODE; |
| 188 | + } |
| 189 | + return DONE; |
| 190 | + } |
| 191 | + }, |
| 192 | + DONE { |
| 193 | + @Override public State processLine(String methodName) { |
| 194 | + return this; |
| 195 | + } |
| 196 | + }; |
| 197 | + |
| 198 | + /** Processes a stack trace element method name, possibly moving to a new state. */ |
| 199 | + protected abstract State processLine(String methodName); |
| 200 | + |
| 201 | + /** Processes a stack trace element, possibly moving to a new state. */ |
| 202 | + public final State processStackTraceElement(StackTraceElement element) { |
| 203 | + return processLine(element.getClassName() + "." + element.getMethodName() + "()"); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private static final String[] TEST_FRAMEWORK_METHOD_NAME_PREFIXES = { |
| 208 | + "org.junit.runner.", |
| 209 | + "org.junit.runners.", |
| 210 | + "org.junit.experimental.runners.", |
| 211 | + "org.junit.internal.", |
| 212 | + "junit.", |
| 213 | + }; |
| 214 | + |
| 215 | + private static final String[] TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES = { |
| 216 | + "org.junit.internal.StackTracesTest", |
| 217 | + }; |
| 218 | + |
| 219 | + private static boolean isTestFrameworkMethod(String methodName) { |
| 220 | + return isMatchingMethod(methodName, TEST_FRAMEWORK_METHOD_NAME_PREFIXES) && |
| 221 | + !isMatchingMethod(methodName, TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES); |
| 222 | + } |
| 223 | + |
| 224 | + private static final String[] REFLECTION_METHOD_NAME_PREFIXES = { |
| 225 | + "sun.reflect.", |
| 226 | + "java.lang.reflect.", |
| 227 | + "org.junit.rules.RunRules.<init>(", |
| 228 | + "org.junit.rules.RunRules.applyAll(", // calls TestRules |
| 229 | + "org.junit.runners.BlockJUnit4ClassRunner.withMethodRules(", // calls MethodRules |
| 230 | + "junit.framework.TestCase.runBare(", // runBare() directly calls setUp() and tearDown() |
| 231 | + }; |
| 232 | + |
| 233 | + private static boolean isReflectionMethod(String methodName) { |
| 234 | + return isMatchingMethod(methodName, REFLECTION_METHOD_NAME_PREFIXES); |
| 235 | + } |
| 236 | + |
| 237 | + private static boolean isMatchingMethod(String methodName, String[] methodNamePrefixes) { |
| 238 | + for (String methodNamePrefix : methodNamePrefixes) { |
| 239 | + if (methodName.startsWith(methodNamePrefix)) { |
| 240 | + return true; |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + return false; |
| 245 | + } |
57 | 246 | }
|
0 commit comments