Archive for the 'groovy' Category

May 27th 2008 Syntactic Sugar in Groovy and Ruby

Over on the RailsEnvy blog (the guys who made the “Rails vs. X commercials” last year), there’s an article that highlights some of the nice syntactic sugar that Groovy has that’s currently not available in Ruby.

I actually came to Groovy/Grails after working with Ruby/Rails for about a year. Both languages and framworks have their strengths and it’s nice to see cross-pollination of good ideas going both ways. From the comments, it sounds like Ruby 1.9 is getting some of the features that we’ve had in Groovy for a while.
Continue Reading »

4 Comments » Posted by tednaleid / grails and groovy

May 19th 2008 Groovy: Don’t Fear the RegExp

Some people, when confronted with a problem, think “I know, I’ll use regular expressions!”
Now they have two problems. — Jaime Zawinski

There is a common and well-earned aversion in the Java world to regular expressions. Prior to Java 1.4, regular expressions weren’t even part of the core language. Post 1.4, using regular expressions is still a painful task of working with Pattern and Matcher objects. Lots of typing is involved to make anything happen. It’s difficult enough that most Java devs don’t end up using them enough to actually remember how to read a regular expression, and they need to dig up the JavaDocs (or cut and paste an old example), every time they want to use them.

This aversion has persisted into the Groovy community to a level that I haven’t seen in other dynamic scripting languages like Ruby, Python, and (obviously) Perl.

The current regexp docs that pop up when doing a google search are all outdated and don’t use any of the best techniques that are available in the groovy 1.5.X and 1.6-beta code that is now available. The recent Groovy Recipes book doesn’t have an entry for regular expressions in the index, and I was unable to find a single example of a regular expression in the entire book.

This is unfortunate because Groovy makes using regular expressions much easier than in Java. Under the covers, you’re still working with the same old Java Pattern and Matcher objects, but the Groovy syntax and additions to those classes are pleasant to work with.
Continue Reading »

10 Comments » Posted by tednaleid / grails and groovy and unit testing

May 7th 2008 What methods does my Groovy/Grails class have?

If you’ve ever wondered what methods a groovy class has available for you to call, all you need to do is ask the metaClass:

"foo".metaClass.methods*.name

Result:

["equals", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait", "wait", 
"wait", "charAt", "codePointAt", "codePointBefore", "codePointCount", "compareTo", 
"compareTo", "compareToIgnoreCase", "concat", "contains", "contentEquals", 
"contentEquals", "copyValueOf", "copyValueOf", "endsWith", "equals", 
"equalsIgnoreCase", "format", "format", "getBytes", "getBytes",
 "getBytes", "getChars", "hashCode", "indexOf", "indexOf", "indexOf", 
"indexOf", "intern", "lastIndexOf", "lastIndexOf", "lastIndexOf", "lastIndexOf", 
"length", "matches", "offsetByCodePoints", "regionMatches", "regionMatches", 
"replace", "replace", "replaceAll", "replaceFirst", "split", "split", "startsWith",
 "startsWith", "subSequence", "substring", "substring", "toCharArray", 
"toLowerCase", "toLowerCase", "toString", "toUpperCase", "toUpperCase", 
"trim", "valueOf", "valueOf", "valueOf", "valueOf", "valueOf", "valueOf", 
"valueOf", "valueOf", "valueOf"]

There’s a lot of duplication in there, and the order is random, so I’ll often fix that like this:

"foo".metaClass.methods*.name.sort().unique()

Result:

["charAt", "codePointAt", "codePointBefore", "codePointCount", "compareTo",
 "compareToIgnoreCase", "concat", "contains", "contentEquals", "copyValueOf", 
"endsWith", "equals", "equalsIgnoreCase", "format", "getBytes", "getChars",
 "getClass", "hashCode", "indexOf", "intern", "lastIndexOf", "length", "matches", 
"notify", "notifyAll", "offsetByCodePoints", "regionMatches", "replace", "replaceAll",
 "replaceFirst", "split", "startsWith", "subSequence", "substring", "toCharArray", 
"toLowerCase", "toString", "toUpperCase", "trim", "valueOf", "wait"]

Continue Reading »

3 Comments » Posted by tednaleid / grails and groovy and shortcut

Apr 23rd 2008 Converting wordpress blog from wordpress.com to slicehost slice

I just finished the conversion over from a wordpress.com hosted blog to a wordpress blog that I’m running on a slicehost slice.

I think that it’s good to maintain your own linux server and get your hands dirty from time in Apache rewrite rules and php code.

The biggest reason for the switch is that the wordpress instance on wordpress.com completely mangles pasted grails and groovy code. It tries to make things pretty by changing a double dash to an mdash, single dashes to an ndash and double quotes to “smart” quotes (an oxymoron if there ever was one). Not to mention the manual &gt; and &lt; that you need to enter instead of > and <. These insidious changes make pasted code worthless and painful for readers to use.

After struggling with trying to get the formatting working correctly for a few weeks, I decided to host my own blog and use the great wp-syntax plugin.

I did need to do some reformatting on my old posts (as I don’t need to encode things anymore) and I believe that I have everything fixed now.  Let me know if you run across anything I’ve missed in the conversion.

No Comments » Posted by tednaleid / command line and grails and groovy

Mar 31st 2008 Using Gant to execute a groovy script within the Grails context (updated)

In a previous post I showed a script that I had created to allow the execution of a groovy script within a grails application context (including access to domain objects, controllers, services, etc). A couple of people reported a problem with the script where they were getting lazy initialization exceptions. I finally tracked this issue down to one where many-to-many relationships are being used between two domain classes.

Here is an updated script that fixes this issue by setting up the hibernate session in the Gant script.
Continue Reading »

13 Comments » Posted by tednaleid / command line and grails and groovy

Mar 24th 2008 Unit Testing Isolated Methods with Groovy

Glen Smith has been running a great series of posts this month to help people get started with unit testing. In that sprit, I thought I’d put out a post on some unit testing work that I’ve been doing recently.

There are a number of libraries and techniques for unit testing with groovy. It comes with MockFor and StubFor out of the box, and you can also leverage Java libraries like EasyMock and Mockito if your needs aren’t satisfied by the built-in constructs.

The mock and stub implementations that I mention above work well for allowing you to control the behavior of any collaborators, but what if your method calls another method within the same class? None of those solutions (as far as I’m aware) allow you to replace the behavior of other methods on the Class Under Test (CUT). If you are trying to unit test your method in true isolation, you need the ability to stub out the behavior of internal methods on the class (for example, to throw an exception to test try/catch logic).

Continue Reading »

2 Comments » Posted by tednaleid / grails and groovy and unit testing

Mar 19th 2008 Using Gant to execute a groovy script within the Grails context

(3/30/08 I’ve posted an update to this script that fixes the problems noted in the comments)

I’ve been working with Gant recently and wanted the ability to be able to execute a script within the grails context, but not add it as part of a controller or needing to paste it every time in a grails console/shell.

After a little playing around, I came up with this:

Continue Reading »

11 Comments » Posted by tednaleid / grails and groovy

Mar 13th 2008 Using the OSX Console.app to aid Grails Test Driven Development

With Groovy built-in, Grails is one of the best platforms running on the JVM for doing unit testing.

One limitation that it does have is that the current testing scripts seem to be oriented more towards creating test reports rather than giving the user the quick feedback that is helpful when doing Test Driven Development.

Here’s an example of the output that you get on a failed test after running “grails test-app”:

-------------------------------------------------------
Running 1 Integration Test...
Running test BookTests...
testBookHasExpectedTitle...FAILURE
Integration Tests Completed in 622ms
-------------------------------------------------------
[junitreport] Transform time: 944ms
 
Tests failed: 0 errors, 1 failures, 0 compilation errors. View reports in /Users/ted/Documents/workspace/foobar/test/reports

Ok, that’s great…a test failed. But why? Where is the stacktrace and any output generated during the test?

Continue Reading »

4 Comments » Posted by tednaleid / grails and groovy and osx and unit testing