1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.util;
17
18 import java.util.concurrent.Callable;
19
20 import org.junit.Rule;
21 import org.junit.rules.MethodRule;
22 import org.junit.runners.model.FrameworkMethod;
23 import org.junit.runners.model.Statement;
24
25
26
27
28
29
30
31 public class ProcessLoggingUnitTest {
32
33
34
35
36 private static final ThreadLocal<Integer> REPETITION = new ThreadLocal<Integer>();
37
38 protected static int getRepetition() {
39 return REPETITION.get() == null ? 0 : REPETITION.get();
40 }
41
42
43
44
45
46 protected int getRepetitions() {
47 return 1;
48 }
49
50 @Rule
51 public MethodRule processLogRule = new MethodRule() {
52 @Override
53 public Statement apply(final Statement base,
54 final FrameworkMethod method, Object target) {
55 return new Statement() {
56 @Override
57 public void evaluate() throws Throwable {
58 int repetitions = getRepetitions();
59 for (int i=0;i < repetitions;i++) {
60 REPETITION.set(i);
61 try {
62 ProcessLogger.follow("test", "Test Run " + method.getName(), new Callable<Void>() {
63
64 @Override
65 public Void call() throws Exception {
66 try {
67 base.evaluate();
68 } catch (Throwable e) {
69 if (e instanceof Error)
70 throw (Error) e;
71 else
72 throw (Exception) e;
73 }
74 return null;
75 }
76
77 @Override
78 public String toString() {
79 return "Test Run " + method.getName();
80 }
81 });
82 } catch (Throwable t) {
83 System.err.println("Error running test "
84 + method.getName());
85 t.printStackTrace();
86 throw t;
87 } finally {
88 REPETITION.remove();
89 }
90 }
91 }
92 };
93 }
94 };
95
96 }