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