类org.springframework.test.context.TestExecutionListener源码实例Demo

下面列出了怎么用org.springframework.test.context.TestExecutionListener的API类实例代码及写法,或者点击链接到github查看源代码。

private List<TestExecutionListener> instantiateListeners(Collection<Class<? extends TestExecutionListener>> classes) {
	List<TestExecutionListener> listeners = new ArrayList<>(classes.size());
	for (Class<? extends TestExecutionListener> listenerClass : classes) {
		try {
			listeners.add(BeanUtils.instantiateClass(listenerClass));
		}
		catch (BeanInstantiationException ex) {
			if (ex.getCause() instanceof NoClassDefFoundError) {
				// TestExecutionListener not applicable due to a missing dependency
				if (logger.isDebugEnabled()) {
					logger.debug(String.format(
							"Skipping candidate TestExecutionListener [%s] due to a missing dependency. " +
							"Specify custom listener classes or make the default listener classes " +
							"and their required dependencies available. Offending class: [%s]",
							listenerClass.getName(), ex.getCause().getMessage()));
				}
			}
			else {
				throw ex;
			}
		}
	}
	return listeners;
}
 
/**
 * Get the default {@link TestExecutionListener} classes for this bootstrapper.
 * <p>This method is invoked by {@link #getTestExecutionListeners()} and
 * delegates to {@link #getDefaultTestExecutionListenerClassNames()} to
 * retrieve the class names.
 * <p>If a particular class cannot be loaded, a {@code DEBUG} message will
 * be logged, but the associated exception will not be rethrown.
 */
@SuppressWarnings("unchecked")
protected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses() {
	Set<Class<? extends TestExecutionListener>> defaultListenerClasses = new LinkedHashSet<>();
	ClassLoader cl = getClass().getClassLoader();
	for (String className : getDefaultTestExecutionListenerClassNames()) {
		try {
			defaultListenerClasses.add((Class<? extends TestExecutionListener>) ClassUtils.forName(className, cl));
		}
		catch (Throwable ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not load default TestExecutionListener class [" + className +
						"]. Specify custom listener classes or make the default listener classes available.", ex);
			}
		}
	}
	return defaultListenerClasses;
}
 
private List<TestExecutionListener> instantiateListeners(Collection<Class<? extends TestExecutionListener>> classes) {
	List<TestExecutionListener> listeners = new ArrayList<>(classes.size());
	for (Class<? extends TestExecutionListener> listenerClass : classes) {
		try {
			listeners.add(BeanUtils.instantiateClass(listenerClass));
		}
		catch (BeanInstantiationException ex) {
			if (ex.getCause() instanceof NoClassDefFoundError) {
				// TestExecutionListener not applicable due to a missing dependency
				if (logger.isDebugEnabled()) {
					logger.debug(String.format(
							"Skipping candidate TestExecutionListener [%s] due to a missing dependency. " +
							"Specify custom listener classes or make the default listener classes " +
							"and their required dependencies available. Offending class: [%s]",
							listenerClass.getName(), ex.getCause().getMessage()));
				}
			}
			else {
				throw ex;
			}
		}
	}
	return listeners;
}
 
/**
 * Get the default {@link TestExecutionListener} classes for this bootstrapper.
 * <p>This method is invoked by {@link #getTestExecutionListeners()} and
 * delegates to {@link #getDefaultTestExecutionListenerClassNames()} to
 * retrieve the class names.
 * <p>If a particular class cannot be loaded, a {@code DEBUG} message will
 * be logged, but the associated exception will not be rethrown.
 */
@SuppressWarnings("unchecked")
protected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses() {
	Set<Class<? extends TestExecutionListener>> defaultListenerClasses = new LinkedHashSet<>();
	ClassLoader cl = getClass().getClassLoader();
	for (String className : getDefaultTestExecutionListenerClassNames()) {
		try {
			defaultListenerClasses.add((Class<? extends TestExecutionListener>) ClassUtils.forName(className, cl));
		}
		catch (Throwable ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not load default TestExecutionListener class [" + className +
						"]. Specify custom listener classes or make the default listener classes available.", ex);
			}
		}
	}
	return defaultListenerClasses;
}
 
private List<TestExecutionListener> instantiateListeners(List<Class<? extends TestExecutionListener>> classesList) {
	List<TestExecutionListener> listeners = new ArrayList<TestExecutionListener>(classesList.size());
	for (Class<? extends TestExecutionListener> listenerClass : classesList) {
		NoClassDefFoundError ncdfe = null;
		try {
			listeners.add(BeanUtils.instantiateClass(listenerClass));
		}
		catch (NoClassDefFoundError err) {
			ncdfe = err;
		}
		catch (BeanInstantiationException ex) {
			if (ex.getCause() instanceof NoClassDefFoundError) {
				ncdfe = (NoClassDefFoundError) ex.getCause();
			}
		}
		if (ncdfe != null) {
			if (logger.isInfoEnabled()) {
				logger.info(String.format("Could not instantiate TestExecutionListener [%s]. "
						+ "Specify custom listener classes or make the default listener classes "
						+ "(and their required dependencies) available. Offending class: [%s]",
					listenerClass.getName(), ncdfe.getMessage()));
			}
		}
	}
	return listeners;
}
 
/**
 * Get the default {@link TestExecutionListener} classes for this bootstrapper.
 * <p>This method is invoked by {@link #getTestExecutionListeners()} and
 * delegates to {@link #getDefaultTestExecutionListenerClassNames()} to
 * retrieve the class names.
 * <p>If a particular class cannot be loaded, a {@code DEBUG} message will
 * be logged, but the associated exception will not be rethrown.
 */
@SuppressWarnings("unchecked")
protected Set<Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses() {
	Set<Class<? extends TestExecutionListener>> defaultListenerClasses = new LinkedHashSet<Class<? extends TestExecutionListener>>();
	ClassLoader cl = getClass().getClassLoader();
	for (String className : getDefaultTestExecutionListenerClassNames()) {
		try {
			defaultListenerClasses.add((Class<? extends TestExecutionListener>) ClassUtils.forName(className, cl));
		}
		catch (Throwable ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not load default TestExecutionListener class [" + className
						+ "]. Specify custom listener classes or make the default listener classes available.", ex);
			}
		}
	}
	return defaultListenerClasses;
}
 
@Bean
public TestExecutionListener listener() {
	return mock(TestExecutionListener.class);
}
 
AsyncTestEventComponent(TestExecutionListener listener) {
	this.listener = listener;
}
 
/**
 * Get the names of the default {@link TestExecutionListener} classes for
 * this bootstrapper.
 * <p>The default implementation looks up all
 * {@code org.springframework.test.context.TestExecutionListener} entries
 * configured in all {@code META-INF/spring.factories} files on the classpath.
 * <p>This method is invoked by {@link #getDefaultTestExecutionListenerClasses()}.
 * @return an <em>unmodifiable</em> list of names of default {@code TestExecutionListener}
 * classes
 * @see SpringFactoriesLoader#loadFactoryNames
 */
protected List<String> getDefaultTestExecutionListenerClassNames() {
	List<String> classNames =
			SpringFactoriesLoader.loadFactoryNames(TestExecutionListener.class, getClass().getClassLoader());
	if (logger.isInfoEnabled()) {
		logger.info(String.format("Loaded default TestExecutionListener class names from location [%s]: %s",
				SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classNames));
	}
	return Collections.unmodifiableList(classNames);
}
 
/**
 * Get the names of the default {@link TestExecutionListener} classes for
 * this bootstrapper.
 * <p>The default implementation looks up all
 * {@code org.springframework.test.context.TestExecutionListener} entries
 * configured in all {@code META-INF/spring.factories} files on the classpath.
 * <p>This method is invoked by {@link #getDefaultTestExecutionListenerClasses()}.
 * @return an <em>unmodifiable</em> list of names of default {@code TestExecutionListener}
 * classes
 * @see SpringFactoriesLoader#loadFactoryNames
 */
protected List<String> getDefaultTestExecutionListenerClassNames() {
	List<String> classNames =
			SpringFactoriesLoader.loadFactoryNames(TestExecutionListener.class, getClass().getClassLoader());
	if (logger.isInfoEnabled()) {
		logger.info(String.format("Loaded default TestExecutionListener class names from location [%s]: %s",
				SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classNames));
	}
	return Collections.unmodifiableList(classNames);
}
 
/**
 * Get the names of the default {@link TestExecutionListener} classes for
 * this bootstrapper.
 * <p>The default implementation looks up all
 * {@code org.springframework.test.context.TestExecutionListener} entries
 * configured in all {@code META-INF/spring.factories} files on the classpath.
 * <p>This method is invoked by {@link #getDefaultTestExecutionListenerClasses()}.
 * @return an <em>unmodifiable</em> list of names of default {@code TestExecutionListener}
 * classes
 * @see SpringFactoriesLoader#loadFactoryNames
 */
protected List<String> getDefaultTestExecutionListenerClassNames() {
	final List<String> classNames = SpringFactoriesLoader.loadFactoryNames(TestExecutionListener.class,
		getClass().getClassLoader());

	if (logger.isInfoEnabled()) {
		logger.info(String.format("Loaded default TestExecutionListener class names from location [%s]: %s",
			SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classNames));
	}
	return Collections.unmodifiableList(classNames);
}
 
 同包方法