use of org.powermock.core.classloader.MockClassLoader 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.core.classloader.MockClassLoader 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);
}
});
}
use of org.powermock.core.classloader.MockClassLoader 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;
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class XStreamClassloaderExecutorTest method worksWithObjectHierarchy.
@Test
public void worksWithObjectHierarchy() throws Exception {
final MockClassLoader classloader = createClassloader();
final MyHierarchicalFieldHolder tested = new MyHierarchicalFieldHolder();
assertSame(tested.getMyArgument1(), tested.getMyArgument2());
assertEquals(tested.getMyArgument3(), tested.getMyArgument2());
new SingleClassloaderExecutor(classloader).execute(new Runnable() {
public void run() {
assertEquals(MockClassLoader.class.getName(), this.getClass().getClassLoader().getClass().getName());
assertSame(tested.getMyArgument1(), tested.getMyArgument2());
assertEquals(tested.getMyArgument3(), tested.getMyArgument2());
}
});
}
use of org.powermock.core.classloader.MockClassLoader in project powermock by powermock.
the class PowerMock method doMock.
@SuppressWarnings("unchecked")
private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy, ConstructorArgs constructorArgs, Method... methods) {
if (type == null) {
throw new IllegalArgumentException("The class to mock cannot be null");
}
/*
* Clear the EasyMock state after the test method is executed.
*/
MockRepository.addAfterMethodRunner(new Runnable() {
@Override
public void run() {
LastControl.reportLastControl(null);
}
});
IMocksControl control = mockStrategy.createMockControl(type);
MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());
T mock;
if (type.isInterface()) {
mock = control.createMock(type);
} else if (type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers())) {
Class<?> replicaType = createReplicaType(type, isStatic, constructorArgs);
final Object replica = doCreateMock(replicaType, constructorArgs, control, methods);
control = mockStrategy.createMockControl(replicaType);
MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
final Set<Method> methodsToMock = toSet(methods);
if (isStatic) {
MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
return null;
} else {
final T newInstance;
if (constructorArgs == null) {
newInstance = Whitebox.newInstance(type);
DefaultFieldValueGenerator.fillWithDefaultValues(newInstance);
} else {
try {
newInstance = (T) constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs());
} catch (Exception e) {
throw new RuntimeException("Internal error", e);
}
}
MockRepository.putInstanceMethodInvocationControl(newInstance, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
if (!(newInstance instanceof InvocationSubstitute<?>)) {
MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance);
}
return newInstance;
}
} else {
mock = doCreateMock(type, constructorArgs, control, methods);
}
MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
final Set<Method> methodsToMock = toSet(methods);
if (isStatic) {
MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h, methodsToMock, mock));
MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
} else {
MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h, methodsToMock));
if (!(mock instanceof InvocationSubstitute<?>)) {
MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);
}
}
ClassLoader classLoader = mock.getClass().getClassLoader();
if (classLoader instanceof MockClassLoader) {
((MockClassLoader) classLoader).cache(mock.getClass());
}
return mock;
}
Aggregations