GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / groovy / grails / test / GroovyPagesTestCase.groovy
blobb91bbc5603dece9e8866ef0c54ebabbf5836a0d2
1 /* Copyright 2004-2005 the original author or authors.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package grails.test
17 import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
18 import org.springframework.web.context.request.RequestContextHolder
20 /**
21 * A test harness that eases testing of GSP and tag libraries for Grails
23 * @author Graeme Rocher
25 class GroovyPagesTestCase extends GroovyTestCase {
26 /**
27 * The GroovyPagesTemplateEngine which gets wired into this GSP
29 GroovyPagesTemplateEngine groovyPagesTemplateEngine
31 /**
32 * Sets the controller name to use. Should be called to override the defaut "test" value
34 void setControllerName(String name) {
35 RequestContextHolder.currentRequestAttributes().controllerName = name
38 /**
39 * Asserts the output of a given template against the specified expected value
41 * @param expected The expected output
42 * @param template A snippet of GSP
43 * @param params An optional parameter that allows variables to be placed in the binding of the GSP
44 * @param transform An optional parameter that allows the specification of a closure to transform the passed StringWriter
46 def assertOutputEquals(expected, template, params = [:], Closure transform = { it.toString() }) {
47 def webRequest = RequestContextHolder.currentRequestAttributes()
48 def engine = groovyPagesTemplateEngine
50 assert engine
51 def t = engine.createTemplate(template, "test_"+ System.currentTimeMillis())
53 def w = t.make(params)
55 def sw = new StringWriter()
56 def out = new PrintWriter(sw)
57 webRequest.out = out
58 w.writeTo(out)
60 assertEquals expected, transform(sw)
63 /**
64 * Applies a GSP template and returns its output as a String
66 * @param template The GSP template
67 * @param params An optional parameter that allows the specification of the binding
69 def applyTemplate(template, params = [:] ) {
70 def webRequest = RequestContextHolder.currentRequestAttributes()
71 def engine = groovyPagesTemplateEngine
73 assert engine
74 def t = engine.createTemplate(template, "test_"+ System.currentTimeMillis())
76 def w = t.make(params)
78 def sw = new StringWriter()
79 def out = new PrintWriter(sw)
80 webRequest.out = out
81 w.writeTo(out)
83 return sw.toString()