org.mockito.internal.util.MockUtil Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import static org.mockito.Mockito.*;
import org.mockito.cglib.proxy.Callback;
import org.mockito.cglib.proxy.Enhancer;
import org.mockito.cglib.proxy.Factory;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.MockHandler;
import org.mockito.internal.creation.MethodInterceptorFilter;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.creation.jmock.ClassImposterizer;
import org.mockito.internal.invocation.MatchersBinder;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.util.reflection.LenientCopyTool;
import java.io.Serializable;
@SuppressWarnings("unchecked")
public class MockUtil {
private final CreationValidator creationValidator;
public MockUtil(CreationValidator creationValidator) {
this.creationValidator = creationValidator;
}
public MockUtil() {
this(new CreationValidator());
}
public T createMock(Class classToMock, MockingProgress progress, MockSettingsImpl settings) {
creationValidator.validateType(classToMock);
creationValidator.validateExtraInterfaces(classToMock, settings.getExtraInterfaces());
creationValidator.validateMockedType(classToMock, settings.getSpiedInstance());
MockName mockName = new MockName(settings.getMockName(), classToMock);
MockHandler mockHandler = new MockHandler(mockName, progress, new MatchersBinder(), settings);
MethodInterceptorFilter filter = new MethodInterceptorFilter(mockHandler, 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) {
MockHandler oldMockHandler = getMockHandler(mock);
MockHandler newMockHandler = new MockHandler(oldMockHandler);
MethodInterceptorFilter newFilter = new MethodInterceptorFilter(newMockHandler,
(MockSettingsImpl) withSettings().defaultAnswer(RETURNS_DEFAULTS));
((Factory) mock).setCallback(0, newFilter);
}
public MockHandler getMockHandler(T mock) {
if (mock == null) {
throw new NotAMockException("Argument should be a mock, but is null!");
}
if (isMockitoMock(mock)) {
return (MockHandler) getInterceptor(mock).getMockHandler();
} else {
throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
}
}
private boolean isMockitoMock(T mock) {
return Enhancer.isEnhanced(mock.getClass()) && getInterceptor(mock) != null;
}
public boolean isMock(Object mock) {
return mock != null && isMockitoMock(mock);
}
private MethodInterceptorFilter getInterceptor(T mock) {
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).getMockName();
}
}