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.util.StringUtil;
    18  import org.as2lib.test.speed.TestSuiteResult;
    19  import org.as2lib.test.speed.ConfigurableTestSuiteResult;
    20  import org.as2lib.test.speed.MethodInvocationHolder;
    21  import org.as2lib.test.speed.AbstractTestSuiteResult;
    22  import org.as2lib.test.speed.TestResult;
    23  import org.as2lib.test.speed.MethodInvocation;
    24  
    25  /**
    26   * {@code MethodInvocationTestSuiteResult} holds multiple sub-test results and is based
    27   * on a method invocation.
    28   * 
    29   * @author Simon Wacker
    30   */
    31  class org.as2lib.test.speed.MethodInvocationTestSuiteResult extends AbstractTestSuiteResult implements ConfigurableTestSuiteResult, MethodInvocationHolder {
    32  	
    33  	/** Makes the static variables of the super-class accessible through this class. */
    34  	private static var __proto__:Function = AbstractTestSuiteResult;
    35  	
    36  	/** The wrapped method invocation. */
    37  	private var methodInvocation:MethodInvocation;
    38  	
    39  	/**
    40  	 * Constructs a new {@code MethodInvocationTestSuiteResult} instance.
    41  	 * 
    42  	 * @param methodInvocation the method invocation to wrap
    43  	 */
    44  	public function MethodInvocationTestSuiteResult(methodInvocation:MethodInvocation) {
    45  		this.methodInvocation = methodInvocation;
    46  	}
    47  	
    48  	/**
    49  	 * Returns the held method invocation.
    50  	 * 
    51  	 * @return the held method invocation
    52  	 */
    53  	public function getMethodInvocation(Void):MethodInvocation {
    54  		return this.methodInvocation;
    55  	}
    56  	
    57  	/**
    58  	 * Returns the name of this test result. This is the name of the wrapped method
    59  	 * invocation.
    60  	 * 
    61  	 * @return the name of this test result
    62  	 */
    63  	public function getName(Void):String {
    64  		return this.methodInvocation.getName();
    65  	}
    66  	
    67  	/**
    68  	 * Returns the total invocation time in milliseconds.
    69  	 * 
    70  	 * @return the total invocation time in milliseconds
    71  	 */
    72  	public function getTime(Void):Number {
    73  		return this.methodInvocation.getTime();
    74  	}
    75  	
    76  	/**
    77  	 * Returns the string representation of this test result. This includes the string
    78  	 * representation of all sub-tests.
    79  	 * 
    80  	 * @param rootTestResult test result that holds the total values needed for
    81  	 * percentage calculations
    82  	 * @return the string representation of this test result
    83  	 */
    84  	public function toString():String {
    85  		var rootTestResult:TestSuiteResult = TestSuiteResult(arguments[0]);
    86  		if (!rootTestResult) rootTestResult = getThis();
    87  		var result:String = getTimePercentage(rootTestResult.getTime()) + "%";
    88  		result += ", " + getThis().getTime() + " ms";
    89  		result += " - " + getMethodInvocationPercentage(rootTestResult.getMethodInvocationCount()) + "%";
    90  		result += ", " + getMethodInvocationCount() + " inv.";
    91  		result += " - " + getAverageTime() + " ms/inv.";
    92  		result += " - " + getName();
    93  		if (hasTestResults()) {
    94  			var totalTime:Number = getThis().getTime();
    95  			for (var i:Number = 0; i < this.testResults.length; i++) {
    96  				var testResult:TestResult = this.testResults[i];
    97  				if (TestSuiteResult(testResult).hasMethodInvocations()
    98  						|| !(testResult instanceof TestSuiteResult)
    99  						|| testResult instanceof MethodInvocationHolder) {
   100  					result += "\n";
   101  					result += StringUtil.addSpaceIndent(testResult.toString(rootTestResult), 2);
   102  				}
   103  			}
   104  		}
   105  		return result;
   106  	}
   107  	
   108  }