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;
}
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);
}
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);
}
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"));
}
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;
}
Aggregations