WALA/com.ibm.wala.cast.java.test/src/com/ibm/wala/cast/java/test/NameAwareTestClassRunner.java

1 line
2.1 KiB
Java

/******************************************************************************
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*****************************************************************************/
package com.ibm.wala.cast.java.test;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.TestClassRunner;
import org.junit.runner.Description;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
/**
* This horrible kludge is used to get the old getName() Junit 3 functionality when running under JUnit 4.
*/
@SuppressWarnings("restriction")
public class NameAwareTestClassRunner extends TestClassRunner {
public NameAwareTestClassRunner(Class<?> klass) throws InitializationError {
super(klass);
}
private static String testName;
protected static String getName() {
return testName;
}
protected static void setName(final String name) {
testName = name;
}
/**
* get the name of the currently running test when using this runner. Similar to TestCase.getName() from JUnit 3.
*/
protected static String getTestName() {
if (testName == null)
return null;
int last = testName.indexOf('(');
if (last < 0)
last = testName.length() + 1;
return testName.substring(0, last);
}
private static class NameListener extends RunListener {
public void testStarted(Description description) throws Exception {
setName(description.isTest() ? description.getDisplayName() : null);
}
public void testFinished(Description description) throws Exception {
if (getName() != null)
if (getName().equals(description.getDisplayName()))
setName(null);
else
throw new Exception("Test name mismatch");
}
}
public void run(final RunNotifier notifier) {
notifier.addListener(new NameListener());
super.run(notifier);
}
}