Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExceptionStackTraceInterceptionAspect |
|
| 0.0;0 |
1 | 0 | package pt.digitalis.dif.utils.development; |
2 | ||
3 | import org.aspectj.lang.annotation.SuppressAjWarnings; | |
4 | ||
5 | import pt.digitalis.dif.utils.logging.DIFLogAspect; | |
6 | import pt.digitalis.log.LogLevel; | |
7 | ||
8 | /** | |
9 | * Intercepts printStackTrace calls from exceptions thrown from unit tests. | |
10 | * | |
11 | * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/> | |
12 | * @created 2007/11/21 | |
13 | */ | |
14 | 0 | privileged public aspect ExceptionStackTraceInterceptionAspect extends DIFLogAspect { |
15 | ||
16 | /** | |
17 | * Captures calls to <code>printStackTrace(..)</code> within DIF's logging appliances. Exposes the thrown | |
18 | * exception. | |
19 | * | |
20 | * @param exception | |
21 | * the thrown exception | |
22 | */ | |
23 | private pointcut printStackTrace(Throwable exception): call(* *.printStackTrace(..)) && | |
24 | 0 | within(pt.digitalis.dif.utils.logging.*) && target(exception) && if(false); |
25 | ||
26 | /** | |
27 | * Advises DIFExceptionLogAspect to inhibit the printStackTrace to run for exceptions throws as part of unit tests. | |
28 | * | |
29 | * @param exception | |
30 | * the thrown exception | |
31 | */ | |
32 | @SuppressAjWarnings({"adviceDidNotMatch"}) | |
33 | void around(Throwable exception): printStackTrace(exception) | |
34 | { | |
35 | 0 | if (wasThrownFromMockupOrTest(exception)) |
36 | 0 | super.getLogger().log(LogLevel.WARN, "[DEV] Exception stack trace omitted!"); |
37 | else | |
38 | 0 | proceed(exception); |
39 | 0 | } |
40 | ||
41 | /** | |
42 | * Returns T if the exception was thrown from a Mockup object or a test class. Checks if it was called from a | |
43 | * junit/surefire class also. | |
44 | * | |
45 | * Just searches the stack top and bottom element to avoid performance penalty. | |
46 | * | |
47 | * @param exception | |
48 | * the thrown exception | |
49 | * @return T if exception was thrown from a Mockup object, F otherwise. | |
50 | */ | |
51 | static private boolean wasThrownFromMockupOrTest(Throwable exception) { | |
52 | 0 | StackTraceElement[] stack = exception.getStackTrace(); |
53 | ||
54 | 0 | for (StackTraceElement element : stack) { |
55 | 0 | String className = element.getClassName().toLowerCase(); |
56 | ||
57 | 0 | if (className.contains("mockup") || className.contains("test") || className.contains("surefire") |
58 | 0 | || className.contains("junit")) |
59 | 0 | return true; |
60 | } | |
61 | ||
62 | 0 | return false; |
63 | } | |
64 | } |