| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Grails
Revision: 7278
Author: pledbrook
Date: 11 Aug 2008 06:50:34
Changes:GRAILS-1263: Transient properties with constraints are now validated when a domain instance is saved or validated.
Files:| ... | ...@@ -0,0 +1,85 @@ | |
| 1 | package org.codehaus.groovy.grails.orm.hibernate.validation | |
| 2 | ||
| 3 | import org.codehaus.groovy.grails.orm.hibernate.AbstractGrailsHibernateTests | |
| 4 | ||
| 5 | /** | |
| 6 | * @author pledbrook | |
| 7 | */ | |
| 8 | class ValidateTransientTests extends AbstractGrailsHibernateTests { | |
| 9 | /** | |
| 10 | * Set up the test domain classes, with a transient field. | |
| 11 | */ | |
| 12 | protected void onSetUp() { | |
| 13 | gcl.parseClass ''' | |
| 14 | class User { | |
| 15 | Long id | |
| 16 | Long version | |
| 17 | String userId | |
| 18 | String lastName | |
| 19 | String firstName | |
| 20 | String isManager | |
| 21 | String isSupervisor | |
| 22 | String isAgent | |
| 23 | String isSuperUser | |
| 24 | String dummyValidator // This is just used for cross field validation | |
| 25 | ||
| 26 | static transients = ["dummyValidator"] | |
| 27 | ||
| 28 | static constraints = { | |
| 29 | userId(size: 0..40, blank: false) | |
| 30 | lastName(size: 0..40, blank: false) | |
| 31 | firstName(size: 0..40, blank: false) | |
| 32 | isManager(size: 0..1, blank: false, inList: ["Y", "N"]) | |
| 33 | isSupervisor(size: 0..1, blank: false, inList: ["Y", "N"]) | |
| 34 | isAgent(size: 0..1, blank: false, inList: ["Y", "N"]) | |
| 35 | isSuperUser(size: 0..1, inList: ["Y", "N"]) | |
| 36 | dummyValidator(validator: {val, obj -> | |
| 37 | if (obj.isSuperUser == "N") { | |
| 38 | if (obj.isManager == "Y" && (obj.isSupervisor == "Y" || obj.isAgent == "Y")) { return ['only.one.user.type'] } | |
| 39 | if (obj.isSupervisor == "Y" && (obj.isManager == "Y" || obj.isAgent == "Y")) { return ['only.one.user.type'] } | |
| 40 | } | |
| 41 | if (obj.isAgent == "Y" && (obj.isManager == "Y" || obj.isSupervisor == "Y")) { return ['only.one.user.type'] } | |
| 42 | }) | |
| 43 | } | |
| 44 | } | |
| 45 | ''' | |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Tests that a validation that ought to succeed actually does. | |
| 50 | */ | |
| 51 | void testSuccessfulValidation() { | |
| 52 | def userClass = ga.getDomainClass("User").clazz | |
| 53 | ||
| 54 | def user = userClass.newInstance( | |
| 55 | userId: "pedro", | |
| 56 | lastName: "Smith", | |
| 57 | firstName: "Peter", | |
| 58 | isManager: "N", | |
| 59 | isSupervisor: "N", | |
| 60 | isAgent: "N", | |
| 61 | isSuperUser: "N") | |
| 62 | def retval = user.validate() | |
| 63 | if (!retval) { | |
| 64 | println "Errors: ${user.errors}" | |
| 65 | } | |
| 66 | assertTrue "User validation failed but it should have passed.", user.validate() | |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Tests that a validation that ought to fail actually does. | |
| 71 | */ | |
| 72 | void testFailedValidation() { | |
| 73 | def userClass = ga.getDomainClass("User").clazz | |
| 74 | ||
| 75 | def user = userClass.newInstance( | |
| 76 | userId: "pedro", | |
| 77 | lastName: "Smith", | |
| 78 | firstName: "Peter", | |
| 79 | isManager: "Y", | |
| 80 | isSupervisor: "Y", | |
| 81 | isAgent: "N", | |
| 82 | isSuperUser: "N") | |
| 83 | assertFalse "User validation passed but there should be an error on the transient field.", user.validate() | |
| 84 | } | |
| 85 | } |
| ... | ...@@ -85,6 +85,7 @@ | |
| 85 | 85 | BeanWrapper bean = new BeanWrapperImpl(obj); |
| 86 | 86 | |
| 87 | 87 | Map constrainedProperties = domainClass.getConstrainedProperties(); |
| 88 | Set constrainedPropertyNames = new HashSet(constrainedProperties.keySet()); | |
| 88 | 89 | |
| 89 | 90 | GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties(); |
| 90 | 91 | |
| ... | ...@@ -98,6 +99,17 @@ | |
| 98 | 99 | if((persistentProperty.isAssociation() || persistentProperty.isEmbedded()) && cascade) { |
| 99 | 100 | cascadeToAssociativeProperty(errors, bean, persistentProperty); |
| 100 | 101 | } |
| 102 | ||
| 103 | // Remove this property from the set of constrained property | |
| 104 | // names because we have already processed it. | |
| 105 | constrainedPropertyNames.remove(propertyName); | |
| 106 | } | |
| 107 | ||
| 108 | // Now process the remaining constrained properties, for example | |
| 109 | // any transients. | |
| 110 | for (Iterator iter = constrainedPropertyNames.iterator(); iter.hasNext();) { | |
| 111 | String name = (String) iter.next(); | |
| 112 | validatePropertyWithConstraint(name, obj, errors, bean, constrainedProperties); | |
| 101 | 113 | } |
| 102 | 114 | |
| 103 | 115 | if(obj instanceof GroovyObject) { |