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 implement and unit test Valang Server-side validation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    hi
    actually i have mvc step3 in which they have performed the junit test.if u want that i can forward that to u.

    Comment


    • #3
      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;
              }
          }
      }

      Comment

      Working...
      X