1
2
3
4
5
6
7
8
9
10
11
12
13 package org.kuali.rice.test;
14
15 import org.apache.log4j.Level;
16 import org.apache.log4j.Logger;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.runner.RunWith;
20 import org.kuali.rice.test.lifecycles.PerTestDataLoaderLifecycle;
21 import org.kuali.rice.test.runners.RiceUnitTestClassRunner;
22
23 import java.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28
29
30
31
32
33
34
35
36
37
38
39 @RunWith(RiceUnitTestClassRunner.class)
40 public abstract class BaseRiceTestCase implements MethodAware {
41
42 protected final Logger log = Logger.getLogger(getClass());
43
44 private static final Map<String, Level> changedLogLevels = new HashMap<String, Level>();
45
46 private String name;
47 private PerTestDataLoaderLifecycle perTestDataLoaderLifecycle;
48 protected Method method;
49
50 public BaseRiceTestCase() {
51 super();
52 }
53
54 public String getName() {
55 return this.name;
56 }
57
58 public void setName(String name) {
59 this.name = name;
60 }
61
62 @Before
63 public void setUp() throws Exception {
64 }
65
66 @After
67 public void tearDown() throws Exception {
68 resetLogLevels();
69 }
70
71
72
73
74
75
76
77
78
79
80
81 protected void setLogLevel(String loggerName, Level newLevel) {
82 Logger logger = Logger.getLogger(loggerName);
83
84 if (!changedLogLevels.containsKey(loggerName)) {
85 Level originalLevel = logger.getLevel();
86 changedLogLevels.put(loggerName, originalLevel);
87 }
88
89 logger.setLevel(newLevel);
90 }
91
92
93
94
95
96 protected void resetLogLevels() {
97 for (Iterator i = changedLogLevels.entrySet().iterator(); i.hasNext();) {
98 Map.Entry e = (Map.Entry) i.next();
99
100 String loggerName = (String) e.getKey();
101 Level originalLevel = (Level) e.getValue();
102
103 Logger.getLogger(loggerName).setLevel(originalLevel);
104 }
105 changedLogLevels.clear();
106 }
107
108
109
110
111 public void setTestMethod(Method testMethod) {
112 this.method = testMethod;
113
114 perTestDataLoaderLifecycle = new PerTestDataLoaderLifecycle(method);
115 }
116
117 protected PerTestDataLoaderLifecycle getPerTestDataLoaderLifecycle() {
118 return this.perTestDataLoaderLifecycle;
119 }
120 }