Search in sources :

Example 1 with TestChunk

use of org.powermock.tests.utils.TestChunk in project powermock by powermock.

the class JUnit3TestSuiteChunkerImpl method runTest.

public void runTest(Test test, TestResult result) {
    final Iterator<TestChunk> iterator = getChunkIterator();
    for (PowerMockJUnit3RunnerDelegate delegate : delegates) {
        TestChunk next = iterator.next();
        final PowerMockJUnit3TestListener listener = new PowerMockJUnit3TestListener(next.getClassLoader());
        result.addListener(listener);
        delegate.runTest(test, result);
        result.removeListener(listener);
    }
}
Also used : TestChunk(org.powermock.tests.utils.TestChunk) PowerMockJUnit3RunnerDelegate(org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate)

Example 2 with TestChunk

use of org.powermock.tests.utils.TestChunk 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 3 with TestChunk

use of org.powermock.tests.utils.TestChunk in project powermock by powermock.

the class AbstractTestSuiteChunkerImpl method createTestDelegators.

public final void createTestDelegators(Class<?> testClass, List<TestChunk> chunks) throws Exception {
    for (TestChunk chunk : chunks) {
        ClassLoader classLoader = chunk.getClassLoader();
        List<Method> methodsToTest = chunk.getTestMethodsToBeExecutedByThisClassloader();
        T runnerDelegator = createDelegatorFromClassloader(classLoader, testClass, methodsToTest);
        delegates.add(runnerDelegator);
    }
    delegatesCreatedForTheseClasses.add(testClass);
}
Also used : TestChunk(org.powermock.tests.utils.TestChunk) Method(java.lang.reflect.Method)

Example 4 with TestChunk

use of org.powermock.tests.utils.TestChunk in project powermock by powermock.

the class JUnit3TestSuiteChunkerImpl method run.

public void run(TestResult result) {
    final Iterator<TestChunk> iterator = getChunkIterator();
    for (PowerMockJUnit3RunnerDelegate delegate : delegates) {
        TestChunk next = iterator.next();
        final PowerMockJUnit3TestListener listener = new PowerMockJUnit3TestListener(next.getClassLoader());
        result.addListener(listener);
        // Initialize mock policies for each test
        new MockPolicyInitializerImpl(delegate.getTestClass()).initialize(this.getClass().getClassLoader());
        delegate.run(result);
        result.removeListener(listener);
    }
}
Also used : TestChunk(org.powermock.tests.utils.TestChunk) PowerMockJUnit3RunnerDelegate(org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate) MockPolicyInitializerImpl(org.powermock.tests.utils.impl.MockPolicyInitializerImpl)

Example 5 with TestChunk

use of org.powermock.tests.utils.TestChunk in project powermock by powermock.

the class AbstractCommonTestSuiteChunkerImpl method chunkClass.

protected void chunkClass(final Class<?> testClass) throws Exception {
    List<Method> testMethodsForOtherClassLoaders = new ArrayList<Method>();
    MockTransformer[] extraMockTransformers = createDefaultExtraMockTransformers(testClass, testMethodsForOtherClassLoaders);
    final String[] ignorePackages = ignorePackagesExtractor.getPackagesToIgnore(testClass);
    final ClassLoader defaultMockLoader = createDefaultMockLoader(testClass, extraMockTransformers, ignorePackages);
    List<Method> currentClassloaderMethods = new LinkedList<Method>();
    // Put the first suite in the map of internal suites.
    TestChunk defaultTestChunk = new TestChunkImpl(defaultMockLoader, currentClassloaderMethods);
    List<TestChunk> testChunks = new LinkedList<TestChunk>();
    testChunks.add(defaultTestChunk);
    internalSuites.add(new TestCaseEntry(testClass, testChunks));
    initEntries(internalSuites);
    if (!currentClassloaderMethods.isEmpty()) {
        List<TestChunk> allTestChunks = internalSuites.get(0).getTestChunks();
        for (TestChunk chunk : allTestChunks.subList(1, allTestChunks.size())) {
            for (Method m : chunk.getTestMethodsToBeExecutedByThisClassloader()) {
                testMethodsForOtherClassLoaders.add(m);
            }
        }
    } else if (2 <= internalSuites.size() || 1 == internalSuites.size() && 2 <= internalSuites.get(0).getTestChunks().size()) {
        /*
             * If we don't have any test that should be executed by the default
             * class loader remove it to avoid duplicate test print outs.
             */
        internalSuites.get(0).getTestChunks().remove(0);
    }
//else{ /*Delegation-runner maybe doesn't use test-method annotations!*/ }
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) TestChunk(org.powermock.tests.utils.TestChunk) MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Aggregations

TestChunk (org.powermock.tests.utils.TestChunk)5 Method (java.lang.reflect.Method)3 PowerMockJUnit3RunnerDelegate (org.powermock.modules.junit3.internal.PowerMockJUnit3RunnerDelegate)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1 MockingFrameworkReporter (org.powermock.core.reporter.MockingFrameworkReporter)1 PowerMockTestListener (org.powermock.core.spi.PowerMockTestListener)1 TestSuiteResult (org.powermock.core.spi.testresult.TestSuiteResult)1 TestSuiteResultImpl (org.powermock.core.spi.testresult.impl.TestSuiteResultImpl)1 MockTransformer (org.powermock.core.transformers.MockTransformer)1 PowerMockJUnitRunnerDelegate (org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate)1 PowerMockTestNotifier (org.powermock.tests.utils.PowerMockTestNotifier)1 MockPolicyInitializerImpl (org.powermock.tests.utils.impl.MockPolicyInitializerImpl)1 PowerMockTestNotifierImpl (org.powermock.tests.utils.impl.PowerMockTestNotifierImpl)1