Skip to content

Instantly share code, notes, and snippets.

View OneTimeDataUnitTestMixin.groovy
class OneTimeDataUnitTestMixin extends GrailsUnitTestMixin {
def mockOneTimeData(spec, controller) {
spec.metaClass.otdData = [:]
spec.metaClass.otdId = 1
controller.metaClass.oneTimeData << { Closure dataSetup ->
dataSetup.delegate = spec.otdData
dataSetup()
spec.otdId
}
View OneTimeDataExample.groovy
@TestFor(MyController)
@TestMixin(OneTimeDataUnitTestMixin)
class MyControllerSpec extends Specification {
def setup() {
mockOneTimeData(this, controller)
}
def "should fail"() {
given:
@jurberg
jurberg / CleanTestApp.groovy
Created Oct 18, 2012
Grails clean-test-app script
View CleanTestApp.groovy
includeTargets << grailsScript("_GrailsClean")
includeTargets << grailsScript("RefreshDependencies")
includeTargets << grailsScript("TestApp")
target(cleanTestApp: "Clean, refresh dependencies, then test app") {
depends(cleanAll, refreshDependencies, 'default')
}
setDefaultTarget("cleanTestApp")
@jurberg
jurberg / expose-jmx.groovy
Created Oct 30, 2012
Exposing a few methods using the Grails JMX plugin
View expose-jmx.groovy
class MyService {
static expose = ['jmx']
static jmxmethods = ['getTimeout', 'setTimeout']
static jmxexpose = ['excludeMethods':
MyService.metaClass.methods*.name.findAll{
!jmxmethods.contains(it)
}.join(',')]
int timeout
@jurberg
jurberg / XmlToFlatFileTest.groovy
Created Nov 11, 2012
Convert XML to Flat File Test
View XmlToFlatFileTest.groovy
def sep = System.getProperty('line.separator')
def expected =
"bk101Gambardella Matthew " + sep +
"bk102Ralls Kim " + sep +
"bk103Corets Eva " + sep
void testParsing() {
def books = new XmlSlurper().parseText(data)
assertEquals expected, xmlToFlatFile(books, 'book', mapping)
}
@jurberg
jurberg / XmlToFlatFile.groovy
Created Nov 11, 2012
Convert XML to Flat File
View XmlToFlatFile.groovy
def xmlToFlatFile(xml, tag, mapping) {
def flatFile = new StringBuffer()
xml[tag].each { row ->
mapping.each { col ->
col.closure.delegate = [(tag): row]
def result = col.closure() ?: ''
flatFile << result
.trim()
.padRight(col.length)
.substring(0, col.length)
@jurberg
jurberg / XmlToFlatFileData.groovy
Created Nov 11, 2012
Convert XML To Flat File Test Data
View XmlToFlatFileData.groovy
def data = '''<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
@jurberg
jurberg / XmlToFlatFileMapping.groovy
Created Nov 11, 2012
Convert XML to Flat File Mapping
View XmlToFlatFileMapping.groovy
def mapping = [[closure: { book.@id.text() }, length: 5],
[closure: { book.author.text().split(',')[0] }, length: 15],
[closure: { book.author.text().split(',')[1] }, length: 10]]
@jurberg
jurberg / GenerateSources.groovy
Created Nov 28, 2012
Groovy script to generate JAXB classes from an XSD
View GenerateSources.groovy
// Add the following to BuildConfig.groovy:
// build('com.sun.xml.bind:jaxb-xjc:2.1')
// build('com.sun.xml.bind:jaxb-impl:2.1')
target('generateSources': "Generate JAXB classes from a schema") {
def classpath = grailsSettings
.getBuildDependencies()
.join(System.getProperty("path.separator"))
Ant.taskdef(name: "xjc",
classname: "com.sun.tools.xjc.XJCTask",
classpath: classpath)
@jurberg
jurberg / Jaxb-no-jxc.groovy
Created Dec 2, 2012
Generate JAXB classes without jaxb-jxc on your classpath
View Jaxb-no-jxc.groovy
Ant.java(classname: "com.sun.tools.internal.xjc.XJCFacade") {
arg(value: "-d")
arg(value: "src/java")
arg(value: "-p")
arg(value: "com.microsoft.books")
arg(value: "etc/schema/books.xsd")
}
You can’t perform that action at this time.