Search in sources :

Example 1 with StaticConstructorSuppressExtractorImpl

use of org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl in project powermock by powermock.

the class PowerMockClassloaderExecutor method forClass.

public static SingleClassloaderExecutor forClass(Class<?> testClass, MockPolicyInitializer mockPolicyInitializer) {
    List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
    mockTransformerChain.add(new ClassMockTransformer());
    mockTransformerChain.add(new InterfaceMockTransformer());
    MockClassLoader mockLoader = new MockClassLoader(new String[0], new String[0]);
    mockLoader.setMockTransformerChain(mockTransformerChain);
    mockLoader.addIgnorePackage(new PowerMockIgnorePackagesExtractorImpl().getPackagesToIgnore(testClass));
    mockLoader.addClassesToModify(new PrepareForTestExtractorImpl().getTestClasses(testClass));
    mockLoader.addClassesToModify(new StaticConstructorSuppressExtractorImpl().getTestClasses(testClass));
    mockPolicyInitializer.initialize(mockLoader);
    return new SingleClassloaderExecutor(mockLoader);
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) PowerMockIgnorePackagesExtractorImpl(org.powermock.tests.utils.impl.PowerMockIgnorePackagesExtractorImpl) StaticConstructorSuppressExtractorImpl(org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) ArrayList(java.util.ArrayList) SingleClassloaderExecutor(org.powermock.classloading.SingleClassloaderExecutor) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) PrepareForTestExtractorImpl(org.powermock.tests.utils.impl.PrepareForTestExtractorImpl)

Example 2 with StaticConstructorSuppressExtractorImpl

use of org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl in project powermock by powermock.

the class PowerMockJUnit44RunnerDelegateImpl method runMethods.

protected void runMethods(final RunNotifier notifier) {
    final StaticConstructorSuppressExtractorImpl staticConstructorSuppressExtractorImpl = new StaticConstructorSuppressExtractorImpl();
    Class<?> testType = getTestClass();
    final ClassLoader thisClassLoader = getClass().getClassLoader();
    if (!thisClassLoader.equals(testType.getClassLoader())) {
        /*
             * The test is loaded from another classloader, this means that we
             * cannot get the correct annotations if we don't load the class
             * from the correct class loader
             */
        try {
            testType = thisClassLoader.loadClass(testType.getName());
        } catch (ClassNotFoundException e) {
            // This should never happen
            throw new RuntimeException("Internal error in PowerMock", e);
        }
    }
    for (Method method : testMethods) {
        if (staticConstructorSuppressExtractorImpl.getTestClasses(method) == null) {
            staticConstructorSuppressExtractorImpl.getTestClasses(testType);
        }
        invokeTestMethod(method, notifier);
    }
}
Also used : StaticConstructorSuppressExtractorImpl(org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl) Method(java.lang.reflect.Method)

Example 3 with StaticConstructorSuppressExtractorImpl

use of org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl in project powermock by powermock.

the class PowerMockJUnit4LegacyTestClassMethodsRunner method run.

@SuppressWarnings("unchecked")
@Override
public void run(RunNotifier notifier) {
    final List<Method> methods = Whitebox.getInternalState(this, "fTestMethods");
    if (methods.isEmpty())
        notifier.testAborted(getDescription(), new Exception("No runnable methods"));
    for (Method method : methods) {
        final StaticConstructorSuppressExtractorImpl staticConstructorSuppressExtractorImpl = new StaticConstructorSuppressExtractorImpl();
        Class<?> testType = getTestClass();
        final ClassLoader thisClassLoader = getClass().getClassLoader();
        if (!thisClassLoader.equals(testType.getClassLoader())) {
            /*
				 * The test is loaded from another classloader, this means that
				 * we cannot get the correct annotations if we don't load the
				 * class from the correct class loader
				 */
            try {
                testType = thisClassLoader.loadClass(testType.getName());
            } catch (ClassNotFoundException e) {
                // This should never happen
                throw new RuntimeException("Internal error in PowerMock", e);
            }
        }
        if (staticConstructorSuppressExtractorImpl.getTestClasses(method) == null) {
            staticConstructorSuppressExtractorImpl.getTestClasses(testType);
        }
        invokeTestMethod(method, notifier);
    }
}
Also used : StaticConstructorSuppressExtractorImpl(org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl) Method(java.lang.reflect.Method)

Example 4 with StaticConstructorSuppressExtractorImpl

use of org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl in project powermock by powermock.

the class PowerMockAgentTestInitializer method redefineClassesToSuppress.

private static void redefineClassesToSuppress(Class<?> testClass, String[] packagesToIgnore, JavaAgentClassRegister agentClassRegister) {
    final String[] classesToSuppress = new StaticConstructorSuppressExtractorImpl().getTestClasses(testClass);
    redefine(classesToSuppress, packagesToIgnore, agentClassRegister);
}
Also used : StaticConstructorSuppressExtractorImpl(org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl)

Aggregations

StaticConstructorSuppressExtractorImpl (org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl)4 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)1 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1 MockTransformer (org.powermock.core.transformers.MockTransformer)1 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)1 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)1 PowerMockIgnorePackagesExtractorImpl (org.powermock.tests.utils.impl.PowerMockIgnorePackagesExtractorImpl)1 PrepareForTestExtractorImpl (org.powermock.tests.utils.impl.PrepareForTestExtractorImpl)1