Search in sources :

Example 1 with ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer 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 ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.

the class MockClassLoaderTest method cannotFindDynamicClassInDeferredClassLoader.

@Test(expected = ClassNotFoundException.class)
public void cannotFindDynamicClassInDeferredClassLoader() throws Exception {
    MockClassLoader mockClassLoader = new MockClassLoader(new String[0]);
    List<MockTransformer> list = new LinkedList<MockTransformer>();
    list.add(new ClassMockTransformer());
    mockClassLoader.setMockTransformerChain(list);
    // setup custom classloader providing our dynamic class, for MockClassLoader to defer to
    mockClassLoader.deferTo = new ClassLoader(getClass().getClassLoader()) {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            return super.loadClass(name);
        }
    };
    //Try to locate and load a class that is not in MockClassLoader.
    Class.forName(DynamicClassHolder.clazz.getName(), false, mockClassLoader);
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) CtClass(javassist.CtClass) LinkedList(java.util.LinkedList) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) Test(org.junit.Test)

Example 3 with ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer in project powermock by powermock.

the class MockClassLoaderTest method autoboxingWorks.

@Test
public void autoboxingWorks() throws Exception {
    String name = this.getClass().getPackage().getName() + ".HardToTransform";
    final MockClassLoader mockClassLoader = new MockClassLoader(new String[] { name });
    List<MockTransformer> list = new LinkedList<MockTransformer>();
    list.add(new ClassMockTransformer());
    mockClassLoader.setMockTransformerChain(list);
    Class<?> c = mockClassLoader.loadClass(name);
    Object object = c.newInstance();
    Whitebox.invokeMethod(object, "run");
    assertThat(5).isEqualTo(Whitebox.invokeMethod(object, "testInt"));
    assertThat(5L).isEqualTo(Whitebox.invokeMethod(object, "testLong"));
    assertThat(5f).isEqualTo(Whitebox.invokeMethod(object, "testFloat"));
    assertThat(5.0).isEqualTo(Whitebox.invokeMethod(object, "testDouble"));
    assertThat(new Short("5")).isEqualTo(Whitebox.invokeMethod(object, "testShort"));
    assertThat(new Byte("5")).isEqualTo(Whitebox.invokeMethod(object, "testByte"));
    assertThat(true).isEqualTo(Whitebox.invokeMethod(object, "testBoolean"));
    assertThat('5').isEqualTo(Whitebox.invokeMethod(object, "testChar"));
    assertThat("5").isEqualTo(Whitebox.invokeMethod(object, "testString"));
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) LinkedList(java.util.LinkedList) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) Test(org.junit.Test)

Example 4 with ClassMockTransformer

use of org.powermock.core.transformers.impl.ClassMockTransformer 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 5 with ClassMockTransformer

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

Aggregations

MockTransformer (org.powermock.core.transformers.MockTransformer)9 ClassMockTransformer (org.powermock.core.transformers.impl.ClassMockTransformer)9 LinkedList (java.util.LinkedList)6 Test (org.junit.Test)6 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 InterfaceMockTransformer (org.powermock.core.transformers.impl.InterfaceMockTransformer)3 CtClass (javassist.CtClass)2 SingleClassloaderExecutor (org.powermock.classloading.SingleClassloaderExecutor)1 MockClassLoader (org.powermock.core.classloader.MockClassLoader)1 UseClassPathAdjuster (org.powermock.core.classloader.annotations.UseClassPathAdjuster)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