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  
    19  /**
    20   * {@code MethodResponse} holds and carries out the response if requested.
    21   *
    22   * @author Simon Wacker
    23   */
    24  class org.as2lib.test.mock.MethodResponse extends BasicClass {
    25  	
    26  	/** Return value to return. */
    27  	private var returnValue;
    28  	
    29  	/** Throwable to throw. */
    30  	private var throwable;
    31  	
    32  	/**
    33  	 * Constructs a new {@code MethodResponse} instance.
    34  	 */
    35  	public function MethodResponse(Void) {
    36  	}
    37  	
    38  	/**
    39  	 * Sets the return value that is returned when invoking the {@link #response}
    40  	 * method.
    41  	 *
    42  	 * <p>Setting a return value disables the throwable behavior.
    43  	 *
    44  	 * @param returnValue the return value to return
    45  	 */
    46  	public function setReturnValue(returnValue):Void {
    47  		throwable = undefined;
    48  		this.returnValue = returnValue;
    49  	}
    50  	
    51  	/**
    52  	 * Sets the throwable to throw when the {@link #response} method is invoked.
    53  	 *
    54  	 * <p>Setting a throwable disables the return value behavior.
    55  	 *
    56  	 * @param throwable the throwable to throw
    57  	 */
    58  	public function setThrowable(throwable):Void {
    59  		returnValue = undefined;
    60  		this.throwable = throwable;
    61  	}
    62  	
    63  	/**
    64  	 * Returns the set return value or throws the set throwable. If both have been set
    65  	 * the one that has been set at last will be used.
    66  	 *
    67  	 * @return the set return value
    68  	 * @throws the set throwable
    69  	 */
    70  	public function response(Void) {
    71  		if (returnValue !== undefined) return returnValue;
    72  		if (throwable !== undefined) throw throwable;
    73  	}
    74  	
    75  }