Practically Groovy 1.6 (Beginning): a 3-day class

"Java is dead -- Long live Java!" If you're a seasoned Java developer, you've heard the "Java is dead" refrain before. Each new programming language du jour claims to be the next big Java-killer, and yet after nearly 15 years Java continues to be one of the most popular platforms for business development. Groovy changes this refrain by focusing on deep, seamless integration with your existing Java code instead of imploring you to "burn the boats" and rewrite everything from scratch. Come find out how learning Groovy can -- ironically -- make you a better Java developer. Based on the book "Groovy Recipes: Greasing the Wheels of Java", you'll be amazed at how quickly you'll be up to speed with this next generation Java language.

Objective

To quickly bring seasoned Java developers up to speed with Groovy, a next-generation Java language. This is a fast-paced, hands-on, "learn by doing" class.

Goals

You will be comfortable installing and upgrading Groovy, as well as configuring your text editor or IDE for Groovy support. You'll see how Groovy seamlessly integrates with existing Java code, and extends familiar classes like Strings, Lists, and Maps in a powerful yet natural way. You'll learn about the File I/O, XML manipulation, database integration, and networking capabilities of Groovy that dramatically reduce the amount of code that you need to write.

Course Outline

Getting Started

First things first: you'll install Groovy and configure your text editor or IDE. You'll run a script and compile Groovy into a Java-compliant class. You'll work with the Groovy Shell and Console. And finally, you'll run a simple Groovlet on a web server.

New to Groovy

In this section, we'll focus on the subtle but important changes that Groovy brings to the familiar Java syntax. Groovy supports 100% of Java syntax, but if you keep writing things the way you used to, you'll miss out on all of the time savers and syntactic sugar. We'll discuss automatic imports and all of the optional syntax -- semicolons, parenthesis, return statements, datatype declaration (i.e. Duck-typing), and exception handling. You'll see new features like operator overloading and null-safe references. We'll discuss autoboxing and Groovy Truth. You'll see Strings and GStrings, List and Map shortcuts, Ranges, Closures, Blocks, and more.

Java and Groovy Integration

Groovy is written by Java developers, for Java developers. In this section, you'll get an introduction to GroovyBeans (or POGOs). You'll see how Groovy auto-generates your getters/setters and adds powerful getProperty()/setProperty() methods. You'll work with constructor shortcuts like the named arg / vararg one that Groovy creates for every class. You'll learn how to set up optional method parameters, varargs, and default values. Calling Java from Groovy and Groovy from Java is a piece of cake if you set up the joint compiler in your build script.

Groovy from the Command-line

Java for shell scripting? (Yeah, right.) Groovy, on the other hand, will pleasantly surprise you. In this section, we'll talk about running uncompiled Groovy scripts from the command prompt and pulling in command-line arguments from the user. You can call other Groovy scripts as easily as you call native operating system commands. Groovy's talent in acting as a glue language is on full display here. Groovy blurs the distinction between native operating system tasks and Java libraries with real aplomb, making administrative tasks -- dare I say it? -- almost enjoyable.

File Tricks

Groovy offers many shortcuts for dealing with files and directories. Listing files, copying files, renaming files, deleting files -- Groovy brings welcome help for all these mundane tasks. The fact that Groovy adds new methods directly to the standard JDK classes such as java.io.File make these new features feel like a natural part of the language. You'll also learn how the leverage native capabilities of Ant outside of build.xml using a Groovy AntBuilder. Creating tarballs and zip files has never been easier.

Parsing XML

Dealing with XML is an everyday part of most software developers lives. Groovy brings extraordinary, ninja-like XML capabilities to Java. You'll understand the difference between XmlParser and XmlSlurper. You'll learn how to parse XML documents, accessing both elements and attributes with ease. You'll work with namespaces, hyphenated element names, and other potentially pathological constructs.

Writing XML

Once you know how to read XML with Groovy, writing XML is the next logical step. You'll understand the difference between MarkupBuilder and StreamingMarkupBuilder. You'll create XML fragments and whole documents. You'll deal with namespaces, declarations, processing instructions, comments, and CDATA blocks. Finally, you'll write XML to a file and handle pretty-printing issues.

Networking and Web Services

Sun famously said, "the network is the computer", but Java doesn't do much to streamline the process. Thankfully, Groovy steps right up. You'll work with IP addresses and resolve DNS names. You'll use Groovy as a RESTful client -- making HTTP GET, POST, PUT, and DELETE requests. You'll manage both text and binary data. You'll make a SOAP request, an XML-RPC request, and parse Atom and RSS feeds.

Databases

We're in the heyday of Object-Relational Mappers like Hibernate and GORM. But sometimes you need to get fast access to the underlying rows and tables. Groovy makes this easy. You'll call Sql.newInstance(). You'll build quick queries with eachRow() and firstRow(). You'll call execute() and executeUpdate(). You'll make database calls from Ant and AntBuilder. And finally, you'll back up a database table to XML and restore it.

Testing Java with Groovy

The flexibility and conciseness of Groovy makes it an ideal testing toolkit. You'll integrate GroovyTestCases with JUnit TestCases. You'll leverage Cobertura for code coverage. You'll mock out interfaces with the "as" keyword. You'll understand Groovy's native MockFor and StubFor facilities. And you'll get started with EasyB -- a Groovy DSL for Behavior-Driven Development (BDD).