View Full Version : How to test Valang validator?
Arno Werr
Aug 7th, 2006, 08:39 AM
Hi everybody!
Here's my problem.
Configured Valang validator for some Person class
<bean
id="personValidator"
class="&ValangValidator;">
<property name="valang">
<value>
<![CDATA[
{ name.first : ? is not blank : 'First Name is Empty' : '' }
{ name.last : ? is not blank : 'Last Name is Empty' : '' }
{ bornOn : ? is not blank : 'Born on field is empty' : '' }
]]>
</value>
</property>
</bean>
Wired it up with a controller
<bean
name="newPersonFormCotroller"
class="&PersonFormController;">
<property
name="validator"
ref="personValidator" />
</bean>
Works like a charm. Very kool. Yet how to test this runtime generated validator...
public class PersonValidatorTest extends TestCase {
Person person = new Person();
BindException errors = new BindException(person, "target");
Validator validator = Now, what???
}
Any ways, any clues?...
Andrew Swan
Aug 13th, 2006, 11:09 PM
I've never used Valang myself, but how about your test case:
Creates a FileSystemXmlApplicationContext that loads the beans from whichever XML file defines your validator.
Calls getBean("personValidator") on that context to get the dynamically-generated Validator.
Tests that Validator in the usual way.Would this work?
Arno Werr
Aug 16th, 2006, 09:57 AM
Yes, it does!
private Person person;
private BindException errors;
private Validator validator;
protected void setUp() throws Exception {
super.setUp();
ApplicationContext ctx = new ClassPathXmlApplicationContext("valang.xml");
validator = (Validator) ctx.getBean("personValidator");
}
public void testNoBOD() throws Exception {
person = new Person();
// person.setBornOn(new Date());
Name name = new Name();
name.setFirst("Arno");
name.setLast("Werr");
person.setName(name);
person.setFavoriteProgrammingLanguage("Java");
errors = new BindException(person, "target");
validator.validate(person, errors);
List lst = errors.getAllErrors();
assertTrue(lst.size() == 1);
}
Thanks Andrew!
Swan? Oh! Swans were deadly. Especially Black Swans like HMS Stork or HMS Starling under Captain Johnny Walker RN (http://www.mikekemble.com/ww2/walker.html)
;)
Arno Werr
Aug 16th, 2006, 05:01 PM
Looks like this version is better :)
public class PersonValidatorTest extends
AbstractDependencyInjectionSpringContextTests {
private Person person;
private BindException errors;
private Validator validator;
public void setValidator(Validator validator) {
this.validator = validator;
}
protected String[] getConfigLocations() {
return new String[] { "valang.xml" };
}
public void testNoBOD() throws Exception {
person = new Person();
// person.setBornOn(new Date());
Name name = new Name();
name.setFirst("Arno");
name.setLast("Werr");
person.setName(name);
person.setFavoriteProgrammingLanguage("Java");
errors = new BindException(person, "target");
validator.validate(person, errors);
List lst = errors.getAllErrors();
assertTrue(lst.size() == 1);
}
}
AbstractDependencyInjectionSpringContextTests SIC! SIC!
Andrew Swan
Aug 16th, 2006, 06:47 PM
Glad to be of help, UnterseeBootKapitane Werr!
jimisola
Dec 9th, 2006, 09:39 AM
Hi!
I am trying to use Valang with annotations for server-side validation and wants to write JUnit tests for it. But, I haven't been able to find any documentation on this and the source jar doesn't contain any test classes (at least not to my knowledge).
My sample code looks this and it fails with a NPE since I don't have a applicationContext. How do I instead?
public class SimpleBeanTest
extends TestCase
{
public SimpleBeanTest(final String arg0)
{
super(arg0);
}
public void testValidation()
{
SimpleBean sb = new SimpleBean();
sb.setSimple("");
ValangValidator vv = new ValangValidator();
BindException errors = new BindException(sb, "target");
vv.validate(sb, errors);
assertEquals("Validation should fail with one (1) errors since length < 1 (0)", 1, errors.getErrorCount());
}
public class SimpleBean
{
@Length(min = 1, max = 255, message = "feature name must be between 1-255 characters")
private String simple;
public void setSimple(String simple)
{
this.simple = simple;
}
public String getSimple()
{
return this.simple;
}
}
}
Regards,
Jimisola
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.