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.env.except.IllegalArgumentException;
    18  import org.as2lib.env.overload.Overload;
    19  import org.as2lib.env.reflect.PropertyInfo;
    20  import org.as2lib.env.reflect.ClassInfo;
    21  import org.as2lib.test.speed.AbstractTest;
    22  import org.as2lib.test.speed.Test;
    23  import org.as2lib.test.speed.SimpleTestSuiteResult;
    24  import org.as2lib.test.speed.MethodTestCase;
    25  
    26  /**
    27   * {@code PropertyTestCase} profiles a property.
    28   * 
    29   * @author Simon Wacker
    30   */
    31  class org.as2lib.test.speed.PropertyTestCase extends AbstractTest implements Test {
    32  	
    33  	/** The property to profile. */
    34  	private var property:PropertyInfo;
    35  	
    36  	/** Test case for the getter method. */
    37  	private var getter:MethodTestCase;
    38  	
    39  	/** Test case for the setter method. */
    40  	private var setter:MethodTestCase;
    41  	
    42  	/**
    43  	 * @overload #PropertyTestCaseByProperty
    44  	 * @overload #PropertyTestCaseByObjectAndName
    45  	 */
    46  	public function PropertyTestCase() {
    47  		var o:Overload = new Overload(this);
    48  		o.addHandler([PropertyInfo], PropertyTestCaseByProperty);
    49  		o.addHandler([Object, String], PropertyTestCaseByObjectAndName);
    50  		o.forward(arguments);
    51  	}
    52  	
    53  	/**
    54  	 * Constructs a new {@code PropertyTestCase} by property.
    55  	 * 
    56  	 * <p>If you want to profile a method, referenced from a different scope and with a
    57  	 * different name you can specify thse with the last two arguments. Note that if
    58  	 * specified the method declared on the class will not be profiled but its
    59  	 * reference.
    60  	 * 
    61  	 * @param property the property to profile
    62  	 * @param referenceScope (optional) the scope of the property reference to profile
    63  	 * @param referenceName (optional) the name of the property reference to profile
    64  	 * @throws IllegalArgumentException if the passed-in {@code property} is
    65  	 * {@code null} or {@code undefined}
    66  	 */
    67  	public function PropertyTestCaseByProperty(property:PropertyInfo, referenceScope, referenceName:String):Void {
    68  		if (!property) {
    69  			throw new IllegalArgumentException("Argument 'property' [" + property + "] must not be 'null' nor 'undefined'.", this, arguments);
    70  		}
    71  		this.property = property;
    72  		setResult(new SimpleTestSuiteResult(property.getFullName()));
    73  		if (property.getGetter()) {
    74  			this.getter = new MethodTestCase(property.getGetter(), referenceScope, "__get__" + referenceName);
    75  			this.result.addTestResult(this.getter.getResult(NONE));
    76  		}
    77  		if (property.getSetter()) {
    78  			this.setter = new MethodTestCase(property.getSetter(), referenceScope, "__set__" + referenceName);
    79  			this.result.addTestResult(this.getter.getResult(NONE));
    80  		}
    81  	}
    82  	
    83  	/**
    84  	 * Constructs a new {@code PropertyTestCase} by object and property name.
    85  	 * 
    86  	 * @param object the object that declares the property to profile
    87  	 * @param propertyName the name of the property to profile
    88  	 * @throws IllegalArgumentException if there is no property with the given
    89  	 * {@code propertyName} on the given {@code object}
    90  	 */
    91  	public function PropertyTestCaseByObjectAndName(object, propertyName:String):Void {
    92  		var c:ClassInfo = ClassInfo.forObject(object);
    93  		if (c.hasProperty(propertyName)) {
    94  			PropertyTestCaseByProperty(c.getPropertyByName(propertyName));
    95  		} else {
    96  			if (!object["__set__" + propertyName] && !object["__get__" + propertyName]) {
    97  				throw new IllegalArgumentException("Property with name [" + propertyName + "] does not exist on object [" + object + "].", this, arguments);
    98  			}
    99  			var setter:Function = object["__set__" + propertyName];
   100  			var getter:Function = object["__get__" + propertyName];
   101  			var p:PropertyInfo = new PropertyInfo(propertyName, c, false, setter, getter);
   102  			PropertyTestCaseByProperty(p, object, propertyName);
   103  		}
   104  	}
   105  	
   106  	/**
   107  	 * Returns the profiled property.
   108  	 * 
   109  	 * @return the profiled property
   110  	 */
   111  	public function getProperty(Void):PropertyInfo {
   112  		return this.property;
   113  	}
   114  	
   115  	/**
   116  	 * Runs this property test case.
   117  	 */
   118  	public function run(Void):Void {
   119  		this.getter.run();
   120  		this.setter.run();
   121  	}
   122  	
   123  }