com.fitbur.mockito.internal.handler.InvocationNotifierHandler Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package com.fitbur.mockito.internal.handler;
import static com.fitbur.mockito.exceptions.Reporter.invocationListenerThrewException;
import java.util.List;
import com.fitbur.mockito.exceptions.Reporter;
import com.fitbur.mockito.internal.InternalMockHandler;
import com.fitbur.mockito.internal.listeners.NotifiedMethodInvocationReport;
import com.fitbur.mockito.internal.stubbing.InvocationContainer;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.invocation.MockHandler;
import com.fitbur.mockito.listeners.InvocationListener;
import com.fitbur.mockito.mock.MockCreationSettings;
import com.fitbur.mockito.stubbing.Answer;
/**
* Handler, that call all listeners wanted for this mock, before delegating it
* to the parameterized handler.
*
* Also imposterize MockHandlerImpl, delegate all call of InternalMockHandler to the real mockHandler
*/
class InvocationNotifierHandler implements MockHandler, InternalMockHandler {
private final List invocationListeners;
private final InternalMockHandler mockHandler;
public InvocationNotifierHandler(InternalMockHandler mockHandler, MockCreationSettings> settings) {
this.mockHandler = mockHandler;
this.invocationListeners = settings.getInvocationListeners();
}
public Object handle(Invocation invocation) throws Throwable {
try {
Object returnedValue = mockHandler.handle(invocation);
notifyMethodCall(invocation, returnedValue);
return returnedValue;
} catch (Throwable t){
notifyMethodCallException(invocation, t);
throw t;
}
}
private void notifyMethodCall(Invocation invocation, Object returnValue) {
for (InvocationListener listener : invocationListeners) {
try {
listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, returnValue));
} catch(Throwable listenerThrowable) {
throw invocationListenerThrewException(listener, listenerThrowable);
}
}
}
private void notifyMethodCallException(Invocation invocation, Throwable exception) {
for (InvocationListener listener : invocationListeners) {
try {
listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, exception));
} catch(Throwable listenerThrowable) {
throw invocationListenerThrewException(listener, listenerThrowable);
}
}
}
public MockCreationSettings getMockSettings() {
return mockHandler.getMockSettings();
}
public void setAnswersForStubbing(List> answers) {
mockHandler.setAnswersForStubbing(answers);
}
public InvocationContainer getInvocationContainer() {
return mockHandler.getInvocationContainer();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy