2009/07/30
This is a continuation of a sporadic set of blog posts about some practical uses for groovy metaprogramming.
Groovy Config DSL Overview
In Java, configuration is normally done with properties files. They’re kind of a pain because they’re inflexible, don’t allow executable code, and don’t easily provide a heirarchy.
Groovy greatly improves on Java by building in support for a simple configuration DSL using the ConfigSlurper. Read the rest of this article »
2009/07/14
I gave a presentation tonight on the build-test-data grails plugin at the Groovy Users of Minnesota (GUM) meeting that was well received.
Lots of good questions from the people in attendance. Thanks to everyone for showing up.
Here’s a version of it on slideshow: Read the rest of this article »
2009/06/25
A recent blog post, Cedric Beust asks about how to cleanly implement “soft asserts”. Soft asserts are test assertions that don’t “fail fast”. Instead, all of the assertion failures in the test method are collected and reported at the end of the test.
So far, the proposals in the comments look fairly clunky to me and include defining whole sets of new methods like “assertEqualsButContinue(“foo”, “foo”)”, chaning assertions together jQuery-style, or using lists to hold all of our assertions.
Wouldn’t it be a lot nicer if we could continue to use the same methods that we’re already using?
In groovy, enabling soft assertions is easy with a little closure magic. Read the rest of this article »
2009/06/18
I’m currently working at a startup that’s small enough that we don’t have a dedicated DBA and I’ve been doing a lot of mysql maintenance work recently. I wanted a quick dashboard for which commands were currently running and how long they’ve been running for. Sort of like top but for mysql.
Combining the unix “watch” command with the mysql “show processlist” command gives me what I’m looking for. A quick, self-updating status of the current state of the database.
watch -n 5 --differences "mysql -u username -psekrit -e 'show processlist'"
Shows something like this:
Every 5.0s: mysql -n 5 -u username -psekrit -e 'show processlist' Thu Jun 18 05:25:14 2009
Id User Host db Command Time State Info
3141 admin localhost mydb Query 34978 freeing items SELECT id, type, active, email FROM user WHERE email
3146 admin localhost mydb Sleep 0 NULL
24876 root localhost NULL Query 0 NULL show processlist
Where the “time” column is the number of seconds the query has been running, and Info holds the actual query (you can use “show full processlist” to see the full query).
Watch is a nice little linux util that runs a command every “n” seconds (it defaults to 2 seconds). If you use the –differences switch, it will highlight the differences between one update and another. I use watch for all kinds of monitoring activities, such as watching a directory to see a file grow in size as it gets transfered.
Here’s a quick shell function that you can add to your .bashrc/.bash_profile/.zshrc to use on arbitrary hosts:
function mysqltop() {
MYSQL_OPTS=$@
watch -n 5 --differences "mysql $MYSQL_OPTS -e 'show processlist'"
}
Then just pass in any creds/host info you need like a normal mysql command:
mysqltop -u ted -psekrit1 -h example.com -P 3307
If you’re on linux, you probably already have “watch” installed. If you’re on OSX, you probably don’t, but you can get it quickly through macports. Install macports, make sure “port” is in your path and run:
There is also a command called mytop that you can get which looks like the same thing, but prints out the processlist details with some nicer formatting and a little extra information. It’s in macports, but it has a number of dependencies including mysql so if you didn’t install mysql through macports, you might want to stick with what I have above or get it another way.
(EDIT: updated with bash function)
2009/06/15
I’ve just released version 0.2.1 of the build-test-data grails plugin.
The build-test-data plugin makes creating integration test data easy. It decorates your domain objects with a “build” method that will create new domain instances and will automatically populate required fields with data and save it to the database. This enables you to create more maintainable tests where the data you create is targeted specifically at the situations you’re trying to test, without having to go through all of the ceremony of creating the rest of the object graph that you don’t care about.
// creates a new Book instance with all required fields
// (like the Author that the book belongsTo) populated
def b = Book.build()
To learn more about the basics of the build-test-data plugin, see this blog post and check out the Basic Usage and Sample Code wiki pages.
Read the rest of this article »
2009/06/14
I’ve recently thrown together a few different command line aliases that have been very helpful in my grails development and I thought others might benefit from them.
The aliases are primarily just the first letter of the words in the command, which makes them easy to remember (and saves on typing). Some examples:
gta # grails test-app
gtai # grails test-app -integration
gtaud AuthorService # grails-debug-suspend test-app -unit AuthorService
Read the rest of this article »
2009/06/1
The need to add some functionality an existing method, but avoiding cutting and pasting the old implementation has come up a few times over the last week.
Sometimes, creating a subclass isn’t feasible, often because you don’t control all of the places where that class gets used. Read the rest of this article »
2009/05/26
On my MacBook pro, I’ve got 4 removable hard drives (2 physical in 2 partitions each) and a JungleDisk mount.
I found it painful to manually eject each individual drive in the finder so I threw together this quick AppleScript to eject all the disks.
tell application "Finder"
eject (every disk)
end tell
Just open up /Applications/AppleScript/ScriptEditor.app and paste that in. Then choose “Save As” and pick “Application”. That will compile the script and create a .app file that you can click on to run, or you can put it in your path and execute it there.
I think the same kind of script could be created with the command line “diskutil eject” command, but this seemed cleaner as I wasn’t able to come up with a generic way to figure out which disks were “ejectable” and which weren’t. AppleScript is able to figure that all out for you.
2009/05/13
Over the years, I’ve had a number of requests for me to share my zshrc file with friends and coworkers. In the past, I’ve normally trimmed out the sensitive parts by hand and then e-mailed the most useful stuff. I’ve always intended to make this an easier process and I’ve finally gotten around to it.
I’ve created a new bitbucket repository to hold my shared zshrc configuration. You can get it for yourself by cloning the repository:
cd ~
hg clone http://bitbucket.org/tednaleid/shared-zshrc/
Read the rest of this article »
2009/04/14
Creating maintainable test data is hard.
Often an entire object graph needs to be created to support the instantiation of a single domain object. This leads to either the cutting and pasting of that creation code, or relying on a canned set of objects that we’ve grown over time and maintained as the domain objects change. After a while, adding just one more Widget to that set of canned data ends up breaking tests just about every time.
There has to be a better solution, right? Read the rest of this article »