use of org.powermock.core.classloader.MockClassLoader 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.classloader.MockClassLoader 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.classloader.MockClassLoader in project powermock by powermock.
the class ClassMockTransformerTest method loadWithMockClassLoader.
private Class<?> loadWithMockClassLoader(String className) throws ClassNotFoundException {
MockClassLoader loader = new MockClassLoader(new String[] { MockClassLoader.MODIFY_ALL_CLASSES });
loader.setMockTransformerChain(Collections.<MockTransformer>singletonList(new ClassMockTransformer()));
return Class.forName(className, true, loader);
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class XStreamClassloaderExecutorTest method loadsObjectGraphThatIncludesEnumsInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.
@Test
public void loadsObjectGraphThatIncludesEnumsInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
MockClassLoader classloader = createClassloader();
final MyEnum expected = MyEnum.MyEnum1;
final MyEnumHolder myClass = new MyEnumHolder(expected);
MyEnum actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyEnum>() {
public MyEnum call() throws Exception {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
MyEnum myEnum = myClass.getMyEnum();
assertEquals(expected, myEnum);
return myEnum;
}
});
assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
assertEquals(expected, actual);
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class XStreamClassloaderExecutorTest method loadsObjectGraphThatIncludesPrimitiveValuesInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.
@Test
public void loadsObjectGraphThatIncludesPrimitiveValuesInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
MockClassLoader classloader = createClassloader();
final Integer expected = 42;
final MyIntegerHolder myClass = new MyIntegerHolder(expected);
Integer actual = new SingleClassloaderExecutor(classloader).execute(new Callable<Integer>() {
public Integer call() throws Exception {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
final int myInteger = myClass.getMyInteger();
assertEquals((int) expected, myInteger);
return myInteger;
}
});
assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
assertEquals(expected, actual);
}
Aggregations