From 6d66bfc12f11b3c2afa52f82ae5dc446a9de0332 Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Wed, 16 Dec 2009 21:29:48 -0600 Subject: [PATCH] get a working unit test in place, including a test for the not yet supported GRAILS-4435 --- .../ValidationGrailsPluginTests.groovy | 58 ++++++++++++++----- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/src/test/org/codehaus/groovy/grails/plugins/ValidationGrailsPluginTests.groovy b/src/test/org/codehaus/groovy/grails/plugins/ValidationGrailsPluginTests.groovy index cd090144b1b..d3162efad56 100644 --- a/src/test/org/codehaus/groovy/grails/plugins/ValidationGrailsPluginTests.groovy +++ b/src/test/org/codehaus/groovy/grails/plugins/ValidationGrailsPluginTests.groovy @@ -1,33 +1,54 @@ package org.codehaus.groovy.grails.plugins -import org.codehaus.groovy.grails.commons.ConfigurationHolder -import org.codehaus.groovy.grails.plugins.web.AbstractGrailsPluginTests -import org.codehaus.groovy.grails.validation.Validateable +import org.codehaus.groovy.grails.plugins.ValidationGrailsPlugin +import org.springframework.context.ApplicationContext import org.springframework.context.support.StaticMessageSource -public class ValidationGrailsPluginTests extends AbstractGrailsPluginTests { +public class ValidationGrailsPluginTests extends GroovyTestCase { - void onSetUp() { - def config = new ConfigSlurper().parse("grails.validateable.packages= ['org.codehaus.groovy.grails.plugins']") - ConfigurationHolder.config = config + protected void tearDown() { + super.tearDown() + def registry = GroovySystem.getMetaClassRegistry() + registry.removeMetaClass(ValidationGrailsPlugin) + registry.removeMetaClass(SomeValidateableClass) + registry.removeMetaClass(SomeValidateableSubclass) + } + + protected void setUp() { + super.setUp() + def ctxMap = [:] + ctxMap.getBean = {new StaticMessageSource()} + ctxMap.getBeansWithAnnotation = {[someValidatableClass: new SomeValidateableClass(), + someValidateableSubclass: new SomeValidateableSubclass()] } + + def mockCtx = ctxMap as ApplicationContext - pluginsToLoad << gcl.loadClass("org.codehaus.groovy.grails.plugins.ValidationGrailsPlugin") - pluginsToLoad << gcl.loadClass("org.codehaus.groovy.grails.plugins.CoreGrailsPlugin") - ctx.registerMockBean('messageSource', new StaticMessageSource()) + ValidationGrailsPlugin.metaClass.getApplication = { [:] } + ValidationGrailsPlugin.metaClass.getLog = { [debug: {}] } + + new ValidationGrailsPlugin().doWithDynamicMethods(mockCtx) + } + + void testBasicValidation() { + def svc = new SomeValidateableClass() + svc.name = 'Jeff' + assertTrue svc.validate() + svc.name = 'Zack' + assertFalse svc.validate() } - void testAnnotatedClass() { + void testInheritedConstraints() { if(notYetImplemented()) return - def svc = gcl.loadClass('org.codehaus.groovy.grails.plugins.SomeValidateableClass').newInstance() + def svc = new SomeValidateableSubclass() + svc.town = 'Saint Charles' svc.name = 'Jeff' assertTrue svc.validate() + svc.name = 'Zack' assertFalse svc.validate() } } - -@Validateable class SomeValidateableClass { String name @@ -36,3 +57,12 @@ class SomeValidateableClass { name matches: /J.*/ } } + +class SomeValidateableSubclass extends SomeValidateableClass { + + String town + + static constraints = { + town size: 3..50 + } +}