Skip to content

Commit

Permalink
remove state from GrailsApplicationTestPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Jan 28, 2014
1 parent fa5182d commit ce5dfeb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Expand Up @@ -10,17 +10,16 @@ import grails.web.CamelCaseUrlConverter
import grails.web.UrlConverter
import groovy.transform.CompileStatic

import org.codehaus.groovy.grails.commons.ApplicationAttributes;
import org.codehaus.groovy.grails.commons.ApplicationAttributes
import org.codehaus.groovy.grails.commons.ClassPropertyFetcher
import org.codehaus.groovy.grails.commons.CodecArtefactHandler
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
import org.codehaus.groovy.grails.commons.DefaultGrailsCodecClass
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.spring.GrailsWebApplicationContext
import org.codehaus.groovy.grails.lifecycle.ShutdownOperations
import org.codehaus.groovy.grails.plugins.converters.ConvertersPluginSupport;
import org.codehaus.groovy.grails.plugins.converters.ConvertersPluginSupport
import org.codehaus.groovy.grails.validation.ConstraintEvalUtils
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.grails.async.factory.SynchronousPromiseFactory
import org.springframework.beans.CachedIntrospectionResults
import org.springframework.beans.factory.support.BeanDefinitionRegistry
Expand All @@ -35,9 +34,6 @@ class GrailsApplicationTestPlugin implements TestPlugin {
String[] providedFeatures = ['grailsApplication']
int ordinal = 0

protected Map validationErrorsMap = new IdentityHashMap()
protected Set loadedCodecs = []

void initGrailsApplication(TestRuntime runtime) {
GrailsWebApplicationContext applicationContext = new GrailsWebApplicationContext()

Expand Down Expand Up @@ -86,7 +82,12 @@ class GrailsApplicationTestPlugin implements TestPlugin {
ConvertersPluginSupport.enhanceApplication(grailsApplication, grailsApplication.mainContext)
}

Set getLoadedCodecs(TestRuntime runtime) {
(Set)runtime.getValueOrCreate("loadedCodecs", { new HashSet() })
}

void mockCodec(TestRuntime runtime, Class codecClass) {
Set loadedCodecs = getLoadedCodecs(runtime)
if (loadedCodecs.contains(codecClass)) {
return
}
Expand All @@ -104,7 +105,7 @@ class GrailsApplicationTestPlugin implements TestPlugin {

void mockForConstraintsTests(TestRuntime runtime, Class clazz, List instances) {
ConstraintEvalUtils.clearDefaultConstraints()
MockUtils.prepareForConstraintsTests(clazz, validationErrorsMap, instances ?: [], ConstraintEvalUtils.getDefaultConstraints(getGrailsApplication(runtime).config))
MockUtils.prepareForConstraintsTests(clazz, (Map)runtime.getValueOrCreate("validationErrorsMap", { new IdentityHashMap() }), instances ?: [], ConstraintEvalUtils.getDefaultConstraints(getGrailsApplication(runtime).config))
}

void defineBeans(TestRuntime runtime, Closure callable) {
Expand Down Expand Up @@ -186,6 +187,6 @@ class GrailsApplicationTestPlugin implements TestPlugin {
}

public void close(TestRuntime runtime) {

}
}
Expand Up @@ -40,6 +40,16 @@ class TestRuntime {
}
}

public Object getValueOrCreate(String name, Closure valueCreator) {
if(containsValueFor(name)) {
return getValue(name)
} else {
Object value = valueCreator.call()
putValue(name, value)
return value
}
}

public boolean containsValueFor(String name) {
registry.containsKey(name)
}
Expand Down

0 comments on commit ce5dfeb

Please sign in to comment.