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