<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joe Kueser&#039;s Blog &#187; Groovy</title>
	<atom:link href="http://joe.kueser.com/tag/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://joe.kueser.com</link>
	<description>Another Geek Finding His Place on the Web</description>
	<lastBuildDate>Sat, 01 Oct 2011 23:01:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>More Groovy Goodies</title>
		<link>http://joe.kueser.com/2007/10/more-groovy-goodies/</link>
		<comments>http://joe.kueser.com/2007/10/more-groovy-goodies/#comments</comments>
		<pubDate>Sat, 13 Oct 2007 15:30:30 +0000</pubDate>
		<dc:creator>Joe Kueser</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hudson]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://joe.kueser.com/2007/10/13/more-groovy-goodies/</guid>
		<description><![CDATA[I've had quite a few people ask me to put together a follow-up article to my previous post titled What Makes Groovy So...Groovy, so, here it is. Here I'll take a quick look at integrating Groovy with Maven 2, using Groovy for web services, XML parsing, and Hudson's Groovy console integration. Integrating Groovy with Maven [...]]]></description>
			<content:encoded><![CDATA[<p>I've had quite a few people ask me to put together a follow-up article to my previous post titled <a href="http://joe.kueser.com/2007/10/06/what-makes-groovy-sogroovy/">What Makes Groovy So...Groovy</a>, so, here it is.  Here I'll take a quick look at integrating Groovy with Maven 2, using Groovy for web services, XML parsing, and Hudson's Groovy console integration.<span id="more-8"></span></p>
<h2>Integrating Groovy with Maven 2</h2>
<p>It took me all of about 5 minutes to set up an existing project to use Groovy.  The first thing (I suppose) to worry about is the directory structure.  I say to do this first simply because it's not completely obvious.  Basically, you just put a "groovy" directory at the same level as your "java" directory under src/main and src/test.  Like this:</p>
<p><a href="http://joe.kueser.com/wp-content/uploads/2007/10/groovy_maven2_directory_structure.jpg" title="Groovy Directory Sturcture"></a></p>
<p style="text-align: center"><a href="http://joe.kueser.com/wp-content/uploads/2007/10/groovy_maven2_directory_structure.jpg" title="Groovy Directory Sturcture"><img src="http://joe.kueser.com/wp-content/uploads/2007/10/groovy_maven2_directory_structure.jpg" alt="Groovy Directory Sturcture" /></a></p>
<p>This tells the Maven 2 plugin where to find your .groovy files.</p>
<p>Next, you need to change your pom.xml file to do 2 things: 1) compile the .groovy classes first, and 2) add the groovy jar dependency.  Both of these are super simple.</p>
<p>First, modify the "plugins" section of your pom.xml file to include the Groovy plugin.  Make sure you put the Groovy plugin before the Java compiler plugin.  This will allow your Java classes to transparently use the Groovy classes.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;plugins<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;plugin<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;groupid<span class="re2">&gt;</span></span></span>org.codehaus.mojo.groovy<span class="sc3"><span class="re1">&lt;/groupid<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;artifactid<span class="re2">&gt;</span></span></span>groovy-maven-plugin<span class="sc3"><span class="re1">&lt;/artifactid<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;extensions<span class="re2">&gt;</span></span></span>true<span class="sc3"><span class="re1">&lt;/extensions<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/plugin<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;plugin<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;artifactid<span class="re2">&gt;</span></span></span>maven-compiler-plugin<span class="sc3"><span class="re1">&lt;/artifactid<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>RELEASE<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;configuration<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;source<span class="re2">&gt;</span></span></span>1.5<span class="sc3"><span class="re1">&lt;/source<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;target<span class="re2">&gt;</span></span></span>1.5<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/configuration<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/plugin<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/plugins<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>Next you need to add the following to your dependency section (note, you may want to check on the version number.  If a more recent version is available, use that instead.)</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;dependency<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;groupid<span class="re2">&gt;</span></span></span>org.codehaus.groovy<span class="sc3"><span class="re1">&lt;/groupid<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;artifactid<span class="re2">&gt;</span></span></span>groovy-all<span class="sc3"><span class="re1">&lt;/artifactid<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>1.1-beta-3<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/dependency<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>And that's it!  Really!  (I highly recommend getting <a href="http://www.jetbrains.com/idea/">IDEA 7.0.</a>  It offers great Groovy support, and a very cool bi-directional compiler that make it so your Groovy and Java classes can have about the same level of IDE support.)</p>
<h2>Groovy Web Services</h2>
<p>You won't believe how incredibly easy web services are in Groovy.  Web services are suddenly easy to use, and not a major pain in the caboose.   If you really want to appreciate what Groovy can to for you, check out how web services in Java are usually handled, using Axis (there's a good article on this at <a href="http://javaboutique.internet.com/tutorials/Axis/">Java Boutique</a>).  Traditionally, you need to deal with wsdl files, and stub classes, and all kind of icky, confusing nastiness.  Not with Groovy.</p>
<p>Check Groovy's sites for a good overview on <a href="http://docs.codehaus.org/display/GROOVY/Groovy+SOAP">Groovy web services</a>.  Here's the quick and dirty.  You want to create a web service in Groovy?  How about something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20public"><span class="kw2">public</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20class"><span class="kw2">class</span></a> MathService <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20double"><span class="kw4">double</span></a> add<span class="br0">&#40;</span><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20double"><span class="kw4">double</span></a> arg0, <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20double"><span class="kw4">double</span></a> arg1<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20return"><span class="kw2">return</span></a> <span class="br0">&#40;</span>arg0 + arg1<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20double"><span class="kw4">double</span></a> square<span class="br0">&#40;</span><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20double"><span class="kw4">double</span></a> arg0<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20return"><span class="kw2">return</span></a> <span class="br0">&#40;</span>arg0 * arg0<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co2">import groovy.net.soap.SoapServer</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> server = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> SoapServer<span class="br0">&#40;</span><span class="st0">&quot;localhost&quot;</span>, <span class="nu0">6980</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; server.<span class="me1">setNode</span><span class="br0">&#40;</span><span class="st0">&quot;MathService&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; server.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20start"><span class="kw8">start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Can you believe that's a web service?  Yep, a fully functional web service with an associated WSDL and everything.  How does a client interact with that?  Oh, you are not going to believe this!</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co2">import groovy.net.soap.SoapClient</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> proxy = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> SoapClient<span class="br0">&#40;</span><span class="st0">&quot;http://localhost:6980/MathServiceInterface?wsdl&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> result = proxy.<span class="me1">add</span><span class="br0">&#40;</span><span class="nu0">1.0</span>, <span class="nu0">2.0</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Where's the magic?  Your looking at it.  We're able to dynamically create the proxy class, and reference the methods exposed in the web service as if they were local methods.  Suddenly web services are cool again!</p>
<p>There is a proposal in place for a RESTful web service API, but currently the push for this is more in the context of Grails.  However, implementing a RESTful web service would likely be quite easy when you can make use of the object-based switch statements, GStrings, and XML parsers.  Not quite as magical as SOAP-based web services, but easy nonetheless.</p>
<h2>Groovy XML Parsing</h2>
<p>In a rather dated article (2004), <a href="http://today.java.net/pub/au/184">Jack Herrington</a> does a good job of explaining <a href="http://today.java.net/pub/a/today/2004/08/12/groovyxml.html">Getting Groovy with Java</a>.  He outlines how XML is usually handled in Java, and how it evolved to being handled in Groovy.  It's certainly worth a read, but let's focus on Groovy today.  What kind of options do we have for parsing XML files in Groovy?</p>
<p>Just for kicks, let's stick with the same XML that was generated in my last post on Groovy:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;people<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;firstname<span class="re2">&gt;</span></span></span>John<span class="sc3"><span class="re1">&lt;/firstname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;lastname<span class="re2">&gt;</span></span></span>Doe<span class="sc3"><span class="re1">&lt;/lastname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;iailaddress<span class="re2">&gt;</span></span></span>john.doe@example.com<span class="sc3"><span class="re1">&lt;/iailaddress<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;firstname<span class="re2">&gt;</span></span></span>George<span class="sc3"><span class="re1">&lt;/firstname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;lastname<span class="re2">&gt;</span></span></span>Bush<span class="sc3"><span class="re1">&lt;/lastname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;iailaddress<span class="re2">&gt;</span></span></span>prez@whitehouse.gov<span class="sc3"><span class="re1">&lt;/iailaddress<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/people<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>How would we go about parsing this in Groovy?  Well, one way would be to use the Groovy XmlParser class.  That code would look something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> people = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> XmlParser<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">parseText</span><span class="br0">&#40;</span>theXml<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">people.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20each"><span class="kw5">each</span></a> <span class="br0">&#123;</span>person -&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> person.<span class="me1">firstName</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> person.<span class="me1">lastName</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> person.<span class="me1">emailAddress</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>You'll notice that, through the magic of the dynamic language, we can treat the XML tree as an object.</p>
<p>What about attributes?  They are accessed with the '@' identifier.  For example, if your emailAddress xml had a "location" attribute, we would access it using:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> person.<span class="me1">emailAddress</span>.@location</div>
</li>
</ol>
</div>
<p>or</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> person.<span class="me1">emailAddress</span><span class="br0">&#91;</span><span class="st0">'@location'</span><span class="br0">&#93;</span></div>
</li>
</ol>
</div>
<p>Groovy's XmlSlurper is another way to parse XML (and fun to say), but I don't completely understand it yet.  I encourage you to <a href="http://www.google.com/search?q=groovy+xmlslurper&amp;">search for it</a>.  <a href="http://docs.codehaus.org/display/GROOVY/Reading+XML+using+Groovy%27s+XmlSlurper">Here's something</a> to get you started.</p>
<h2>Hudson's Groovy Console Integration</h2>
<p><a href="https://hudson.dev.java.net/">Hudson</a> is a great Continuous Integration system that's really getting some attention lately.  One of the nice features it has is an integrated Groovy console where you can run arbitrary Groovy script.  Great for figuring out the state of the build.  Here's a screen shot of the console screen.</p>
<p><a href="http://joe.kueser.com/wp-content/uploads/2007/10/groovy_hudson_console.jpg" title="Hudson Groovy Console"><img src="http://joe.kueser.com/wp-content/uploads/2007/10/groovy_hudson_console.thumbnail.jpg" alt="Hudson Groovy Console" /></a></p>
<h2>In Conclusion...</h2>
<p>I'm sure there are plenty of other examples of what makes Groovy so groovy, so please, if you have any other ideas leave a comment.  I'll be happy to look into your suggestions and expand on them.</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://joe.kueser.com/2007/10/more-groovy-goodies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Makes Groovy So&#8230;Groovy?</title>
		<link>http://joe.kueser.com/2007/10/what-makes-groovy-sogroovy/</link>
		<comments>http://joe.kueser.com/2007/10/what-makes-groovy-sogroovy/#comments</comments>
		<pubDate>Sun, 07 Oct 2007 04:04:34 +0000</pubDate>
		<dc:creator>Joe Kueser</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[nfjs]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://joe.kueser.com/?p=7</guid>
		<description><![CDATA[Ever since attending the No Fluff Just Stuff symposium last month, I've been digging into Groovy, and have frankly been blown away. As I watched the presentations, and as I have done my own research, I have been blown away by Groovy's power. I'm convinced. I have to use Groovy. It's that good. I am, [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since attending the <a href="http://www.nofluffjuststuff.com/index.jsp">No Fluff Just Stuff</a> symposium last month, I've been digging into <a href="http://groovy.codehaus.org/">Groovy</a>, and have frankly been blown away.  As I watched the presentations, and as I have done my own research, I have been blown away by Groovy's power.  I'm convinced.  I <em>have</em> to use Groovy.  It's that good.  I am, however, having a little trouble convincing others of its coolness.  Maybe this will help others.  Please leave your comments and let me know what you think.  I wrote a blog about it.  Here it goes!</p>
<p>As I dig deeper, I find that features of the language fall in to four basic categories:</p>
<ul>
<li>Wow! That's really groovy!</li>
<li>Wow! That's really useful!</li>
<li>Hmmm...that's good to know.</li>
<li>Nice...but will I use it?</li>
</ul>
<p>I will focus on the really useful, much of which is also very groovy, but as my boss might say, "groovy don't pay the rent."  (He's kind of weird like that.)</p>
<p><span id="more-7"></span></p>
<h1>Top 10 Grooviest Groovy Features</h1>
<h3>10 - Brevity (Less is More!)</h3>
<p><span class="hw">brev∑i∑ty</span> (<span style="color: blue" class="pointer" onclick="pw = window.open('http://content.answers.com/main/content/pronkey-answers.html', 'PronunciationKey', 'height=650,width=520,resizable,scrollbars');if(pw){pw.focus();}" onmouseout="status='';return true;" onmouseover="status='Click for pronunciation key';return true;"><span class="pron">br?v<strong>'</strong>?-t?</span></span>) <span style="cursor: pointer" onmouseover="status='Click to hear pronunciation';return true;" onmouseout="status='';return true;" onclick="playIt('http://content.answers.com/main/content/ahd4/pron/B0475800.wav')"><img src="http://content.answers.com/main/content/img/pron.gif" alt="pronunciation" align="middle" border="0" /></span><br />
<em>n.</em></p>
<ol>
<li>The quality or state of being brief in duration.</li>
<li> Concise expression; terseness.</li>
</ol>
<p>Yeah.  Definitions are cool!  And that pretty well sums up what Groovy gives you in terms of coding.  By eliminating some of the scaffolding we normally put in our Java classes, Groovy can cut down that amount of code we have to write substantially.  I'm not sure that this is typical, but I recently converted a fairly complex Java class to Groovy.  The Java class had 1,800 lines of code.  The Groovy equivalent was down to only 1,100 lines of code.  I'm not real good with statistics, but that seems to be about a 40% reduction.</p>
<p>This is more of a "Wow!  That's groovy!" than useful, but it certainly is nice to have.  For me typing less per method means more functionality in the same amount of time.  And this is with only about 1 week worth of experience.  I look forward to what I can do when I learn the ins and outs of the language.</p>
<h3>9 - Ranges</h3>
<p>OK, fess up.  How many times have you written code that looked a little something like this?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; upperBoundry; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Do something interesting</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>or tried to figure out if a number is between two other number by doing something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span>x &gt; <span class="nu0">3</span> &amp;amp;&amp;amp; x &lt; <span class="nu0">10</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Do something else</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Well, Groovy makes this easier, and it's really pretty intuitive.  The range format is</p>
<pre>[lowerBound..upperBound], [upperBound..lowerBound]or [lowerBound..&lt;upperBound]<upperbound]></upperbound]></pre>
<p>So can you guess what this does?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="br0">&#91;</span><span class="nu0">1</span>..<span class="nu0">10</span><span class="br0">&#93;</span>.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20each"><span class="kw5">each</span></a> <span class="br0">&#123;</span> item -&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> item</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Yep, it prints the numbers 1 through 10.  Easy, right?  Ok, how do you like this?</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20if"><span class="kw1">if</span></a> <span class="br0">&#91;</span><span class="st0">'a'</span>..<span class="st0">'z'</span><span class="br0">&#93;</span>.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20contains"><span class="kw6">contains</span></a><span class="br0">&#40;</span>test<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>That simple little bit of code could be used to see if a letter is between 'a' and 'z'.  (<em>Note: you probably wouldn't use ranges to figure out if a character was a letter, that's just here for illustration purposes.  More likely you'd use the "~", "=~", or "==~"  operators, which let's you evaluate regular expressions.  Make sure you check out Groovy's <a href="http://groovy.codehaus.org/Regular+Expressions">Regular Expression</a> support page.</em>)</p>
<p>Now falling right in to the groovy and useful category is how you can use these ranges in switch statements.  Let's say, for example, that you have an insurance company that has different rates depending on the age range of the policy holder.  That code might look a little something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20switch"><span class="kw1">switch</span></a><span class="br0">&#40;</span>age<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20case"><span class="kw1">case</span></a> <span class="nu0">16</span>..<span class="nu0">20</span> : insuranceRate = <span class="nu0">0.05</span> ; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20break"><span class="kw2">break</span></a></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20case"><span class="kw1">case</span></a> <span class="nu0">21</span>..<span class="nu0">50</span> : insuranceRate = <span class="nu0">0.06</span> ; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20break"><span class="kw2">break</span></a></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20case"><span class="kw1">case</span></a> <span class="nu0">51</span>..<span class="nu0">65</span> : insuranceRate = <span class="nu0">0.07</span> ; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20break"><span class="kw2">break</span></a></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20default"><span class="kw2">default</span></a> : insuranceRate = <span class="nu0">0.10</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Ranges aren't limited to predefined goodies like numbers and letters.  You could also create an array, or dates, or any other comparable objects, and use those in your range statements.</p>
<h3>8 - GroovyBeans</h3>
<p>Much more "groovy" than useful, but certainly does cut down substantially on the extra scaffold code you need to write, GroovyBeans are just like JavaBeans, but a lot smarter.</p>
<p>In Java, you might create a simple Person class with a first name, a last name, and an email address.  You create the private variables for each of these, and then getters and setters for each.  You do the same thing time after time...or usually let your IDE do it for you.  Your code for this Person object would probably look almost exactly like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> Person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> firstName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> lastName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> emailAddress;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getFirstName<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> firstName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> setFirstName<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> firstName<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">firstName</span> = firstName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getLastName<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> lastName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> setLastName<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> lastName<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">lastName</span> = lastName;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getEmailAddress<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> emailAddress;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> setEmailAddress<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> emailAddress<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">emailAddress</span> = emailAddress;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Ho hum...how monotonous.  How boring!  If the IDE can generate those getters and setters for us, why can't Java?  Well, because Java's not Groovy (though Groovy <em>is</em> Java).  The Groovy equivalent of the above class skips all of that getter/setter silliness and you are left with this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20class"><span class="kw2">class</span></a> Person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?as_q=String&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">String</span></a> firstName</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?as_q=String&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">String</span></a> lastName</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?as_q=String&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">String</span></a> emailAddress</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>I know, you're saying, "but I <em>need</em> my getters and setters!"  Don't worry sport, they're still there, at least as far as your code is concerned.  How would Java call each of the methods in the Java class?  Maybe...</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;First Name:&quot;</span> + getFirstName<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;Last Name:&quot;</span> + getLastName<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;Email:&quot;</span> + getEmailAddress<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And how would Java call each of the methods in the Groovy class?  Would you guess...</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;First Name:&quot;</span> + getFirstName<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;Last Name:&quot;</span> + getLastName<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;Email:&quot;</span> + getEmailAddress<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Exactly the same.  How would Groovy access each of these?  Either using the property name or the getter/setter.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> <span class="st0">&quot;First Name:&quot;</span> + getFirstName<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> <span class="st0">&quot;Last Name:&quot;</span> + lastName</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> <span class="st0">&quot;Email Address: ${emailAddress}&quot;</span></div>
</li>
</ol>
</div>
<p>Notice the last line.  This introduces something called GStrings (I'm not making this up).  It's a way to include the variable values right within the screen.  This same methodology is used in Maven, Ant, JSF, and other technologies, so it's nothing new.  Just new to your Java code.</p>
<p>How do you set the values?  Pretty much like you'd think:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">emailAddress = <span class="st0">&quot;sample@example.com&quot;</span></div>
</li>
</ol>
</div>
<h3>7 - Expando</h3>
<p>The Expando class is fashioned off the real-life file folders that expand to fit whatever you put inside them.  These fit more into the "groovy" than "useful" category, at least on the surface, but this capability opens the door for some huge dynamic possibilities.  Within your code, you can create methods as needed.  Here's a trivial example that I borrowed from an excellent book, "<a href="http://www.amazon.com/Groovy-Action-Dierk-Koenig/dp/1932394842/ref=pd_bbs_sr_1/105-7669815-7935638?ie=UTF8&amp;s=books&amp;qid=1191719841&amp;sr=8-1">Groovy in Action</a>" (buy it now!)</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> boxer = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> Expando<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span class="kw2">assert</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20null"><span class="kw2">null</span></a> == boxer.<span class="me1">takeThis</span></div>
</li>
<li class="li1">
<div class="de1">boxer.<span class="me1">takeThis</span> = <span class="st0">'ouch!'</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span class="kw2">assert</span></a> <span class="st0">'ouch!'</span> == boxer.<span class="me1">takeThis</span></div>
</li>
<li class="li2">
<div class="de2">boxer.<span class="me1">fightBack</span> = <span class="br0">&#123;</span><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20times"><span class="kw8">times</span></a> -&gt; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20return"><span class="kw2">return</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20this"><span class="kw2">this</span></a>.<span class="me1">takeThis</span> * <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20times"><span class="kw8">times</span></a><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span class="kw2">assert</span></a> <span class="st0">'ouch!ouch!ouch!'</span> == boxer.<span class="me1">fightBack</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>This example shows the creation of a "boxer" Expando, and then shows that the "takeThis" method didn't exist.  The "takeThis" method is then created and accessed, as is a fightBack method.  This shows one use for Closures, which I'll get to later.  While the use for Expando classes may not seem immediately evident, the power of the Expando enables some of Groovy's grooviest features.</p>
<h3>6 - Grep</h3>
<p>To most geeks, <a href="http://en.wikipedia.org/wiki/Grep">grep</a> is a very useful UNIX command.   Groovy brings this kind of power to Java.  Here's another example from "Groovy in Action":</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">beasts = <span class="br0">&#123;</span><span class="st0">'bear'</span>,<span class="st0">'wolf'</span>,<span class="st0">'tiger'</span>,<span class="st0">'regex'</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20assert"><span class="kw2">assert</span></a> beasts.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20grep"><span class="kw5">grep</span></a><span class="br0">&#40;</span>~/..../<span class="br0">&#41;</span> == <span class="br0">&#91;</span><span class="st0">'bear'</span>,<span class="st0">'wolf'</span><span class="br0">&#93;</span></div>
</li>
</ol>
</div>
<p>Grep here is used to find all of the 4-letter words in the array.  A full array of options, and regular expressions, are available to the grep command.</p>
<h3>5 - Spring Integration</h3>
<p>Spring is a very cool technology, which allows you to dynamically include classes in your classes at runtime.  On the downside, Spring configuration is a pain in the butt.  From the SpringFrameworks web site:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;UTF-8&quot;</span><span class="re2">?&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;beans</span> <span class="re0">xmlns</span>=<span class="st0">&quot;http://www.springframework.org/schema/beans&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">xmlns:xsi</span>=<span class="st0">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">xsi:schemaLocation</span>=<span class="st0">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;...&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;...&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;em</span> <span class="re0">class</span>=<span class="st0">&quot;lineannotation&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;lineannotation&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="coMULTI">&lt;!-- collaborators and configuration for this bean go here --&gt;</span></span><span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span><span class="sc3"><span class="re1">&lt;/em<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;...&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;...&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;em</span> <span class="re0">class</span>=<span class="st0">&quot;lineannotation&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;lineannotation&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="coMULTI">&lt;!-- collaborators and configuration for this bean go here --&gt;</span></span><span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span><span class="sc3"><span class="re1">&lt;/em<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;em</span> <span class="re0">class</span>=<span class="st0">&quot;lineannotation&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;lineannotation&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="coMULTI">&lt;!-- more bean definitions go here... --&gt;</span></span><span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span><span class="sc3"><span class="re1">&lt;/em<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/beans<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<h3 class="title"><a title="beans-factory-instantiation" name="beans-factory-instantiation"></a></h3>
<p>Using Groovy's builder classes, we are able to use Spring within Groovy without a single bit of XML configuration.  (I would put an example below, but I can't find one <img src='http://joe.kueser.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  )</p>
<p>UPDATE:† Ok, my Spring portion was lame.† So, special thanks to Paul King for pointing me to this link.† Told you this Spring stuff was Groovy!† <a href="http://groovy.codehaus.org/Using+Spring+Factories+with+Groovy">http://groovy.codehaus.org/Using+Spring+Factories+with+Groovy†</a></p>
<p>Speaking of builders...</p>
<h3>4 - Builders</h3>
<p>In Groovy, the Builder classes provide an easy way to narrow down repetitive tasks and build non-Groovy items, such as XML and HTML documents, Swing GUIs, and pretty much anything else you can think of that follows a pattern.  Let's take a really simple XML example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;people<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;firstName<span class="re2">&gt;</span></span></span>John<span class="sc3"><span class="re1">&lt;/firstName<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;lastName<span class="re2">&gt;</span></span></span>Doe<span class="sc3"><span class="re1">&lt;/lastName<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;emailAddress<span class="re2">&gt;</span></span></span>john.doe@example.com<span class="sc3"><span class="re1">&lt;/emailAddress<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;firstName<span class="re2">&gt;</span></span></span>George<span class="sc3"><span class="re1">&lt;/firstName<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;lastName<span class="re2">&gt;</span></span></span>Bush<span class="sc3"><span class="re1">&lt;/lastName<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;emailAddress<span class="re2">&gt;</span></span></span>prez@whitehouse.gov<span class="sc3"><span class="re1">&lt;/emailAddress<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sc3"><span class="re1">&lt;/person<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/people<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>In Java in order to create such a document, you'd have to create DOM object, create an element for each person in the tree, and child elements for each attribute, all in some really ugly, hard to read code that I refuse to put here (that exercise is left to the reader...I've been there, and it sucks).  In Groovy, to build this XML object, you would do something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> builder = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> groovy.<span class="me1">xml</span>.<span class="me1">MarkupBuilder</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">builder.<span class="me1">people</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; firstName <span class="br0">&#123;</span> <span class="st0">&quot;John&quot;</span> <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; lastName <span class="br0">&#123;</span><span class="st0">&quot;Doe&quot;</span> <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; emailAddress <span class="br0">&#123;</span><span class="st0">&quot;john.doe@example.com&quot;</span> <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; person <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; firstName <span class="br0">&#123;</span> <span class="st0">&quot;George&quot;</span> <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; lastName <span class="br0">&#123;</span> <span class="st0">&quot;Bush&quot;</span> <span class="st0">&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp; &nbsp;emailAddress { &quot;</span>prez@whitehouse.<span class="me1">gov</span><span class="st0">&quot; }</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"> &nbsp;}</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">}</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0"</span></div>
</li>
</ol>
</div>
<p>Can you tell what that does?  Easy right?</p>
<p>I put Swing behind me years ago, 'cause it's such a nightmare, but here's a blog post that gives a quick look at building a simple Swing application in both standard Swing, and using Groovy's SwingBuilder class.</p>
<p><a href="http://codetojoy.blogspot.com/2007/08/groovy-swingbuilder-and-secret-methods.html">http://codetojoy.blogspot.com/2007/08/groovy-swingbuilder-and-secret-methods.html</a></p>
<p>Builders are not limited to what the guys at Groovy imagined.  Through the magic of the Expando classes mentioned earlier, Groovy allows you to create your own builders with ease.  More on these builders can be found on the <a href="http://groovy.codehaus.org/Builders">Groovy web site</a>.</p>
<h3>3 - Unit Testing, Mocks, and Stubs</h3>
<p>Unit testing in Groovy is so useful, and, well, groovy, that the "Groovy in Action" book mentioned earlier has an entire chapter devoted to it.  Because I feel this is important, I am going to include a boatload of links, and an example or two for your convenience.  Groovy makes unit tests, mock classes, and stubs much easier than standard JUnit tests.  Almost easy enough to encourage developers to actually write tests.  Here are some important reads:</p>
<ul>
<li><a href="http://groovy.codehaus.org/Unit+Testing">Groovy - Unit Testing</a></li>
<li><a href="http://www.google.com/url?sa=t&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fjava%2Flibrary%2Fj-pg11094%2F&amp;ei=CkUIR6S_EJP-iQGm9_iVDA&amp;usg=AFQjCNF6BBMgyRFZTUXBapVIv9ji1AuPNw&amp;sig2=K3ac7CmjH1CWuSEKzG4C6g">Practically Groovy: Unit Test Your Java Code Faster with Groovy</a> (IBM)</li>
<li><a href="http://docs.codehaus.org/display/GROOVY/Unit+Testing">Groovy Unit Testing</a></li>
<li><a href="http://docs.codehaus.org/display/GROOVY/Groovy+Mocks">Groovy Mocks</a></li>
<li><a href="http://docs.codehaus.org/display/GROOVY/Testing+Guide">Groovy Testing Guide</a></li>
</ul>
<p>The gist of it is that you can create you unit tests, to test your Java classes, in Groovy.  Or, if you are testing your Groovy code, you can create in-line stubs and mocks to test your code.  <a href="http://javajeff.blogspot.com/">Jeff Brown</a> gave some good examples at his NFJS presentation.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20class"><span class="kw2">class</span></a> GroovyFileHelper <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> populateMessageBackwards<span class="br0">&#40;</span>pathToFile, message<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> f = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> <a href="http://www.google.de/search?as_q=File&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">File</span></a><span class="br0">&#40;</span>pathToFile<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20if"><span class="kw1">if</span></a><span class="br0">&#40;</span>f.<span class="me1">exists</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20throw"><span class="kw2">throw</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> <a href="http://www.google.de/search?as_q=IllegalArgumentException&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">IllegalArgumentException</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;$pathToFile already exists.&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; f.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20write"><span class="kw7">write</span></a><span class="br0">&#40;</span>message.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20reverse"><span class="kw5">reverse</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This class simply writes a message to a file in reverse.  How would you unit test this in Java.  Well, in reality, you really couldn't, not with a unit test.  Maybe an integration test, because in Java you'd end up testing the file system (using the file.exists and file.write methods), not just your code.  That's an integration test, my friend.  Unless you can somehow throw a mock or stub at the problem.  But how, oh how when the file object is inside the method?  Groovy makes mocks and stubs easy.</p>
<p>Let's take a look at using the StubFor class to stub out the file object.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20void"><span class="kw4">void</span></a> testFileContents<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> messageWrittenToFile</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> stubFile = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> StubFor<span class="br0">&#40;</span><a href="http://www.google.de/search?as_q=File&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">File</span></a><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; stubFile.<span class="me1">demand</span>.<span class="me1">exists</span> <span class="br0">&#123;</span><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20false"><span class="kw2">false</span></a><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; stubFile.<span class="me1">demand</span>.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20write"><span class="kw7">write</span></a> <span class="br0">&#123;</span>data -&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; messageWrittenToFile = data</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; stubFile.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20use"><span class="kw8">use</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> helper = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> GroovyFileHelper<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> path = <span class="st0">'/Users/jeffbrown/someFile.txt'</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; helper.<span class="me1">populateMessageBackwards</span><span class="br0">&#40;</span>path, <span class="st0">'this is some data'</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertEquals <span class="st0">'wrong message was written to file'</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">'atad emos si siht'</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;messageWrittenToFile</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20void"><span class="kw4">void</span></a> testFileAlreadyExists<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> stubFile = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> StubFor<span class="br0">&#40;</span><a href="http://www.google.de/search?as_q=File&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">File</span></a><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; stubFile.<span class="me1">demand</span>.<span class="me1">exists</span> <span class="br0">&#123;</span><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20true"><span class="kw2">true</span></a><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> msg = shouldFail<span class="br0">&#40;</span><a href="http://www.google.de/search?as_q=IllegalArgumentException&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">IllegalArgumentException</span></a><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; stubFile.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20use"><span class="kw8">use</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> helper = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> GroovyFileHelper<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> path = <span class="st0">'/Users/jeffbrown/someFile.txt'</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; helper.<span class="me1">populateMessageBackwards</span><span class="br0">&#40;</span>path,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">'this is some data'</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertEquals <span class="st0">'incorrect exception message'</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">'/Users/jeffbrown/someFile.txt already exists.'</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msg</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Here we are telling the JVM to use our stubFile in place of the File object within our stubFile.use block.  How easy is that?</p>
<p>Mocks work the same way, but it's more of a white box test, where you can specify which methods are to be called while testing.</p>
<h3>2 - Closures (and Delegates)</h3>
<p>On the surface, <a href="http://groovy.codehaus.org/Closures+-+Informal+Guide">closures</a> seem a whole lot like Java's anonymous inner classes, though they are easier to use, and much more flexible.  They have been shown in previous example without any explanation.  Closures are basically blocks of code that take parameters.  Sound a little bit like a function?  Yep.  A function that you can pass around, and which continues to have access to the local variables of where it was created, even when passed around.  Like anonymous inner classes without the need to worry about that ugly "final" keyword.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Closure add = <span class="br0">&#123;</span>firstNumber, secondNumber -&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20return"><span class="kw2">return</span></a> firstNumber + secondNumber<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span class="kw8">println</span></a> <span class="st0">&quot;1 + 2 = ${add(1,2}}&quot;</span></div>
</li>
</ol>
</div>
<p>This simple concept can be used in unit tests, pretty much anywhere else you can think of that it might be useful to pass a function around.</p>
<p>Then there's the idea "delegates".  Pretty cool stuff.  You can define a closure, and then delegate all calls within that closure to any given class.  For example, I might have a closure that looks like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Closure appender = <span class="br0">&#123;</span>first, second -&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20append"><span class="kw7">append</span></a><span class="br0">&#40;</span>first<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20append"><span class="kw7">append</span></a><span class="br0">&#40;</span>second<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This closure could work with many different classes.  Anything that has an "append" method.  the StringBuffer comes to mind.  So how do I get the appender to use the StringBuffer?  Something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?as_q=StringBuffer&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">StringBuffer</span></a> buffer = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> <a href="http://www.google.de/search?as_q=StringBuffer&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">StringBuffer</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">Closure appender = <span class="br0">&#123;</span>first, second -&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20append"><span class="kw7">append</span></a><span class="br0">&#40;</span>first<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20append"><span class="kw7">append</span></a><span class="br0">&#40;</span>second<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">appender.<span class="me1">delegate</span><span class="br0">&#40;</span>buffer<span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Here the closure will call the append method on the buffer class.  The possibilities here are endless!</p>
<h3>1 - ExpandoMetaClass</h3>
<p>The ExpandoMetaClass allows you to do the impossible.  Add methods to existing classes.  The String class, by design, can not be extended.  So how in the world would you go about adding a "swapCase" method to the String class?  (An incredibly useful method that would return a reverse-cased representation of a given String.)  Easy.  Use the <a href="http://groovy.codehaus.org/ExpandoMetaClass">ExpandoMetaClass</a> from Groovy.  (From the Groovy Web Site)</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.de/search?as_q=String&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">String</span></a>.<span class="me1">metaClass</span>.<span class="me1">swapCase</span> = <span class="br0">&#123;</span>-&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span class="kw2">def</span></a> sb = <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20new"><span class="kw2">new</span></a> <a href="http://www.google.de/search?as_q=StringBuffer&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">StringBuffer</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; delegate.<a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20each"><span class="kw5">each</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;sb <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20&amp;lt;"><span class="kw1">&lt;&lt;</span></a> <span class="br0">&#40;</span><a href="http://www.google.de/search?as_q=Character&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">Character</span></a>.<span class="me1">isUpperCase</span><span class="br0">&#40;</span>it <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20as"><span class="kw2">as</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20char"><span class="kw4">char</span></a><span class="br0">&#41;</span> ? <a href="http://www.google.de/search?as_q=Character&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">Character</span></a>.<span class="me1">toLowerCase</span><span class="br0">&#40;</span>it <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20as"><span class="kw2">as</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20char"><span class="kw4">char</span></a><span class="br0">&#41;</span> :</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.de/search?as_q=Character&#038;num=100&#038;hl=en&#038;as_occt=url&#038;as_sitesearch=java.sun.com%2Fj2se%2F1.5.0%2Fdocs%2Fapi%2F"><span class="kw3">Character</span></a>.<span class="me1">toUpperCase</span><span class="br0">&#40;</span>it <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20as"><span class="kw2">as</span></a> <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20char"><span class="kw4">char</span></a><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; sb.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This actually changes the byte code for the String class, making the swapCase method available for the entire life of the JVM.  Imagine being able to dynamically change any class you want to, on the fly.</p>
<p>This same methodology can be used to override methods in otherwise un-overridable files.  Let's say you find a bug in some open source software you are using.  You can either wait for the bug to be fix, fix it yourself and submit it back to the project, or override the method in your own code to make use of a quick fix.</p>
<p>I'm sure I'll look back at this post a few short months from now and have different options, and a different order, but as of right now, this stuff just rocks.</p>
]]></content:encoded>
			<wfw:commentRss>http://joe.kueser.com/2007/10/what-makes-groovy-sogroovy/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
<enclosure url="http://content.answers.com/main/content/ahd4/pron/B0475800.wav" length="7991" type="audio/x-wav" />
		</item>
		<item>
		<title>No Fluff Just Stuff: Fascination and Frustration</title>
		<link>http://joe.kueser.com/2007/10/no-fluff-just-stuff-fascination-and-frustration/</link>
		<comments>http://joe.kueser.com/2007/10/no-fluff-just-stuff-fascination-and-frustration/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 22:30:54 +0000</pubDate>
		<dc:creator>Joe Kueser</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[nfjs]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://joe.kueser.com/?p=4</guid>
		<description><![CDATA[I've always prided myself for always being on the leading, if not bleeding edge of technology. After a weekend at the No Fluff Just Stuff conference in St. Louis (NFJS), I'm starting to feel both fascinated and frustrated.]]></description>
			<content:encoded><![CDATA[<p>I've always prided myself for always being on the leading, if not bleeding edge of technology.  After a weekend at the No Fluff Just Stuff conference in St. Louis (<a href="http://www.nofluffjuststuff.com/show_view.jsp?showId=107" title="No Fluff Just Stuff - St. Louis" target="_blank">NFJS</a>), I'm starting to feel both fascinated and frustrated.</p>
<p>The sessions I attended were heavy into <a href="http://en.wikipedia.org/wiki/Dynamic_programming_language" target="_blank">dynamic programming languages</a>, and <a href="http://en.wikipedia.org/wiki/Agile_software_development" target="_blank">agile software development</a>.  The dynamic programming languages that we got deepest into were <a href="http://en.wikipedia.org/wiki/Groovy_%28programming_language%29" target="_blank">Groovy</a> and <a href="http://en.wikipedia.org/wiki/Ruby_programming_language" target="_blank">Ruby</a>.  Both of these languages make the job of the developer exponentially easier, and are ideal for rapid application development.  This in and of itself should be enough to convince developers to use it, but it gets better.  Through the magic of dynamic programming, things become possible that were not possible before, which opens up a whole new world of possibilities that blow my mind.</p>
<p>The agile discussions focused on what's right, and wrong, with various agile processes.  There were a lot of really great tips, and it was as good to hear about what we are doing wrong as it was to hear about what we are doing wrong.<span id="more-4"></span></p>
<p>So the fascination part should be pretty clear.  By the end of the weekend I had soaked in so much great information, and had learned so much, that I had that partially numb, partially tingly feeling that is fairly unique to such an experience.  There is no question that I will be attending as many NFJS conferences as I can manage.  At least one a year.  The knowledge exchange, and networking, were absolutely invaluable.</p>
<p>What was frustrating for me is coming to the realization that I have lost (or more likely misplaced) my edge.  I find it exceedingly important to select the technologies that are best to solve a given problem.  Choosing the right technology can make the difference between a good solution, and a great solution, or even between a failed project and a raging success.  Despite my strong feelings on this, I find myself settling on technologies that I am comfortable with.  This is clearly not the way I want to develop my career, and not fair to those that count on my technical expertise to solve their problems.</p>
<p>So this stagnant period in my technological development is frustrating.  It is certainly possible that I am being too hard on myself, and that always sticking with Java has been the right decision, but I seriously doubt it.  The project I am currently working on, for example, could clearly benefit from the dynamic nature of Groovy, and would also likely have been well served by Grails, and had I been paying more attention, I could have offered that guidance early in the project's lifecycle.  We're almost to the point where it's too late to bring something like this in, so convincing those I work with that, "I was wrong before...but I'm right now!" is going to be nearly impossible.</p>
<p>On a positive note, like they say, the first step in dealing with a problem is recognizing that there is a problem, so keep checking back here to follow me in my quest to find my place in this techno-world again.</p>
]]></content:encoded>
			<wfw:commentRss>http://joe.kueser.com/2007/10/no-fluff-just-stuff-fascination-and-frustration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

