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.MethodBehavior;
    22  import org.as2lib.test.mock.Behavior;
    23  import org.as2lib.test.mock.MethodCallRange;
    24  import org.as2lib.test.mock.MethodCall;
    25  import org.as2lib.test.mock.MethodResponse;
    26  import org.as2lib.test.mock.ArgumentsMatcher;
    27  
    28  /**
    29   * {@code ReplayState} replays behavior.
    30   *
    31   * @author Simon Wacker
    32   */
    33  class org.as2lib.test.mock.support.ReplayState extends BasicClass implements MockControlState {
    34  	
    35  	/** Used to verify the expectations and to store actual method calls. */
    36  	private var behavior:Behavior;
    37  	
    38  	/**
    39  	 * Constructs a new {@code ReplayState} instance.
    40  	 *
    41  	 * @param behavior used to verify the expectations and to store actual method
    42  	 * calls
    43  	 * @throws IllegalArgumentException if the passed-in {@code behavior} is
    44  	 * {@code null}
    45  	 */
    46  	public function ReplayState(behavior:Behavior) {
    47  		if (!behavior) throw new IllegalArgumentException("Behavior is not allowed to be null or undefined.", this, arguments);
    48  		this.behavior = behavior;
    49  	}
    50  	
    51  	/**
    52  	 * Returns the behavior set during instantiation.
    53  	 * 
    54  	 * @return the behavior
    55  	 */
    56  	public function getBehavior(Void):Behavior {
    57  		return behavior;
    58  	}
    59  	
    60  	/**
    61  	 * Registers the actual {@code methodCall} in the method behavior if it was
    62  	 * expected or registers a new unexpected method call.
    63  	 *
    64  	 * @return the value returned by the method behaviour's response method
    65  	 * @throws * the exception thrown by the method behaviour's response method
    66  	 */
    67  	public function invokeMethod(methodCall:MethodCall) {
    68  		var methodBehavior:MethodBehavior = behavior.getMethodBehavior(methodCall);
    69  		if (methodBehavior) {
    70  			methodBehavior.addActualMethodCall(methodCall);
    71  			return methodBehavior.response();
    72  		} else {
    73  			methodBehavior = behavior.createMethodBehavior(null);
    74  			if (methodCall.getMethodName() && methodCall.getMethodName() != "") {
    75  				behavior.addMethodBehavior(methodCall.getMethodName(), methodBehavior);
    76  			} else {
    77  				behavior.addMethodBehavior("[unknown]", methodBehavior);
    78  			}
    79  			methodBehavior.addActualMethodCall(methodCall);
    80  			return methodBehavior.response();
    81  		}
    82  	}
    83  	
    84  	/**
    85  	 * Forwards the verification to the behavior of the mock.
    86  	 */
    87  	public function verify(Void):Void {
    88  		behavior.verify();
    89  	}
    90  	
    91  	/**
    92  	 * @throws IllegalStateException
    93  	 */ 
    94  	public function setMethodResponse(methodResponse:MethodResponse, methodCallRange:MethodCallRange):Void {
    95  		throw new IllegalStateException("Method must not be called in replay state.", this, arguments);
    96  	}
    97  	
    98  	/**
    99  	 * @throws IllegalStateException
   100  	 */
   101  	public function setArgumentsMatcher(argumentsMatcher:ArgumentsMatcher):Void {
   102  		throw new IllegalStateException("Method must not be called in replay state.", this, arguments);
   103  	}
   104  	
   105  }