Skip to content

Commit

Permalink
get a working unit test in place, including a test for the not yet su…
Browse files Browse the repository at this point in the history
…pported GRAILS-4435
  • Loading branch information
Jeff Brown committed Dec 17, 2009
1 parent c536a94 commit 6d66bfc
Showing 1 changed file with 44 additions and 14 deletions.
@@ -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
Expand All @@ -36,3 +57,12 @@ class SomeValidateableClass {
name matches: /J.*/
}
}

class SomeValidateableSubclass extends SomeValidateableClass {

String town

static constraints = {
town size: 3..50
}
}

0 comments on commit 6d66bfc

Please sign in to comment.