Results 1 to 6 of 6

Thread: How to test Valang validator?

  1. #1
    Join Date
    Dec 2005
    Location
    U-241
    Posts
    237

    Default How to test Valang validator?

    Hi everybody!
    Here's my problem.
    Configured Valang validator for some Person class
    Code:
    <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
    Code:
    <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...
    Code:
    public class PersonValidatorTest extends TestCase {
    
    	Person person = new Person();
    	BindException errors = new BindException(person, "target");
    	Validator validator = Now, what???
    	
    }
    Any ways, any clues?...
    Spring, it's a wonderful thing...

  2. #2
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb Instantiate via Spring config file?

    I've never used Valang myself, but how about your test case:
    1. Creates a FileSystemXmlApplicationContext that loads the beans from whichever XML file defines your validator.
    2. Calls getBean("personValidator") on that context to get the dynamically-generated Validator.
    3. Tests that Validator in the usual way.
    Would this work?
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  3. #3
    Join Date
    Dec 2005
    Location
    U-241
    Posts
    237

    Default

    Yes, it does!
    Code:
      
        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)
    Spring, it's a wonderful thing...

  4. #4
    Join Date
    Dec 2005
    Location
    U-241
    Posts
    237

    Default

    Looks like this version is better
    Code:
    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!
    Spring, it's a wonderful thing...

  5. #5
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Cool No worries!

    Glad to be of help, UnterseeBootKapitane Werr!
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  6. #6
    Join Date
    Dec 2006
    Posts
    8

    Default How to test Valang validation for Server-side validation?

    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?

    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •