Tuesday, 7 December 2010

Testing cron expressions in Grails Jobs

One big problem I have is I can never remember what the cron expression is supposed to be and once I've got it in the code if it is actually correct.

Very simple test below. I create a CronTrigger and pass in the expression from the job. I then check that it will fire by passing in a Calendar object. The only key here is you can't test something in the past hence why I am adding another day to the LocalDate object.

void testJobSchedule() {
def trigger = new CronTrigger("name", "group", JobName.cronExpression)
assertTrue "job should fire at 1:15", trigger.willFireOn(time(1, 15))
}

private Calendar time(int hour, int minute) {
new LocalDate().plusDays(1).toDateTime(new LocalTime(hour, minute, 0, 0)).toCalendar(Locale.UK)
}

Hopefully this helps someone out.


Friday, 19 November 2010

Unexpected search results

Bit of friday fun - just tried searching for Node GString, and Google helpfully suggested...

Did you mean: nude g string

Using Liquibase DropAll Automagically 2

A while back gus posted about how to get liquibase to drop all on application start up. He had to jump through some hoops because dependsOn wasn't working properly. This seems to be OK now so I think the process can be simplified a bit...

Assuming the changelog is specified in grails-app/conf/liquibase/master.xml

resources.groovy
if (config.liquibase.on) {
liquibaseDropAll(LiquibaseDropAll) { bean ->
dataSource = dataSource
changeLog = "classpath:liquibase/master.xml"
bean.initMethod = 'init'
}

liquibase(SpringLiquibase) { bean ->
dataSource = dataSource
changeLog = "classpath:liquibase/master.xml"
bean.dependsOn = ['liquibaseDropAll']
}
}

LiquibaseDropAll.groovy
import liquibase.spring.SpringLiquibase
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class LiquibaseDropAll extends SpringLiquibase {

void init() {
if (ConfigurationHolder.config.liquibase.dropAll) {
super.createLiquibase(dataSource.connection).dropAll()
}
}
}

Disclaimer: I've only just started using it so it may still thow up some gotchas.

Sunday, 17 October 2010

Reducing backend load

I've recently been looking at alternatives to F5 and I remember from this year's QCon a lot of people mentioning Zeus so I thought I'd give them a look.

So far they look promising; being able to run Zeus on your own generic hardware could be a cost saving in the long run. For example we had to buy some spare PSUs out of fear we wouldn't be able to buy them in the future.

One feature I really like the sound of is the "webcache!refresh_time". It smooths out the load to your backend by only sending one request to your appserver while serving the rest from cache. For sites that must have low cache times this makes a lot of sense. If you get 30 requests per second for an item, you only send one request to your backend as it expires from cache.

Turns out F5 also have a similar sounding feature called Fast Cache but for us it would be an additional module (read additional cost).

If anyone has experience with either I'd love to hear their thoughts.

Thursday, 23 September 2010

Stubbing g.message

It's easy to mock grails taglibs using gmock and hamcrest. You can do something like...

...
def g

void setup() {
g = mock()
mock(tagLib).getG().returns(g).stub()
}

void someTest() {
g.message(hasEntry('code', 'foo.bar')).returns('FOO BAR')
String result
play {
result = tagLib.someMethod([:]).toString()
}
assert result.contains('FOO BAR')
}
}

But when you've got a lot of calls to g.message this can become noisy and usually ends up being repeated in multiple tests. One option is to be relax the argument matching and switch to using a stub.

g.message(instanceOf(Map)).returns('FOO BAR').stub()

This is fine for the tests where you don't really care about the message, but too loose for the ones you do. I'm playing with the an alternative which gives you both stubbing and the option of explicitly asserting g.message was called with the correct arguments. Be warned though it may raise a WTF exception on first glance.

...
def g

void setup() {
g = mock()
mock(tagLib).getG().returns(g).stub()
stubMessages(g)
}

void someTest() {
String result
play {
result = tagLib.someMethod([:]).toString()
}
assert result.contains('code:foo.bar')
}
}

class MessageMatcher extends BaseMatcher {

String code = ''

static void stubMessages(def g) {
MessageMatcher matcher = new MessageMatcher()
g.message(matcher).returns(matcher).stub()
}

boolean matches(Object o) {
code = ((Map) o).containsKey('code') ? o.code : ''
return true;
}

void describeTo(Description description) { }

String toString() {
return "code:${code}"
}
}

The tricky bit was getting the g.message stub to return a value derived from the matcher arguments. I couldn't just do

g.message(matcher).returns(matcher.code).stub()

because matcher.code won't have a value at this point. The solution is to override the matcher's toString() method to return the code and rely on groovy / grails invoking toString() when adding the matcher to the output. This certainly violates the rule of least surprises, but I think I'm OK with that if it reduces duplication and the noise level of my tests - at least until I find a better way.

Friday, 17 September 2010

Clean TagLib Tests

Despite my best efforts I've always found it hard to write clean taglib tests. Now thanks to Spock and GroovyShell things are getting easier...

void setup() {
mockDomain Invoice
}

def "Attachments icon has correct markup"() {
given:
Invoice invoice = new InvoiceBuilder().buildAndSave()

when:
renderAttachmentsIcon([target: invoice])

then:
valueOf('img.@id') == "toggle-attachments-${invoice.id}"
valueOf('script') == "\$('#toggle-attachments-${invoice.id}').bind('click', Books.attachments.toggle);"
}

Because I want to use GPath make assertions about the resulting HTML I've overriden TagLibSpec's methodMissing closure as follows...

def methodMissing(String name, args) {
String html = super.methodMissing(name, args)
createDocument(html)
}

void createDocument(String html) {
String xml = "<results>${html}</results>"
document = new XmlSlurper().parseText(xml)
}

And added helper methods for evaluating GPath expressions...

String valueOf(String gPath) {
evaluate(gPath).text()
}

GPathResult evaluate(String gPath) {
new GroovyShell(new Binding(document: document)).evaluate("document.${gPath}")
}

There's a little bit more to this story unfortunately...

Firstly my taglibs use the MarkupBuilder and when run from my Spock test this only outputs the opening tag of the first element I genererate. I haven't had a chance to look into this yet, but a workaround is to add "out << '' to the end of the taglib method

Secondly Spock interactions aren't yet as powerful as gmock, so I usually end up adding code to (g)mock grails taglibs.

Thirdly another one of my tests outputs &nbsp; in the HTML which causes the XML parsing to barf. The solution is to map the &nbsp entity to a known character (in this case underscore).

Finally there was a bug in Grails 1.3.3 / Spock 0.5 which breaks mockDomain. This is reportedly fixed in 1.3.4 and the latest Spock code, but I haven't tried upgrading yet.

Here's how things really look...

@WithGMock
class MetaAttachmentsTagLibSpec extends TagLibSpec {

def g
def document

void setup() {
g = mock()
mock(tagLib).getG().returns(g).stub()
mockDomain Invoice

PluginManagerHolder.pluginManager = [hasGrailsPlugin: { String name -> true }] as GrailsPluginManager // Workaround for JIRA GRAILS-6482
}

def cleanup() {
PluginManagerHolder.pluginManager = null // Workaround for JIRA GRAILS-6482
}

def "Attachments icon has correct markup"() {
given:
Invoice invoice = new InvoiceBuilder().buildAndSave()
g.resource(instanceOf(Map)).returns '/foo.jpg'

when:
renderAttachmentsIcon([target: invoice])

then:
valueOf('img.@id') == "toggle-attachments-${invoice.id}"
valueOf('img.@src') == "/foo.jpg"
valueOf('script') == "\$('#toggle-attachments-${invoice.id}').bind('click', Books.attachments.toggle);"
}

def methodMissing(String name, args) {
String html
play {
html = super.methodMissing(name, args)
}
createDocument(html)
}

def createDocument(String html) {
String xml = """<!DOCTYPE html [<!ENTITY nbsp "_">]>\n<results>${html}</results>"""
document = new XmlSlurper().parseText(xml)
}
}

And the method under test...

def renderAttachmentsIcon = { Map attrs, def body ->
String targetId = attrs.target.id
String iconId = "toggle-attachments-${targetId}"
String imgSrc = g.resource(dir:'/images/skin', file:'paperclip.png')

MarkupBuilder builder = new MarkupBuilder(out)
builder.img(id: iconId, src: imgSrc)
builder.script(type:'text/javascript') {
mkp.yield "\$('#${iconId}').bind('click', Books.attachments.toggle);"
}
out << '' // flush for unit tests
}

Thursday, 2 September 2010

Internet Explorer and the 1 item remaining bug

We've started using Selenium 2 / WebDriver to soak test our application, and were caught once again by a bug in ie7 and ie8 that randomly causes the browser to hang, waiting for some resource to finish loading, even though the page is visible and works fine.

I hit the problem a year or so ago and solved it by hacking something in prototype.js, but can't remember exactly what. Thankfully there's now a more recent version of prototype (we're stuck on grails 1.1.1 which is bundled with prototype 1.6.0), so I upgraded to prototype 1.6.1 and problem solved :)

Saturday, 7 August 2010

Grails and unit#junit;4.8.1: configuration not found in junit#junit;4.8.1:

After upgrading to grails 1.3.x I started getting the following error...


::::::::::::::::::::::::::::::::::::::::::::::

:: UNRESOLVED DEPENDENCIES ::

::::::::::::::::::::::::::::::::::::::::::::::

:: junit#junit;4.8.1: configuration not found in junit#junit;4.8.1: 'master'. It was required from org.grails.internal#Books;0.1 test

::::::::::::::::::::::::::::::::::::::::::::::


A found a few posts suggesting I needed to clear my ivy cache, but that did sfa. Turned out to be something in the jawr 3.3.2 plugin. It went away when I excluded junit from compile scope in the plugins dependencies.groovy


dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
compile ('net.jawr:jawr:3.3.2') {
excludes "mail", "activation", "ejb","jms","jmxri","jmxremote", "junit"
}

Friday, 23 July 2010

IntelliJ v9.0.3 adds support for Grails v1.3.x

JetBrains released IntelliJ IDEA v9.0.3 two days ago, which includes support for Grails v1.3.x, along with nicer SVN merging.

See more details here:

http://www.jetbrains.com/idea/whatsnew/index.html

Sunday, 2 May 2010

AppStatus Plugin 0.1 Released

The AppStatus plugin enables you to easily display useful info about your application.

Install the plugin and navigate to http://yourapp/appStatus to see application.properties, server time and locale.

If you want to see more details you can specify the "providers" in resources.groovy

import uk.co.acuminous.app.status.provider.*
import uk.co.acuminous.app.status.AppStatusConfig

beans = {
appStatusConfig(AppStatusConfig) {
providers = [

// !!!DANGER WILL ROBINSON!!!
// The ApplicationConfigProvider will dump the entire
// contents of Config.groovy including datasource.password
// The SystemPropertiesProvider could also make available
// potentially sensitive system variables

properties: new ApplicationPropertiesProvider(),
config: new ApplicationConfigProvider(),
system: new SystemPropertiesProvider(),
locale: new LocaleProvider()
]
}
}


If you want to add your own providers just write something that implements the Provider interface and add it to the providers map.

Finally if you're already using jQuery and jQuery UI you pick an nicer view

beans = {
appStatusConfig(AppStatusConfig) {
providers = [
// blah
]
view = 'tabs'
}
}