View Javadoc

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