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