GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / validation / ValidatorConstraintTests.java
blob05879be7de26f6157c159f04770173a5866cd05a
1 package org.codehaus.groovy.grails.validation;
3 import groovy.lang.Closure;
4 import groovy.lang.GroovyShell;
6 /**
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}"),
30 "test",
31 new String[] { "testClass.testString.validator.error","testClass.testString.validator.invalid"},
32 new Object[] { "testString", TestClass.class, "test" }
35 testConstraintPassed(
36 getConstraint( "{val,obj -> return true}" ),
37 "test"
40 testConstraintDefaultMessage(
41 getConstraint("{val,obj -> return false}"),
42 "test",
43 "Property [{0}] of class [{1}] with value [{2}] does not pass custom validation"
46 // Test null and blank values.
47 testConstraintFailed(
48 getConstraint( "{val,obj -> return val == null}" ),
49 "test"
52 testConstraintPassed(
53 getConstraint( "{val,obj -> return val == null}" ),
54 null
57 testConstraintFailed(
58 getConstraint( "{val,obj -> return val?.trim() == ''}" ),
59 "test"
62 testConstraintPassed(
63 getConstraint( "{val,obj -> return val?.trim() == ''}" ),
64 " "
69 public void testStringReturn() {
70 testConstraintMessageCode(
71 getConstraint("{val,obj -> return 'test.message'}"),
72 "test",
73 "testClass.testString.test.message",
74 new Object[] { "testString", TestClass.class, "test" }
77 try {
78 testConstraintFailed(
79 getConstraint("{val,obj -> return 123L}"),
80 "test"
82 fail("Validator constraint must throw an exception about wrong closure return");
83 } catch( IllegalArgumentException iae ) {
84 // Greate
88 public void testListReturn() {
89 testConstraintMessageCode(
90 getConstraint("{val,obj -> return ['test.message', 'arg', 123L]}"),
91 "test",
92 "testClass.testString.test.message",
93 new Object[] { "testString", TestClass.class, "test", "arg", new Long(123) }
95 try {
96 testConstraintFailed(
97 getConstraint("{val,obj -> return [123L,'arg1','arg2']}"),
98 "test"
100 fail("Validator constraint must throw an exception about wrong closure return");
101 } catch( IllegalArgumentException iae ) {
102 // Greate
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'}"),
113 "test");
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 );
125 try {
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
132 try {
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