Search in sources :

Example 1 with TestExecutionListener

use of org.springframework.test.context.TestExecutionListener in project spring-framework by spring-projects.

the class AbstractTestContextBootstrapper method getTestExecutionListeners.

/**
	 * {@inheritDoc}
	 */
@Override
public final List<TestExecutionListener> getTestExecutionListeners() {
    Class<?> clazz = getBootstrapContext().getTestClass();
    Class<TestExecutionListeners> annotationType = TestExecutionListeners.class;
    List<Class<? extends TestExecutionListener>> classesList = new ArrayList<>();
    boolean usingDefaults = false;
    AnnotationDescriptor<TestExecutionListeners> descriptor = MetaAnnotationUtils.findAnnotationDescriptor(clazz, annotationType);
    // Use defaults?
    if (descriptor == null) {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("@TestExecutionListeners is not present for class [%s]: using defaults.", clazz.getName()));
        }
        usingDefaults = true;
        classesList.addAll(getDefaultTestExecutionListenerClasses());
    } else {
        // Traverse the class hierarchy...
        while (descriptor != null) {
            Class<?> declaringClass = descriptor.getDeclaringClass();
            TestExecutionListeners testExecutionListeners = descriptor.synthesizeAnnotation();
            if (logger.isTraceEnabled()) {
                logger.trace(String.format("Retrieved @TestExecutionListeners [%s] for declaring class [%s].", testExecutionListeners, declaringClass.getName()));
            }
            boolean inheritListeners = testExecutionListeners.inheritListeners();
            AnnotationDescriptor<TestExecutionListeners> superDescriptor = MetaAnnotationUtils.findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType);
            // locally declared listeners with the defaults.
            if ((!inheritListeners || superDescriptor == null) && testExecutionListeners.mergeMode() == MergeMode.MERGE_WITH_DEFAULTS) {
                if (logger.isDebugEnabled()) {
                    logger.debug(String.format("Merging default listeners with listeners configured via " + "@TestExecutionListeners for class [%s].", descriptor.getRootDeclaringClass().getName()));
                }
                usingDefaults = true;
                classesList.addAll(getDefaultTestExecutionListenerClasses());
            }
            classesList.addAll(0, Arrays.asList(testExecutionListeners.listeners()));
            descriptor = (inheritListeners ? superDescriptor : null);
        }
    }
    // Remove possible duplicates if we loaded default listeners.
    if (usingDefaults) {
        Set<Class<? extends TestExecutionListener>> classesSet = new HashSet<>();
        classesSet.addAll(classesList);
        classesList.clear();
        classesList.addAll(classesSet);
    }
    List<TestExecutionListener> listeners = instantiateListeners(classesList);
    // Sort by Ordered/@Order if we loaded default listeners.
    if (usingDefaults) {
        AnnotationAwareOrderComparator.sort(listeners);
    }
    if (logger.isInfoEnabled()) {
        logger.info(String.format("Using TestExecutionListeners: %s", listeners));
    }
    return listeners;
}
Also used : TestExecutionListeners(org.springframework.test.context.TestExecutionListeners) ArrayList(java.util.ArrayList) TestExecutionListener(org.springframework.test.context.TestExecutionListener) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with TestExecutionListener

use of org.springframework.test.context.TestExecutionListener in project spring-security by spring-projects.

the class WithSecurityContextTestExcecutionListenerTests method orderOverridden.

// SEC-2709
@Test
public void orderOverridden() {
    AbstractTestExecutionListener otherListener = new AbstractTestExecutionListener() {
    };
    List<TestExecutionListener> listeners = new ArrayList<TestExecutionListener>();
    listeners.add(otherListener);
    listeners.add(this.listener);
    AnnotationAwareOrderComparator.sort(listeners);
    assertThat(listeners).containsSequence(this.listener, otherListener);
}
Also used : ArrayList(java.util.ArrayList) TestExecutionListener(org.springframework.test.context.TestExecutionListener) AbstractTestExecutionListener(org.springframework.test.context.support.AbstractTestExecutionListener) SqlScriptsTestExecutionListener(org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener) AbstractTestExecutionListener(org.springframework.test.context.support.AbstractTestExecutionListener) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 TestExecutionListener (org.springframework.test.context.TestExecutionListener)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1 TestExecutionListeners (org.springframework.test.context.TestExecutionListeners)1 SqlScriptsTestExecutionListener (org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener)1 AbstractTestExecutionListener (org.springframework.test.context.support.AbstractTestExecutionListener)1