Search in sources :

Example 1 with PowerMockJUnitRunnerDelegate

use of org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate in project powermock by powermock.

the class JUnit4TestSuiteChunkerImpl method getDescription.

@Override
public Description getDescription() {
    if (description == null) {
        if (delegates.size() == 0) {
            /*
				 * This happens if Test A extends Test B and B uses the @RunWith
				 * annotation and there are no tests defined in class B.
				 */
            return Description.createTestDescription(this.getClass(), "no tests in this class");
        }
        // Use the first delegator as the base for the description.
        PowerMockJUnitRunnerDelegate delegate = delegates.get(0);
        description = delegate.getDescription();
        /*
			 * Add the remaining descriptions of all the chunked delegators. We
			 * do this to make sure that we avoid adding chunks as "Unrooted
			 * tests".
			 */
        for (int i = 1; i < delegates.size(); i++) {
            // Get the method-level descriptions
            ArrayList<Description> children = delegates.get(i).getDescription().getChildren();
            // Add all method-level descriptions to the main description.
            for (Description methodDescription : children) {
                description.addChild(methodDescription);
            }
        }
    }
    return description;
}
Also used : Description(org.junit.runner.Description) PowerMockJUnitRunnerDelegate(org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate)

Example 2 with PowerMockJUnitRunnerDelegate

use of org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate 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 PowerMockJUnitRunnerDelegate

use of org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate in project powermock by powermock.

the class JUnit4TestSuiteChunkerImpl method createDelegatorFromClassloader.

@Override
protected PowerMockJUnitRunnerDelegate createDelegatorFromClassloader(ClassLoader classLoader, Class<?> testClass, final List<Method> methodsToTest) throws Exception {
    Set<String> methodNames = new HashSet<String>();
    for (Method method : methodsToTest) {
        methodNames.add(method.getName());
    }
    final Class<?> testClassLoadedByMockedClassLoader = Class.forName(testClass.getName(), false, classLoader);
    /*
		 * Array classes cannot be loaded be classloader.loadClass(..) in JDK 6.
		 * See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6500212.
		 */
    final Class<?> powerMockTestListenerArrayType = Class.forName(PowerMockTestListener[].class.getName(), false, classLoader);
    final Class<?> delegateClass = Class.forName(runnerDelegateImplementationType.getName(), false, classLoader);
    Constructor<?> con = delegateClass.getConstructor(Class.class, String[].class, powerMockTestListenerArrayType);
    return (PowerMockJUnitRunnerDelegate) con.newInstance(testClassLoadedByMockedClassLoader, methodNames.toArray(new String[methodNames.size()]), getPowerMockTestListenersLoadedByASpecificClassLoader(testClass, classLoader));
}
Also used : Method(java.lang.reflect.Method) PowerMockJUnitRunnerDelegate(org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

PowerMockJUnitRunnerDelegate (org.powermock.modules.junit4.common.internal.PowerMockJUnitRunnerDelegate)3 Method (java.lang.reflect.Method)2 LinkedHashSet (java.util.LinkedHashSet)2 HashSet (java.util.HashSet)1 Description (org.junit.runner.Description)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 PowerMockTestNotifier (org.powermock.tests.utils.PowerMockTestNotifier)1 TestChunk (org.powermock.tests.utils.TestChunk)1 PowerMockTestNotifierImpl (org.powermock.tests.utils.impl.PowerMockTestNotifierImpl)1