Permalink
Browse files

Add logging to constraints and tests to make sure it works

Signed-off-by: Geoff Lane <geoff@zorched.net>
  • Loading branch information...
1 parent 74e1eaa commit f68bb6839402c674a00d9baf8c2f2e3b8c335fdb @geofflane committed Jan 2, 2011
View
@@ -1,8 +1,8 @@
#Grails Metadata file
-#Tue May 11 21:33:31 CDT 2010
-app.grails.version=1.3.0
+#Tue Jun 29 20:31:42 CDT 2010
+app.grails.version=1.3.2
app.name=constraints
app.version=0.5.1
-plugins.hibernate=1.3.0
-plugins.tomcat=1.3.0
-plugins.webflow=1.2.2
+plugins.hibernate=1.3.2
+plugins.tomcat=1.3.2
+plugins.webflow=1.3.0
@@ -84,4 +84,4 @@ log4j = {
warn 'org.mortbay.log'
}
-grails.validateable.packages = ['net.zorched.test']
+grails.validateable.packages = ['net.zorched.test']
@@ -23,6 +23,7 @@ class ComparisonConstraint {
static defaultMessage = "Property [{0}] of class [{1}] with value [{2}] does not match the property [{3}]"
def validate = { val, target ->
+ log.debug("Called ComparisonConstraint.validate: ${val}")
def compareVal = target."$params"
if (null == val || null == compareVal)
return false
@@ -24,9 +24,10 @@ class SsnConstraint {
return type!= null && String.class.isAssignableFrom(type);
}
- def validate = { propertyValue ->
+ def validate = { val ->
+ log.debug("Called SsnConstraint.validate: ${val}")
if (! params)
return true
- return propertyValue ==~ /\d{3}(-)?\d{2}(-)?\d{4}/
+ return val ==~ /\d{3}(-)?\d{2}(-)?\d{4}/
}
}
@@ -25,6 +25,7 @@ class UsPhoneConstraint {
}
def validate = { val ->
+ log.debug("Called UsPhoneConstraint.validate: ${val}")
if (! params)
return true
return val ==~ /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/
@@ -25,6 +25,7 @@ class UsZipConstraint {
}
def validate = { val ->
+ log.debug("Called UsZipConstraint.validate: ${val}")
if (! params)
return true
return val ==~ /^\d{5}(\-\d{4})?$/
@@ -1,37 +1,43 @@
import net.zorched.constraints.ComparisonConstraint
+import org.apache.commons.logging.LogFactory
class ComparisonTests extends GroovyTestCase {
void testComparisonOfMatchingWorks() {
- def v = new ComparisonConstraint()
- v.metaClass.getParams = {-> "other" }
+ def v = getConstraint()
assert v.validate("xxx", new Test(other:"xxx"))
}
void testComparisonOfNotMatchingWorks() {
- def v = new ComparisonConstraint()
- v.metaClass.getParams = {-> "other" }
+ def v = getConstraint()
assert ! v.validate("yyy", new Test(other:"xxx"))
}
void testComparisonOfNumber() {
- def v = new ComparisonConstraint()
- v.metaClass.getParams = {-> "other" }
+ def v = getConstraint()
assert v.validate(1, new Test(other: 1))
assert ! v.validate(1, new Test(other: 2))
}
void testComparisonOfNullValues() {
- def v = new ComparisonConstraint()
- v.metaClass.getParams = {-> "other" }
+ def v = getConstraint()
assert ! v.validate(null, new Test(other:"xxx"))
assert ! v.validate(null, new Test(other:null))
assert ! v.validate("xxx", new Test(other:null))
}
+
+ def log = LogFactory.getLog("xxx")
+ private def getConstraint() {
+ def v = new ComparisonConstraint()
+ v.metaClass.getParams = {-> "other" }
+ v.metaClass.getLog = {-> return log }
+
+ return v
+ }
}
class Test {
View
@@ -1,27 +1,25 @@
import net.zorched.constraints.SsnConstraint
+import org.apache.commons.logging.LogFactory
class SsnTests extends GroovyTestCase {
void testValidationNotCalledIfFalsePassedAsParam() {
- def v = new SsnConstraint()
- v.metaClass.getParams = {-> false }
+ def v = getConstraint(false)
assert v.validate("555-44-1212")
assert v.validate("")
- assert v.validate( null)
+ assert v.validate(null)
}
-
+
void testValidatesSsn() {
- def v = new SsnConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert v.validate("123-45-6789")
assert v.validate("000-00-0000")
}
void testDoesntValidateInvalidSsns() {
- def v = new SsnConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert ! v.validate("1")
assert ! v.validate("12-12-1234")
@@ -32,4 +30,12 @@ class SsnTests extends GroovyTestCase {
assert ! v.validate(" ")
assert ! v.validate(null)
}
+
+ def log = LogFactory.getLog("xxx");
+ private def getConstraint(boolean param) {
+ def v = new SsnConstraint()
+ v.metaClass.getParams = {-> param }
+ v.metaClass.getLog = {-> return log }
+ return v
+ }
}
@@ -1,27 +1,25 @@
import net.zorched.constraints.UsPhoneConstraint
+import org.apache.commons.logging.LogFactory
class UsPhoneTests extends GroovyTestCase {
void testPhoneValidationNotCalledIfFalsePassedAsParam() {
- def v = new UsPhoneConstraint()
- v.metaClass.getParams = {-> false }
+ def v = getConstraint(false)
assert v.validate("555-555-1212")
assert v.validate("")
assert v.validate( null)
}
void testValidUsPhones() {
- def v = new UsPhoneConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert v.validate("555-555-1212")
assert v.validate("1-800-555-1212")
}
void testInvalidUsPhones() {
- def v = new UsPhoneConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert ! v.validate("555-555-12123")
assert ! v.validate("1-800-5553-1212")
@@ -30,4 +28,12 @@ class UsPhoneTests extends GroovyTestCase {
assert ! v.validate("")
assert ! v.validate(null)
}
+
+ def log = LogFactory.getLog("xxx")
+ private def getConstraint(boolean param) {
+ def v = new UsPhoneConstraint()
+ v.metaClass.getParams = {-> param }
+ v.metaClass.getLog = {-> return log }
+ return v
+ }
}
@@ -1,35 +1,32 @@
import net.zorched.constraints.UsZipConstraint
+import org.apache.commons.logging.LogFactory
class UsZipTests extends GroovyTestCase {
void testValidationNotCalledIfFalsePassedAsParam() {
- def v = new UsZipConstraint()
- v.metaClass.getParams = {-> false }
+ def v = getConstraint(false)
assert v.validate("55512")
assert v.validate("")
assert v.validate( null)
}
void testValidatesFiveZip() {
- def v = new UsZipConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert v.validate("53212")
assert v.validate("00000")
}
void testValidatesFivePlus4Zip() {
- def v = new UsZipConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert v.validate("53212-4567")
assert v.validate("00000-5678")
}
void testDoesntValidateInvalidZips() {
- def v = new UsZipConstraint()
- v.metaClass.getParams = {-> true }
+ def v = getConstraint(true)
assert ! v.validate("5321")
assert ! v.validate("53215-456")
@@ -40,4 +37,12 @@ class UsZipTests extends GroovyTestCase {
assert ! v.validate(" ")
assert ! v.validate(null)
}
+
+ def log = LogFactory.getLog("xxx")
+ private def getConstraint(boolean param) {
+ def v = new UsZipConstraint()
+ v.metaClass.getParams = {-> param }
+ v.metaClass.getLog = {-> return log }
+ return v
+ }
}

0 comments on commit f68bb68

Please sign in to comment.