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.test.mock.AssertionFailedError;
    18  import org.as2lib.test.mock.MethodCall;
    19  import org.as2lib.test.mock.MethodCallRange;
    20  import org.as2lib.env.reflect.ReflectUtil;
    21  
    22  /**
    23   * {@code MethodCallRangeError} is thrown if the expected method call range has not
    24   * been met.
    25   * 
    26   * @author Simon Wacker
    27   */
    28  class org.as2lib.test.mock.MethodCallRangeError extends AssertionFailedError {
    29  	
    30  	/** The method calls. */
    31  	private var methodCalls:Array;
    32  	
    33  	/** The expected call range. */
    34  	private var expectedMethodCallRanges:Array;
    35  	
    36  	/** The actual call range. */
    37  	private var actualMethodCallRanges:Array;
    38  	
    39  	/** Type of the mock. */
    40  	private var type:Function;
    41  	
    42  	/**
    43  	 * Constructs a new {@code MethodCallRangeError} instance.
    44  	 * 
    45  	 * <p>All arguments are allowed to be {@code null} or {@code undefined}. But if
    46  	 * one is, the string representation returned by the {@code toString} method will
    47  	 * not be complete.
    48  	 *
    49  	 * <p>The {@code args} array should be the internal arguments array of the method
    50  	 * that throws the throwable. The internal arguments array exists in every method
    51  	 * and contains its parameters, the callee method and the caller method. You can
    52  	 * refernce it in every method using the name {@code "arguments"}.
    53  	 * 
    54  	 * @param message the message that describes the problem in detail
    55  	 * @param thrower the object that declares the method that throws this fatal
    56  	 * exception
    57  	 * @param args the arguments of the throwing method
    58  	 */
    59  	public function MethodCallRangeError(message:String, thrower, args:Array) {
    60  		super (message, thrower, args);
    61  		methodCalls = new Array();
    62  		expectedMethodCallRanges = new Array();
    63  		actualMethodCallRanges = new Array();
    64  	}
    65  	
    66  	/**
    67  	 * Adds a new method call together with its expected and actual call range.
    68  	 *
    69  	 * @param methodCall the new method call
    70  	 * @param expectedMethodCallRange the expected method call range
    71  	 * @param actualMethodCallRange the actual method call range
    72  	 */
    73  	public function addMethodCall(methodCall:MethodCall, expectedMethodCallRange:MethodCallRange, actualMethodCallRange:MethodCallRange):Void {
    74  		methodCalls.push(methodCall);
    75  		expectedMethodCallRanges.push(expectedMethodCallRange);
    76  		actualMethodCallRanges.push(actualMethodCallRange);
    77  	}
    78  	
    79  	/**
    80  	 * Sets the type of the mock that did not met all expectations.
    81  	 * 
    82  	 * @param type the type of the mock
    83  	 */
    84  	public function setType(type:Function):Void  {
    85  		this.type = type;
    86  	}
    87  	
    88  	/**
    89  	 * Returns the string representation of this error.
    90  	 *
    91  	 * @return the string representation of this error
    92  	 */
    93  	public function doToString(Void):String {
    94  		var message:String = getMessage() + ":";
    95  		for (var i:Number = 0; i < methodCalls.length; i++) {
    96  			message += "\n  ";
    97  			if (type !== undefined && type !== null) {
    98  				message += ReflectUtil.getTypeNameForType(type) + ".";
    99  			}
   100  			message += methodCalls[i] + ": expected: " + expectedMethodCallRanges[i] + ", actual: " + actualMethodCallRanges[i];
   101  		}
   102  		return message;
   103  	}
   104  	
   105  }