/*
* Copyright 2001-2009 OFFIS, Tammo Freese
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.easymock;
import static org.easymock.EasyMock.*;
import java.io.Serializable;
import org.easymock.internal.*;
/**
* A MockControl object controls the behavior of its associated
* mock object. For more information, see the EasyMock documentation.
*
* @param type of the mock controlled
*
* @deprecated Since EasyMock 2.0, static methods on EasyMock are
* used to create and control mock objects.
*/
@Deprecated
public class MockControl implements Serializable {
private static final long serialVersionUID = 8741244302173698092L;
private final T mock;
private final MocksControl ctrl;
protected MockControl(MocksControl ctrl, Class toMock) {
this.ctrl = ctrl;
this.mock = ctrl.createMock(toMock);
}
/**
* Creates a mock control object for the specified interface. The
* MockControl and its associated mock object will not check
* the order of expected method calls. An unexpected method call on the mock
* object will lead to an AssertionError.
*
* @param type of the mock controlled
* @param toMock
* the class of the interface to mock.
* @return the mock control.
*/
public static MockControl createControl(Class toMock) {
return new MockControl((MocksControl) EasyMock.createControl(),
toMock);
}
/**
* Creates a mock control object for the specified interface. The
* MockControl and its associated mock object will check the
* order of expected method calls. An unexpected method call on the mock
* object will lead to an AssertionError.
*
* @param type of the mock controlled
* @param toMock
* the class of the interface to mock.
* @return the mock control.
*/
public static MockControl createStrictControl(Class toMock) {
return new MockControl(
(MocksControl) EasyMock.createStrictControl(), toMock);
}
/**
* Creates a mock control object for the specified interface. The
* MockControl and its associated mock object will not check
* the order of expected method calls. An unexpected method call on the mock
* object will return an empty value (0, null, false).
*
* @param type of the mock controlled
* @param toMock
* the class of the interface to mock.
* @return the mock control.
*/
public static MockControl createNiceControl(Class toMock) {
return new MockControl((MocksControl) EasyMock.createNiceControl(),
toMock);
}
/**
* Returns the mock object.
*
* @return the mock object of this control
*/
public T getMock() {
return mock;
}
/**
* Resets the mock control and the mock object to the state directly after
* creation.
*/
public final void reset() {
ctrl.reset();
}
/**
* Switches the mock object from record state to replay state. For more
* information, see the EasyMock documentation.
*
* @throws IllegalStateException
* if the mock object already is in replay state.
*/
public void replay() {
ctrl.replay();
}
/**
* Verifies that all expectations have been met. For more information, see
* the EasyMock documentation.
*
* @throws IllegalStateException
* if the mock object is in record state.
* @throws AssertionError
* if any expectation has not been met.
*/
public void verify() {
ctrl.verify();
}
/**
* Records that the mock object will expect the last method call once, and
* will react by returning silently.
*
* @exception IllegalStateException
* if the mock object is in replay state, if no method was
* called on the mock object before, or if the last method
* called on the mock was no void method.
*/
public void setVoidCallable() {
expectLastCall(
"method call on the mock needed before setting void callable")
.once();
}
/**
* Records that the mock object will expect the last method call once, and
* will react by throwing the provided Throwable.
*
* @param throwable
* the Throwable to throw.
* @exception IllegalStateException
* if the mock object is in replay state or if no method was
* called on the mock object before.
* @exception IllegalArgumentException
* if the last method called on the mock cannot throw the
* provided Throwable.
* @exception NullPointerException
* if throwable is null.
*/
public void setThrowable(Throwable throwable) {
expectLastCall(
"method call on the mock needed before setting Throwable")
.andThrow(throwable).once();
}
/**
* Records that the mock object will expect the last method call once, and
* will react by returning the provided return value.
*
* @param value
* the return value.
* @throws IllegalStateException
* if the mock object is in replay state, if no method was
* called on the mock object before. or if the last method
* called on the mock does not return boolean.
*/
public void setReturnValue(Object value) {
expectLastCall(
"method call on the mock needed before setting return value")
.andReturn(value).once();
}
/**
* Records that the mock object will expect the last method call a fixed
* number of times, and will react by returning silently.
*
* @param times
* the number of times that the call is expected.
* @exception IllegalStateException
* if the mock object is in replay state, if no method was
* called on the mock object before, or if the last method
* called on the mock was no void method.
*/
public void setVoidCallable(int times) {
expectLastCall(
"method call on the mock needed before setting void callable")
.times(times);
}
/**
* Records that the mock object will expect the last method call a fixed
* number of times, and will react by throwing the provided Throwable.
*
* @param throwable
* the Throwable to throw.
* @param times
* the number of times that the call is expected.
* @exception IllegalStateException
* if the mock object is in replay state or if no method was
* called on the mock object before.
* @exception IllegalArgumentException
* if the last method called on the mock cannot throw the
* provided Throwable.
* @exception NullPointerException
* if throwable is null.
*/
public void setThrowable(Throwable throwable, int times) {
expectLastCall(
"method call on the mock needed before setting Throwable")
.andThrow(throwable).times(times);
}
/**
* Records that the mock object will expect the last method call a fixed
* number of times, and will react by returning the provided return value.
*
* @param value
* the return value.
* @param times
* the number of times that the call is expected.
* @throws IllegalStateException
* if the mock object is in replay state, if no method was
* called on the mock object before. or if the last method
* called on the mock does not return boolean.
*/
public void setReturnValue(Object value, int times) {
expectLastCall(
"method call on the mock needed before setting return value")
.andReturn(value).times(times);
}
/**
* Records that the mock object will expect the last method call a fixed
* number of times, and will react by returning the provided return value.
*
* @param value
* the return value.
* @param range
* the number of times that the call is expected.
* @throws IllegalStateException
* if the mock object is in replay state, if no method was
* called on the mock object before. or if the last method
* called on the mock does not return boolean.
*/
public void setReturnValue(Object value, Range range) {
IExpectationSetters