2010/06/15
Groovy closures combined with iterators make it simple to create our own enhanced iterators that let us process a collection how we want to.
I write my own custom iterators all the time and name them something descriptive. This makes the code much more readable. Rather than trying to decipher what a for loop is trying to do, we wrap up all of that iteration logic into a meaningful name and we cleanly separate that iteration from the processing that we’re doing with each element.
This kind of design is a core concept in Uncle Bob’s Clean Code, one of my favorite programming books in the last few years.
This example iterates over a collection and calls the passed in closure until we hit a value greater than 5.
def eachUntilGreaterThanFive = { collection, closure ->
for ( value in collection ) {
if ( value > 5 ) break
closure(value)
}
}
def a = [1, 2, 3, 4, 5, 6, 7]
eachUntilGreaterThanFive(a) {
println it
}
prints:
This code makes it obvious what the iterator is doing (looping till we hit a condition) as well as what will happen with each element iterated over (print it out).
For a real life example, I had a need to iterate over a list of values and where I needed both the current object as well as a peek at the next object in the list.
Doing this is Java is a bit of a pain, but groovy makes it easy to write and (hopefully) to read, we can also add it directly onto the Collection metaClass so that it’s available for all of our Collection instances:
Collection.metaClass.eachWithPeek = { closure ->
def last = null
delegate?.each { current ->
if (last) closure(last, current)
last = current
}
if (last) closure(last, null)
}
These test cases show that as we iterate through the collection, we can see the current item and peek at the next one (if any). If the collection is empty, we don’t execute the closure it, and if we’re at the end of the list there isn’t anything to peek at:
[].eachWithPeek { current, peek ->
assert false // shouldn't get here, nothing to iterate through
}
[1].eachWithPeek { current, peek ->
assert current == 1
assert peek == null // only 1 element, nothing to peek at
}
def results = []
[1, 2, 3, 4, 5].eachWithPeek { current, peek ->
results << [current, peek]
}
assert results == [[1, 2], [2, 3], [3, 4], [4, 5], [5, null]]
2010/03/17
I’ve finally released version 1.0 of the grails build-test-data plugin.
If you’re not familiar with build-test-data, the quick summary is that it puts a build() method on all grails domain objects. Calling that method will automatically construct and save an instance of that domain object that conforms to all of the domain’s constraints. It also allows you to override the values that you want to explicitly set. It makes your tests much cleaner and less fragile as you only need to specify the values that actually matter to a particular test method instead of building a huge graph of objects just to satisfy constraints.
The plugin has been quite stable for the past 6 months or so, and has survived upgrades from grails 1.0.X through grails 1.2.1 with minimal changes. Because of this, I’ve decided to move the version from 0.2.3 to 1.0 to indicate that the plugin is stable and ready to be used. I’ve also added the LICENSE file releasing the plugin under the Apache 2.0 open source license (the same license as grails). It was always open source, but I had neglected to add the official license file in the past.
If you’re not familiar with the build-test-data plugin, the documentation on the wiki is thorough, I’ve also given a presentation on build-test-data that explains why it’s better than other existing data generation technologies.
The biggest changes for this release compared to the last are a number of bugfixes around making sure that both sides of a one-to-many relationship get populated correctly and that there isn’t a need to refresh() from the database.
Previously, if you had an Author that hasMany Books, and each Book belongsTo an Author, you’d need to refresh the Author if you tried to build a new book with an existing author:
Author eap = Author.findByName("Edgar Allan Poe")
Book b = Book.build(author: eap, title: "The Tell-Tale Heart")
assertEquals eap.name, b.author.name // works, linked in OK previously
assertEquals 1, eap.books.size() // FAILED previously WORKS now, previously the book wasn't added properly to the author side of things
The previous workaround was to call eap.refresh() to reload the author from the database, or to have the user manually addToBooks(b). Both solutions were ugly and kludgy and this issue has now been fixed.
2010/01/24
Inspired by this question on stackoverflow, I decided to create a utility class that allowed me to determine generically what calls a closure makes (without actually letting it make any calls). This lets me see what it’s trying to do before letting it actually do it.
Read the rest of this article »
2009/11/3
A while ago I blogged about my grails testing aliases and how much time they save me.
I’ve made some enhancements to them in the interim that have made them even easier to use.
The most important alias is gtaf, which is short for “grails test-app” for failed tests.
It will search through your test output directory and look for any tests that failed. If it finds any, it will rerun only those tests. Otherwise, it will rerun all tests. That makes it easy to just use gtaf all the time. If any tests fail, it will open them up using Console.app.
If you’re not on OSX, or would like to use something else to view the failed test logs, just modify the testlog alias to do something different.
Read the rest of this article »
2009/10/6
I’ve just released a quick plugin that I put together tonight to help render markdown text as HTML within a grails application. It leverages the MarkdownJ library.
I’m a big fan of storing information in markdown format as it is easy to read, easy to write, and easy to transform. A number of big websites like stackoverflow.com support the entry and display of markdown text to help people format their questions and answers without having to remember to properly format HTML.
If you’re unfamiliar with Markdown there are a number of good references on it’s use.
The grails markdown plugin is a very basic plugin. Currently, it just wraps the markdown libarary with a single TagLib that lets you easily render html from a markdown string within your gsp pages.
To install it, just type:
grails install-plugin markdown
To use it in your gsp file:
<markdown:renderHtml>
The *four* cardinal directions are
- North
- South
- East
- West
</markdown:renderHtml>
renders the html:
<p>The <em>four</em> cardinal directions are</p>
<ul>
<li>North</li>
<li>South</li>
<li>East</li>
<li>West</li>
</ul>
Or you can use the “text” attribute of the taglib:
<markdown:renderHtml text="${post.body}"/>
See the Grails Markdown Wiki and source for more details.
2009/10/1
I’ve spent some time over the last couple of weeks working on a Grails service that allows us to import new records into our MySQL database.
A number of interesting techniques have popped out of this that I think would be useful for others doing similar types of importing using GORM/Hibernate/MySQL. Read the rest of this article »
2009/08/25
I’ve just released the latest version of the grails build-test-data plugin. This version has a couple of bugfixes that were reported by users. Including one with a nice patch including a test from Robert Fletcher (patches with tests are always appreciated).
For those that aren’t familiar with the plugin, it make creating testing data easy.
It automatically adapts to changes in your domain classes and makes your integration tests easier to write and much less fragile.
Resources
Overall, the plugin has been very stable for the past few releases and I’m thinking about just bumping the version up to 1.0 fairly soon. The plugin seems pretty feature complete when balanced with it’s ease of use. Most of the other features I’ve thought of would explode the complexity level of the plugin and defeat it’s purpose. Might be best to leave well enough alone for now :).
I had a hell of a time with the current grails release-plugin cycle (6 attempts before it finally went through). The grails release-plugin command uses an old version of svnkit that’s only compatible with svn 1.5. I’ve long since upgraded to 1.6 on my mac, though luckily I did it through macports and still had the old version available to activate (sudo port deactivate subversion && sudo port activate subversion @1.5.6_0).
I did some searching around to find out how to install an old version of subversion, but didn’t come up with anything after a quick google so I might be screwed after my wipe/reinstall for snow leopard next week till Grails patches things with svnkit 1.3.
I did open up a JIRA ticket for grails to upgrade to svnkit 1.3, and I was surprised that there wasn’t already a ticket open on it (and that trunk still had svnkit 1.2 in it).
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 »