PageRenderTime 2522ms CodeModel.GetById 37ms RepoModel.GetById 2ms app.codeStats 0ms

/grails/src/test/org/codehaus/groovy/grails/validation/RangeConstraintTests.java

https://github.com/rodchile/grails
Java | 73 lines | 54 code | 12 blank | 7 comment | 0 complexity | 88aec7253b45aaeccefd7cd2e89e2fc5 MD5 | raw file
  1. package org.codehaus.groovy.grails.validation;
  2. import groovy.lang.IntRange;
  3. import groovy.lang.ObjectRange;
  4. /**
  5. * Test cases for 'range' constraint.
  6. *
  7. * @author Sergey Nebolsin (<a href="mailto:nebolsin@gmail.com"/>)
  8. */
  9. public class RangeConstraintTests extends AbstractConstraintTests {
  10. protected Class getConstraintClass() {
  11. return RangeConstraint.class;
  12. }
  13. public void testValidation() {
  14. testConstraintMessageCodes(
  15. getConstraint( "testInteger", new IntRange( 1, 5 )),
  16. new Long(7),
  17. new String[] {"testClass.testInteger.range.error","testClass.testInteger.range.toobig"},
  18. new Object[] {"testInteger",TestClass.class,new Long(7),new Integer(1),new Integer(5)}
  19. );
  20. testConstraintMessageCodes(
  21. getConstraint( "testInteger", new IntRange( 1, 5 )),
  22. new Integer(0),
  23. new String[] {"testClass.testInteger.range.error","testClass.testInteger.range.toosmall"},
  24. new Object[] {"testInteger",TestClass.class,new Integer(0),new Integer(1),new Integer(5)}
  25. );
  26. testConstraintPassed(
  27. getConstraint( "testString", new ObjectRange("abca","abcf")),
  28. "abcd"
  29. );
  30. testConstraintPassed(
  31. getConstraint( "testInteger", new IntRange( 1, 7 )),
  32. new Integer( 5 )
  33. );
  34. // must always pass for null value
  35. testConstraintPassed(
  36. getConstraint( "testInteger", new IntRange( 1, 7 )),
  37. null
  38. );
  39. testConstraintDefaultMessage(
  40. getConstraint( "testInteger", new IntRange( 1, 5 ) ),
  41. new Integer( 7 ),
  42. "Property [{0}] of class [{1}] with value [{2}] does not fall within the valid range from [{3}] to [{4}]"
  43. );
  44. }
  45. public void testCreation() {
  46. RangeConstraint constraint = (RangeConstraint) getConstraint( "testInteger", new IntRange(1,5) );
  47. assertEquals( ConstrainedProperty.RANGE_CONSTRAINT, constraint.getName() );
  48. assertTrue( constraint.supports( Integer.class ));
  49. assertTrue( constraint.supports( Long.class ));
  50. assertTrue( constraint.supports( Double.class ));
  51. assertFalse( constraint.supports( Object.class ));
  52. assertFalse( constraint.supports( null ));
  53. assertEquals( new IntRange(1,5), constraint.getRange() );
  54. try {
  55. getConstraint( "testInteger", "wrong");
  56. fail("RangeConstraint must throw an exception for non-range parameters.");
  57. } catch( IllegalArgumentException iae ) {
  58. // Great
  59. }
  60. }
  61. }