GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / validation / ScaleConstraintTests.java
blobdfbabc8835d320e395da48ea05436903994b068b
1 package org.codehaus.groovy.grails.validation;
3 import org.springframework.beans.BeanWrapper;
4 import org.springframework.beans.BeanWrapperImpl;
5 import org.springframework.validation.Errors;
6 import org.springframework.validation.BindException;
8 import java.math.BigDecimal;
10 /**
11 * Test cases for 'scale' constraint.
13 * @author Sergey Nebolsin (<a href="mailto:nebolsin@gmail.com"/>)
15 public class ScaleConstraintTests extends AbstractConstraintTests {
16 protected Class getConstraintClass() {
17 return ScaleConstraint.class;
20 public void testValidation() {
21 testFloat(3, 0.1234f, 0.123f);
23 // test a Float value that should round up
24 testFloat(3, 0.1235f, 0.124f);
26 // test a Float value that should not change (i.e., should require no rounding)
27 testFloat(3, 0.12f, 0.120f);
29 // test an integral value masquerading as a Float
30 testFloat(3, 47f, 47.000f);
32 // test a scale of zero applied to a Float
33 testFloat(0, 0.123f, 0f);
35 // test a Double value that should round down
36 testDouble(3, 0.1234, 0.123);
38 // test a Double value that should round up
39 testDouble(3, 0.1235, 0.124);
41 // test a Double value that should not change (i.e., should require no rounding)
42 testDouble(3, 0.12, 0.120);
44 // test an integral value masquerading as a Double
45 testDouble(3, 47d, 47.000);
47 // test a scale of zero applied to a Double
48 testDouble(0, 0.123, 0d);
50 // test a BigDecimal value that should round down
51 testBigDecimal(3, "0.1234", "0.123");
53 // test a BigDecimal value that should round up
54 testBigDecimal(3, "0.1235", "0.124");
56 // test a BigDecimal value that should not change (i.e., should require no rounding)
57 testBigDecimal(3, "0.12", "0.120");
59 // test an integral value masquerading as a BigDecimal
60 testBigDecimal(3, "47", "47.000");
62 // test a scale of zero applied to a BigDecimal
63 testBigDecimal(0, "0.123", "0");
67 public void testNullPasses() {
68 Constraint constraint = getConstraint( "testBigDecimal", new Integer( 2 ));
69 assertEquals( null, proceedValidation( constraint, null));
72 public void testValidationOnInvalidField() {
73 Constraint constraint = getConstraint( "testString", new Integer( 2 ));
74 try {
75 proceedValidation( constraint, "123");
76 fail("ScaleConstraint must throw an exception when applied to field with unsupported type");
77 } catch( IllegalArgumentException iae ) {
78 // Great
83 public void testCreation() {
84 ScaleConstraint constraint = (ScaleConstraint) getConstraint( "testFloat", new Integer(2) );
85 assertEquals( ConstrainedProperty.SCALE_CONSTRAINT, constraint.getName() );
86 assertTrue( constraint.supports( BigDecimal.class ));
87 assertTrue( constraint.supports( Float.class ));
88 assertTrue( constraint.supports( Double.class ));
89 assertFalse( constraint.supports( String.class ));
90 assertFalse( constraint.supports( Object.class ));
91 assertFalse( constraint.supports( null ));
93 assertEquals( 2, constraint.getScale() );
95 try {
96 getConstraint( "testFloat", "wrong");
97 fail("EmailConstraint must throw an exception for non-integer parameters.");
98 } catch( IllegalArgumentException iae ) {
99 // Great
102 try {
103 getConstraint( "testFloat", new Integer(-1));
104 fail("EmailConstraint must throw an exception for negative parameters.");
105 } catch( IllegalArgumentException iae ) {
106 // Great
110 private void testFloat( int scale, float value, float result ) {
111 Constraint constraint = getConstraint( "testFloat", new Integer( scale ));
112 assertEquals( new Float(result), proceedValidation( constraint, new Float(value) ));
115 private void testDouble( int scale, double value, double result ) {
116 Constraint constraint = getConstraint( "testDouble", new Integer( scale ));
117 assertEquals( new Double(result), proceedValidation( constraint, new Double(value) ));
119 private void testBigDecimal( int scale, String value, String result ) {
120 Constraint constraint = getConstraint( "testBigDecimal", new Integer( scale ));
121 assertEquals( new BigDecimal( result ), proceedValidation( constraint, new BigDecimal( value )));
124 private Object proceedValidation( Constraint constraint, Object value ) {
125 BeanWrapper constrainedBean = new BeanWrapperImpl( new TestClass() );
126 constrainedBean.setPropertyValue( constraint.getPropertyName(), value );
127 Errors errors = new BindException( constrainedBean.getWrappedInstance(), constrainedBean.getWrappedClass().getName() );
128 assertFalse( errors.hasErrors() );
129 constraint.validate( constrainedBean.getWrappedInstance(), value, errors );
130 return constrainedBean.getPropertyValue( constraint.getPropertyName() );