01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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 java.lang.reflect.Method;
19:
20: import org.apache.commons.beanutils.MethodUtils;
21: import org.junit.internal.runners.TestClassMethodsRunner;
22: import org.junit.internal.runners.TestMethodRunner;
23: import org.junit.runner.notification.RunNotifier;
24:
25: /**
26: * A Runner which invokes setName() on the Test (if the method exists) and sets
27: * it to the name of the test method being invoked.
28: *
29: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
30: * @version $Revision: 1.2.16.1.6.1 $ $Date: 2007/10/17 20:32:05 $
31: * @since 0.9
32: */
33: public class NamedTestClassMethodsRunner extends TestClassMethodsRunner {
34:
35: public NamedTestClassMethodsRunner(final Class<?> klass) {
36: super (klass);
37: }
38:
39: @Override
40: protected TestMethodRunner createMethodRunner(final Object test,
41: final Method method, final RunNotifier notifier) {
42: final TestMethodRunner runner = super .createMethodRunner(test,
43: method, notifier);
44: setTestName(test, method.getName());
45: return runner;
46: }
47:
48: protected void setTestName(final Object test, final String name) {
49: try {
50: final Method setNameMethod = MethodUtils
51: .getAccessibleMethod(test.getClass(), "setName",
52: new Class[] { String.class });
53: setNameMethod.invoke(test, new Object[] { name });
54: } catch (final Exception e) {
55: // no setName method, or we failed to invoke it, so we can't set the
56: // name
57: }
58: }
59: }
|