View Javadoc

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.runners;
17  
18  import org.apache.commons.beanutils.MethodUtils;
19  import org.junit.runner.notification.RunNotifier;
20  import org.junit.runners.BlockJUnit4ClassRunner;
21  import org.junit.runners.model.FrameworkMethod;
22  import org.junit.runners.model.InitializationError;
23  import org.kuali.rice.test.MethodAware;
24  
25  import java.lang.reflect.Method;
26  
27  /**
28   * A Runner which sets up Rice unit tests appropriately. 
29   * 1) It invokes setName() on the Test (if the method exists) and sets it to the name of the test method being invoked. 
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   * @since 0.9
33   */
34  public class RiceUnitTestClassRunner extends BlockJUnit4ClassRunner {
35      private Method currentMethod;
36      
37      public RiceUnitTestClassRunner(final Class<?> testClass) throws InitializationError {
38          super(testClass);
39          
40      }
41  
42      @Override
43      protected void runChild(FrameworkMethod method, RunNotifier runNotifier) {
44          this.currentMethod = method.getMethod();
45          try {
46              super.runChild(method, runNotifier);
47          } finally {
48              this.currentMethod = null;
49          }
50      }
51  
52      @Override
53      protected Object createTest() throws Exception {
54          Object test = super.createTest();
55          setTestName(test, currentMethod);
56          setTestMethod(test, currentMethod);
57          return test;
58      }
59  
60      /**
61       * Sets the {@link java.lang.reflect.Method} on the test case if it is {@link MethodAware}
62       * @param method the current method to be run
63       * @param test the test instance
64       */
65      protected void setTestMethod(Object test, Method method) {
66          if (test instanceof MethodAware) {
67              ((MethodAware) test).setTestMethod(method);
68          }
69      }
70  
71      protected void setTestName(final Object test, final Method testMethod) throws Exception {
72              String name = testMethod == null ? "" : testMethod.getName();
73              final Method setNameMethod = MethodUtils.getAccessibleMethod(test.getClass(), "setName", new Class[]{String.class});
74              if (setNameMethod != null) {
75                  setNameMethod.invoke(test, name);
76              }
77      }
78  
79  }