Jeté Sample: Method Invocation

Home Overview
The method invocation sample is the methodTest1 test case defined in the system test configuration.  This sample file is located in sampleSystemTests/subtests/MethodTest.xml.  


MethodTest.xml Test Cases
The top level beans to be configured for the MethodTest.xml test file are the 2 test case beans.  These beans define the appropriate actions taken for these test cases, and the name of the particular test case (for junitreport functionality).  

    <bean id="methodTest.1" class="org.jete.cases.ActionTestCase">
        <property name="name">
            <value>method.invocation.success</value>
        </property>
        <property name="testActions">
            <list>
                <!-- set the value on the underlying sample object -->
                <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="sampleMethodInvocation"/>
                    </property>
                    <property name="targetMethod">
                        <value>setValue</value>
                    </property>
                    <property name="arguments">
                        <list>
                            <value>myTest</value>
                        </list>
                    </property>
                </bean>
                <!-- get the above sample from the container -->
                <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="sampleContainer"/>
                    </property>
                     <property name="targetMethod">
                         <value>getSample</value>
                     </property>
                     <property name="validators">
                         <list>
                             <ref bean="sample.methodTest.value.validator"/>
                         </list>
                     </property>
                 </bean>
            </list>
        </property>
    </bean>

    <bean id="methodTest.2" class="org.jete.cases.ActionTestCase">
        <property name="name">
            <value>method.invocation.failure</value>
        </property>
        <property name="testActions">
            <list>
                <!-- validate with the same validator above, resulting in a failure -->
                <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="sampleContainer2"/>
                    </property>
                     <property name="targetMethod">
                         <value>getSample</value>
                     </property>
                      <property name="validators">
                         <list>
                             <ref bean="sample.methodTest.value.validator"/>
                         </list>
                     </property>
                 </bean>
            </list>
        </property>
    </bean>

Company Specific Objects

For the actions to function properly the test defines a couple of company specific entities using standard Spring POJO configuration.  The test actions will then modify these entities and validate the results.  

It's important to note that these entities could be any number of company specific entites, from DAO, model, WSDL generated code, etc.

One shouldn't get too hung up on these sample objects.  They are essentially meaningless beans with setter / getter functionality, and only useful for a sample of the framework.
     <bean id="sampleMethodInvocation" class="com.company.pkg.SampleMethodInvocation"/>

    <bean id="sampleContainer" class="com.company.pkg2.SampleContainer">
        <property name="sample" ref="sampleMethodInvocation"/>
        <property name="containerValue" value="someContainerValue"/>
    </bean>

    <bean id="sampleMethodInvocation2" class="com.company.pkg.SampleMethodInvocation">
        <property name="value" value="badTest"/>
    </bean>

    <bean id="sampleContainer2" class="com.company.pkg2.SampleContainer">
        <property name="sample"><ref bean="sampleMethodInvocation2"/></property>
    </bean>

Actions

The test cases reference specific actions.  All actions in this sample are method invocation actions, meant to get and set data in the company specific entities above.

The actions are as defined:
- action1 calls the method setValue in the sampleMethodInvocation object, setting it's value to "myTest"
- action2 calls the method getSample in the sampleContainer object, and validates the result with validator1 (which should be looking for the value set in action1.
- action3 makes the same call to sampleContainer2 object, which was configured just like sampleContainer, only it's sampleMethodInvocation object wasn't set, so using the same validator1 will result in a failure.

The properties for 
MethodInvocationAction are:
- targetObject (a ref to a previously Spring defined object)
- targetMethod
- arguments

                <!--
                       inline methodTest.1
                       set the value on the underlying sample object
                 -->
                <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="sampleMethodInvocation"/>
                    </property>
                    <property name="targetMethod">
                        <value>setValue</value>
                    </property>
                    <property name="arguments">
                        <list>
                            <value>myTest</value>
                        </list>
                    </property>
                </bean>
                <!--
                       inline methodTest.1
                       get the above sample from the container
                 -->
                <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="sampleContainer"/>
                    </property>
                     <property name="targetMethod">
                         <value>getSample</value>
                     </property>
                     <property name="validators">
                         <list>
                             <ref bean="sample.methodTest.value.validator"/>
                         </list>
                     </property>
                 </bean>  
Validator
The validator is a 
MethodResultValidator that works with MethodResultActions (MethodInvocationAction implements this) by calling getResult() to retrieve the result object, and accessing the appropriate values using Spring's PropertyValue object.  

It should be noted that PropertyValue allows nested ojbect access.  This is done by changing the first argument in the constructor to indicate this nested behavior (e.g. "field1.field2.field3").  
    <bean id="sample.methodTest.value.validator" class="org.jete.model.MethodResultValidator">
        <property name="propertyValues">
            <list>
                <bean class="org.springframework.beans.PropertyValue">
                    <constructor-arg value="value"/>
                    <constructor-arg value="myTest"/>
                </bean>
            </list>
        </property>
    </bean>

<!--
       inline methodTest.5
       Sample exception validation
-->
             <bean class="org.jete.validator.ThrowableValidator">
                    <property name="throwableName" value="com.company.pkg.SampleException"/>
                    <property name="message" value="My sample exception"/>
             </bean>

<!--
        inline methodTest.4
        Sample map retrieval
-->
             <bean class="org.springframework.beans.PropertyValue">
                    <constructor-arg value="value[key3]"/>
                    <constructor-arg value="value3"/>
             </bean>
Validator Notes
Primitive values need to be treated a little differently by the framework.  Because there is no getter / setter associated with a primitive value when that's what the method returns, these values are wrapped in an object that provides a getter.

Current wrapped result values are:
  • Object arrays
  • Primitive objects (Integer, Float, etc)
  • Collections
  • Maps
Sample access of these data are as follows (these are also in the sample file)
    <bean class="org.jete.model.MethodResultValidator">
        <property name="propertyValues">
            <list>
                <bean class="org.springframework.beans.PropertyValue">
One of the following:
                    <!-- map-->
                    <constructor-arg value="value[key]"/>

                    <constructor-arg value="value"/>
                    <constructor-arg value=""/>
                    <constructor-arg value="value[1]"/>
Finished with the expected value:
                     <constructor-arg value="2"/>
                </bean>
            </list>
        </property>
    </bean>
hosted by SourceForge Logo