use of org.powermock.classloading.SingleClassloaderExecutor 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.classloading.SingleClassloaderExecutor 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.classloading.SingleClassloaderExecutor 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);
}
use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.
the class XStreamClassloaderExecutorTest method loadsObjectGraphInSpecifiedClassloaderAndReturnsResultInOriginalClassloader.
@Test
public void loadsObjectGraphInSpecifiedClassloaderAndReturnsResultInOriginalClassloader() throws Exception {
MockClassLoader classloader = createClassloader();
final MyReturnValue expectedConstructorValue = new MyReturnValue(new MyArgument("first value"));
final MyClass myClass = new MyClass(expectedConstructorValue);
final MyArgument expected = new MyArgument("A value");
MyReturnValue[] actual = new SingleClassloaderExecutor(classloader).execute(new Callable<MyReturnValue[]>() {
public MyReturnValue[] call() throws Exception {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
return myClass.myMethod(expected);
}
});
assertFalse(MockClassLoader.class.getName().equals(this.getClass().getClassLoader().getClass().getName()));
final MyReturnValue myReturnValue = actual[0];
assertEquals(expectedConstructorValue.getMyArgument().getValue(), myReturnValue.getMyArgument().getValue());
assertEquals(expected.getValue(), actual[1].getMyArgument().getValue());
}
use of org.powermock.classloading.SingleClassloaderExecutor in project powermock by powermock.
the class XStreamClassloaderExecutorTest method usesReferenceCloningWhenTwoFieldsPointToSameInstance.
@Test
public void usesReferenceCloningWhenTwoFieldsPointToSameInstance() throws Exception {
final MockClassLoader classloader = createClassloader();
final MyReferenceFieldHolder tested = new MyReferenceFieldHolder();
assertSame(tested.getMyArgument1(), tested.getMyArgument2());
assertSame(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
new SingleClassloaderExecutor(classloader).execute(new Runnable() {
public void run() {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
assertEquals(tested.getMyArgument1(), tested.getMyArgument2());
assertEquals(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
assertSame(tested.getMyArgument1(), tested.getMyArgument2());
// FIXME: This assertion should work:
// assertSame(tested.getMyArgument1(), MyReferenceFieldHolder.MY_ARGUMENT);
}
});
}
Aggregations