Skip to content

Instantly share code, notes, and snippets.

View ddelponte's full-sized avatar

Dean Del Ponte ddelponte

View GitHub Profile
@ddelponte
ddelponte / controllerAndServiceValidationHandling.groovy
Created December 5, 2012 16:14
Grails Service: Validation Errors and Rollback
Validation Errors and Rollback
A common use case is to rollback a transaction if there are validation errors. For example consider this service:
import grails.validation.ValidationException
class AuthorService {
void updateAge(id, int age) {
def author = Author.get(id)
author.age = age
if (!author.validate()) {
@ddelponte
ddelponte / TagLibRenderVerification
Created July 19, 2016 14:36
Mock out the render method on a tagLib so that it's easy to verify the template and model utilized by the tagLib when rendering
// Setup
private mockTagLibRender() {
tagLib.metaClass.render = { Map attrs ->
render = attrs
}
}
// Utilization
given:
mockTagLibRender()
@ddelponte
ddelponte / remoteTomcatRestart.groovy
Last active February 4, 2016 11:39
Restart remote tomcat instance
/**
* This script restarts a remote tomcat instance
* Usage: groovy restartRemoteTomcat.groovy or groovy restartRemoteTomcat.groovy remoteServer remoteUser pathToTomcatBinDirectory
* Example: groovy restartRemoteTomcat myServer username "/home/username/apache-tomcat-7.0.39/bin/"
*/
@Grapes([
@Grab('org.apache.ant:ant:1.8.3'),
@Grab(group = 'ant', module = 'ant-jsch', version = '1.6.5'),
@Grab(group = 'com.jcraft', module = 'jsch', version = '0.1.48'),
@ddelponte
ddelponte / runAsyncMock.groovy
Created September 12, 2013 12:29
runAsync mocking
service.metaClass.runAsync = { it() }
// The first line basically says ‘take the code within the runAsync closure, and just execute it’.
// http://fbflex.wordpress.com/2011/10/26/grails-quick-tip-testing-spock-interactions-wrapped-by-the-executor-plugin/
@ddelponte
ddelponte / ServiceMock.groovy
Created August 9, 2013 19:48
How to mockout a service depended on by a controller. For more info, please see: http://refaktor.blogspot.com/2012/08/how-to-use-mocks-in-controller-tests.html
TwitterReaderService twitterReaderServiceMock = Mock(TwitterReaderService)
def setup() {
controller.twitterReaderService = twitterReaderServiceMock
}
@ddelponte
ddelponte / grailsMimeUtility.groovy
Created July 31, 2013 12:49
How to get mimeType from file extension in Grails. Be sure to inject grailsMimeUtility into your controller or service before calling it.
def mimeType = grailsMimeUtility.getMimeTypeForExtension(extension)
println mimeType.name
@ddelponte
ddelponte / ErrorMessage.groovy
Created April 30, 2013 12:07
How to get the error message from a ValidationException
def errorMessages = ex.errors.collect { g.message(error:it) }
@ddelponte
ddelponte / git goodness
Created April 9, 2013 12:42
Push a new local branch to a new remote branch
git push -u origin your_branch
@ddelponte
ddelponte / Associated inputs on form
Created March 26, 2013 19:31
Handling associated form inputs in Grails
// A form with associated inputs
<g:form action="seed">
<g:each in="${teams}" status="i" var="team">
<input type="hidden" name="teams.${i}.id" value="${team.id}">
<input type="text" size="2" name="teams.${i}.seed" value="${team.seed}">
</g:each>
@ddelponte
ddelponte / rowCount.groovy
Created March 15, 2013 17:45
Groovy SQL example of counting rows
def countRows = db.firstRow("select count(*) as numberOfRows from languages")
assert 4 == countRows.numberOfRows