/
AbstractGrailsTagTests.groovy
416 lines (334 loc) · 16.3 KB
/
AbstractGrailsTagTests.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package org.codehaus.groovy.grails.web.taglib
import grails.util.GrailsWebUtil
import grails.web.CamelCaseUrlConverter
import grails.web.UrlConverter
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
import org.codehaus.groovy.grails.commons.*
import org.codehaus.groovy.grails.commons.spring.WebRuntimeSpringConfiguration
import org.codehaus.groovy.grails.plugins.DefaultGrailsPlugin
import org.codehaus.groovy.grails.plugins.GrailsPluginManager
import org.codehaus.groovy.grails.plugins.MockGrailsPluginManager
import org.codehaus.groovy.grails.support.MockApplicationContext
import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.codehaus.groovy.grails.web.pages.*
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.codehaus.groovy.grails.web.sitemesh.GSPSitemeshPage
import org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.MessageSource
import org.springframework.context.support.StaticMessageSource
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
import org.springframework.mock.web.MockHttpServletRequest
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.mock.web.MockServletContext
import org.springframework.ui.context.Theme
import org.springframework.ui.context.ThemeSource
import org.springframework.ui.context.support.SimpleTheme
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.servlet.DispatcherServlet
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
import org.springframework.web.servlet.support.JstlUtils
import org.springframework.web.servlet.theme.SessionThemeResolver
import org.w3c.dom.Document
import com.opensymphony.module.sitemesh.RequestConstants
abstract class AbstractGrailsTagTests extends GroovyTestCase {
MockServletContext servletContext
GrailsWebRequest webRequest
MockHttpServletRequest request
MockHttpServletResponse response
ApplicationContext ctx
def originalHandler
ApplicationContext appCtx
GrailsApplication ga
GrailsPluginManager mockManager
GroovyClassLoader gcl = new GroovyClassLoader()
boolean enableProfile = false
GrailsApplication grailsApplication
StaticMessageSource messageSource
DocumentBuilder domBuilder
XPath xpath
def withConfig(String text, Closure callable) {
def config = new ConfigSlurper().parse(text)
try {
buildMockRequest(config)
callable()
}
finally {
RequestContextHolder.setRequestAttributes(null)
}
}
GrailsWebRequest buildMockRequest(ConfigObject config) throws Exception {
ga.config = config
servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, appCtx)
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx)
GrailsWebRequest request = GrailsWebUtil.bindMockWebRequest(appCtx)
initThemeSource(request.getCurrentRequest(), messageSource)
return request
}
def profile(String name, Closure callable) {
if (!enableProfile) return callable.call()
def now = System.currentTimeMillis()
for (i in 0..100) {
callable.call()
}
println "$name took ${System.currentTimeMillis()-now}ms"
}
def withTag(String tagName, Writer out, Closure callable) {
def result = null
runTest {
def webRequest = RequestContextHolder.currentRequestAttributes()
webRequest.out = out
def mockController = grailsApplication.getControllerClass("MockController").newInstance()
request.setAttribute(GrailsApplicationAttributes.CONTROLLER, mockController)
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver())
def tagLibrary = grailsApplication.getArtefactForFeature(TagLibArtefactHandler.TYPE, "g:" + tagName)
if (!tagLibrary) {
fail("No tag library found for tag $tagName")
}
def go = tagLibrary.newInstance()
if (go.properties.containsKey("grailsUrlMappingsHolder")) {
go.grailsUrlMappingsHolder = appCtx.grailsUrlMappingsHolder
}
if (go instanceof ApplicationContextAware) {
go.applicationContext = appCtx
}
GroovyPageOutputStack stack=GroovyPageOutputStack.createNew(out)
println "calling tag '${tagName}'"
def tag = go.getProperty(tagName)
def tagWrapper = { Object[] args ->
// the first or second arg may be a Map
// wrap Map args in GroovyPageAttributes
def newArgs = []
if (args?.length > 0) {
args.each {arg ->
if (arg instanceof Map && (!(arg instanceof GroovyPageAttributes))) {
newArgs << new GroovyPageAttributes(arg)
}
else {
newArgs << arg
}
}
}
tag.call(*newArgs)
}
result = callable.call(tagWrapper)
}
return result
}
protected void onSetUp() {}
protected void setUp() throws Exception {
domBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
xpath = XPathFactory.newInstance().newXPath()
originalHandler = GroovySystem.metaClassRegistry.metaClassCreationHandle
GroovySystem.metaClassRegistry.metaClassCreationHandle = new ExpandoMetaClassCreationHandle()
onSetUp()
grailsApplication = new DefaultGrailsApplication(gcl.loadedClasses, gcl)
ga = grailsApplication
def mainContext = new MockApplicationContext()
mainContext.registerMockBean UrlConverter.BEAN_NAME, new CamelCaseUrlConverter()
ga.mainContext = mainContext
grailsApplication.initialise()
mockManager = new MockGrailsPluginManager(grailsApplication)
mockManager.registerProvidedArtefacts(grailsApplication)
def mockControllerClass = gcl.parseClass("class MockController { def index = {} } ")
ctx = new MockApplicationContext()
ctx.servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, ctx)
grailsApplication.setApplicationContext(ctx)
ctx.registerMockBean(GrailsApplication.APPLICATION_ID, grailsApplication)
ctx.registerMockBean("pluginManager", mockManager)
grailsApplication.addArtefact(ControllerArtefactHandler.TYPE, mockControllerClass)
messageSource = new StaticMessageSource()
ctx.registerMockBean("manager", mockManager)
ctx.registerMockBean("messageSource", messageSource)
ctx.registerMockBean("grailsApplication",grailsApplication)
ctx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService())
def dependantPluginClasses = []
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.CoreGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.CodecsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.i18n.I18nGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.web.ServletsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.web.mapping.UrlMappingsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.web.ControllersGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.web.GroovyPagesGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.codehaus.groovy.grails.plugins.log4j.Log4jGrailsPlugin")
def dependentPlugins = dependantPluginClasses.collect { new DefaultGrailsPlugin(it, grailsApplication)}
dependentPlugins.each{ mockManager.registerMockPlugin(it); it.manager = mockManager }
mockManager.registerProvidedArtefacts(grailsApplication)
def springConfig = new WebRuntimeSpringConfiguration(ctx)
webRequest = GrailsWebUtil.bindMockWebRequest(ctx)
onInit()
JstlUtils.exposeLocalizationContext webRequest.getRequest(),null
servletContext = webRequest.servletContext
ServletContextHolder.servletContext = servletContext
springConfig.servletContext = servletContext
dependentPlugins*.doWithRuntimeConfiguration(springConfig)
springConfig.addSingletonBean("messageSource", StaticMessageSource)
appCtx = springConfig.getApplicationContext()
ctx.servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, appCtx)
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx)
mockManager.applicationContext = appCtx
GroovySystem.metaClassRegistry.removeMetaClass(String)
GroovySystem.metaClassRegistry.removeMetaClass(Object)
// Why are the TagLibClasses removed?
//grailsApplication.tagLibClasses.each { tc -> GroovySystem.metaClassRegistry.removeMetaClass(tc.clazz)}
mockManager.doDynamicMethods()
request = webRequest.currentRequest
initThemeSource(request, messageSource)
request.characterEncoding = "utf-8"
response = webRequest.currentResponse
}
private def initThemeSource(request, StaticMessageSource messageSource) {
request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, new MockThemeSource(messageSource))
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new SessionThemeResolver())
}
protected void tearDown() {
// Clear the page cache in the template engine since it's
// static and likely to cause tests to interfere with each other.
appCtx.groovyPagesTemplateEngine.clearPageCache()
RequestContextHolder.setRequestAttributes(null)
GroovySystem.metaClassRegistry.setMetaClassCreationHandle(originalHandler)
onDestroy()
ServletContextHolder.servletContext = null
}
protected void onInit() {}
protected void onDestroy() {}
protected MockServletContext createMockServletContext() { new MockServletContext() }
protected MockApplicationContext createMockApplicationContext() { new MockApplicationContext() }
protected Resource[] getResources(String pattern) {
new PathMatchingResourcePatternResolver().getResources(pattern)
}
void runTest(Closure callable) {
callable.call()
}
void printCompiledSource(template, params = [:]) {
def text = getCompiledSource(template, params)
println "----- GSP SOURCE -----"
println text
}
def getCompiledSource(template, params = [:]) {
def engine = appCtx.groovyPagesTemplateEngine
assert engine
def t = engine.createTemplate(template, "test_"+ System.currentTimeMillis())
def w = t.make(params)
w.showSource = true
def sw = new StringWriter()
def out = new PrintWriter(sw)
webRequest.out = out
w.writeTo(out)
String text = sw.toString()
}
def assertCompiledSourceContains(expected, template, params = [:]) {
def text = getCompiledSource(template, params)
return text.indexOf(expected) > -1
}
void assertOutputContains(expected, template, params = [:]) {
def result = applyTemplate(template, params)
assertTrue "Output does not contain expected string [$expected]. Output was: ${result}", result.indexOf(expected) > -1
}
void assertOutputNotContains(expected, template, params = [:]) {
def result = applyTemplate(template, params)
assertFalse "Output should not contain the expected string [$expected]. Output was: ${result}", result.indexOf(expected) > -1
}
/**
* Compares the output generated by a template with a string.
* @param expected The string that the template output is expected
* to match.
* @param template The template to run.
* @param params A map of variables to pass to the template - by
* default an empty map is used.
* @param transform A closure that is passed a StringWriter instance
* containing the output generated by the template. It is the result
* of this transformation that is actually compared with the expected
* string. The default transform simply converts the contents of the
* writer to a string.
*/
void assertOutputEquals(expected, template, params = [:], Closure transform = { it.toString() }) {
def engine = appCtx.groovyPagesTemplateEngine
//printCompiledSource(template)
assert engine
def t = engine.createTemplate(template, "test_"+ System.currentTimeMillis())
t.allowSettingContentType = true
/*
println "------------HTMLPARTS----------------------"
t.metaInfo.htmlParts.eachWithIndex {it, i -> print "htmlpart[${i}]:\n>${it}<\n--------\n" }
*/
def w = t.make(params)
MockHttpServletResponse mockResponse = new MockHttpServletResponse()
mockResponse.setCharacterEncoding("UTF-8")
GSPResponseWriter writer = GSPResponseWriter.getInstance(mockResponse)
webRequest.out = writer
w.writeTo(writer)
writer.flush()
assertEquals expected, transform(mockResponse.contentAsString)
}
def applyTemplate(template, params = [:], target = null, String filename = null) {
GroovyPagesTemplateEngine engine = appCtx.groovyPagesTemplateEngine
printCompiledSource(template)
assert engine
def t = engine.createTemplate(template, filename ?: "test_"+ System.currentTimeMillis())
def w = t.make(params)
if (!target) {
target = new FastStringWriter()
}
webRequest.out = target
w.writeTo(target)
target.flush()
return target.toString()
}
/**
* Applies sitemesh preprocessing to a template
*/
String sitemeshPreprocess(String template) {
def preprocessor=new SitemeshPreprocessor()
preprocessor.addGspSitemeshCapturing(template)
}
String applyLayout(String layout, String template, Map params=[:]) {
def page = new GSPSitemeshPage()
request.setAttribute(GrailsPageFilter.GSP_SITEMESH_PAGE, page)
applyTemplate(template, params)
request.removeAttribute(GrailsPageFilter.GSP_SITEMESH_PAGE)
try {
request.setAttribute(RequestConstants.PAGE, page)
return applyTemplate(layout, params,null, "/layouts/test_"+System.currentTimeMillis())
}
finally {
request.removeAttribute(RequestConstants.PAGE)
}
}
/**
* Parses the given XML text and creates a DOM document from it.
*/
protected final Document parseText(String xml) {
return domBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")))
}
/**
* Asserts that the given XPath expression matches at least one
* node in the given DOM document.
*/
protected final void assertXPathExists(Document doc, String expr) {
assertTrue xpath.evaluate(expr, doc, XPathConstants.BOOLEAN)
}
/**
* Asserts that the given XPath expression matches no nodes in the
* given DOM document.
*/
protected final void assertXPathNotExists(Document doc, String expr) {
assertFalse xpath.evaluate(expr, doc, XPathConstants.BOOLEAN)
}
}
class MockThemeSource implements ThemeSource {
private messageSource
MockThemeSource(MessageSource messageSource) {
this.messageSource = messageSource
}
Theme getTheme(String themeName) { new SimpleTheme(themeName, messageSource) }
}