With the OVal validation framework (http://oval.sourceforge.net/) 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