⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beforeandafterrunner.java

📁 这是Junit4.1.3的源码程序
💻 JAVA
字号:
package org.junit.internal.runners;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public abstract class BeforeAndAfterRunner {
	private static class FailedBefore extends Exception {
		private static final long serialVersionUID= 1L;
	}

	private final Class<? extends Annotation> fBeforeAnnotation;

	private final Class<? extends Annotation> fAfterAnnotation;

	private TestIntrospector fTestIntrospector;

	private Object fTest;

	public BeforeAndAfterRunner(Class<?> testClass,
			Class<? extends Annotation> beforeAnnotation,
			Class<? extends Annotation> afterAnnotation, 
			Object test) {
		fBeforeAnnotation= beforeAnnotation;
		fAfterAnnotation= afterAnnotation;
		fTestIntrospector= new TestIntrospector(testClass);
		fTest= test;
	}

	public void runProtected() {
		try {
			runBefores();
			runUnprotected();
		} catch (FailedBefore e) {
		} finally {
			runAfters();
		}
	}

	protected abstract void runUnprotected();

	protected abstract void addFailure(Throwable targetException);

	// Stop after first failed @Before
	private void runBefores() throws FailedBefore {
		try {
			List<Method> befores= fTestIntrospector.getTestMethods(fBeforeAnnotation);
			for (Method before : befores)
				invokeMethod(before);
		} catch (InvocationTargetException e) {
			addFailure(e.getTargetException());
			throw new FailedBefore();
		} catch (Throwable e) {
			addFailure(e);
			throw new FailedBefore();
		}
	}

	// Try to run all @Afters regardless
	private void runAfters() {
		List<Method> afters= fTestIntrospector.getTestMethods(fAfterAnnotation);
		for (Method after : afters)
			try {
				invokeMethod(after);
			} catch (InvocationTargetException e) {
				addFailure(e.getTargetException());
			} catch (Throwable e) {
				addFailure(e); // Untested, but seems impossible
			}
	}
	
	private void invokeMethod(Method method) throws Exception {
		method.invoke(fTest);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -