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.env.reflect.MethodInfo;
    19  import org.as2lib.test.speed.TestResultLayout;
    20  import org.as2lib.test.speed.TestSuiteResult;
    21  import org.as2lib.test.speed.MethodInvocation;
    22  import org.as2lib.test.speed.ConfigurableTestSuiteResult;
    23  import org.as2lib.test.speed.SimpleTestSuiteResult;
    24  
    25  /**
    26   * {@code MethodLayout} lays test results out with methods as root elements of the
    27   * structure.
    28   * 
    29   * @author Simon Wacker
    30   */
    31  class org.as2lib.test.speed.layout.MethodLayout extends BasicClass implements TestResultLayout {
    32  	
    33  	/** The result of the lay-outing of the current test suite result. */
    34  	private var result:ConfigurableTestSuiteResult;
    35  	
    36  	/** All method invocations of the test suite result to lay-out. */
    37  	private var methodInvocations:Array;
    38  	
    39  	/**
    40  	 * Constructs a new {@code MethodLayout} instance.
    41  	 */
    42  	public function MethodLayout(Void) {
    43  	}
    44  	
    45  	/**
    46  	 * Lays the passed-in {@code testSuiteResult} out with methods as root elements of
    47  	 * the structure and returns a new lay-outed test suite result.
    48  	 * 
    49  	 * @param testSuiteResult the test suite result to lay-out
    50  	 * @return the lay-outed test suite result
    51  	 */
    52  	public function layOut(testSuiteResult:TestSuiteResult):TestSuiteResult {
    53  		this.result = new SimpleTestSuiteResult(testSuiteResult.getName());
    54  		this.methodInvocations = testSuiteResult.getAllMethodInvocations();
    55  		for (var i:Number = 0; i < this.methodInvocations.length; i++) {
    56  			var methodInvocation:MethodInvocation = this.methodInvocations[i];
    57  			i -= addMethodInvocations(methodInvocation.getMethod());
    58  		}
    59  		this.result.sort(SimpleTestSuiteResult.TIME, true);
    60  		return this.result;
    61  	}
    62  	
    63  	/**
    64  	 * Adds all method invocations of the passed-in {@code method} to the result and
    65  	 * removes the added invocations from the {@code methodInvocations} array.
    66  	 * 
    67  	 * @param method the method to add the invocations for
    68  	 * @return the number of method invocations added
    69  	 */
    70  	private function addMethodInvocations(method:MethodInfo):Number {
    71  		var count:Number = 0;
    72  		var methodResult:ConfigurableTestSuiteResult = new SimpleTestSuiteResult(method.getFullName());
    73  		for (var i:Number = 0; i < this.methodInvocations.length; i++) {
    74  			var methodInvocation:MethodInvocation = this.methodInvocations[i];
    75  			if (methodInvocation.getMethod() == method) {
    76  				methodResult.addTestResult(methodInvocation);
    77  				this.methodInvocations.splice(i, 1);
    78  				i--;
    79  				count++;
    80  			}
    81  		}
    82  		this.result.addTestResult(methodResult);
    83  		return count;
    84  	}
    85  	
    86  }