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.test.mock.ArgumentsMatcher;
    19  import org.as2lib.test.mock.support.DefaultArgumentsMatcher;
    20  
    21  /**
    22   * {@code MethodCall} stores all information available about a method call.
    23   * 
    24   * @author Simon Wacker
    25   */
    26  class org.as2lib.test.mock.MethodCall extends BasicClass {
    27  	
    28  	/** The name of the called method. */
    29  	private var methodName:String;
    30  	
    31  	/** The arguments passed to the method. */
    32  	private var args:Array;
    33  	
    34  	/** The matcher that compares expected and actual arguments. */
    35  	private var argumentsMatcher:ArgumentsMatcher;
    36  	
    37  	/**
    38  	 * Constructs a new {@code MethodCall} instance.
    39  	 *
    40  	 * <p>If {@code args} is {@code null} an empty array will be used instead.
    41  	 *
    42  	 * @param methodName the name of the called method
    43  	 * @param args the arguments used for the method call
    44  	 */
    45  	public function MethodCall(methodName:String, args:Array) {
    46  		this.methodName = methodName;
    47  		this.args = args ? args : new Array();
    48  	}
    49  	
    50  	/**
    51  	 * Returns the name of the called method.
    52  	 *
    53  	 * @return the name of the called method
    54  	 */
    55  	public function getMethodName(Void):String {
    56  		return methodName;
    57  	}
    58  	
    59  	/**
    60  	 * Returns the arguments used for the method call.
    61  	 *
    62  	 * @return the arguments used for the method call
    63  	 */
    64  	public function getArguments(Void):Array {
    65  		return args;
    66  	}
    67  	
    68  	/**
    69  	 * Returns the currently used arguments matcher.
    70  	 * 
    71  	 * <p>That is either the arguments matcher set via the
    72  	 * {@link #setArgumentsMatcher} method or an instance of the default
    73  	 * {@link DefaultArgumentsMatcher} class.
    74  	 *
    75  	 * @return the currently used arguments matcher
    76  	 */
    77  	public function getArgumentsMatcher(Void):ArgumentsMatcher {
    78  		if (!argumentsMatcher) argumentsMatcher = new DefaultArgumentsMatcher();
    79  		return argumentsMatcher;
    80  	}
    81  	
    82  	/**
    83  	 * Sets the new arguments matcher.
    84  	 * 
    85  	 * <p>If {@code argumentsMatcher} is {@code null} the {@link #getArgumentsMatcher}
    86  	 * method returns the default arguments matcher.
    87  	 *
    88  	 * @param argumentsMatcher the new arguments matcher
    89  	 */
    90  	public function setArgumentsMatcher(argumentsMatcher:ArgumentsMatcher):Void {
    91  		this.argumentsMatcher = argumentsMatcher;
    92  	}
    93  	
    94  	/**
    95  	 * Checks whether this method call matches the passed-in {@code methodCall}.
    96  	 *
    97  	 * <p>{@code false} will be returned if:
    98  	 * <ul>
    99  	 *   <li>The passed-in {@code methodCall} is {@code null}.</li>
   100  	 *   <li>The method names do not match.</li>
   101  	 *   <li>The arguments do not match.</li>
   102  	 * </ul>
   103  	 *
   104  	 * @param methodCall the method call to compare with this instance
   105  	 */
   106  	public function matches(methodCall:MethodCall):Boolean {
   107  		if (!methodCall) return false;
   108  		if (methodName != methodCall.getMethodName()) return false;
   109  		return getArgumentsMatcher().matchArguments(args, methodCall.getArguments() ? methodCall.getArguments() : new Array());
   110  	}
   111  	
   112  	/**
   113  	 * Returns the string representation of this method call.
   114  	 *
   115  	 * <p>The returned string is constructed as follows:
   116  	 * <pre>
   117  	 *   theMethodName(theFirstArgument, ..)
   118  	 * </pre>
   119  	 *
   120  	 * @return the string representation of this method call
   121  	 */
   122  	public function toString():String {
   123  		return (methodName + "(" + args + ")");
   124  	}
   125  	
   126  }