/
ControllersDynamicMethodsTests.groovy
165 lines (135 loc) · 5.83 KB
/
ControllersDynamicMethodsTests.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package org.grails.web.servlet.mvc
import grails.util.MockHttpServletResponse
import org.grails.plugins.DefaultGrailsPlugin
import org.grails.plugins.MockGrailsPluginManager
import org.grails.web.servlet.context.support.WebRuntimeSpringConfiguration
import org.grails.commons.test.AbstractGrailsMockTests
import grails.web.util.GrailsApplicationAttributes
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.validation.BindException
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.servlet.ModelAndView
class ControllersDynamicMethodsTests extends AbstractGrailsMockTests {
void onSetUp() {
gcl.parseClass """
@grails.artefact.Artefact('Controller')
class TestController {
def list = {}
}
"""
}
def request
def response
void runTest(Closure callable) {
def originalHandler = GroovySystem.metaClassRegistry.metaClassCreationHandle
try {
GroovySystem.metaClassRegistry.metaClassCreationHandle = new ExpandoMetaClassCreationHandle()
def mockManager = new MockGrailsPluginManager(ga)
ctx.registerMockBean("manager", mockManager)
def dependantPluginClasses = []
dependantPluginClasses << gcl.loadClass("org.grails.plugins.CoreGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.i18n.I18nGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.ServletsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.mapping.UrlMappingsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.controllers.ControllersGrailsPlugin")
def dependentPlugins = dependantPluginClasses.collect { new DefaultGrailsPlugin(it, ga)}
def springConfig = new WebRuntimeSpringConfiguration(ctx)
def servletContext = createMockServletContext()
springConfig.servletContext = servletContext
dependentPlugins*.doWithRuntimeConfiguration(springConfig)
dependentPlugins.each{ mockManager.registerMockPlugin(it); it.manager = mockManager }
def appCtx = springConfig.getApplicationContext()
mockManager.applicationContext = appCtx
servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, ctx)
ga.mainContext = appCtx
mockManager.doDynamicMethods()
request = new MockHttpServletRequest()
response = new MockHttpServletResponse()
RequestContextHolder.setRequestAttributes(new GrailsWebRequest(request, response, servletContext))
callable()
}
finally {
GroovySystem.metaClassRegistry.metaClassCreationHandle = originalHandler
RequestContextHolder.setRequestAttributes(null)
}
}
void testFlashObject() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
testCtrl.flash.test = "hello"
assertEquals "hello", testCtrl.flash.test
}
}
void testParamsObject() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
testCtrl.params.test = "hello"
assertEquals "hello", testCtrl.params.test
}
}
void testSessionObject() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
testCtrl.session.test = "hello"
assertEquals "hello", testCtrl.session.test
}
}
void testRequestObjects() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
assertNotNull(testCtrl.request)
assertNotNull(testCtrl.response)
assertNotNull(testCtrl.servletContext)
}
}
void testErrorsObject() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
def errors = new BindException(this, "test")
testCtrl.errors = errors
assertEquals errors, testCtrl.errors
}
}
void testModelAndViewObject() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
def mav = new ModelAndView("myView",[hello:"world"])
testCtrl.modelAndView = mav
assertEquals mav, testCtrl.modelAndView
}
}
// the following tests just test the the method is invoked successfully
// we test the actual functionally of each method in separate tests (eg. RenderMethodTests)
/*void testRedirectMethod() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
testCtrl.redirect(controller:"blah",action:"list")
}
}*/
void testRenderMethod() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
testCtrl.render "test"
}
}
Integer test1
void testBindDataMethod() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
testCtrl.bindData(this, [test1:"1"])
assertEquals 1, test1
}
}
void testActionInfoMethods() {
runTest {
def testCtrl = ga.getControllerClass("TestController").newInstance()
def webRequest = RequestContextHolder.currentRequestAttributes()
webRequest.actionName = "action"
webRequest.controllerName = "contrl"
assertEquals "action", testCtrl.actionName
assertEquals "contrl", testCtrl.controllerName
assertEquals "/contrl/action", testCtrl.actionUri
assertEquals "/contrl", testCtrl.controllerUri
}
}
}