1  /**
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  import org.as2lib.env.except.IllegalStateException;
    19  import org.as2lib.env.except.IllegalArgumentException;
    20  import org.as2lib.test.mock.MockControlState;
    21  import org.as2lib.test.mock.Behavior;
    22  import org.as2lib.test.mock.MethodCallRange;
    23  import org.as2lib.test.mock.MethodResponse;
    24  import org.as2lib.test.mock.MethodCall;
    25  import org.as2lib.test.mock.ArgumentsMatcher;
    26  
    27  /**
    28   * {@code RecordState} records behaviors.
    29   *
    30   * @author Simon Wacker
    31   */
    32  class org.as2lib.test.mock.support.RecordState extends BasicClass implements MockControlState {
    33  	
    34  	/** Used to add and get behaviors of the mock. */
    35  	private var behavior:Behavior;
    36  	
    37  	/**
    38  	 * Constructs a new {@code RecordState} instance.
    39  	 *
    40  	 * @param behavior the behavior to add and get behaviors of the mock
    41  	 * @throws IllegalArgumentException if the passed-in {@code behavior} is
    42  	 * {@code null}
    43  	 */
    44  	public function RecordState(behavior:Behavior) {
    45  		if (!behavior) throw new IllegalArgumentException("Behavior is not allowed to be null or undefined.", this, arguments);
    46  		this.behavior = behavior;
    47  	}
    48  	
    49  	/**
    50  	 * Returns the behavior set during instantiation.
    51  	 * 
    52  	 * @return the behavior
    53  	 */
    54  	public function getBehavior(Void):Behavior {
    55  		return behavior;
    56  	}
    57  	
    58  	/**
    59  	 * Adds the expected {@code methodCall} to the expected behavior of the mock.
    60  	 *
    61  	 * @param methodCall contains all information about the method call
    62  	 */
    63  	public function invokeMethod(methodCall:MethodCall) {
    64  		behavior.addMethodBehavior(methodCall.getMethodName(), behavior.createMethodBehavior(methodCall));
    65  	}
    66  	
    67  	/**
    68  	 * Sets the expectation that the lastly called method is called the passed-in
    69  	 * number of times. When called between that range it responses the given way.
    70  	 *
    71  	 * @param methodResponse the response of the method during the expected call
    72  	 * range
    73  	 * @param methodCallRange the expected range of method calls
    74  	 */ 
    75  	public function setMethodResponse(methodResponse:MethodResponse, methodCallRange:MethodCallRange):Void {
    76  		behavior.getLastMethodBehavior().addMethodResponse(methodResponse, methodCallRange);
    77  	}
    78  	
    79  	/**
    80  	 * Sets the arguments matcher for the lastly called method.
    81  	 *
    82  	 * <p>The arguments matcher is used by the expected method call to check whether
    83  	 * it matches an actual method call.
    84  	 *
    85  	 * @param argumentsMatcher the new arguments matcher for the expected method call
    86  	 */
    87  	public function setArgumentsMatcher(argumentsMatcher:ArgumentsMatcher):Void {
    88  		behavior.getLastMethodBehavior().setArgumentsMatcher(argumentsMatcher);
    89  	}
    90  	
    91  	/**
    92  	 * @throws IllegalStateException
    93  	 */
    94  	public function verify(Void):Void {
    95  		throw new IllegalStateException("Method must not be called in record state.", this, arguments);
    96  	}
    97  	
    98  }