1 package org
.codehaus
.groovy
.grails
.web
.servlet
.filter
3 import grails
.util
.GrailsWebUtil
5 import javax
.servlet
.Filter
6 import javax
.servlet
.ServletContext
7 import javax
.servlet
.http
.HttpServletRequest
8 import javax
.servlet
.http
.HttpServletResponse
10 import org
.codehaus
.groovy
.grails
.commons
.ApplicationAttributes
11 import org
.codehaus
.groovy
.grails
.commons
.DefaultGrailsApplication
12 import org
.codehaus
.groovy
.grails
.commons
.GrailsApplication
13 import org
.codehaus
.groovy
.grails
.plugins
.GrailsPluginManager
14 import org
.codehaus
.groovy
.grails
.plugins
.MockGrailsPluginManager
15 import org
.codehaus
.groovy
.grails
.plugins
.PluginManagerHolder
16 import org
.codehaus
.groovy
.grails
.support
.MockApplicationContext
17 import org
.codehaus
.groovy
.grails
.web
.context
.GrailsConfigUtils
18 import org
.codehaus
.groovy
.grails
.web
.context
.ServletContextHolder
19 import org
.codehaus
.groovy
.grails
.web
.mapping
.DefaultUrlMappingEvaluator
20 import org
.codehaus
.groovy
.grails
.web
.servlet
.mvc
.GrailsWebRequest
21 import org
.springframework
.mock
.web
.MockFilterConfig
22 import org
.springframework
.mock
.web
.MockServletContext
23 import org
.springframework
.web
.context
.WebApplicationContext
26 * Abstract test case to make testing servlet filters easier.
28 abstract class AbstractServletFilterTests
extends GroovyTestCase
{
30 ServletContext servletContext
31 GrailsWebRequest webRequest
32 HttpServletRequest request
33 HttpServletResponse response
34 WebApplicationContext appCtx
35 GrailsApplication application
36 GrailsPluginManager pluginManager
43 servletContext
= new MockServletContext();
44 webRequest
= GrailsWebUtil
.bindMockWebRequest()
45 request
= webRequest
.currentRequest
46 response
= webRequest
.currentResponse
47 appCtx
= new MockApplicationContext()
49 evaluator
= new DefaultUrlMappingEvaluator()
51 // Mimic AbstractGrailsPluginTests: create a new class loader
52 // and allow sub-classes to use for parsing and loading classes.
53 gcl
= new GroovyClassLoader()
58 ServletContextHolder
.setServletContext(null);
59 PluginManagerHolder
.setPluginManager(null)
62 protected void onSetup() {}
65 * Initialise the given filter so that it can tested.
67 protected final void initFilter(Filter filter
) {
68 filter
.init(new MockFilterConfig(this.servletContext
))
72 * Set up the mock parent application context and bind it to the
75 protected final void bindApplicationContext() {
76 this.servletContext
.setAttribute(ApplicationAttributes
.PARENT_APPLICATION_CONTEXT
, this.appCtx
);
80 * Set up the Grails application and bind it to the servlet context.
82 protected final void bindGrailsApplication() {
83 // Create a new Grails application with the stored Groovy class
85 this.application
= new DefaultGrailsApplication(gcl
.loadedClasses
, gcl
)
86 this.pluginManager
= new MockGrailsPluginManager(this.application
)
88 // Register the application instance and plugin manager with
89 // the mock application context.
90 this.appCtx
.registerMockBean(GrailsApplication
.APPLICATION_ID
, this.application
)
91 this.appCtx
.registerMockBean(GrailsPluginManager
.BEAN_NAME
, this.pluginManager
)
92 PluginManagerHolder
.setPluginManager(this.pluginManager
)
94 // Configure everything as if it's a running app.
95 GrailsConfigUtils
.configureWebApplicationContext(this.servletContext
, this.appCtx
)