Skip to content

Commit

Permalink
First cut of grails support for 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ldaley committed Jun 2, 2011
1 parent 57f61c2 commit d16dde3
Show file tree
Hide file tree
Showing 42 changed files with 2,191 additions and 2 deletions.
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ include 'spock-specs'
include 'spock-spring'
include 'spock-tapestry'
include 'spock-unitils'


include 'spock-grails-support'
include 'spock-grails'
34 changes: 34 additions & 0 deletions spock-grails-support/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apply from: profile("publishMaven")

description = "Spock Framework - Grails Plugin Support"
grailsVersion = "1.4.0.M1"

repositories {
mavenRepo name: "Grails Repo", urls: "http://repo.grails.org/grails/libs-releases-local"
}

dependencies {
compile project(":spock-core")

["core", "bootstrap", "web", "test", "plugin-testing", "plugin-converters"].each { module ->
compile("org.grails:grails-$module:$grailsVersion") {
transitive = false
}
}

["core", "context", "context-support", "beans", "webmvc", "web"].each { module ->
compile("org.springframework:spring-$module:3.0.5.RELEASE") {
transitive = false
}
}

compile("org.apache.ant:ant-junit:1.8.2") {
transitive = false
}

compile("javax.servlet:servlet-api:2.5")
}

modifyPom {
it.dependencies.findAll { it.groupId != "org.spockframework" }*.scope = "provided"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package grails.plugin.spock

import grails.test.MockUtils
import grails.util.GrailsNameUtils
import groovy.xml.StreamingMarkupBuilder

/**
* Support class for writing unit tests for controllers. Its main job
* is to mock the various properties and methods that Grails injects
* into controllers. By default it determines what controller to mock
* based on the name of the test, but this can be overridden by one
* of the constructors.
*
* @author Graeme Rocher
* @author Peter Ledbrook
*/
class ControllerSpec extends MvcSpec {
def setup() {
webRequest.controllerName = GrailsNameUtils.getLogicalPropertyName(controllerClass.name, "Controller")
}

def provideMvcClassUnderTest() {
findClassUnderTestConventiallyBySuffix('Controller')
}

def initializeMvcMocking(Class classUnderTest) {
mockController(classUnderTest)
}

def getControllerClass() {
classUnderTest
}

def getController() {
instanceUnderTest
}

def getForwardArgs() { instanceUnderTest.forwardArgs }

def getRedirectArgs() { instanceUnderTest.redirectArgs }

def getChainArgs() { instanceUnderTest.chainArgs }

void reset() {
super.reset()
redirectArgs.clear()
forwardArgs.clear()
chainArgs.clear()
}

protected mockCommandObject(Class clazz) {
registerMetaClass(clazz)
MockUtils.mockCommandObject(clazz, errorsMap)
}

protected void setXmlRequestContent(content) {
setXmlRequestContent("UTF-8", content)
}

protected void setXmlRequestContent(String encoding, content) {
mockRequest.contentType = "application/xml; charset=$encoding"

if (content instanceof Closure) {
def xml = new StreamingMarkupBuilder(encoding: encoding).bind(content)
def out = new ByteArrayOutputStream()
out << xml

mockRequest.contentType = "application/xml; charset=$encoding"
mockRequest.content = out.toByteArray()
} else {
mockRequest.content = content.getBytes(encoding)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package grails.plugin.spock

import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
import org.codehaus.groovy.grails.web.util.GrailsPrintWriter
import org.springframework.web.context.request.RequestContextHolder

class GroovyPagesSpec extends IntegrationSpec {
GroovyPagesTemplateEngine groovyPagesTemplateEngine // autowired

String template = ''
Map params = [:]

def getRequest() {
RequestContextHolder.currentRequestAttributes()
}

void setControllerName(String name) {
getRequest().controllerName = name
}

def getOutput() {
assert groovyPagesTemplateEngine

def webRequest = getRequest()

def t = groovyPagesTemplateEngine.createTemplate(template, "test_"+ System.currentTimeMillis())
def w = t.make(params)

def sw = new StringWriter()
def out = new GrailsPrintWriter(sw)
webRequest.out = out
w.writeTo(out)
sw.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package grails.plugin.spock

import org.codehaus.groovy.grails.commons.ApplicationHolder
import org.codehaus.groovy.grails.test.support.GrailsTestAutowirer
import org.codehaus.groovy.grails.test.support.GrailsTestTransactionInterceptor
import org.codehaus.groovy.grails.test.support.GrailsTestRequestEnvironmentInterceptor
import org.codehaus.groovy.grails.test.support.ControllerNameExtractor

import grails.plugin.spock.test.GrailsSpecTestType

import spock.lang.Specification
import spock.lang.Shared
import spock.lang.Stepwise

class IntegrationSpec extends Specification {
@Shared private applicationContext = ApplicationHolder.application.mainContext
@Shared private autowirer = new GrailsTestAutowirer(applicationContext)

@Shared private perSpecTransactionInterceptor
@Shared private perSpecRequestEnvironmentInterceptor

private perMethodTransactionInterceptor = null
private perMethodRequestEnvironmentInterceptor = null

def setupSpec() {
if (isStepwiseSpec()) {
perSpecTransactionInterceptor = initTransaction()
perSpecRequestEnvironmentInterceptor = initRequestEnv()
}

autowirer.autowire(this)
}

def setup() {
if (!isStepwiseSpec()) {
perMethodTransactionInterceptor = initTransaction()
perMethodRequestEnvironmentInterceptor = initRequestEnv()
}

autowirer.autowire(this)
}

def cleanup() {
perMethodRequestEnvironmentInterceptor?.destroy()
perMethodTransactionInterceptor?.destroy()
}

def cleanupSpec() {
perSpecRequestEnvironmentInterceptor?.destroy()
perSpecTransactionInterceptor?.destroy()
}

private boolean isStepwiseSpec() {
getClass().isAnnotationPresent(Stepwise)
}

private initTransaction() {
def interceptor = new GrailsTestTransactionInterceptor(applicationContext)
if (interceptor.isTransactional(this)) interceptor.init()
interceptor
}

private initRequestEnv() {
def interceptor = new GrailsTestRequestEnvironmentInterceptor(applicationContext)
def controllerName = ControllerNameExtractor.extractControllerNameFromTestClassName(
this.class.name, GrailsSpecTestType.TEST_SUFFIXES as String[])
interceptor.init(controllerName ?: GrailsTestRequestEnvironmentInterceptor.DEFAULT_CONTROLLER_NAME)
interceptor
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package grails.plugin.spock

import org.codehaus.groovy.grails.commons.ApplicationAttributes
import org.codehaus.groovy.grails.support.MockApplicationContext
import org.codehaus.groovy.grails.web.pages.DefaultGroovyPagesUriService
import org.codehaus.groovy.grails.web.pages.GroovyPagesUriService
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.web.context.request.RequestContextHolder
import spock.lang.Shared

/**
* Support class for writing unit tests for controllers. Its main job
* is to mock the various properties and methods that Grails injects
* into controllers. By default it determines what controller to mock
* based on the name of the test, but this can be overridden by one
* of the constructors.
*
* @author Graeme Rocher
* @author Peter Ledbrook
*/
abstract class MvcSpec extends UnitSpec {
@Shared classUnderTest
def instanceUnderTest

protected webRequest

abstract provideMvcClassUnderTest()
abstract initializeMvcMocking(Class classUnderTest)

def setupSpec() {
classUnderTest = provideMvcClassUnderTest()
}

def findClassUnderTestConventiallyBySuffix(suffix) {
def matcher = getClass().name =~ /^([\w\.]*?[A-Z]\w*?${suffix})\w+/
if (matcher) {
def classUnderTestName = matcher[0][1]
classUnderTest = Thread.currentThread().contextClassLoader.loadClass(classUnderTestName)
} else {
throw new RuntimeException("Can not find classUnderTest conventionally for ${this.class.name} as it doesn't contain $suffix".toString())
}
}

def getMockRequest() { instanceUnderTest.request }
def getMockResponse() { instanceUnderTest.response }
def getMockSession() { instanceUnderTest.session }

def getRenderArgs() { instanceUnderTest.renderArgs }

def getMockParams() { instanceUnderTest.params }
def getMockFlash() { instanceUnderTest.flash }

def setup() {
initializeMvcMocking(classUnderTest)
instanceUnderTest = classUnderTest.newInstance()

MockApplicationContext ctx = new MockApplicationContext()
ctx.registerMockBean(GroovyPagesUriService.BEAN_ID, new DefaultGroovyPagesUriService())
mockRequest.servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, ctx)

webRequest = new GrailsWebRequest(
mockRequest,
mockResponse,
mockRequest.servletContext
)

mockRequest.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest)
RequestContextHolder.setRequestAttributes(webRequest)
}

void reset() {
mockRequest.clearAttributes()
mockRequest.removeAllParameters()
mockResponse.committed = false
mockSession.clearAttributes()
mockSession.setNew(true)

renderArgs.clear()

mockParams.clear()
mockFlash.clear()
}
}
Loading

0 comments on commit d16dde3

Please sign in to comment.