View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  
17  package org.springunit.framework.junit4;
18  
19  import org.junit.internal.runners.InitializationError;
20  import org.junit.internal.runners.JUnit4ClassRunner;
21  import org.junit.runner.notification.RunNotifier;
22  
23  /**
24   * Stores the name of the currently running test from the TestRunner.<br/>
25   * This implementation follows the example given by Gabriel Cooper at
26   * http://tech.groups.yahoo.com/group/junit/message/18728.<br/>
27   * 
28   * @author <a href="mailto:ted@velkoff.com">Ted Velkoff</a>
29   *
30   */
31  public class NameRunner extends JUnit4ClassRunner {
32  	
33  	public NameRunner(Class<?> clazz) throws InitializationError {
34  		super(clazz);
35  	}
36  	
37  	public static synchronized String getTestName() {
38  		return testName;
39  	}
40  
41  	public static synchronized void setTestName(String name) {
42  		testName = name;
43  	}
44  	
45  	public static String getTestMethodName() {
46  		String name = getTestName();
47  		if (name == null) {
48  			return null;
49  		}
50  		int last = name.indexOf('(');
51  		last = last < 0 ? name.length() : last;
52  		return name.substring(0, last);
53  	}
54  	
55  	public void run(final RunNotifier notifier) {
56  		notifier.addListener(new NameListener());
57  		super.run(notifier);
58  	}
59  
60  	private static String testName;
61  
62  }