Search in sources :

Example 1 with MockTransformer

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

the class MockClassLoaderFactory method createMockClassLoader.

protected ClassLoader createMockClassLoader(final String[] classesToLoadByMockClassloader) {
    List<MockTransformer> mockTransformerChain = getMockTransformers(extraMockTransformers);
    final UseClassPathAdjuster useClassPathAdjuster = testClass.getAnnotation(UseClassPathAdjuster.class);
    ClassLoader mockLoader = AccessController.doPrivileged(new PrivilegedAction<MockClassLoader>() {

        @Override
        public MockClassLoader run() {
            return new MockClassLoader(classesToLoadByMockClassloader, packagesToIgnore, useClassPathAdjuster);
        }
    });
    MockClassLoader mockClassLoader = (MockClassLoader) mockLoader;
    mockClassLoader.setMockTransformerChain(mockTransformerChain);
    new MockPolicyInitializerImpl(testClass).initialize(mockLoader);
    return mockLoader;
}
Also used : MockTransformer(org.powermock.core.transformers.MockTransformer) ClassMockTransformer(org.powermock.core.transformers.impl.ClassMockTransformer) InterfaceMockTransformer(org.powermock.core.transformers.impl.InterfaceMockTransformer) UseClassPathAdjuster(org.powermock.core.classloader.annotations.UseClassPathAdjuster) MockClassLoader(org.powermock.core.classloader.MockClassLoader) MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Example 2 with MockTransformer

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

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

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

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

the class XStreamClassloaderExecutorTest method createClassloader.

private MockClassLoader createClassloader() {
    MockClassLoader classloader = new MockClassLoader(new String[] { MyClass.class.getName(), MyArgument.class.getName(), MyReturnValue.class.getName() });
    MockTransformer mainMockTransformer = new MockTransformer() {

        public CtClass transform(CtClass clazz) throws Exception {
            return clazz;
        }
    };
    LinkedList<MockTransformer> linkedList = new LinkedList<MockTransformer>();
    linkedList.add(mainMockTransformer);
    classloader.setMockTransformerChain(linkedList);
    return classloader;
}
Also used : MyClass(powermock.classloading.classes.MyClass) MyArgument(powermock.classloading.classes.MyArgument) MockTransformer(org.powermock.core.transformers.MockTransformer) CtClass(javassist.CtClass) MockClassLoader(org.powermock.core.classloader.MockClassLoader) MyReturnValue(powermock.classloading.classes.MyReturnValue) LinkedList(java.util.LinkedList)

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