Results 1 to 3 of 3

Thread: How to implement and unit test Valang Server-side validation?

  1. #1
    Join Date
    Dec 2006
    Posts
    8

    Default How to implement and unit test Valang Server-side validation?

    (I am reposting in a new thread since my reply to an old thread didn't give any feedback)

    Hi!

    I am trying to use Valang with annotations for server-side validation and want to write JUnit tests for it. But, I haven't been able to find any documentation on how to perform validation nor test the validation it and the source jar doesn't contain any test classes (at least not to my knowledge).

    I thought I should be able to use code similar to the one seen below, but that obviously does not work. How do I implement server-side validation and, also, unit tests of the same?

    Valang looks great, but if I am not able to resolve this soon I have to drop it. It has taken too much time already to get me started.

    Please, give me some examples or, better yet, provide some examples on the web page (xml configuration and annotations configuration, client-side and server-side).

    Kind Regards,
    Jimisola

    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

  2. #2
    Join Date
    Dec 2006
    Posts
    3

    Default

    hi
    actually i have mvc step3 in which they have performed the junit test.if u want that i can forward that to u.

  3. #3
    Join Date
    Dec 2006
    Posts
    8

    Default

    I found the solution to my problems Bean Validation Framework (see documentation 0.7 release).

    Code:
    package se.stickybit.valangtest;
    
    import junit.framework.TestCase;
    
    import org.springframework.validation.BindException;
    import org.springframework.validation.FieldError;
    import org.springframework.validation.ObjectError;
    import org.springmodules.validation.bean.BeanValidator;
    import
    org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader;
    import
    org.springmodules.validation.bean.conf.loader.annotation.handler.Length;
    
    public class SimpleBeanTest
        extends TestCase
    {
        public SimpleBeanTest(final String arg0)
        {
            super(arg0);
        }
    
        public void testValidation() throws Exception
        {        
            SimpleBean sb  = new SimpleBean();
            sb.setSimple("");
    
            AnnotationBeanValidationConfigurationLoader l = new
    AnnotationBeanValidationConfigurationLoader();
            l.loadConfiguration(sb.getClass());
    
            BeanValidator bv = new BeanValidator(l);
            
            BindException errors = new BindException(sb, "target");
                    
            bv.validate(sb, errors);
            
            assertEquals("Validation should fail with one (1) errors since
    length < 1 (0)", 1, errors.getErrorCount());        
    
            for (Object o : errors.getAllErrors())
            {
                ObjectError oe = (ObjectError)o;
                
                System.out.println(oe.toString());
                if (oe instanceof FieldError) {
                    FieldError fieldError = (FieldError) oe;
                    System.out.println(fieldError.getField());
                }
                
            }
       }
        
        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;
            }
        }
    }

Posting Permissions

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