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 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   * A generic Rice Unit Test base class.
30   * 
31   * 1) Sets up a generic logger.
32   * 2) Sets the name of the class being run to mimic jUnit 3 functionality.
33   * 3) Stores the name of the method being run for use by subclasses (set by {@link RiceUnitTestClassRunner}
34   * 4) Sets the PerTestDataLoaderLifecycle that will load sql for the currently running test.
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   * @since 0.9
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       * Changes the logging-level associated with the given loggerName to the
73       * given level. The original logging-level is saved, and will be
74       * automatically restored at the end of each test.
75       * 
76       * @param loggerName
77       *            name of the logger whose level to change
78       * @param newLevel
79       *            the level to change to
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       * Restores the logging-levels changed through calls to setLogLevel to their
94       * original values.
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 	 * @see org.kuali.rice.test.MethodAware#setTestMethod(java.lang.reflect.Method)
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 }