|
| 1 | +package androidx.test.services.events.internal; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.PrintWriter; |
| 6 | +import java.io.StringReader; |
| 7 | +import java.io.StringWriter; |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.util.AbstractList; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Copy of JUnit 4.13 org.junit.internal.Throwables.java |
| 17 | + * |
| 18 | + * <p>TODO(b/128614857): Remove once androidx.test can use 4.13 directly |
| 19 | + */ |
| 20 | +final class Throwables { |
| 21 | + |
| 22 | + private Throwables() {} |
| 23 | + |
| 24 | + /** |
| 25 | + * Rethrows the given {@code Throwable}, allowing the caller to declare that it throws {@code |
| 26 | + * Exception}. This is useful when your callers have nothing reasonable they can do when a {@code |
| 27 | + * Throwable} is thrown. This is declared to return {@code Exception} so it can be used in a |
| 28 | + * {@code throw} clause: |
| 29 | + * |
| 30 | + * <pre> |
| 31 | + * try { |
| 32 | + * doSomething(); |
| 33 | + * } catch (Throwable e} { |
| 34 | + * throw Throwables.rethrowAsException(e); |
| 35 | + * } |
| 36 | + * doSomethingLater(); |
| 37 | + * </pre> |
| 38 | + * |
| 39 | + * @param e exception to rethrow |
| 40 | + * @return does not return anything |
| 41 | + * @since 4.12 |
| 42 | + */ |
| 43 | + public static Exception rethrowAsException(Throwable e) throws Exception { |
| 44 | + Throwables.<Exception>rethrow(e); |
| 45 | + return null; // we never get here |
| 46 | + } |
| 47 | + |
| 48 | + @SuppressWarnings("unchecked") |
| 49 | + private static <T extends Throwable> void rethrow(Throwable e) throws T { |
| 50 | + throw (T) e; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns the stacktrace of the given Throwable as a String. |
| 55 | + * |
| 56 | + * @since 4.13 |
| 57 | + */ |
| 58 | + public static String getStacktrace(Throwable exception) { |
| 59 | + StringWriter stringWriter = new StringWriter(); |
| 60 | + PrintWriter writer = new PrintWriter(stringWriter); |
| 61 | + exception.printStackTrace(writer); |
| 62 | + return stringWriter.toString(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Gets a trimmed version of the stack trace of the given exception. Stack trace elements that are |
| 67 | + * below the test method are filtered out. |
| 68 | + * |
| 69 | + * @return a trimmed stack trace, or the original trace if trimming wasn't possible |
| 70 | + */ |
| 71 | + public static String getTrimmedStackTrace(Throwable exception) { |
| 72 | + List<String> trimmedStackTraceLines = getTrimmedStackTraceLines(exception); |
| 73 | + if (trimmedStackTraceLines.isEmpty()) { |
| 74 | + return getFullStackTrace(exception); |
| 75 | + } |
| 76 | + |
| 77 | + StringBuilder result = new StringBuilder(exception.toString()); |
| 78 | + appendStackTraceLines(trimmedStackTraceLines, result); |
| 79 | + appendStackTraceLines(getCauseStackTraceLines(exception), result); |
| 80 | + return result.toString(); |
| 81 | + } |
| 82 | + |
| 83 | + private static List<String> getTrimmedStackTraceLines(Throwable exception) { |
| 84 | + List<StackTraceElement> stackTraceElements = Arrays.asList(exception.getStackTrace()); |
| 85 | + int linesToInclude = stackTraceElements.size(); |
| 86 | + |
| 87 | + State state = State.PROCESSING_OTHER_CODE; |
| 88 | + for (StackTraceElement stackTraceElement : asReversedList(stackTraceElements)) { |
| 89 | + state = state.processStackTraceElement(stackTraceElement); |
| 90 | + if (state == State.DONE) { |
| 91 | + List<String> trimmedLines = new ArrayList<String>(linesToInclude + 2); |
| 92 | + trimmedLines.add(""); |
| 93 | + for (StackTraceElement each : stackTraceElements.subList(0, linesToInclude)) { |
| 94 | + trimmedLines.add("\tat " + each); |
| 95 | + } |
| 96 | + if (exception.getCause() != null) { |
| 97 | + trimmedLines.add( |
| 98 | + "\t... " + (stackTraceElements.size() - trimmedLines.size()) + " trimmed"); |
| 99 | + } |
| 100 | + return trimmedLines; |
| 101 | + } |
| 102 | + linesToInclude--; |
| 103 | + } |
| 104 | + return Collections.emptyList(); |
| 105 | + } |
| 106 | + |
| 107 | + private static final Method getSuppressed = initGetSuppressed(); |
| 108 | + |
| 109 | + private static Method initGetSuppressed() { |
| 110 | + try { |
| 111 | + return Throwable.class.getMethod("getSuppressed"); |
| 112 | + } catch (Throwable e) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean hasSuppressed(Throwable exception) { |
| 118 | + if (getSuppressed == null) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + try { |
| 122 | + Throwable[] suppressed = (Throwable[]) getSuppressed.invoke(exception); |
| 123 | + return suppressed.length != 0; |
| 124 | + } catch (Throwable e) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static List<String> getCauseStackTraceLines(Throwable exception) { |
| 130 | + if (exception.getCause() != null || hasSuppressed(exception)) { |
| 131 | + String fullTrace = getFullStackTrace(exception); |
| 132 | + BufferedReader reader = |
| 133 | + new BufferedReader(new StringReader(fullTrace.substring(exception.toString().length()))); |
| 134 | + List<String> causedByLines = new ArrayList<String>(); |
| 135 | + |
| 136 | + try { |
| 137 | + String line; |
| 138 | + while ((line = reader.readLine()) != null) { |
| 139 | + if (line.startsWith("Caused by: ") || line.trim().startsWith("Suppressed: ")) { |
| 140 | + causedByLines.add(line); |
| 141 | + while ((line = reader.readLine()) != null) { |
| 142 | + causedByLines.add(line); |
| 143 | + } |
| 144 | + return causedByLines; |
| 145 | + } |
| 146 | + } |
| 147 | + } catch (IOException e) { |
| 148 | + // We should never get here, because we are reading from a StringReader |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return Collections.emptyList(); |
| 153 | + } |
| 154 | + |
| 155 | + private static String getFullStackTrace(Throwable exception) { |
| 156 | + StringWriter stringWriter = new StringWriter(); |
| 157 | + PrintWriter writer = new PrintWriter(stringWriter); |
| 158 | + exception.printStackTrace(writer); |
| 159 | + return stringWriter.toString(); |
| 160 | + } |
| 161 | + |
| 162 | + private static void appendStackTraceLines( |
| 163 | + List<String> stackTraceLines, StringBuilder destBuilder) { |
| 164 | + for (String stackTraceLine : stackTraceLines) { |
| 165 | + destBuilder.append(String.format("%s%n", stackTraceLine)); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private static <T> List<T> asReversedList(final List<T> list) { |
| 170 | + return new AbstractList<T>() { |
| 171 | + |
| 172 | + @Override |
| 173 | + public T get(int index) { |
| 174 | + return list.get(list.size() - index - 1); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public int size() { |
| 179 | + return list.size(); |
| 180 | + } |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + private enum State { |
| 185 | + PROCESSING_OTHER_CODE { |
| 186 | + @Override |
| 187 | + public State processLine(String methodName) { |
| 188 | + if (isTestFrameworkMethod(methodName)) { |
| 189 | + return PROCESSING_TEST_FRAMEWORK_CODE; |
| 190 | + } |
| 191 | + return this; |
| 192 | + } |
| 193 | + }, |
| 194 | + PROCESSING_TEST_FRAMEWORK_CODE { |
| 195 | + @Override |
| 196 | + public State processLine(String methodName) { |
| 197 | + if (isReflectionMethod(methodName)) { |
| 198 | + return PROCESSING_REFLECTION_CODE; |
| 199 | + } else if (isTestFrameworkMethod(methodName)) { |
| 200 | + return this; |
| 201 | + } |
| 202 | + return PROCESSING_OTHER_CODE; |
| 203 | + } |
| 204 | + }, |
| 205 | + PROCESSING_REFLECTION_CODE { |
| 206 | + @Override |
| 207 | + public State processLine(String methodName) { |
| 208 | + if (isReflectionMethod(methodName)) { |
| 209 | + return this; |
| 210 | + } else if (isTestFrameworkMethod(methodName)) { |
| 211 | + // This is here to handle TestCase.runBare() calling TestCase.runTest(). |
| 212 | + return PROCESSING_TEST_FRAMEWORK_CODE; |
| 213 | + } |
| 214 | + return DONE; |
| 215 | + } |
| 216 | + }, |
| 217 | + DONE { |
| 218 | + @Override |
| 219 | + public State processLine(String methodName) { |
| 220 | + return this; |
| 221 | + } |
| 222 | + }; |
| 223 | + |
| 224 | + /** Processes a stack trace element method name, possibly moving to a new state. */ |
| 225 | + protected abstract State processLine(String methodName); |
| 226 | + |
| 227 | + /** Processes a stack trace element, possibly moving to a new state. */ |
| 228 | + public final State processStackTraceElement(StackTraceElement element) { |
| 229 | + return processLine(element.getClassName() + "." + element.getMethodName() + "()"); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + private static final String[] TEST_FRAMEWORK_METHOD_NAME_PREFIXES = { |
| 234 | + "org.junit.runner.", |
| 235 | + "org.junit.runners.", |
| 236 | + "org.junit.experimental.runners.", |
| 237 | + "org.junit.internal.", |
| 238 | + "junit.", |
| 239 | + }; |
| 240 | + |
| 241 | + private static final String[] TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES = { |
| 242 | + "org.junit.internal.StackTracesTest", |
| 243 | + }; |
| 244 | + |
| 245 | + private static boolean isTestFrameworkMethod(String methodName) { |
| 246 | + return isMatchingMethod(methodName, TEST_FRAMEWORK_METHOD_NAME_PREFIXES) |
| 247 | + && !isMatchingMethod(methodName, TEST_FRAMEWORK_TEST_METHOD_NAME_PREFIXES); |
| 248 | + } |
| 249 | + |
| 250 | + private static final String[] REFLECTION_METHOD_NAME_PREFIXES = { |
| 251 | + "sun.reflect.", |
| 252 | + "java.lang.reflect.", |
| 253 | + "jdk.internal.reflect.", |
| 254 | + "org.junit.rules.RunRules.<init>(", |
| 255 | + "org.junit.rules.RunRules.applyAll(", // calls TestRules |
| 256 | + "org.junit.runners.RuleContainer.apply(", // calls MethodRules & TestRules |
| 257 | + "junit.framework.TestCase.runBare(", // runBare() directly calls setUp() and tearDown() |
| 258 | + }; |
| 259 | + |
| 260 | + private static boolean isReflectionMethod(String methodName) { |
| 261 | + return isMatchingMethod(methodName, REFLECTION_METHOD_NAME_PREFIXES); |
| 262 | + } |
| 263 | + |
| 264 | + private static boolean isMatchingMethod(String methodName, String[] methodNamePrefixes) { |
| 265 | + for (String methodNamePrefix : methodNamePrefixes) { |
| 266 | + if (methodName.startsWith(methodNamePrefix)) { |
| 267 | + return true; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + return false; |
| 272 | + } |
| 273 | +} |
0 commit comments