001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.test; 017 018 import org.apache.log4j.Level; 019 import org.apache.log4j.Logger; 020 import org.junit.After; 021 import org.junit.Before; 022 import org.junit.runner.RunWith; 023 import org.kuali.rice.test.lifecycles.PerTestDataLoaderLifecycle; 024 import org.kuali.rice.test.runners.RiceUnitTestClassRunner; 025 026 import java.lang.reflect.Method; 027 import java.util.HashMap; 028 import java.util.Iterator; 029 import java.util.Map; 030 031 /** 032 * A generic Rice Unit Test base class. 033 * 034 * 1) Sets up a generic logger. 035 * 2) Sets the name of the class being run to mimic jUnit 3 functionality. 036 * 3) Stores the name of the method being run for use by subclasses (set by {@link RiceUnitTestClassRunner} 037 * 4) Sets the PerTestDataLoaderLifecycle that will load sql for the currently running test. 038 * 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 * @since 0.9 041 */ 042 @RunWith(RiceUnitTestClassRunner.class) 043 public abstract class BaseRiceTestCase implements MethodAware { 044 045 protected final Logger log = Logger.getLogger(getClass()); 046 047 private static final Map<String, Level> changedLogLevels = new HashMap<String, Level>(); 048 049 private String name; 050 private PerTestDataLoaderLifecycle perTestDataLoaderLifecycle; 051 protected Method method; 052 053 public BaseRiceTestCase() { 054 super(); 055 } 056 057 public String getName() { 058 return this.name; 059 } 060 061 public void setName(String name) { 062 this.name = name; 063 } 064 065 @Before 066 public void setUp() throws Exception { 067 } 068 069 @After 070 public void tearDown() throws Exception { 071 resetLogLevels(); 072 } 073 074 /** 075 * Changes the logging-level associated with the given loggerName to the 076 * given level. The original logging-level is saved, and will be 077 * automatically restored at the end of each test. 078 * 079 * @param loggerName 080 * name of the logger whose level to change 081 * @param newLevel 082 * the level to change to 083 */ 084 protected void setLogLevel(String loggerName, Level newLevel) { 085 Logger logger = Logger.getLogger(loggerName); 086 087 if (!changedLogLevels.containsKey(loggerName)) { 088 Level originalLevel = logger.getLevel(); 089 changedLogLevels.put(loggerName, originalLevel); 090 } 091 092 logger.setLevel(newLevel); 093 } 094 095 /** 096 * Restores the logging-levels changed through calls to setLogLevel to their 097 * original values. 098 */ 099 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 @Override 115 public void setTestMethod(Method testMethod) { 116 this.method = testMethod; 117 118 perTestDataLoaderLifecycle = new PerTestDataLoaderLifecycle(method); 119 } 120 121 public Method getTestMethod() { 122 return this.method; 123 } 124 125 protected PerTestDataLoaderLifecycle getPerTestDataLoaderLifecycle() { 126 return this.perTestDataLoaderLifecycle; 127 } 128 }