View Javadoc

1   /*
2    * Copyright 2007 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.runners;
14  
15  import java.lang.reflect.Method;
16  
17  import org.apache.commons.beanutils.MethodUtils;
18  import org.junit.internal.runners.InitializationError;
19  import org.junit.internal.runners.JUnit4ClassRunner;
20  import org.junit.runner.notification.RunNotifier;
21  import org.kuali.rice.test.MethodAware;
22  
23  /**
24   * A Runner which sets up Rice unit tests appropriately. 
25   * 1) It invokes setName() on the Test (if the method exists) and sets it to the name of the test method being invoked. 
26   * 
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   * @since 0.9
29   */
30  public class RiceUnitTestClassRunner extends JUnit4ClassRunner {
31      //private PerTestDataLoaderLifecycle perTestDataLoaderLifecycle;
32      private Method currentMethod;
33      
34      public RiceUnitTestClassRunner(final Class<?> testClass) throws InitializationError {
35          super(testClass);
36          
37      }
38  
39      @Override
40      protected void invokeTestMethod(Method method, RunNotifier runNotifier) {
41          this.currentMethod = method;
42          try {
43              //perTestDataLoaderLifecycle = new PerTestDataLoaderLifecycle(method);
44              super.invokeTestMethod(method, runNotifier);
45          } finally {
46              this.currentMethod = null;
47          }
48      }
49  
50      @Override
51      protected Object createTest() throws Exception {
52          Object test = super.createTest();
53          setTestName(test, currentMethod);
54          setTestMethod(test, currentMethod);
55          //setTestPerTestDataLoaderLifecycle(test);
56          return test;
57      }
58  
59      /**
60       * Sets the {@link java.lang.reflect.Method} on the test case if it is {@link MethodAware}
61       * @param method the current method to be run
62       */
63      protected void setTestMethod(Object test, Method method) {
64          try {
65              if (test instanceof MethodAware) {
66                  ((MethodAware) test).setTestMethod(method);
67              }
68          } catch (Exception e) {
69              e.printStackTrace();
70              // something went horribly wrong?
71          }
72      }
73  
74      /*
75      protected void setTestPerTestDataLoaderLifecycle(final Object test) {
76          try {
77              final Method setPerTestDataLoaderLifecycle = MethodUtils.getAccessibleMethod(test.getClass(), "setPerTestDataLoaderLifecycle", new Class[]{PerTestDataLoaderLifecycle.class});
78              setPerTestDataLoaderLifecycle.invoke(test, new Object[]{perTestDataLoaderLifecycle});
79          } catch (final Exception e) {
80              // no setPerTestDataLoaderLifecycle method or we failed to invoke it so we can't set the lifecycle
81          }
82      }*/
83  
84      protected void setTestName(final Object test, final Method testMethod) {
85          try {
86              String name = testMethod == null ? "" : testMethod.getName();
87              final Method setNameMethod = MethodUtils.getAccessibleMethod(test.getClass(), "setName", new Class[]{String.class});
88              if (setNameMethod != null) {
89                  setNameMethod.invoke(test, new Object[]{name});
90              }
91          } catch (final Exception e) {
92              // no setName method or we failed to invoke it so we can't set the name
93          }
94      }
95  
96  }