001 /* 002 * Copyright 2007 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.runners; 014 015 import java.lang.reflect.Method; 016 017 import org.apache.commons.beanutils.MethodUtils; 018 import org.junit.internal.runners.InitializationError; 019 import org.junit.internal.runners.JUnit4ClassRunner; 020 import org.junit.runner.notification.RunNotifier; 021 import org.kuali.rice.test.MethodAware; 022 023 /** 024 * A Runner which sets up Rice unit tests appropriately. 025 * 1) It invokes setName() on the Test (if the method exists) and sets it to the name of the test method being invoked. 026 * 027 * @author Kuali Rice Team (rice.collab@kuali.org) 028 * @since 0.9 029 */ 030 public class RiceUnitTestClassRunner extends JUnit4ClassRunner { 031 //private PerTestDataLoaderLifecycle perTestDataLoaderLifecycle; 032 private Method currentMethod; 033 034 public RiceUnitTestClassRunner(final Class<?> testClass) throws InitializationError { 035 super(testClass); 036 037 } 038 039 @Override 040 protected void invokeTestMethod(Method method, RunNotifier runNotifier) { 041 this.currentMethod = method; 042 try { 043 //perTestDataLoaderLifecycle = new PerTestDataLoaderLifecycle(method); 044 super.invokeTestMethod(method, runNotifier); 045 } finally { 046 this.currentMethod = null; 047 } 048 } 049 050 @Override 051 protected Object createTest() throws Exception { 052 Object test = super.createTest(); 053 setTestName(test, currentMethod); 054 setTestMethod(test, currentMethod); 055 //setTestPerTestDataLoaderLifecycle(test); 056 return test; 057 } 058 059 /** 060 * Sets the {@link java.lang.reflect.Method} on the test case if it is {@link MethodAware} 061 * @param method the current method to be run 062 */ 063 protected void setTestMethod(Object test, Method method) { 064 try { 065 if (test instanceof MethodAware) { 066 ((MethodAware) test).setTestMethod(method); 067 } 068 } catch (Exception e) { 069 e.printStackTrace(); 070 // something went horribly wrong? 071 } 072 } 073 074 /* 075 protected void setTestPerTestDataLoaderLifecycle(final Object test) { 076 try { 077 final Method setPerTestDataLoaderLifecycle = MethodUtils.getAccessibleMethod(test.getClass(), "setPerTestDataLoaderLifecycle", new Class[]{PerTestDataLoaderLifecycle.class}); 078 setPerTestDataLoaderLifecycle.invoke(test, new Object[]{perTestDataLoaderLifecycle}); 079 } catch (final Exception e) { 080 // no setPerTestDataLoaderLifecycle method or we failed to invoke it so we can't set the lifecycle 081 } 082 }*/ 083 084 protected void setTestName(final Object test, final Method testMethod) { 085 try { 086 String name = testMethod == null ? "" : testMethod.getName(); 087 final Method setNameMethod = MethodUtils.getAccessibleMethod(test.getClass(), "setName", new Class[]{String.class}); 088 if (setNameMethod != null) { 089 setNameMethod.invoke(test, new Object[]{name}); 090 } 091 } catch (final Exception e) { 092 // no setName method or we failed to invoke it so we can't set the name 093 } 094 } 095 096 }