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.PackageInfo;
    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.ClassLayout;
    25  
    26  /**
    27   * {@code PackageLayout} lays test suite results out with packages as root elements of
    28   * the structure.
    29   * 
    30   * @author Simon Wacker
    31   */
    32  class org.as2lib.test.speed.layout.PackageLayout 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 PackageLayout} instance.
    42  	 */
    43  	public function PackageLayout(Void) {
    44  	}
    45  	
    46  	/**
    47  	 * Lays the passed-in {@code testSuiteResult} out with packages as root elements of
    48  	 * the structure and returns the 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  	 * @todo support multiple package layers, not just the one of the declaring type
    53  	 */
    54  	public function layOut(testSuiteResult:TestSuiteResult):TestSuiteResult {
    55  		this.result = new SimpleTestSuiteResult(testSuiteResult.getName());
    56  		this.methodInvocations = testSuiteResult.getAllMethodInvocations();
    57  		for (var i:Number = 0; i < this.methodInvocations.length; i++) {
    58  			var methodInvocation:MethodInvocation = this.methodInvocations[i];
    59  			i -= addMethodInvocations(methodInvocation.getMethod().getDeclaringType().getPackage());
    60  		}
    61  		this.result.sort(SimpleTestSuiteResult.NAME);
    62  		return this.result;
    63  	}
    64  	
    65  	/**
    66  	 * Adds all method invocations of methods of the passed-in {@code package} to the
    67  	 * result and removes these invocations from the {@code methodInvocations} array.
    68  	 * 
    69  	 * @param package the package to add method invocations for
    70  	 * @return the number of removed method invocations
    71  	 */
    72  	private function addMethodInvocations(package:PackageInfo):Number {
    73  		var count:Number = 0;
    74  		var classResult:ConfigurableTestSuiteResult = new SimpleTestSuiteResult(package.getFullName());
    75  		for (var i:Number = 0; i < this.methodInvocations.length; i++) {
    76  			var methodInvocation:MethodInvocation = this.methodInvocations[i];
    77  			if (methodInvocation.getMethod().getDeclaringType().getPackage() == package) {
    78  				classResult.addTestResult(methodInvocation);
    79  				this.methodInvocations.splice(i, 1);
    80  				i--;
    81  				count++;
    82  			}
    83  		}
    84  		this.result.addTestResult((new ClassLayout()).layOut(classResult));
    85  		return count;
    86  	}
    87  	
    88  }