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/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 »
2009/04/7
Groovy 1.6.1 was released today, and it includes a patch I submitted a few weeks ago to make working with regular expressions much more groovy. Thanks to everyone that voted for the patch in the Groovy JIRA.
The main functionality is the addition of a variety of find and findAll regular expression aware methods that have been added to string.
Read the rest of this article »
2009/03/8
I work on a couple of different grails projects that use a variety of versions of groovy and grails. I’ve thrown together a quick shell script that makes it easy to create a new alias to switch between different versions depending on what project you’re working with.
function switchGrails() {
echo "Switching to groovy version: $1"
echo "Switching to grails version: $2"
sudo rm /usr/local/{groovy,grails}
sudo ln -s /usr/local/$1 /usr/local/groovy
sudo ln -s /usr/local/$2 /usr/local/grails
echo "Done!"
ls -latr /usr/local/{groovy,grails}
}
alias g104='switchGrails "groovy-1.5.7" "grails-1.0.4"'
alias g11rc2='switchGrails "groovy-1.6.0" "grails-1.1-RC2"'
You can create your own aliases like the ones above to switch to the groovy/grails combinations that you happen to be working with.
Just stick the code above in your .profile/.bashrc/.zshrc file and restart your shell to make the aliases available.
This function assumes that you’ve got you’ve got groovy and grails installed in your /usr/local directory and that you use a symlink at /usr/local/groovy that $GROOVY_HOME is pointed to and /usr/local/grails that $GRAILS_HOME is pointed to. If those assumptions aren’t correct for you, you’ll have to tweak the script.
It also uses “sudo” as it assumes that /usr/local is owned by root and not by your logged in user. If you’ve chowned the directory to yourself, you can remove the sudo (and the need to enter your password).
Now it’s easy for me to switch between different projects by just typing the appropriate alias:
pollux% g104
Switching to groovy version: groovy-1.5.7
Switching to grails version: grails-1.0.4
Done!
lrwxr-xr-x 1 root wheel 23 Mar 8 19:55 /usr/local/groovy -> /usr/local/groovy-1.5.7
lrwxr-xr-x 1 root wheel 23 Mar 8 19:55 /usr/local/grails -> /usr/local/grails-1.0.4
pollux% g11rc2
Switching to groovy version: groovy-1.6.0
Switching to grails version: grails-1.1-RC2
Done!
lrwxr-xr-x 1 root wheel 23 Mar 8 19:56 /usr/local/groovy -> /usr/local/groovy-1.6.0
lrwxr-xr-x 1 root wheel 25 Mar 8 19:56 /usr/local/grails -> /usr/local/grails-1.1-RC2
2009/02/8
I’ve just opened JIRA issue with a patch to enhance the groovy and grails consoles so that the default font size and the dimensions of the input and output areas can be overridden.
The initial patch I submitted used a custom GroovyConfig file, but at the suggestion of Guillame, I’ve modified the patch to use the preferences API. In my initial poke through the code, I didn’t see the preferences API being used (and couldn’t remember any settings that were saved by the console). After a closer look, it was being used in a couple of places.
The updated patch has been accepted and scheduled for inclusion in Groovy 1.6.1.
If you’re interested in making sure this change gets in, please vote for the JIRA.
I know that I open a groovy or grails console probably 10 times on an average day of programming, and the first thing that I do every time is to hit cmd-shift-L 3 times to bump the font size up to 18 points (yes, my eyes aren’t that great and I’m running my monitor at 1920×1200). The next thing that I do is drag it from the postage stamp size that it defaults to to cover at least half my screen.
This patch will probably save me more frustration than time, but I’m hoping it’ll save a lot of both for others out there.
This change won’t get in till 1.6.1, so if you want to tweak your current groovy install, it’s actually pretty easy to do.
Read the rest of this article »
2008/12/24
So groovy has this cool operator called the “spread” operator: “*.”.
Right now, it’s listed as the top hidden feature of Groovy over on StackOverflow. It works like this:
[1, 2, 3, 4]*.toString()
// equals ["1", "2", "3", "4"]
It applies the method/property to the right of the operator to each member of the collection and returns the results as another collection. It’s syntactic sugar for doing this:
[1, 2, 3, 4].collect { item -> item.toString() }
(and that’s syntactic sugar for a whole pile of Java code :)
Did you know that for properties, you don’t actually need to use the spread operator? Neither did I till I fat-fingered a command working with a list of Grails domain objects and it still worked.
Read the rest of this article »
2008/12/9
In my current project, I’ve been doing a lot of tweaking of the default grails scaffolding templates. Because of this, I need to run the new uber generate-all command quite a bit to recreate things.
The one problem with this script is that if the files already exist, a prompt will come up after ~10 seconds or so (after the grails environment bootstraps) asking you if you want to Overwrite everything:
grails generate-all "*"
...
~10 seconds pass
...
Generating views for domain class Baz ...
File /foobar/grails-app/views/baz/list.gsp already exists. Overwrite?y,n,a
This was a bit of a pain as I’d often kick the script off, get distracted and then come back to the shell with that prompt still waiting for me to tell it what to do. I’d rather just start working with my shiny new scaffolding.
The easy solution to this is simply to pipe the answer you want into the grails command:
echo "a" | grails generate-all "*"
Doing that will pipe the “a” into the grails command so that when the prompt finally comes up, it knows that it can continue regenerating all of my scaffolding.
It seems simple enough after I figured it out, but I thought it could save some other people time when they’re hacking around with templates and know they want to regenerate all of them.
Read the rest of this article »
2008/12/1
Out of the box, groovy gives you a number of powerful methods to iterate over lists and maps:
def fibList = [1, 1, 2, 3, 5]
fibList.each { println it } // prints all of the numbers in the list
assert fibList.any { it == 3 }
assert fibList.every { it > 0 }
assert fibList.collect { it - 1 } == [0, 0, 1, 2, 4]
assert fibList.findAll { it > 1 && it < 5 } == [2, 3]
assert fibList.find { it > 1 } == 2
assert fibList.inject("fib: ") { str, val -> str < < val }.toString() == "fib: 11235"
That’s really nice if you’re working with raw lists and maps, but what if you have a class that doesn’t extend list or map? How hard is it to empower that class with the groovy iteration methods? If this were Java, you’d likely need to implement an interface with these methods (and throw a “not implemented” exception for those you didn’t feel like taking the time to implement).
Since it’s not Java, but groovy (and you’ve read the title of this blog post :), you know it’s easy!
Read the rest of this article »