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