Additional context for XML method constraint

2011-11-25
2013-01-25
  • David Weber
    David Weber
    2011-11-25

    How do I add additional context / values for a custom XML based method constraint with the OVal validation framework?

    With the OVal validation framework it is possible to create custom annotation or XML based constraints (http://oval.sourceforge.net/userguide.html#d4e493).

    My intention is to generate an OVal XML configuration file out of some constraint definitions, that's why I would like to do the complete OVal constraint definition with the XML configuration (http://oval.sourceforge.net/userguide.html#d4e551).

    I would like to validate the return value of a certain method (getDomain) of a class (Attend) and I have to add additional values (six strings) for the isSatisfied method of my custom check class.

    My XML configuration so far looks like this:

        <class type="my.package.Attend"
            overwrite="false" applyFieldConstraintsToSetter="true">
            <field name="NAME">
                <notNull />
                <maxLength max="4" />
            </field>
            <method name="getDomain">
                <returnValue>
                    <my.package.DomainCheck />
                </returnValue>
            </method>
        </class>
    

    I have a checker class DomainCheck which should receive the return value from the getDomain method.
    In the isSatisfied method of the DomainCheck I have to validate the return value with some additional parameters that I have to configure somehow in the XML.

    My first problem is, that the isSatisfied method of the DomainCheck is not invoked.
    If I delete the method constraint, the validation result is invalid as I expect it from the field constraint. But if I add the method constraint, the DomainCheck is not invoked and the validation result is valid (should still be invalid). I can not see why the custom check is not invoked. Something must be wrong with my method constraint definition.
    Here are my custom check class and the appropriate interface:

        package my.package;
        
        import ...
        
        public class DomainCheck extends AbstractAnnotationCheck<Domain> {
        
        
            public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) {
                if (valueToValidate == null) {
                    return true;
                }
        
                List<?> domainMembers = (ArrayList<?>) valueToValidate;
                for (Object domainMember : domainMembers) {
                    // do validation
                }
        
                return false
            }
        
        }
        
        package my.package;
        
        import ...
        
        @Retention(RetentionPolicy.RUNTIME)
        @Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
        @net.sf.oval.configuration.annotation.Constraint(checkWith = DomainCheck.class)
        public @interface Domain {
        
            String message() default "must be conform to ...";
        }
    

    If this would work, my second problem would be to configure the additional parameters.
    I thought of something like:

            <method name="getDomain">
                <returnValue>
                    <my.package.DomainCheck />
                </returnValue>
                <parameter type="java.lang.String">OneName</parameter>
                <parameter type="java.lang.String">AnotherName</parameter>
                <parameter type="java.lang.String">0</parameter>
                <parameter type="java.lang.String">*</parameter>
                <parameter type="java.lang.String">5</parameter>
                <parameter type="java.lang.String">100</parameter>
            </method>
    

    The syntax above is for defining constraints for a method signature, so this obviously does not work. But I can not find any possible definition for my purpose.

    So, why is my custom check not invoked and if there is a solution for that, how can I define additional parameters for the isSatisfied method in the XML configuration and how do I access them in the isSatisfied method?

    Thank you in advance for any advice!
    Cheers David

     
  • seb
    seb
    2011-11-26

    Hi David,

    please try:

    <method name="getDomain" isInvariant="true"> <returnValue> <my.package.DomainCheck /> </returnValue> </method>
    

    If you omit the isInvariant attribute your constraint will only be applied on method invocation if the programming by contract feature is activated.

    Regards,
    Seb

     
  • David Weber
    David Weber
    2011-11-28

    Hi Seb

    Thank you very much for the reply.
    With the isInvariant="true" my custom check is now invoked.

    But how is it possible to add additional values in the configuration, that are available when the isSatisfied method is invoked?

    I'm generating the XML configuration. During generation some additional values should be written to the configuration in order to have them available for validation in the custom check.

    Cheers
    David

     
  • seb
    seb
    2011-11-30

    hi David,

    simply add getter methods for the configuration parameters you need to your check class.

    class DomainCheck {
      void setMyParameter(String value)
      {
         ....
      }
    }
    

    Then do

    <method name="getDomain" isInvariant="true"> 
       <returnValue>
           <my.package.DomainCheck myParameter="foobar" />
       </returnValue> 
    </method>
    
     
  • David Weber
    David Weber
    2011-11-30

    Hi Seb

    Thanks a lot for the code snippets!
    That works very well.
    It is really nice, what can be done with the OVal validation framework!

    Can you tell me, if there exists OVal documentation or some examples where it is possible to get such kind of information from?

    Cheers David

     
  • seb
    seb
    2011-12-01

    Hi David,

    the documentation on who to do things is probably the source code. Just look at the built-in constraints to see how things are done.
    The requirement to use isInvariant is described here http://oval.sourceforge.net/userguide.html#declaring-getter-constraints
    For XML configuration we are using the XStream framework, so you can also checkout their documentation if you have specific requirements.

    But of course there is room for improvment in the current userguide…

    Regards,
    Seb

     
  • David Weber
    David Weber
    2011-12-02

    Alright, thanks a lot!

    Cheers David