on Monday, 29 December 2014
I was having some real headaches with debugging my unit tests today. With the introduction of forked execution came the breakage of Intellij debugging.

There are some rather long winded ways of applying remote debuggers, and Ted does a great job of breaking the problem down and offering a solution. However, in my rather tiny Grails application I was happy to sacrifice the benefits of forked execution - at least for my tests.

So, in BuildConfig.groovy I changed the following block:

/**** FROM ****/
grails.project.fork = [
test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]
/**** TO ****/
grails.project.fork = [
test: false,
run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]

This change tells Grails that when we run the test configuration it shouldn't run in forked mode - this is anything we would run using the 'test-app' Grails command.
on Friday, 21 November 2014
 java.lang.IllegalStateException: Method on class [com.willis.heimdall.Booking] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.  
      at com.willis.heimdall.BookingIntegSpec.test saving a booking to the db(BookingIntegSpec.groovy:24)  

 I recently had this error on one of my simple examples. A real facepalm moment when I look back in retrospect but ho hum, the fix is nice and easy.

Broken Code:
package com.willis.heimdall
import org.joda.time.DateTime
import spock.lang.Shared
import spock.lang.Specification
/**
* Integration tests for the Booking model
* @author Sion Williams
*/
class BookingIntegSpec extends Specification {
@Shared def today = new DateTime()
@Shared def todayPlusWeek = today.plusWeeks(1)
def 'test saving a booking to the db'() {
given: 'a new booking booking'
def booking = new Booking(name: 'my booking',
startTime: today.toDate(),
endTime: todayPlusWeek.toDate())
when: 'the booking is saved'
booking.save()
then: 'it can be successfully found in the database'
booking.errors.errorCount == 0
booking.id != null
Booking.get(booking.id).name == 'my booking'
Booking.get(booking.id).startTime == today.toDate()
Booking.get(booking.id).endTime == todayPlusWeek.toDate()
}
}
Fixed Code:
package com.willis.heimdall
import grails.test.mixin.TestFor
import org.joda.time.DateTime
import spock.lang.Shared
import spock.lang.Specification
/**
* Integration tests for the Booking model
* @author Sion Williams
*/
@TestFor(Booking)
class BookingIntegSpec extends Specification {
@Shared def today = new DateTime()
@Shared def todayPlusWeek = today.plusWeeks(1)
def 'test saving a booking to the db'() {
given: 'a new booking booking'
def booking = new Booking(name: 'my booking',
startTime: today.toDate(),
endTime: todayPlusWeek.toDate())
when: 'the booking is saved'
booking.save()
then: 'it can be successfully found in the database'
booking.errors.errorCount == 0
booking.id != null
Booking.get(booking.id).name == 'my booking'
Booking.get(booking.id).startTime == today.toDate()
Booking.get(booking.id).endTime == todayPlusWeek.toDate()
}
}

Note here that we have told Grails what we are testing with the @TestFor() annotation so that it can set up the relevant mocks and stubs in the background.
on Monday, 17 November 2014
Edit .git/config in the [receive] section:


# no rewriting history
denyNonFastForwards = true
# no deleting history
denyDeletes = true
# check object consistency
fsckObjects = true


on Monday, 13 October 2014

I'm pleased to announce the arrival of the Gradle Weblogic Plugin. Its been a while in the making, but I've finally got a stable version out with the 6 primary tasks. They are :
  • wlDeploy - Deploys or redeploys an application or module.
  • wlUndeploy - Stops the deployment unit and removes staged files from target servers.'
  • wlCancel - Attempt to cancel a running deployment task.
  • wlRedeploy - Redeploys a running application or part of a running application.
  • wlStart - Makes a stopped (inactive) application available to clients on target servers.
  • wlStop - Makes an application inactive and unavailable administration and client requests.

The latest version is currently:



You can find the source code and README at the following address:
Github - Gradle Weblogic Plugin

and the binaries are hosted at:
Bintray - Gradle Weblogic Plugin



In this example I'm using Xubuntu 14.04 LTS:


$ wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - && \
$ sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
$ sudo apt-get update && \
$ sudo apt-get install google-chrome-stable
view raw gistfile1.txt hosted with ❤ by GitHub
on Friday, 19 September 2014

Problem

I have an archive compiled with properties pre-defined within, but they need to change as I promote my build through the different stages of the SDLC. One example could be with a connection.xml that has URLs pointing to different databases.

Gradle already offers some great solutions such as keyword expansion and ant token replace, but both methods require the token to be in a predefined format. The expand() solution looks very similar to groovy string interpolation and looks for ${..}, whereas ant replace tokens looks for @..@.

Solution

The solution here was to pass a closure to filter which is called for every line of the filtered file. The closure performs some simple processing using the MapFilter() class and its MapReplace() method. An example is shown below:

buildscript {
apply plugin: 'groovy'
dependencies {
gradleApi()
}
}
ext{
earDir = "${projectDir}/src/dist"
explodedDir = "${buildDir}/exploded"
}
/**
* SINGLETON VIA METAPROGRAMMING
* http://groovy.codehaus.org/Singleton+Pattern
*/
class MapFilter {
String MapReplace( def input, Map map ){
try {
map.each { key, value ->
input = input.replace( key, value )
}
input
} catch ( e ) {
logger.error e.message
}
}
}
class MapFilterMetaClass extends MetaClassImpl {
private final static INSTANCE = new MapFilter()
MapFilterMetaClass() { super( MapFilter ) }
def invokeConstructor( Object[] arguments ) { return INSTANCE }
}
def registry = GroovySystem.metaClassRegistry
registry.setMetaClass( MapFilter, new MapFilterMetaClass() )
def myMap = ["devURL" : "productionURL",
"devProp1" : "productionProp1",
"devProp2" : "productionProp2",
"devProp3" : "productionProp3",
"devProp4" : "productionProp4"]
task unzipFilterWithMap(type: Copy){
def zipFile = file( "${earDir}/myZip.zip")
from zipTree(zipFile)
into explodedDir
filter { line ->
new MapFilter().MapReplace(line, myMap)
}
}

There's more ...

You may have noticed that this example also includes a lot of meta-programming. This was just my first dip into the world of meta-programming. In this example the metaclass simply intercepts every new call to map filter and returns the original map filter instance.
on Monday, 19 May 2014
myFile.toURL() deprecated The fix to this is quite simple:
def env = project.hasProperty('env') ? project.getProperty('env') : 'NULL'
logger.quiet "Loading configuration for environment '$env'"
def configFile = file("$rootDir/gradle/config/buildConfig.groovy")
def parsedConfig = new ConfigSlurper(env).parse(configFile.toURI().toURL())
def key = project.hasProperty( 'key') ? project.getProperty('key') : 'NULL'
allprojects {
ext.config = parsedConfig
}
view raw gistfile1.txt hosted with ❤ by GitHub