Coverage Report - org.kuali.rice.test.BaseRiceTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseRiceTestCase
0%
0/28
0%
0/4
1.222
 
 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  0
         protected final Logger log = Logger.getLogger(getClass());
 46  
 
 47  0
         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  0
                 super();
 55  0
         }
 56  
 
 57  
         public String getName() {
 58  0
                 return this.name;
 59  
         }
 60  
 
 61  
         public void setName(String name) {
 62  0
                 this.name = name;
 63  0
         }
 64  
     
 65  
         @Before
 66  
         public void setUp() throws Exception {
 67  0
         }
 68  
         
 69  
     @After
 70  
     public void tearDown() throws Exception {
 71  0
         resetLogLevels();
 72  0
     }
 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  0
         Logger logger = Logger.getLogger(loggerName);
 86  
 
 87  0
         if (!changedLogLevels.containsKey(loggerName)) {
 88  0
             Level originalLevel = logger.getLevel();
 89  0
             changedLogLevels.put(loggerName, originalLevel);
 90  
         }
 91  
 
 92  0
         logger.setLevel(newLevel);
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Restores the logging-levels changed through calls to setLogLevel to their
 97  
      * original values.
 98  
      */
 99  
     protected void resetLogLevels() {
 100  0
         for (Iterator i = changedLogLevels.entrySet().iterator(); i.hasNext();) {
 101  0
             Map.Entry e = (Map.Entry) i.next();
 102  
 
 103  0
             String loggerName = (String) e.getKey();
 104  0
             Level originalLevel = (Level) e.getValue();
 105  
 
 106  0
             Logger.getLogger(loggerName).setLevel(originalLevel);
 107  0
         }
 108  0
         changedLogLevels.clear();
 109  0
     }
 110  
     
 111  
         /**
 112  
          * @see org.kuali.rice.test.MethodAware#setTestMethod(java.lang.reflect.Method)
 113  
          */
 114  
         public void setTestMethod(Method testMethod) {
 115  0
         this.method = testMethod;
 116  
 
 117  0
         perTestDataLoaderLifecycle = new PerTestDataLoaderLifecycle(method);
 118  0
     }
 119  
         
 120  
     protected PerTestDataLoaderLifecycle getPerTestDataLoaderLifecycle() {
 121  0
                 return this.perTestDataLoaderLifecycle;
 122  
         }
 123  
 }