View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A generic Rice Unit Test base class.
33   * 
34   * 1) Sets up a generic logger.
35   * 2) Sets the name of the class being run to mimic jUnit 3 functionality.
36   * 3) Stores the name of the method being run for use by subclasses (set by {@link RiceUnitTestClassRunner}
37   * 4) Sets the PerTestDataLoaderLifecycle that will load sql for the currently running test.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   * @since 0.9
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       * Changes the logging-level associated with the given loggerName to the
76       * given level. The original logging-level is saved, and will be
77       * automatically restored at the end of each test.
78       * 
79       * @param loggerName
80       *            name of the logger whose level to change
81       * @param newLevel
82       *            the level to change to
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       * Restores the logging-levels changed through calls to setLogLevel to their
97       * original values.
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 	 * @see org.kuali.rice.test.MethodAware#setTestMethod(java.lang.reflect.Method)
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 }