1 package org
.codehaus
.groovy
.grails
.validation
;
3 import groovy
.lang
.Closure
;
4 import groovy
.lang
.GroovyShell
;
7 * Test cases for custom 'validator' constraint.
9 * @author Sergey Nebolsin (<a href="mailto:nebolsin@gmail.com"/>)
11 public class ValidatorConstraintTests
extends AbstractConstraintTests
{
12 private static final String PROP_NAME
= "firstName";
13 private GroovyShell shell
= new GroovyShell();
15 protected Class
getConstraintClass() {
16 return ValidatorConstraint
.class;
19 private Closure
getClosure( String code
) {
20 return (Closure
) shell
.evaluate( code
);
23 protected Constraint
getConstraint( String closure
) {
24 return super.getConstraint( "testString", getClosure(closure
) );
27 public void testBooleanReturn() {
28 testConstraintMessageCodes(
29 getConstraint("{val,obj -> return false}"),
31 new String
[] { "testClass.testString.validator.error","testClass.testString.validator.invalid"},
32 new Object
[] { "testString", TestClass
.class, "test" }
36 getConstraint( "{val,obj -> return true}" ),
40 testConstraintDefaultMessage(
41 getConstraint("{val,obj -> return false}"),
43 "Property [{0}] of class [{1}] with value [{2}] does not pass custom validation"
46 // Test null and blank values.
48 getConstraint( "{val,obj -> return val == null}" ),
53 getConstraint( "{val,obj -> return val == null}" ),
58 getConstraint( "{val,obj -> return val?.trim() == ''}" ),
63 getConstraint( "{val,obj -> return val?.trim() == ''}" ),
69 public void testStringReturn() {
70 testConstraintMessageCode(
71 getConstraint("{val,obj -> return 'test.message'}"),
73 "testClass.testString.test.message",
74 new Object
[] { "testString", TestClass
.class, "test" }
79 getConstraint("{val,obj -> return 123L}"),
82 fail("Validator constraint must throw an exception about wrong closure return");
83 } catch( IllegalArgumentException iae
) {
88 public void testListReturn() {
89 testConstraintMessageCode(
90 getConstraint("{val,obj -> return ['test.message', 'arg', 123L]}"),
92 "testClass.testString.test.message",
93 new Object
[] { "testString", TestClass
.class, "test", "arg", new Long(123) }
97 getConstraint("{val,obj -> return [123L,'arg1','arg2']}"),
100 fail("Validator constraint must throw an exception about wrong closure return");
101 } catch( IllegalArgumentException iae
) {
107 * Tests that the delegate that provides access to the name of the
108 * constrained property is available to custom validators.
110 public void testDelegate() {
111 testConstraintPassed(
112 getConstraint("{val, obj -> return propertyName == 'testString'}"),
116 public void testConstraintCreation() {
117 Constraint validatorConstraint
= new ValidatorConstraint();
118 assertEquals( ConstrainedProperty
.VALIDATOR_CONSTRAINT
, validatorConstraint
.getName() );
119 assertTrue( validatorConstraint
.supports( TestClass
.class ));
120 assertFalse( validatorConstraint
.supports( null ));
122 validatorConstraint
.setOwningClass( TestClass
.class );
123 validatorConstraint
.setPropertyName( PROP_NAME
);
126 getConstraint( "testString", "Test");
127 fail("ValidatorConstraint must throw an exception for non-closure parameter.");
128 } catch ( IllegalArgumentException iae
) {
129 // Great since validator constraint only applicable for Closure parameter
133 getConstraint( "{ param1, param2, param3, param4 -> return true}" );
134 fail("ValidatorConstraint must throw exception about closure with more that 3 params");
135 } catch ( IllegalArgumentException iae
) {
136 // Great since validator constraint only applicable for Closure's with 1, 2 or 3 params