/grails/src/test/org/codehaus/groovy/grails/validation/ConstrainedPropertyTests.java

https://github.com/jabley/grails · Java · 374 lines · 270 code · 75 blank · 29 comment · 4 complexity · 479cbd5a30e90ea8806759502947fcae MD5 · raw file

  1. package org.codehaus.groovy.grails.validation;
  2. import groovy.lang.GroovyClassLoader;
  3. import groovy.lang.GroovyObject;
  4. import groovy.lang.IntRange;
  5. import groovy.lang.ObjectRange;
  6. import junit.framework.TestCase;
  7. import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass;
  8. import org.codehaus.groovy.grails.commons.GrailsDomainClass;
  9. import org.springframework.validation.BindException;
  10. import org.springframework.validation.Errors;
  11. import java.math.BigDecimal;
  12. import java.math.BigInteger;
  13. import java.util.Collection;
  14. import java.util.Date;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. public class ConstrainedPropertyTests extends TestCase {
  18. private int testValidatorValue = 0;
  19. public int getTestValidatorValue()
  20. {
  21. return testValidatorValue;
  22. }
  23. public void setTestValidatorValue(int testValidatorValue)
  24. {
  25. this.testValidatorValue = testValidatorValue;
  26. }
  27. public void testGetSetURL() {
  28. ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
  29. cp.setUrl(true);
  30. assertTrue("should be an url", cp.isUrl());
  31. }
  32. public void testGetSetEmail() {
  33. ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
  34. cp.setEmail(true);
  35. assertTrue("should be an email", cp.isEmail());
  36. }
  37. public void testGetSetBlank() {
  38. ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
  39. cp.setBlank(true);
  40. assertTrue("should be blank", cp.isBlank());
  41. }
  42. public void testGetSetMatches() {
  43. ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
  44. cp.setMatches("\\.+");
  45. assertEquals("should match expression","\\.+", cp.getMatches());
  46. }
  47. public void testGetSetCreditCart() {
  48. ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
  49. cp.setCreditCard(true);
  50. assertTrue("should be credit cart", cp.isCreditCard());
  51. }
  52. /*
  53. * Test method for 'org.codehaus.groovy.grails.validation.ConstrainedProperty.supportsContraint(String)'
  54. */
  55. public void testSupportsContraint() {
  56. ConstrainedProperty cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", String.class);
  57. assertTrue(cp.supportsContraint( ConstrainedProperty.BLANK_CONSTRAINT ));
  58. assertTrue(cp.supportsContraint( ConstrainedProperty.EMAIL_CONSTRAINT ));
  59. assertTrue(cp.supportsContraint( ConstrainedProperty.MATCHES_CONSTRAINT ));
  60. assertTrue(cp.supportsContraint( ConstrainedProperty.IN_LIST_CONSTRAINT ));
  61. assertTrue(cp.supportsContraint( ConstrainedProperty.MAX_CONSTRAINT ));
  62. assertTrue(cp.supportsContraint( ConstrainedProperty.MIN_CONSTRAINT ));
  63. assertTrue(cp.supportsContraint( ConstrainedProperty.NOT_EQUAL_CONSTRAINT ));
  64. assertTrue(cp.supportsContraint( ConstrainedProperty.NULLABLE_CONSTRAINT ));
  65. assertTrue(cp.supportsContraint( ConstrainedProperty.RANGE_CONSTRAINT ));
  66. assertTrue(cp.supportsContraint( ConstrainedProperty.URL_CONSTRAINT ));
  67. assertTrue(cp.supportsContraint( ConstrainedProperty.VALIDATOR_CONSTRAINT ));
  68. assertTrue(cp.supportsContraint( ConstrainedProperty.MAX_SIZE_CONSTRAINT ));
  69. assertTrue(cp.supportsContraint( ConstrainedProperty.MIN_SIZE_CONSTRAINT ));
  70. assertTrue(cp.supportsContraint( ConstrainedProperty.SIZE_CONSTRAINT ));
  71. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  72. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Collection.class);
  73. assertTrue(cp.supportsContraint( ConstrainedProperty.MAX_SIZE_CONSTRAINT ));
  74. assertTrue(cp.supportsContraint( ConstrainedProperty.MIN_SIZE_CONSTRAINT ));
  75. assertTrue(cp.supportsContraint( ConstrainedProperty.SIZE_CONSTRAINT ));
  76. assertTrue(cp.supportsContraint( ConstrainedProperty.IN_LIST_CONSTRAINT ));
  77. assertTrue(cp.supportsContraint( ConstrainedProperty.NOT_EQUAL_CONSTRAINT ));
  78. assertTrue(cp.supportsContraint( ConstrainedProperty.NULLABLE_CONSTRAINT ));
  79. assertTrue(cp.supportsContraint( ConstrainedProperty.VALIDATOR_CONSTRAINT ));
  80. assertFalse(cp.supportsContraint( ConstrainedProperty.BLANK_CONSTRAINT ));
  81. assertFalse(cp.supportsContraint( ConstrainedProperty.EMAIL_CONSTRAINT ));
  82. assertFalse(cp.supportsContraint( ConstrainedProperty.MATCHES_CONSTRAINT ));
  83. assertFalse(cp.supportsContraint( ConstrainedProperty.MAX_CONSTRAINT ));
  84. assertFalse(cp.supportsContraint( ConstrainedProperty.MIN_CONSTRAINT ));
  85. assertFalse(cp.supportsContraint( ConstrainedProperty.RANGE_CONSTRAINT ));
  86. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  87. assertFalse(cp.supportsContraint( ConstrainedProperty.URL_CONSTRAINT ));
  88. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Number.class);
  89. assertTrue(cp.supportsContraint( ConstrainedProperty.IN_LIST_CONSTRAINT ));
  90. assertTrue(cp.supportsContraint( ConstrainedProperty.NOT_EQUAL_CONSTRAINT ));
  91. assertTrue(cp.supportsContraint( ConstrainedProperty.NULLABLE_CONSTRAINT ));
  92. assertTrue(cp.supportsContraint( ConstrainedProperty.MAX_CONSTRAINT ));
  93. assertTrue(cp.supportsContraint( ConstrainedProperty.MIN_CONSTRAINT ));
  94. assertTrue(cp.supportsContraint( ConstrainedProperty.RANGE_CONSTRAINT ));
  95. assertTrue(cp.supportsContraint( ConstrainedProperty.VALIDATOR_CONSTRAINT ));
  96. assertFalse(cp.supportsContraint( ConstrainedProperty.MAX_SIZE_CONSTRAINT ));
  97. assertFalse(cp.supportsContraint( ConstrainedProperty.MIN_SIZE_CONSTRAINT ));
  98. assertFalse(cp.supportsContraint( ConstrainedProperty.SIZE_CONSTRAINT ));
  99. assertFalse(cp.supportsContraint( ConstrainedProperty.BLANK_CONSTRAINT ));
  100. assertFalse(cp.supportsContraint( ConstrainedProperty.EMAIL_CONSTRAINT ));
  101. assertFalse(cp.supportsContraint( ConstrainedProperty.MATCHES_CONSTRAINT ));
  102. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  103. assertFalse(cp.supportsContraint( ConstrainedProperty.URL_CONSTRAINT ));
  104. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Date.class);
  105. assertTrue(cp.supportsContraint( ConstrainedProperty.MAX_CONSTRAINT ));
  106. assertTrue(cp.supportsContraint( ConstrainedProperty.MIN_CONSTRAINT ));
  107. assertTrue(cp.supportsContraint( ConstrainedProperty.RANGE_CONSTRAINT ));
  108. assertTrue(cp.supportsContraint( ConstrainedProperty.IN_LIST_CONSTRAINT ));
  109. assertTrue(cp.supportsContraint( ConstrainedProperty.NOT_EQUAL_CONSTRAINT ));
  110. assertTrue(cp.supportsContraint( ConstrainedProperty.NULLABLE_CONSTRAINT ));
  111. assertTrue(cp.supportsContraint( ConstrainedProperty.VALIDATOR_CONSTRAINT ));
  112. assertFalse(cp.supportsContraint( ConstrainedProperty.BLANK_CONSTRAINT ));
  113. assertFalse(cp.supportsContraint( ConstrainedProperty.EMAIL_CONSTRAINT ));
  114. assertFalse(cp.supportsContraint( ConstrainedProperty.MATCHES_CONSTRAINT ));
  115. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  116. assertFalse(cp.supportsContraint( ConstrainedProperty.URL_CONSTRAINT ));
  117. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Object.class);
  118. assertTrue(cp.supportsContraint( ConstrainedProperty.IN_LIST_CONSTRAINT ));
  119. assertTrue(cp.supportsContraint( ConstrainedProperty.NOT_EQUAL_CONSTRAINT ));
  120. assertTrue(cp.supportsContraint( ConstrainedProperty.NULLABLE_CONSTRAINT ));
  121. assertTrue(cp.supportsContraint( ConstrainedProperty.VALIDATOR_CONSTRAINT ));
  122. assertFalse(cp.supportsContraint( ConstrainedProperty.MAX_CONSTRAINT ));
  123. assertFalse(cp.supportsContraint( ConstrainedProperty.MIN_CONSTRAINT ));
  124. assertFalse(cp.supportsContraint( ConstrainedProperty.RANGE_CONSTRAINT ));
  125. assertFalse(cp.supportsContraint( ConstrainedProperty.BLANK_CONSTRAINT ));
  126. assertFalse(cp.supportsContraint( ConstrainedProperty.EMAIL_CONSTRAINT ));
  127. assertFalse(cp.supportsContraint( ConstrainedProperty.MATCHES_CONSTRAINT ));
  128. assertFalse(cp.supportsContraint( ConstrainedProperty.URL_CONSTRAINT ));
  129. assertFalse(cp.supportsContraint( ConstrainedProperty.MAX_SIZE_CONSTRAINT ));
  130. assertFalse(cp.supportsContraint( ConstrainedProperty.MIN_SIZE_CONSTRAINT ));
  131. assertFalse(cp.supportsContraint( ConstrainedProperty.SIZE_CONSTRAINT ));
  132. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  133. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Comparable.class);
  134. assertTrue(cp.supportsContraint( ConstrainedProperty.IN_LIST_CONSTRAINT ));
  135. assertTrue(cp.supportsContraint( ConstrainedProperty.NOT_EQUAL_CONSTRAINT ));
  136. assertTrue(cp.supportsContraint( ConstrainedProperty.NULLABLE_CONSTRAINT ));
  137. assertTrue(cp.supportsContraint( ConstrainedProperty.MAX_CONSTRAINT ));
  138. assertTrue(cp.supportsContraint( ConstrainedProperty.MIN_CONSTRAINT ));
  139. assertTrue(cp.supportsContraint( ConstrainedProperty.RANGE_CONSTRAINT ));
  140. assertFalse(cp.supportsContraint( ConstrainedProperty.BLANK_CONSTRAINT ));
  141. assertFalse(cp.supportsContraint( ConstrainedProperty.EMAIL_CONSTRAINT ));
  142. assertFalse(cp.supportsContraint( ConstrainedProperty.MATCHES_CONSTRAINT ));
  143. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  144. assertFalse(cp.supportsContraint( ConstrainedProperty.URL_CONSTRAINT ));
  145. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Float.class);
  146. assertTrue(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  147. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Double.class);
  148. assertTrue(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  149. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", BigDecimal.class);
  150. assertTrue(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  151. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Integer.class);
  152. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  153. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", Long.class);
  154. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  155. cp = new ConstrainedProperty(ConstrainedPropertyTests.class,"testProperty", BigInteger.class);
  156. assertFalse(cp.supportsContraint( ConstrainedProperty.SCALE_CONSTRAINT ));
  157. }
  158. public void testGetMin() {
  159. // validate that getMin returns null if the property has no min constraint and no range constraint
  160. ConstrainedProperty cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  161. assertNull(cp.getMin());
  162. // validate that getMin returns the correct value when the min constraint is defined for the property (but no range constraint is defined)
  163. cp.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new Double(123.45));
  164. assertEquals(new Double(123.45), cp.getMin());
  165. // validate that getMin returns the correct value when the range constraint is defined for the property (but no min constraint is defined)
  166. cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  167. cp.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT, new ObjectRange(new Double(123.45), new Double(678.90)));
  168. assertEquals(new Double(123.45), cp.getMin());
  169. // validate that getMin returns the maximum of the min constraint and the lower bound of the range constraint
  170. // 1) validate where the lower bound of the range constraint is greater than the min constraint
  171. cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  172. cp.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new Double(1.23));
  173. cp.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT, new ObjectRange(new Double(4.56), new Double(7.89)));
  174. assertEquals(new Double(4.56), cp.getMin());
  175. // 2) validate where the min constraint is greater than the lower bound of the range constraint
  176. cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  177. cp.applyConstraint(ConstrainedProperty.MIN_CONSTRAINT, new Double(4.56));
  178. cp.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT, new ObjectRange(new Double(1.23), new Double(7.89)));
  179. assertEquals(new Double(4.56), cp.getMin());
  180. }
  181. public void testGetMinSize() {
  182. // validate that getMinSize returns null if the property has no minSize constraint and no size constraint
  183. ConstrainedProperty cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  184. assertNull(cp.getMinSize());
  185. // validate that getMinSize returns the correct value when the minSize constraint is defined for the property (but no size constraint is defined)
  186. cp.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, new Integer(5));
  187. assertEquals(5, cp.getMinSize().intValue());
  188. // validate that getMinSize returns the correct value when the size constraint is defined for the property (but no minSize constraint is defined)
  189. cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  190. cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(10, 20));
  191. assertEquals(10, cp.getMinSize().intValue());
  192. // validate that getMinSize returns the maximum of the minSize constraint and the lower bound of the size constraint
  193. // 1) validate where the lower bound of the size constraint is greater than the minSize constraint
  194. cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  195. cp.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, new Integer(6));
  196. cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(11, 21));
  197. assertEquals(11, cp.getMinSize().intValue());
  198. // 2) validate where the minSize constraint is greater than the lower bound of the size constraint
  199. cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  200. cp.applyConstraint(ConstrainedProperty.MIN_SIZE_CONSTRAINT, new Integer(12));
  201. cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(9, 22));
  202. assertEquals(12, cp.getMinSize().intValue());
  203. }
  204. public void testGetMax() {
  205. // validate that getMax returns null if the property has no max constraint and no range constraint
  206. ConstrainedProperty cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  207. assertNull(cp.getMax());
  208. // validate that getMax returns the correct value when the max constraint is defined for the property (but no range constraint is defined)
  209. cp.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new Double(123.45));
  210. assertEquals(new Double(123.45), cp.getMax());
  211. // validate that getMax returns the correct value when the range constraint is defined for the property (but no max constraint is defined)
  212. cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  213. cp.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT, new ObjectRange(new Double(123.45), new Double(678.90)));
  214. assertEquals(new Double(678.90), cp.getMax());
  215. // validate that getMax returns the minimum of the max constraint and the upper bound of the range constraint
  216. // 1) validate where the upper bound of the range constraint is less than the max constraint
  217. cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  218. cp.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new Double(7.89));
  219. cp.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT, new ObjectRange(new Double(1.23), new Double(4.56)));
  220. assertEquals(new Double(4.56), cp.getMax());
  221. // 2) validate where the max constraint is less than the upper bound of the range constraint
  222. cp = new ConstrainedProperty(TestClass.class, "testDouble", Double.class);
  223. cp.applyConstraint(ConstrainedProperty.MAX_CONSTRAINT, new Double(4.56));
  224. cp.applyConstraint(ConstrainedProperty.RANGE_CONSTRAINT, new ObjectRange(new Double(1.23), new Double(7.89)));
  225. assertEquals(new Double(4.56), cp.getMax());
  226. }
  227. public void testGetMaxSize() {
  228. // validate that getMaxSize returns null if the property has no maxSize constraint and no size constraint
  229. ConstrainedProperty cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  230. assertNull(cp.getMaxSize());
  231. // validate that getMaxSize returns the correct value when the maxSize constraint is defined for the property (but no size constraint is defined)
  232. cp.applyConstraint(ConstrainedProperty.MAX_SIZE_CONSTRAINT, new Integer(5));
  233. assertEquals(5, cp.getMaxSize().intValue());
  234. // validate that getMaxSize returns the correct value when the size constraint is defined for the property (but no maxSize constraint is defined)
  235. cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  236. cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(10, 20));
  237. assertEquals(20, cp.getMaxSize().intValue());
  238. // validate that getMaxSize returns the minimum of the maxSize constraint and the upper bound of the size constraint
  239. // 1) validate where the upper bound of the size constraint is less than the maxSize constraint
  240. cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  241. cp.applyConstraint(ConstrainedProperty.MAX_SIZE_CONSTRAINT, new Integer(29));
  242. cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(11, 21));
  243. assertEquals(21, cp.getMaxSize().intValue());
  244. // 2) validate where the maxSize constraint is less than the upper bound of the size constraint
  245. cp = new ConstrainedProperty(this.getClass(), "testURL", String.class);
  246. cp.applyConstraint(ConstrainedProperty.MAX_SIZE_CONSTRAINT, new Integer(12));
  247. cp.applyConstraint(ConstrainedProperty.SIZE_CONSTRAINT, new IntRange(9, 22));
  248. assertEquals(12, cp.getMaxSize().intValue());
  249. }
  250. public void testConstraintBuilder() throws Exception {
  251. GroovyClassLoader gcl = new GroovyClassLoader();
  252. Class groovyClass = gcl.parseClass( "class TestClass {\n" +
  253. "Long id\n" +
  254. "Long version\n" +
  255. "String login\n" +
  256. "String other\n" +
  257. "String email\n" +
  258. "static constraints = {\n" +
  259. "login(size:5..15,nullable:false,blank:false)\n" +
  260. "other(blank:false,size:5..15,nullable:false)\n" +
  261. "email(email:true)\n" +
  262. "}\n" +
  263. "}" );
  264. GrailsDomainClass domainClass = new DefaultGrailsDomainClass(groovyClass);
  265. Map constrainedProperties = domainClass.getConstrainedProperties();
  266. assertTrue(constrainedProperties.size() == 3);
  267. ConstrainedProperty loginConstraint = (ConstrainedProperty)constrainedProperties.get("login");
  268. Collection appliedConstraints = loginConstraint.getAppliedConstraints();
  269. assertTrue(appliedConstraints.size() == 3);
  270. // Check the order of the constraints for the 'login' property...
  271. int index = 0;
  272. String[] constraintNames = new String[] { "size", "nullable", "blank" };
  273. for (Iterator iter = appliedConstraints.iterator(); iter.hasNext();) {
  274. Constraint c = (Constraint) iter.next();
  275. assertEquals(constraintNames[index], c.getName());
  276. index++;
  277. }
  278. // ...and for the 'other' property.
  279. appliedConstraints = ((ConstrainedProperty) constrainedProperties.get("other")).getAppliedConstraints();
  280. index = 0;
  281. constraintNames = new String[] { "blank", "size", "nullable" };
  282. for (Iterator iter = appliedConstraints.iterator(); iter.hasNext();) {
  283. Constraint c = (Constraint) iter.next();
  284. assertEquals(constraintNames[index], c.getName());
  285. index++;
  286. }
  287. ConstrainedProperty emailConstraint = (ConstrainedProperty)constrainedProperties.get("email");
  288. assertEquals(2,emailConstraint.getAppliedConstraints().size());
  289. GroovyObject go = (GroovyObject)groovyClass.newInstance();
  290. go.setProperty("email", "rubbish_email");
  291. Errors errors = new BindException(go, "TestClass");
  292. emailConstraint.validate(go, go.getProperty("email"), errors );
  293. assertTrue(errors.hasErrors());
  294. go.setProperty("email", "valid@email.com");
  295. errors = new BindException(go, "TestClass");
  296. emailConstraint.validate(go, go.getProperty("email"), errors );
  297. assertFalse(errors.hasErrors());
  298. }
  299. }