Clover coverage report - EasyMock 2.2
Coverage timestamp: Mo Apr 17 2006 21:21:22 CEST
file stats: LOC: 597   Methods: 41
NCLOC: 187   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MockControl.java 100% 100% 100% 100%
coverage
 1    /*
 2    * Copyright (c) 2001-2006 OFFIS, Tammo Freese.
 3    * This program is made available under the terms of the MIT License.
 4    */
 5    package org.easymock;
 6   
 7    import static org.easymock.EasyMock.expect;
 8   
 9    import org.easymock.internal.*;
 10   
 11    /**
 12    * A <code>MockControl</code> object controls the behavior of its associated
 13    * mock object. For more information, see the EasyMock documentation.
 14    *
 15    * @deprecated Since EasyMock 2.0, static methods on <code>EasyMock</code> are
 16    * used to create and control mock objects.
 17    */
 18    public class MockControl<T> {
 19    private final T mock;
 20   
 21    private final MocksControl ctrl;
 22   
 23  307 protected MockControl(MocksControl ctrl, Class<T> toMock) {
 24  307 this.ctrl = ctrl;
 25  307 this.mock = ctrl.createMock(toMock);
 26    }
 27   
 28    /**
 29    * Creates a mock control object for the specified interface. The
 30    * <code>MockControl</code> and its associated mock object will not check
 31    * the order of expected method calls. An unexpected method call on the mock
 32    * object will lead to an <code>AssertionError</code>.
 33    *
 34    * @param toMock
 35    * the class of the interface to mock.
 36    * @return the mock control.
 37    */
 38  265 public static <T> MockControl<T> createControl(Class<T> toMock) {
 39  265 return new MockControl<T>((MocksControl) EasyMock.createControl(),
 40    toMock);
 41    }
 42   
 43    /**
 44    * Creates a mock control object for the specified interface. The
 45    * <code>MockControl</code> and its associated mock object will check the
 46    * order of expected method calls. An unexpected method call on the mock
 47    * object will lead to an <code>AssertionError</code>.
 48    *
 49    * @param toMock
 50    * the class of the interface to mock.
 51    * @return the mock control.
 52    */
 53  33 public static <T> MockControl<T> createStrictControl(Class<T> toMock) {
 54  33 return new MockControl<T>(
 55    (MocksControl) EasyMock.createStrictControl(), toMock);
 56    }
 57   
 58    /**
 59    * Creates a mock control object for the specified interface. The
 60    * <code>MockControl</code> and its associated mock object will not check
 61    * the order of expected method calls. An unexpected method call on the mock
 62    * object will return an empty value (0, null, false).
 63    *
 64    * @param toMock
 65    * the class of the interface to mock.
 66    * @return the mock control.
 67    */
 68  9 public static <T> MockControl<T> createNiceControl(Class<T> toMock) {
 69  9 return new MockControl<T>((MocksControl) EasyMock.createNiceControl(),
 70    toMock);
 71    }
 72   
 73    /**
 74    * Returns the mock object.
 75    *
 76    * @return the mock object of this control
 77    */
 78  222 public T getMock() {
 79  222 return mock;
 80    }
 81   
 82    /**
 83    * Resets the mock control and the mock object to the state directly after
 84    * creation.
 85    */
 86  6 public final void reset() {
 87  6 ctrl.reset();
 88    }
 89   
 90    /**
 91    * Switches the mock object from record state to replay state. For more
 92    * information, see the EasyMock documentation.
 93    *
 94    * @throws IllegalStateException
 95    * if the mock object already is in replay state.
 96    */
 97  234 public void replay() {
 98  234 ctrl.replay();
 99    }
 100   
 101    /**
 102    * Verifies that all expectations have been met. For more information, see
 103    * the EasyMock documentation.
 104    *
 105    * @throws IllegalStateException
 106    * if the mock object is in record state.
 107    * @throws AssertionError
 108    * if any expectation has not been met.
 109    */
 110  124 public void verify() {
 111  124 ctrl.verify();
 112    }
 113   
 114    /**
 115    * Records that the mock object will expect the last method call once, and
 116    * will react by returning silently.
 117    *
 118    * @exception IllegalStateException
 119    * if the mock object is in replay state, if no method was
 120    * called on the mock object before, or if the last method
 121    * called on the mock was no void method.
 122    */
 123  7 public void setVoidCallable() {
 124  7 expectLastCall(
 125    "method call on the mock needed before setting void callable")
 126    .once();
 127    }
 128   
 129    /**
 130    * Records that the mock object will expect the last method call once, and
 131    * will react by throwing the provided Throwable.
 132    *
 133    * @param throwable
 134    * the Throwable to throw.
 135    * @exception IllegalStateException
 136    * if the mock object is in replay state or if no method was
 137    * called on the mock object before.
 138    * @exception IllegalArgumentException
 139    * if the last method called on the mock cannot throw the
 140    * provided Throwable.
 141    * @exception NullPointerException
 142    * if throwable is null.
 143    */
 144  18 public void setThrowable(Throwable throwable) {
 145  18 expectLastCall(
 146    "method call on the mock needed before setting Throwable")
 147    .andThrow(throwable).once();
 148    }
 149   
 150    /**
 151    * Records that the mock object will expect the last method call once, and
 152    * will react by returning the provided return value.
 153    *
 154    * @param value
 155    * the return value.
 156    * @throws IllegalStateException
 157    * if the mock object is in replay state, if no method was
 158    * called on the mock object before. or if the last method
 159    * called on the mock does not return <code>boolean</code>.
 160    */
 161  92 public void setReturnValue(Object value) {
 162  92 expectLastCall(
 163    "method call on the mock needed before setting return value")
 164    .andReturn(value).once();
 165    }
 166   
 167    /**
 168    * Records that the mock object will expect the last method call a fixed
 169    * number of times, and will react by returning silently.
 170    *
 171    * @param times
 172    * the number of times that the call is expected.
 173    * @exception IllegalStateException
 174    * if the mock object is in replay state, if no method was
 175    * called on the mock object before, or if the last method
 176    * called on the mock was no void method.
 177    */
 178  5 public void setVoidCallable(int times) {
 179  5 expectLastCall(
 180    "method call on the mock needed before setting void callable")
 181    .times(times);
 182    }
 183   
 184    /**
 185    * Records that the mock object will expect the last method call a fixed
 186    * number of times, and will react by throwing the provided Throwable.
 187    *
 188    * @param throwable
 189    * the Throwable to throw.
 190    * @param times
 191    * the number of times that the call is expected.
 192    * @exception IllegalStateException
 193    * if the mock object is in replay state or if no method was
 194    * called on the mock object before.
 195    * @exception IllegalArgumentException
 196    * if the last method called on the mock cannot throw the
 197    * provided Throwable.
 198    * @exception NullPointerException
 199    * if throwable is null.
 200    */
 201  4 public void setThrowable(Throwable throwable, int times) {
 202  4 expectLastCall(
 203    "method call on the mock needed before setting Throwable")
 204    .andThrow(throwable).times(times);
 205    }
 206   
 207    /**
 208    * Records that the mock object will expect the last method call a fixed
 209    * number of times, and will react by returning the provided return value.
 210    *
 211    * @param value
 212    * the return value.
 213    * @param times
 214    * the number of times that the call is expected.
 215    * @throws IllegalStateException
 216    * if the mock object is in replay state, if no method was
 217    * called on the mock object before. or if the last method
 218    * called on the mock does not return <code>boolean</code>.
 219    */
 220  20 public void setReturnValue(Object value, int times) {
 221  20 expectLastCall(
 222    "method call on the mock needed before setting return value")
 223    .andReturn(value).times(times);
 224    }
 225   
 226    /**
 227    * Records that the mock object will expect the last method call a fixed
 228    * number of times, and will react by returning the provided return value.
 229    *
 230    * @param value
 231    * the return value.
 232    * @param range
 233    * the number of times that the call is expected.
 234    * @throws IllegalStateException
 235    * if the mock object is in replay state, if no method was
 236    * called on the mock object before. or if the last method
 237    * called on the mock does not return <code>boolean</code>.
 238    */
 239  15 public void setReturnValue(Object value, Range range) {
 240  15 IExpectationSetters setter = expectLastCall(
 241    "method call on the mock needed before setting return value")
 242    .andReturn(value);
 243  10 callWithConvertedRange(setter, range);
 244    }
 245   
 246    /**
 247    * Records that the mock object will by default allow the last method
 248    * specified by a method call.
 249    *
 250    * @exception IllegalStateException
 251    * if the mock object is in replay state, if no method was
 252    * called on the mock object before, or if the last method
 253    * called on the mock was no void method.
 254    */
 255  3 public void setDefaultVoidCallable() {
 256  3 ((MocksControl) expectLastCall("method call on the mock needed before setting default void callable"))
 257    .setLegacyDefaultVoidCallable();
 258    }
 259   
 260    /**
 261    * Records that the mock object will by default allow the last method
 262    * specified by a method call, and will react by throwing the provided
 263    * Throwable.
 264    *
 265    * @param throwable
 266    * throwable the throwable to be thrown
 267    * @exception IllegalArgumentException
 268    * if the last method called on the mock cannot throw the
 269    * provided Throwable.
 270    * @exception NullPointerException
 271    * if throwable is null.
 272    * @exception IllegalStateException
 273    * if the mock object is in replay state, or if no method was
 274    * called on the mock object before.
 275    */
 276  9 public void setDefaultThrowable(Throwable throwable) {
 277  9 ctrl.setLegacyDefaultThrowable(throwable);
 278    }
 279   
 280    /**
 281    * Records that the mock object will by default allow the last method
 282    * specified by a method call, and will react by returning the provided
 283    * return value.
 284    *
 285    * @param value
 286    * the return value.
 287    * @throws IllegalStateException
 288    * if the mock object is in replay state, if no method was
 289    * called on the mock object before. or if the last method
 290    * called on the mock does not return <code>boolean</code>.
 291    */
 292  36 public void setDefaultReturnValue(Object value) {
 293  36 ctrl.setLegacyDefaultReturnValue(value);
 294    }
 295   
 296    /**
 297    * Sets the ArgumentsMatcher for the last method called on the mock object.
 298    * The matcher must be set before any behavior for the method is defined.
 299    *
 300    * @throws IllegalStateException
 301    * if called in replay state, or if no method was called on the
 302    * mock object before.
 303    */
 304  15 public void setMatcher(ArgumentsMatcher matcher) {
 305  15 ctrl.setLegacyMatcher(matcher);
 306    }
 307   
 308    /**
 309    * Records that the mock object will expect the last method call between
 310    * <code>minCount</code> and <code>maxCount</code> times, and will react
 311    * by returning silently.
 312    *
 313    * @param minCount
 314    * the minimum number of times that the call is expected.
 315    * @param maxCount
 316    * the maximum number of times that the call is expected.
 317    * @exception IllegalStateException
 318    * if the mock object is in replay state, if no method was
 319    * called on the mock object before, or if the last method
 320    * called on the mock was no void method.
 321    */
 322  4 public void setVoidCallable(int minCount, int maxCount) {
 323  4 expectLastCall(
 324    "method call on the mock needed before setting void callable")
 325    .times(minCount, maxCount);
 326    }
 327   
 328  7 public void setVoidCallable(Range range) {
 329  7 IExpectationSetters setter = expectLastCall("method call on the mock needed before setting void callable");
 330  6 callWithConvertedRange(setter, range);
 331    }
 332   
 333    /**
 334    * Records that the mock object will expect the last method call between
 335    * <code>minCount</code> and <code>maxCount</code> times, and will react
 336    * by throwing the provided Throwable.
 337    *
 338    * @param throwable
 339    * the Throwable to throw.
 340    * @param minCount
 341    * the minimum number of times that the call is expected.
 342    * @param maxCount
 343    * the maximum number of times that the call is expected.
 344    * @exception IllegalStateException
 345    * if the mock object is in replay state or if no method was
 346    * called on the mock object before.
 347    * @exception IllegalArgumentException
 348    * if the last method called on the mock cannot throw the
 349    * provided Throwable.
 350    * @exception NullPointerException
 351    * if throwable is null.
 352    */
 353  1 public void setThrowable(Throwable throwable, int minCount, int maxCount) {
 354  1 expectLastCall(
 355    "method call on the mock needed before setting Throwable")
 356    .andThrow(throwable).times(minCount, maxCount);
 357    }
 358   
 359  4 public void setThrowable(Throwable throwable, Range range) {
 360  4 IExpectationSetters setter = expectLastCall(
 361    "method call on the mock needed before setting Throwable")
 362    .andThrow(throwable);
 363  3 callWithConvertedRange(setter, range);
 364    }
 365   
 366    /**
 367    * Records that the mock object will expect the last method call between
 368    * <code>minCount</code> and <code>maxCount</code> times, and will react
 369    * by returning the provided return value.
 370    *
 371    * @param value
 372    * the return value.
 373    * @param minCount
 374    * the minimum number of times that the call is expected.
 375    * @param maxCount
 376    * the maximum number of times that the call is expected.
 377    * @throws IllegalStateException
 378    * if the mock object is in replay state, if no method was
 379    * called on the mock object before. or if the last method
 380    * called on the mock does not return <code>boolean</code>.
 381    */
 382  7 public void setReturnValue(Object value, int minCount, int maxCount) {
 383  7 expectLastCall(
 384    "method call on the mock needed before setting return value")
 385    .andReturn(value).times(minCount, maxCount);
 386    }
 387   
 388    /**
 389    * Exactly one call.
 390    */
 391    public static final Range ONE = MocksControl.ONCE;
 392   
 393    /**
 394    * One or more calls.
 395    */
 396    public static final Range ONE_OR_MORE = MocksControl.AT_LEAST_ONCE;
 397   
 398    /**
 399    * Zero or more calls.
 400    */
 401    public static final Range ZERO_OR_MORE = MocksControl.ZERO_OR_MORE;
 402   
 403    /**
 404    * Matches if each expected argument is equal to the corresponding actual
 405    * argument.
 406    */
 407    public static final ArgumentsMatcher EQUALS_MATCHER = new EqualsMatcher();
 408   
 409    /**
 410    * Matches always.
 411    */
 412    public static final ArgumentsMatcher ALWAYS_MATCHER = new AlwaysMatcher();
 413   
 414    /**
 415    * Matches if each expected argument is equal to the corresponding actual
 416    * argument for non-array arguments; array arguments are compared with the
 417    * appropriate <code>java.util.Arrays.equals()</code> -method.
 418    */
 419    public static final ArgumentsMatcher ARRAY_MATCHER = new ArrayMatcher();
 420   
 421    /**
 422    * Sets the default ArgumentsMatcher for all methods of the mock object. The
 423    * matcher must be set before any behavior is defined on the mock object.
 424    *
 425    * @throws IllegalStateException
 426    * if called in replay state, or if any behavior is already
 427    * defined on the mock object.
 428    */
 429  8 public void setDefaultMatcher(ArgumentsMatcher matcher) {
 430  8 ctrl.setLegacyDefaultMatcher(matcher);
 431    }
 432   
 433    /**
 434    * Same as {@link MockControl#setReturnValue(Object)}. For explanation, see
 435    * "Convenience Methods for Return Values" in the EasyMock documentation.
 436    *
 437    * @param ignored
 438    * an ignored value.
 439    */
 440  10 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value) {
 441  10 EasyMock.expectLastCall().andReturn(value).once();
 442    }
 443   
 444  1 public void expectAndReturn(int ignored, int value) {
 445  1 this.expectAndReturn((Object) ignored, (Object) value);
 446    }
 447   
 448    /**
 449    * Same as {@link MockControl#setReturnValue(Object, Range)}. For
 450    * explanation, see "Convenience Methods for Return Values" in the EasyMock
 451    * documentation.
 452    *
 453    * @param ignored
 454    * an ignored value.
 455    */
 456  10 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value,
 457    Range range) {
 458  10 IExpectationSetters expectAndReturn = EasyMock.expectLastCall()
 459    .andReturn(value);
 460  5 callWithConvertedRange(expectAndReturn, range);
 461    }
 462   
 463  1 public void expectAndReturn(int ignored, int value, Range range) {
 464  1 this.expectAndReturn((Object) ignored, (Object) value, range);
 465    }
 466   
 467    /**
 468    * Same as {@link MockControl#setReturnValue(Object, int)}. For
 469    * explanation, see "Convenience Methods for Return Values" in the EasyMock
 470    * documentation.
 471    *
 472    * @param ignored
 473    * an ignored value.
 474    */
 475  10 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value,
 476    int count) {
 477  10 EasyMock.expectLastCall().andReturn(value).times(count);
 478    }
 479   
 480  1 public void expectAndReturn(int ignored, int value, int count) {
 481  1 this.expectAndReturn((Object) ignored, (Object) value, count);
 482    }
 483   
 484    /**
 485    * Same as {@link MockControl#setReturnValue(Object, int, int)}. For
 486    * explanation, see "Convenience Methods for Return Values" in the EasyMock
 487    * documentation.
 488    *
 489    * @param ignored
 490    * an ignored value.
 491    */
 492  10 public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value,
 493    int min, int max) {
 494  10 EasyMock.expectLastCall().andReturn(value).times(min, max);
 495    }
 496   
 497  1 public void expectAndReturn(int ignored, int value, int min, int max) {
 498  1 this.expectAndReturn((Object) ignored, (Object) value, min, max);
 499    }
 500   
 501    /**
 502    * Same as {@link MockControl#setThrowable(Throwable)}. For explanation,
 503    * see "Convenience Methods for Throwables" in the EasyMock documentation.
 504    *
 505    * @param ignored
 506    * an ignored value.
 507    */
 508  10 public void expectAndThrow(Object ignored, Throwable throwable) {
 509  10 EasyMock.expect(ignored).andThrow(throwable).once();
 510    }
 511   
 512    /**
 513    * Same as {@link MockControl#setThrowable(Throwable, Range)}. For
 514    * explanation, see "Convenience Methods for Throwables" in the EasyMock
 515    * documentation.
 516    *
 517    * @param ignored
 518    * an ignored value.
 519    */
 520  10 public void expectAndThrow(Object ignored, Throwable throwable, Range range) {
 521  10 IExpectationSetters setter = EasyMock.expect(ignored).andThrow(
 522    throwable);
 523  5 callWithConvertedRange(setter, range);
 524    }
 525   
 526    /**
 527    * Same as {@link MockControl#setThrowable(Throwable, int)}. For
 528    * explanation, see "Convenience Methods for Throwables" in the EasyMock
 529    * documentation.
 530    *
 531    * @param ignored
 532    * an ignored value.
 533    */
 534  10 public void expectAndThrow(Object ignored, Throwable throwable, int count) {
 535  10 expect(ignored).andThrow(throwable).times(count);
 536    }
 537   
 538    /**
 539    * Same as {@link MockControl#setThrowable(Throwable, int, int)}. For
 540    * explanation, see "Convenience Methods for Throwables" in the EasyMock
 541    * documentation.
 542    *
 543    * @param ignored
 544    * an ignored value.
 545    */
 546  10 public void expectAndThrow(Object ignored, Throwable throwable, int min,
 547    int max) {
 548  10 expect(ignored).andThrow(throwable).times(min, max);
 549    }
 550   
 551    /**
 552    * Same as {@link MockControl#setDefaultReturnValue(Object)}. For
 553    * explanation, see "Convenience Methods for Return Values" in the EasyMock
 554    * documentation.
 555    *
 556    * @param ignored
 557    * an ignored value.
 558    */
 559  5 public <V1, V2 extends V1> void expectAndDefaultReturn(V1 ignored, V2 value) {
 560  5 EasyMock.expectLastCall().andStubReturn(value);
 561    }
 562   
 563    /**
 564    * Same as {@link MockControl#setDefaultThrowable(Throwable)}. For
 565    * explanation, see "Convenience Methods for Throwables" in the EasyMock
 566    * documentation.
 567    *
 568    * @param ignored
 569    * an ignored value.
 570    */
 571  5 public void expectAndDefaultThrow(Object ignored, Throwable throwable) {
 572  5 expectLastCall(
 573    "method call on the mock needed before setting default Throwable")
 574    .andStubThrow(throwable);
 575    }
 576   
 577  192 private IExpectationSetters<Object> expectLastCall(String failureMessage) {
 578  192 try {
 579  192 return EasyMock.expectLastCall();
 580    } catch (IllegalStateException e) {
 581  47 throw new IllegalStateException(failureMessage);
 582    }
 583    }
 584   
 585  29 private void callWithConvertedRange(IExpectationSetters setter, Range range) {
 586  29 if (range == ONE) {
 587  10 setter.once();
 588  19 } else if (range == ONE_OR_MORE) {
 589  15 setter.atLeastOnce();
 590  4 } else if (range == ZERO_OR_MORE) {
 591  3 setter.anyTimes();
 592    } else {
 593  1 throw new IllegalArgumentException("Unexpected Range");
 594    }
 595    }
 596   
 597    }