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.test.unit.Test;
    19  import org.as2lib.test.unit.TestCaseHelper;
    20  import org.as2lib.test.unit.TestSuite;
    21  import org.as2lib.util.ClassUtil;
    22  import org.as2lib.util.AccessPermission;
    23  import org.as2lib.env.reflect.PackageInfo;
    24  import org.as2lib.env.reflect.TypeInfo;
    25  
    26  /**
    27   * Factory to create TestSuites.
    28   * This factory can be used to create TestSuites that contain all
    29   * TestCases that are available.
    30   * 
    31   * @author Martin Heidegger
    32   */
    33  class org.as2lib.test.unit.TestSuiteFactory extends BasicClass {
    34  	
    35  	/**
    36  	 * Constructs a new Factory.
    37  	 */
    38  	public function TestSuiteFactory() {}
    39  	
    40  	/**
    41  	 * Collects all TestCases that are available.
    42  	 * 
    43  	 * @return TestSuite that contains all available TestCases.
    44  	 */
    45  	public function collectAllTestCases(Void):TestSuite {
    46  		return collectTestCases(PackageInfo.getRootPackage(), true);
    47  	}
    48  	
    49  	/**
    50  	 * Collects all TestCases by a given package.
    51  	 * 
    52  	 * @param package Package to start with.
    53  	 * @param recursive Recursive Flag.
    54  	 * @return TestSuite that contains all available TestCases.
    55  	 */
    56  	public function collectTestCases(package:PackageInfo, recursive:Boolean):TestSuite {
    57  		var result:TestSuite = new TestSuite("<Generated TestSuite>");
    58  		AccessPermission.set(package, null, AccessPermission.ALLOW_ALL);
    59  		collectAgent(package, result, recursive);
    60  		return result;
    61  	}
    62  
    63  	/**
    64  	 * Agent to collect TestCases within a package.
    65  	 * <p>Note: If you want that a class gets blocked from collection simple add
    66  	 * a static method "blockCollecting" that returns true.
    67  	 * 
    68  	 * <p>Example:
    69  	 * <code>
    70  	 *   import org.as2lib.test.unit.TestCase;
    71  	 * 
    72  	 *   class MyTest extends TestCase {
    73  	 *     public static function blockCollecting(Void):Boolean {
    74  	 *	     return true;
    75  	 *     }
    76  	 *   }  
    77  	 * </code>
    78  	 * 
    79  	 * 
    80  	 * @param package Package to search in.
    81  	 * @param suite TestSuite to add the found TestCase.
    82  	 * @param recursive Recursive Flag.
    83  	 */
    84  	private function collectAgent(package:PackageInfo, suite:TestSuite, recursive:Boolean):Void {
    85  
    86  		var members:Array = package.getMemberClasses();
    87  		for(var i:Number = 0; i < members.length; i++) {
    88  			var childType:TypeInfo = members[i];
    89  			var child:Function = childType.getType();
    90  			if (
    91  				   ClassUtil.isImplementationOf(child, Test)
    92  				&& child != Test
    93  				&& !child.blockCollecting()
    94  				&& !ClassUtil.isSubClassOf(child, TestCaseHelper)
    95  				&& !ClassUtil.isSubClassOf(child, TestSuite)
    96  				) {
    97  				suite.addTest(Test(ClassUtil.createCleanInstance(child)));
    98  			}
    99  		}
   100  		
   101  		var subPackages:Array = package.getMemberPackages();
   102  		for(var j:Number = 0; j < subPackages.length && recursive; j++) {
   103  			var subPackage:PackageInfo = subPackages[j];
   104  			collectAgent(subPackage, suite, true);
   105  		}
   106  	}
   107  }