My suggestion is that you create a business validation class with a
boolean "isValid(args)" method that has no ties to the presentation
layer.
Then you subclass that class to create your JSF validator, adding
attribute accessors, validator id, calling the business validator with
the appropriate arguments and throw the appropriate validation
exceptions with appropriate text messages.
You'd test your POJO business validation object just like anything else.
Your JSF converter wrapper code tests only need to insure that the
correct arguments are passed to isValid().
Instead of making it a subclass, you can also use a delegation pattern
to delegate to your business validation class (in case you want to
inherit from something like Tomahawk's ValidatorBase class).
I've then been able to use my business validator with no changes in
other environments (such as database conversion programs).
public class TownshipRangeValidator extends
xyz.business.validator.TownshipRangeValidator implements Validator,
StateHolder
{
public void validate(FacesContext context, UIComponent component,
Object valueObject) throws ValidatorException
{
if (TYPE_UNSPECIFIED == type)
{
throw new ValidatorException(new
FacesMessage(FacesMessage.SEVERITY_ERROR, "Programming Error",
"\"{0}\": type not set on validator for township or range."));
}
if (null == valueObject)
{
return;
}
String value = valueObject.toString().trim().toUpperCase();
if (0 == value.length())
{
return;
}
if (false == isValid(value, type))
{
if (TYPE_TOWNSHIP == type)
{
throw new ValidatorException(new
FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", "\"" +
component.getId() + "\": Expected number between 1-36 followed by
N,S"));
}
else if (TYPE_RANGE == type)
{
throw new ValidatorException(new
FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Error", "\"" +
component.getId() + "\": Expected number between 1-36 followed by
E,W"));
}
}
}
On 11/1/06, Mick Knutson <[EMAIL PROTECTED]> wrote:
I am looking for some examples on how to create seperate validation classes
for my MyFaces validation that can be Unit Testable.
--
Thanks
DJ MICK
http://www.djmick.com
http://www.myspace.com/mickknutson