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.unit.AbstractAssertInfo;
    18  import org.as2lib.app.exec.Executable;
    19  import org.as2lib.util.StringUtil;
    20  import org.as2lib.env.reflect.ClassInfo;
    21  
    22  /**
    23   * Information holder and examiner of a assertNotThrows call.
    24   * 
    25   * @author Martin Heidegger.
    26   */
    27  class org.as2lib.test.unit.info.AssertNotThrowsInfo extends AbstractAssertInfo {
    28  	
    29  	/** Holder for the Exception type. */ 
    30  	private var type;
    31  	
    32  	/** Holder for the call to be executed. */
    33  	private var toCall:Executable;
    34  	
    35  	/** Holder for the arguments for the call. */
    36  	private var args:Array;
    37  	
    38  	/** Holder for a thrown exception. */
    39  	private var exception;
    40  	
    41  	/** Flag if a exception was thrown. */
    42  	private var exceptionThrown:Boolean = false;
    43  	
    44  	/**
    45  	 * Constructs a new AssertNotThrowsInfo.
    46  	 * 
    47  	 * @param message Message if the assertion fails.
    48  	 * @param type Exception type that should not be thrown. (if null is given, it fails on every exception).
    49  	 * @param toCall Call to be executed
    50  	 * @param args Arguments for the Call.
    51  	 */
    52  	public function AssertNotThrowsInfo(message:String, type, toCall:Executable, args:Array) {
    53  		super(message);
    54  		this.type = type;
    55  		this.toCall = toCall;
    56  		this.args = args;
    57  	}
    58  	
    59  	/**
    60  	 * Overriding of @see AbstractAssertInfo#execute
    61  	 * 
    62  	 * @return True if the execution fails.
    63  	 */
    64  	public function execute(Void):Boolean {
    65  		try {
    66  			toCall.execute.apply(Object(toCall), args);
    67  		} catch(e) {
    68  			exception = e;
    69  			exceptionThrown = true;
    70  			if(type != null) {
    71  				return (e instanceof type);
    72  			} else {
    73  				return true;
    74  			}
    75  		}
    76  		return false;
    77  	}
    78  	
    79  	/**
    80  	 * Implementation of @see AbstractAssertInfo#getFailureMessage
    81  	 * 
    82  	 * @return Message on failure
    83  	 */
    84  	private function getFailureMessage(Void):String {
    85  		var result:String = "assertNotThrows failed";
    86  		if(hasMessage()) {
    87  			result += " with message: "+message;
    88  		}
    89  		result += "!\n";
    90  		if(typeof type == "function") {
    91  			result += "  - Expected exception:\n      "+ClassInfo.forClass(type).getFullName();
    92  		} else if(type == null) {
    93  			result += "  - No exception expected.";
    94  		} else {
    95  			result += "  - Expected exception:\n      "+type;
    96  		}
    97  		if(exceptionThrown) {
    98  			result += "\n  - Thrown exception:\n"+StringUtil.addSpaceIndent(exception.toString(), 6);
    99  		} else {
   100  			result += "\n  - No exception thrown.";
   101  		}
   102  		return result;
   103  	}
   104  	
   105  	/**
   106  	 * Implementation of @see AbstractAssertInfo#getSuccessMessage
   107  	 * 
   108  	 * @return Message on success
   109  	 */
   110  	private function getSuccessMessage(Void):String {
   111  		var result:String = "assertNotThrows executed. ";
   112  		
   113  		if(typeof type == "function") {
   114  			result += ClassInfo.forClass(type).getFullName();
   115  		} else {
   116  			result += type;
   117  		}
   118  		
   119  		result += "was not thrown by calling "+toCall.toString()+".";
   120  		
   121  		return result;
   122  	}
   123  }