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.data.holder.array.TypedArray;
    19  import org.as2lib.data.holder.Iterator;
    20  import org.as2lib.data.holder.array.ArrayIterator;
    21  import org.as2lib.env.reflect.MethodInfo;
    22  import org.as2lib.util.StopWatch;
    23  import org.as2lib.util.StringUtil;
    24  import org.as2lib.test.unit.ExecutionInfo;
    25  
    26  /**
    27   * Informationholder for all Informations related to the execution of a Method within a Testcase.
    28   * 
    29   * @author Martin Heidegger
    30   */
    31  class org.as2lib.test.unit.TestCaseMethodInfo extends BasicClass {
    32  	
    33  	/** Internal Holder for the Reflections Information about the method. */
    34  	private var methodInfo:MethodInfo;
    35  	
    36  	/** Holder for the Stopwatch that contains the execution time. */
    37  	private var stopWatch:StopWatch;
    38  	
    39  	/** All collected Informations for the execution. */
    40  	private var infos:TypedArray;
    41  	
    42  	/** Information if the method was executed by @see #executeTo */
    43  	private var executed:Boolean = false;
    44  	
    45  	/**	
    46  	 * Constructs a new Informations for a TestCase method.
    47  	 * 
    48  	 * @param methodInfo Reflection based information of the method.
    49  	 */
    50  	public function TestCaseMethodInfo(methodInfo:MethodInfo) {
    51  		this.methodInfo = methodInfo;
    52  		
    53  		stopWatch = new StopWatch();
    54  		infos = new TypedArray(ExecutionInfo);
    55  	}
    56  	
    57  	/**
    58  	 * Getter for the Stopwatch that is held by this class.
    59  	 * 
    60  	 * @return StopWatch related to this class.
    61  	 */
    62  	public function getStopWatch(Void):StopWatch {
    63  		return stopWatch;
    64  	}
    65  	
    66  	/**
    67  	 * Information if the method contains errors.
    68  	 * 
    69  	 * @return true if the method contains errors.
    70  	 */
    71  	public function hasErrors(Void):Boolean {
    72  		var i:Number = infos.length;
    73  		while (--i-(-1)) {
    74  			if(infos[i].isFailed()) {
    75  				return true;
    76  			}
    77  		}
    78  		return false;
    79  	}
    80  	
    81  	/**
    82  	 * Getter for the available operation time.
    83  	 * 
    84  	 * @return Operation time in milliseconds.
    85  	 */
    86  	public function getOperationTime(Void):Number {
    87  		return getStopWatch().getTimeInMilliSeconds();
    88  	}
    89  	
    90  	/**
    91  	 * Getter for the Reflection based method information.
    92  	 * 
    93  	 * @return Original MethodInfo to the executed method.
    94  	 */
    95  	public function getMethodInfo(Void):MethodInfo {
    96  		return this.methodInfo;
    97  	}
    98  	
    99  	/**
   100  	 * Adds a Information about the execution of the method.
   101  	 * 
   102  	 * @param info Information that should be added.
   103  	 */
   104  	public function addInfo(info:ExecutionInfo):Void {
   105  		infos.push(info);
   106  	}
   107  	
   108  	/**
   109  	 * Getter for all infos occured during the execution.
   110  	 * Returns a copy of all infos!
   111  	 * 
   112  	 * @return List of all infos occured during execution.
   113  	 */
   114  	public function getInfos(Void):TypedArray {
   115  		return infos.concat();
   116  	}
   117  	
   118  	/**
   119  	 * Getter for all errors occured during the execution.
   120  	 * 
   121  	 * @return List of all errors occured during execution of the method.
   122  	 */
   123  	public function getErrors(Void):TypedArray {
   124  		var result:TypedArray = new TypedArray(ExecutionInfo);
   125  		var i:Number = 0;
   126  		var l:Number = infos.length;
   127  		while (i<l) {
   128  			if(infos[i].isFailed()) {
   129  				result.push(infos[i]);
   130  			}
   131  			i-=(-1);
   132  		}
   133  		return result;
   134  	}
   135  	
   136  	/**
   137  	 * @return True if the method was executed by @see #executeTo
   138  	 */
   139  	public function isExecuted(Void):Boolean {
   140  		return executed;
   141  	}
   142  
   143  	/**
   144  	 * Sets the current state of execution.
   145  	 * 
   146  	 * @param executed True if the method has been executed, else false.
   147  	 */
   148  	public function setExecuted(executed:Boolean):Void {
   149  		this.executed = executed;
   150  	}
   151  	
   152  	/**
   153  	 * Extended .toString implementation.
   154  	 * 
   155  	 * @return TestCaseMethodInfo as well formated String.
   156  	 */
   157  	public function toString():String {
   158  		var result:String = getMethodInfo().getName()+"() ["+getStopWatch().getTimeInMilliSeconds()+"ms]";
   159  		var errors:Array = getErrors();
   160  		if(errors.length > 1) {
   161  			result += " "+errors.length+" errors occured";
   162  		} else if(errors.length > 0) {
   163  			result += " 1 error occured";
   164  			
   165  		}
   166  		var errorIterator:Iterator = new ArrayIterator(errors);
   167  		while(errorIterator.hasNext()) {
   168  			var error = errorIterator.next();
   169  			result += "\n"+StringUtil.addSpaceIndent(error.getMessage(), 2);
   170  		}
   171  		if(hasErrors()) {
   172  			result += "\n";
   173  		}
   174  		return result;
   175  	}
   176  	
   177  	/**
   178  	 * Getter for the complete name of the methodinfo
   179  	 * 
   180  	 * @return Complete name as string of the methodinfo.
   181  	 */
   182  	public function getName(Void):String {
   183  		return methodInfo.getFullName();
   184  	}
   185  }