Search in sources :

Example 1 with PowerMockListener

use of org.powermock.core.classloader.annotations.PowerMockListener in project powermock by powermock.

the class AbstractTestSuiteChunkerImpl method getPowerMockTestListenersLoadedByASpecificClassLoader.

protected Object getPowerMockTestListenersLoadedByASpecificClassLoader(Class<?> clazz, ClassLoader classLoader) {
    try {
        int defaultListenerSize = DEFAULT_TEST_LISTENERS_SIZE;
        Class<?> annotationEnablerClass = null;
        try {
            annotationEnablerClass = Class.forName("org.powermock.api.extension.listener.AnnotationEnabler", false, classLoader);
        } catch (ClassNotFoundException e) {
            // Annotation enabler wasn't found in class path
            defaultListenerSize = 0;
        }
        final Class<?> powerMockTestListenerType = Class.forName(PowerMockTestListener.class.getName(), false, classLoader);
        Object testListeners = null;
        if (clazz.isAnnotationPresent(PowerMockListener.class)) {
            PowerMockListener annotation = clazz.getAnnotation(PowerMockListener.class);
            final Class<? extends PowerMockTestListener>[] powerMockTestListeners = annotation.value();
            if (powerMockTestListeners.length > 0) {
                testListeners = Array.newInstance(powerMockTestListenerType, powerMockTestListeners.length + defaultListenerSize);
                for (int i = 0; i < powerMockTestListeners.length; i++) {
                    String testListenerClassName = powerMockTestListeners[i].getName();
                    final Class<?> listenerTypeLoadedByClassLoader = Class.forName(testListenerClassName, false, classLoader);
                    Array.set(testListeners, i, Whitebox.newInstance(listenerTypeLoadedByClassLoader));
                }
            }
        } else {
            testListeners = Array.newInstance(powerMockTestListenerType, defaultListenerSize);
        }
        // Add default annotation enabler listener
        if (annotationEnablerClass != null) {
            Array.set(testListeners, Array.getLength(testListeners) - 1, Whitebox.newInstance(annotationEnablerClass));
        }
        return testListeners;
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("PowerMock internal error: Failed to load class.", e);
    }
}
Also used : PowerMockTestListener(org.powermock.core.spi.PowerMockTestListener) PowerMockListener(org.powermock.core.classloader.annotations.PowerMockListener)

Aggregations

PowerMockListener (org.powermock.core.classloader.annotations.PowerMockListener)1 PowerMockTestListener (org.powermock.core.spi.PowerMockTestListener)1