GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / validation / MatchesConstraintTests.java
blobf281e1e2814ad7b24707b8de72723159dac68ba1
1 package org.codehaus.groovy.grails.validation;
3 /**
4 * Test cases for 'matches' constraint.
6 * @author Sergey Nebolsin (<a href="mailto:nebolsin@gmail.com"/>)
7 */
8 public class MatchesConstraintTests extends AbstractConstraintTests {
9 protected Class getConstraintClass() {
10 return MatchesConstraint.class;
13 public void testValidation() {
14 testConstraintMessageCodes(
15 getConstraint( "testString", "[a-zA-Z]"),
16 "$",
17 new String[] {"testClass.testString.matches.error","testClass.testString.matches.invalid"},
18 new Object[] {"testString",TestClass.class,"$","[a-zA-Z]"}
21 testConstraintPassed(
22 getConstraint( "testString", "[a-zA-Z]+"),
23 "asdfdf"
26 // must always pass for null values
27 testConstraintPassed(
28 getConstraint( "testString", "[a-zA-Z]+" ),
29 null
32 testConstraintDefaultMessage(
33 getConstraint( "testString", "[a-zA-Z]+" ),
34 "$",
35 "Property [{0}] of class [{1}] with value [{2}] does not match the required pattern [{3}]"
40 public void testCreation() {
41 MatchesConstraint constraint = new MatchesConstraint();
42 assertEquals( ConstrainedProperty.MATCHES_CONSTRAINT, constraint.getName() );
43 assertTrue( constraint.supports( String.class ));
44 assertFalse( constraint.supports( null ));
45 assertFalse( constraint.supports( Long.class ));
47 constraint = (MatchesConstraint) getConstraint( "testString", "[a-z]");
48 assertEquals( "[a-z]", constraint.getRegex());
50 try {
51 getConstraint( "testString", new Long(123));
52 fail("MatchesConstraint must throw an exception for non-string parameters.");
53 } catch( IllegalArgumentException iae ) {
54 // Great