Search in sources :

Example 6 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class MockClassLoader method loadMockClass.

/**
     * Load a mocked version of the class.
     */
private Class<?> loadMockClass(String name, ProtectionDomain protectionDomain) {
    final byte[] clazz;
    ClassPool.doPruning = false;
    try {
        CtClass type = classPool.get(name);
        for (MockTransformer transformer : mockTransformerChain) {
            type = transformer.transform(type);
        }
        javaAssistClassMarker.mark(type);
        /*
             * ClassPool may cause huge memory consumption if the number of CtClass
             * objects becomes amazingly large (this rarely happens since Javassist
             * tries to reduce memory consumption in various ways). To avoid this
             * problem, you can explicitly remove an unnecessary CtClass object from
             * the ClassPool. If you call detach() on a CtClass object, then that
             * CtClass object is removed from the ClassPool.
             */
        type.detach();
        clazz = type.toBytecode();
    } catch (Exception e) {
        throw new IllegalStateException("Failed to transform class with name " + name + ". Reason: " + e.getMessage(), e);
    }
    return defineClass(name, clazz, 0, clazz.length, protectionDomain);
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) CtClass(javassist.CtClass)

Example 7 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class ClassLoaderFactory method getMockTransformers.

private List<MockTransformer> getMockTransformers() {
    List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
    mockTransformerChain.add(new ClassMockTransformer());
    mockTransformerChain.add(new InterfaceMockTransformer());
    return mockTransformerChain;
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ArrayList(java.util.ArrayList) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer)

Example 8 with MockTransformer

use of org.powermock.core.transformers.MockTransformer 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)

Example 9 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class MockClassLoaderFactory method getMockTransformers.

protected List<MockTransformer> getMockTransformers(MockTransformer[] extraMockTransformers) {
    List<MockTransformer> mockTransformerChain = new ArrayList<MockTransformer>();
    mockTransformerChain.add(new ClassMockTransformer());
    mockTransformerChain.add(new InterfaceMockTransformer());
    Collections.addAll(mockTransformerChain, extraMockTransformers);
    return mockTransformerChain;
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ArrayList(java.util.ArrayList) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer)

Example 10 with MockTransformer

use of org.powermock.core.transformers.MockTransformer in project powermock by powermock.

the class MockClassLoaderTest method canFindResource.

@Test
public void canFindResource() throws Exception {
    final MockClassLoader mockClassLoader = new MockClassLoader(new String[0]);
    List<MockTransformer> list = new LinkedList<MockTransformer>();
    list.add(new ClassMockTransformer());
    mockClassLoader.setMockTransformerChain(list);
    // Force a ClassLoader that can find 'foo/bar/baz/test.txt' into
    // mockClassLoader.deferTo.
    mockClassLoader.deferTo = new ResourcePrefixClassLoader(getClass().getClassLoader(), "org/powermock/core/classloader/");
    // MockClassLoader will only be able to find 'foo/bar/baz/test.txt' if it
    // properly defers the resource lookup to its deferTo ClassLoader.
    URL resource = mockClassLoader.getResource("foo/bar/baz/test.txt");
    assertThat(resource).isNotNull();
    assertThat(resource.getPath()).endsWith("test.txt");
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) LinkedList(java.util.LinkedList) URL(java.net.URL) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) Test(org.junit.Test)

Aggregations

MockTransformer (org.powermock.core.transformers.MockTransformer)14 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)10 LinkedList (java.util.LinkedList)9 Test (org.junit.Test)6 CtClass (javassist.CtClass)5 MockClassLoader (org.powermock.core.classloader.MockClassLoader)5 ArrayList (java.util.ArrayList)4 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)4 URL (java.net.URL)3 UseClassPathAdjuster (org.powermock.core.classloader.annotations.UseClassPathAdjuster)2 MyArgument (powermock.classloading.classes.MyArgument)2 MyClass (powermock.classloading.classes.MyClass)2 MyReturnValue (powermock.classloading.classes.MyReturnValue)2 Method (java.lang.reflect.Method)1 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)1 TestChunk (org.powermock.tests.utils.TestChunk)1 PowerMockIgnorePackagesExtractorImpl (org.powermock.tests.utils.impl.PowerMockIgnorePackagesExtractorImpl)1 PrepareForTestExtractorImpl (org.powermock.tests.utils.impl.PrepareForTestExtractorImpl)1 StaticConstructorSuppressExtractorImpl (org.powermock.tests.utils.impl.StaticConstructorSuppressExtractorImpl)1