GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / validation / AbstractConstraintTests.java
blob8be3d905298b6c1e19ba45373edcfe6e3ad2be58
1 package org.codehaus.groovy.grails.validation;
3 import junit.framework.TestCase;
4 import org.springframework.beans.BeanWrapper;
5 import org.springframework.beans.BeanWrapperImpl;
6 import org.springframework.validation.Errors;
7 import org.springframework.validation.BindException;
8 import org.springframework.validation.FieldError;
10 /**
11 * Abstract class for all constraint tests.
13 * @author Sergey Nebolsin (<a href="mailto:nebolsin@gmail.com"/>)
15 public abstract class AbstractConstraintTests extends TestCase {
17 protected Constraint getConstraint( String field, Object parameter ) {
18 Constraint constraint = null;
19 try {
20 constraint = ( Constraint ) getConstraintClass().newInstance();
21 } catch( Exception e ) {
22 fail( "Cannot instantiate constraint class [" + getConstraintClass().getName() + "]" );
24 constraint.setOwningClass( TestClass.class );
25 constraint.setPropertyName( field );
26 constraint.setParameter( parameter );
27 return constraint;
30 protected void testConstraintDefaultMessage( Constraint constraint, Object value, String message ) {
31 Errors errors = testConstraintFailed( constraint, value );
32 assertEquals( message, errors.getFieldError( constraint.getPropertyName() ).getDefaultMessage() );
35 protected void testConstraintMessageCode( Constraint constraint, Object value, String code ) {
36 Errors errors = testConstraintFailed( constraint, value );
37 checkCode( errors.getFieldError( constraint.getPropertyName()), code );
40 protected void testConstraintMessageCode( Constraint constraint, Object value, String code, Object[] args ) {
41 Errors errors = testConstraintFailed( constraint, value );
42 FieldError fieldError = errors.getFieldError( constraint.getPropertyName() );
43 checkCode( fieldError, code );
44 checkArguments( args, fieldError.getArguments() );
47 protected void testConstraintMessageCodes( Constraint constraint, Object value, String[] code, Object[] args ) {
48 Errors errors = testConstraintFailed( constraint, value );
49 FieldError fieldError = errors.getFieldError( constraint.getPropertyName() );
50 for( int j = 0; j < code.length; j++ ) {
51 checkCode( fieldError, code[j] );
53 checkArguments( args, fieldError.getArguments() );
56 protected Errors testConstraintFailed(Constraint constraint, Object value) {
57 Errors errors = validateConstraint(constraint, value);
58 assertEquals(true, errors.hasErrors());
59 return errors;
62 protected Errors testConstraintFailedAndVetoed(Constraint constraint, Object value) {
63 Errors errors = validateConstraint(constraint, value, Boolean.TRUE);
64 assertEquals(true, errors.hasErrors());
65 return errors;
68 protected void testConstraintPassed(Constraint constraint, Object value) {
69 Errors errors = validateConstraint(constraint, value);
70 assertEquals(false, errors.hasErrors());
73 protected void testConstraintPassedAndVetoed(Constraint constraint, Object value) {
74 Errors errors = validateConstraint(constraint, value, Boolean.TRUE);
75 assertEquals(false, errors.hasErrors());
78 protected Errors validateConstraint(Constraint constraint, Object value) {
79 return validateConstraint(constraint, value, null);
82 protected Errors validateConstraint(Constraint constraint, Object value, Boolean shouldVeto) {
83 BeanWrapper constrainedBean = new BeanWrapperImpl(new TestClass());
84 constrainedBean.setPropertyValue(constraint.getPropertyName(), value);
85 Errors errors = new BindException(constrainedBean.getWrappedInstance(), constrainedBean.getWrappedClass().getName());
86 if(!(constraint instanceof VetoingConstraint) || shouldVeto == null) {
87 constraint.validate(constrainedBean.getWrappedInstance(), value, errors);
88 } else {
89 boolean vetoed = ((VetoingConstraint) constraint).validateWithVetoing(constrainedBean.getWrappedInstance(), value, errors);
90 if(shouldVeto.booleanValue() && !vetoed) fail("Constraint should veto");
91 else if(!shouldVeto.booleanValue() && vetoed) fail("Constraint shouldn't veto");
93 return errors;
96 private void checkCode( FieldError error, String code ) {
97 String[] codes = error.getCodes();
98 boolean result = false;
99 for( int i = 0; i < codes.length; i++ ) {
100 if( code.equals( codes[i] ) ) {
101 result = true;
102 break;
105 assertTrue( "Code " + code + " is not found in error", result );
108 private void checkArguments( Object[] left, Object[] right ) {
109 assertEquals( left.length, right.length );
110 for( int i = 0; i < left.length; i++ ) {
111 assertEquals( left[i], right[i] );
115 protected abstract Class getConstraintClass();