Search in sources :

Example 1 with PowerMockTestListener

use of org.powermock.core.spi.PowerMockTestListener in project powermock by powermock.

the class JUnit4TestSuiteChunkerImpl method run.

@Override
public void run(RunNotifier notifier) {
    List<TestChunk> chunkEntries = getTestChunks();
    Iterator<TestChunk> iterator = chunkEntries.iterator();
    if (delegates.size() != getChunkSize()) {
        throw new IllegalStateException("Internal error: There must be an equal number of suites and delegates.");
    }
    final Class<?> testClass = getTestClasses()[0];
    final PowerMockTestListener[] powerMockTestListeners = (PowerMockTestListener[]) getPowerMockTestListenersLoadedByASpecificClassLoader(testClass, this.getClass().getClassLoader());
    final Set<Method> allMethods = new LinkedHashSet<Method>();
    for (TestChunk testChunk : getTestChunks()) {
        allMethods.addAll(testChunk.getTestMethodsToBeExecutedByThisClassloader());
    }
    final Method[] allMethodsAsArray = allMethods.toArray(new Method[allMethods.size()]);
    final PowerMockTestNotifier powerMockTestNotifier = new PowerMockTestNotifierImpl(powerMockTestListeners);
    powerMockTestNotifier.notifyBeforeTestSuiteStarted(testClass, allMethodsAsArray);
    int failureCount = 0;
    int successCount = 0;
    int ignoreCount = 0;
    for (PowerMockJUnitRunnerDelegate delegate : delegates) {
        TestChunk next = iterator.next();
        final ClassLoader key = next.getClassLoader();
        PowerMockJUnit4RunListener powerMockListener = new PowerMockJUnit4RunListener(key, powerMockTestNotifier);
        notifier.addListener(powerMockListener);
        final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(key);
        try {
            MockingFrameworkReporter mockingFrameworkReporter = getMockingFrameworkReporter();
            mockingFrameworkReporter.enable();
            delegate.run(notifier);
            mockingFrameworkReporter.disable();
        } finally {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
        final int failureCountForThisPowerMockListener = powerMockListener.getFailureCount();
        final int ignoreCountForThisPowerMockListener = powerMockListener.getIgnoreCount();
        failureCount += failureCountForThisPowerMockListener;
        ignoreCount += ignoreCountForThisPowerMockListener;
        successCount += delegate.getTestCount() - failureCountForThisPowerMockListener - ignoreCountForThisPowerMockListener;
        notifier.removeListener(powerMockListener);
    }
    final TestSuiteResult testSuiteResult = new TestSuiteResultImpl(failureCount, successCount, getTestCount(), ignoreCount);
    powerMockTestNotifier.notifyAfterTestSuiteEnded(testClass, allMethodsAsArray, testSuiteResult);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PowerMockTestNotifierImpl(org.powermock.tests.utils.impl.PowerMockTestNotifierImpl) MockingFrameworkReporter(org.powermock.core.reporter.MockingFrameworkReporter) Method(java.lang.reflect.Method) PowerMockTestNotifier(org.powermock.tests.utils.PowerMockTestNotifier) TestSuiteResultImpl(org.powermock.core.spi.testresult.impl.TestSuiteResultImpl) TestChunk(org.powermock.tests.utils.TestChunk) TestSuiteResult(org.powermock.core.spi.testresult.TestSuiteResult) PowerMockTestListener(org.powermock.core.spi.PowerMockTestListener) PowerMockJUnitRunnerDelegate(org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate)

Example 2 with PowerMockTestListener

use of org.powermock.core.spi.PowerMockTestListener 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)

Example 3 with PowerMockTestListener

use of org.powermock.core.spi.PowerMockTestListener in project powermock by powermock.

the class PowerMockTestNotifierImpl method notifyBeforeTestMethod.

@Override
public void notifyBeforeTestMethod(Object testInstance, Method testMethod, Object[] arguments) {
    MockRepository.putAdditionalState(Keys.CURRENT_TEST_INSTANCE, testInstance);
    MockRepository.putAdditionalState(Keys.CURRENT_TEST_METHOD, testMethod);
    MockRepository.putAdditionalState(Keys.CURRENT_TEST_METHOD_ARGUMENTS, arguments);
    for (final PowerMockTestListener testListener : powerMockTestListeners) {
        try {
            testListener.beforeTestMethod(testInstance, testMethod, arguments);
        } catch (Exception e) {
            throw new RuntimeException(String.format(ERROR_MESSAGE_TEMPLATE, "beforeTestMethod", testListener), e);
        }
    }
}
Also used : PowerMockTestListener(org.powermock.core.spi.PowerMockTestListener)

Aggregations

PowerMockTestListener (org.powermock.core.spi.PowerMockTestListener)3 Method (java.lang.reflect.Method)1 LinkedHashSet (java.util.LinkedHashSet)1 PowerMockListener (org.powermock.core.classloader.annotations.PowerMockListener)1 MockingFrameworkReporter (org.powermock.core.reporter.MockingFrameworkReporter)1 TestSuiteResult (org.powermock.core.spi.testresult.TestSuiteResult)1 TestSuiteResultImpl (org.powermock.core.spi.testresult.impl.TestSuiteResultImpl)1 PowerMockJUnitRunnerDelegate (org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate)1 PowerMockTestNotifier (org.powermock.tests.utils.PowerMockTestNotifier)1 TestChunk (org.powermock.tests.utils.TestChunk)1 PowerMockTestNotifierImpl (org.powermock.tests.utils.impl.PowerMockTestNotifierImpl)1