Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
How to test Valang validator? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?...

  • #2
    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?

    Comment


    • #3
      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)

      Comment


      • #4
        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!

        Comment


        • #5
          No worries!

          Glad to be of help, UnterseeBootKapitane Werr!

          Comment


          • #6
            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

            Comment

            Working...
            X