org.mockito.internal.util.MockUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-all Show documentation
Show all versions of mockito-all Show documentation
Mock objects library for java
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import org.mockito.cglib.proxy.Callback;
import org.mockito.cglib.proxy.Factory;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.InvocationNotifierHandler;
import org.mockito.internal.MockHandler;
import org.mockito.internal.MockHandlerInterface;
import org.mockito.internal.creation.MethodInterceptorFilter;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.creation.jmock.ClassImposterizer;
import org.mockito.internal.util.reflection.LenientCopyTool;
import java.io.Serializable;
@SuppressWarnings("unchecked")
public class MockUtil {
private final MockCreationValidator creationValidator;
public MockUtil(MockCreationValidator creationValidator) {
this.creationValidator = creationValidator;
}
public MockUtil() {
this(new MockCreationValidator());
}
public T createMock(Class classToMock, MockSettingsImpl settings) {
creationValidator.validateType(classToMock);
creationValidator.validateExtraInterfaces(classToMock, settings.getExtraInterfaces());
creationValidator.validateMockedType(classToMock, settings.getSpiedInstance());
settings.initiateMockName(classToMock);
MethodInterceptorFilter filter = newMethodInterceptorFilter(settings);
Class>[] interfaces = settings.getExtraInterfaces();
Class>[] ancillaryTypes;
if (settings.isSerializable()) {
ancillaryTypes = interfaces == null ? new Class>[] {Serializable.class} : new ArrayUtils().concat(interfaces, Serializable.class);
} else {
ancillaryTypes = interfaces == null ? new Class>[0] : interfaces;
}
Object spiedInstance = settings.getSpiedInstance();
T mock = ClassImposterizer.INSTANCE.imposterise(filter, classToMock, ancillaryTypes);
if (spiedInstance != null) {
new LenientCopyTool().copyToMock(spiedInstance, mock);
}
return mock;
}
public void resetMock(T mock) {
MockHandlerInterface oldMockHandler = getMockHandler(mock);
MethodInterceptorFilter newFilter = newMethodInterceptorFilter(oldMockHandler.getMockSettings());
((Factory) mock).setCallback(0, newFilter);
}
private MethodInterceptorFilter newMethodInterceptorFilter(MockSettingsImpl settings) {
MockHandler mockHandler = new MockHandler(settings);
InvocationNotifierHandler invocationNotifierHandler = new InvocationNotifierHandler(mockHandler, settings);
return new MethodInterceptorFilter(invocationNotifierHandler, settings);
}
public MockHandlerInterface getMockHandler(T mock) {
if (mock == null) {
throw new NotAMockException("Argument should be a mock, but is null!");
}
if (isMockitoMock(mock)) {
return (MockHandlerInterface) getInterceptor(mock).getHandler();
} else {
throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
}
}
private boolean isMockitoMock(T mock) {
return getInterceptor(mock) != null;
}
public boolean isMock(Object mock) {
return mock != null && isMockitoMock(mock);
}
private MethodInterceptorFilter getInterceptor(T mock) {
if (!(mock instanceof Factory)) {
return null;
}
Factory factory = (Factory) mock;
Callback callback = factory.getCallback(0);
if (callback instanceof MethodInterceptorFilter) {
return (MethodInterceptorFilter) callback;
}
return null;
}
public MockName getMockName(Object mock) {
return getMockHandler(mock).getMockSettings().getMockName();
}
}