
- •brief contents
- •contents
- •foreword
- •preface
- •acknowledgments
- •about this book
- •Roadmap
- •Code conventions and downloads
- •Author Online
- •About the author
- •about the cover illustration
- •1 Why add Groovy to Java?
- •1.1 Issues with Java
- •1.1.1 Is static typing a bug or a feature?
- •1.1.2 Methods must be in a class, even if you don’t need or want one
- •1.1.3 Java is overly verbose
- •1.1.4 Groovy makes testing Java much easier
- •1.1.5 Groovy tools simplify your build
- •1.2 Groovy features that help Java
- •1.3 Java use cases and how Groovy helps
- •1.3.1 Spring framework support for Groovy
- •1.3.2 Simplified database access
- •1.3.3 Building and accessing web services
- •1.3.4 Web application enhancements
- •1.4 Summary
- •2 Groovy by example
- •2.1 Hello, Groovy
- •2.2 Accessing Google Chart Tools
- •2.2.1 Assembling the URL with query string
- •2.2.2 Transmitting the URL
- •2.2.3 Creating a UI with SwingBuilder
- •2.3 Groovy Baseball
- •2.3.1 Database data and Plain Old Groovy Objects
- •2.3.2 Parsing XML
- •2.3.3 HTML builders and groovlets
- •2.4 Summary
- •3 Code-level integration
- •3.1 Integrating Java with other languages
- •3.2 Executing Groovy scripts from Java
- •3.2.1 Using JSR223 scripting for the Java Platform API
- •3.2.2 Working with the Groovy Eval class
- •3.2.3 Working with the GroovyShell class
- •3.2.4 Calling Groovy from Java the easy way
- •3.2.5 Calling Java from Groovy
- •3.3 Summary
- •4 Using Groovy features in Java
- •4.1 Treating POJOs like POGOs
- •4.2 Implementing operator overloading in Java
- •4.3 Making Java library classes better: the Groovy JDK
- •4.4 Cool AST transformations
- •4.4.1 Delegating to contained objects
- •4.4.2 Creating immutable objects
- •4.4.3 Creating singletons
- •4.5 Working with XML
- •4.6 Working with JSON data
- •4.7 Summary
- •5 Build processes
- •5.1 The build challenge
- •5.2 The Java approach, part 1: Ant
- •5.3 Making Ant Groovy
- •5.3.1 The <groovy> Ant task
- •5.3.2 The <groovyc> Ant task
- •5.3.3 Writing your build in Groovy with AntBuilder
- •5.3.4 Custom build scripts with Gant
- •5.3.5 Ant summary
- •5.4 The Java approach, part 2: Maven
- •5.4.2 The GMaven project
- •5.4.3 Maven summary
- •5.5 Grapes and @Grab
- •5.6 The Gradle build system
- •5.6.1 Basic Gradle builds
- •5.6.2 Interesting configurations
- •5.7 Summary
- •6 Testing Groovy and Java projects
- •6.1 Working with JUnit
- •6.1.1 A Java test for the Groovy implementation
- •6.1.2 A Groovy test for the Java implementation
- •6.1.3 A GroovyTestCase test for a Java implementation
- •6.2 Testing scripts written in Groovy
- •6.2.1 Useful subclasses of GroovyTestCase: GroovyShellTestCase
- •6.2.2 Useful subclasses of GroovyTestCase: GroovyLogTestCase
- •6.3 Testing classes in isolation
- •6.3.1 Coerced closures
- •6.3.2 The Expando class
- •6.3.3 StubFor and MockFor
- •6.4 The future of testing: Spock
- •6.4.1 The Search for Spock
- •6.4.2 Test well, and prosper
- •6.4.4 The trouble with tribbles
- •6.4.5 Other Spock capabilities
- •6.5 Summary
- •7 The Spring framework
- •7.1 A Spring application
- •7.2 Refreshable beans
- •7.3 Spring AOP with Groovy beans
- •7.4 Inline scripted beans
- •7.5 Groovy with JavaConfig
- •7.6 Building beans with the Grails BeanBuilder
- •7.7 Summary
- •8 Database access
- •8.1 The Java approach, part 1: JDBC
- •8.2 The Groovy approach, part 1: groovy.sql.Sql
- •8.3 The Java approach, part 2: Hibernate and JPA
- •8.4 The Groovy approach, part 2: Groovy and GORM
- •8.4.1 Groovy simplifications
- •8.5 Groovy and NoSQL databases
- •8.5.1 Populating Groovy vampires
- •8.5.2 Querying and mapping MongoDB data
- •8.6 Summary
- •9 RESTful web services
- •9.1 The REST architecture
- •9.3 Implementing JAX-RS with Groovy
- •9.4 RESTful Clients
- •9.5 Hypermedia
- •9.5.1 A simple example: Rotten Tomatoes
- •9.5.2 Adding transitional links
- •9.5.3 Adding structural links
- •9.5.4 Using a JsonBuilder to control the output
- •9.6 Other Groovy approaches
- •9.6.1 Groovlets
- •9.6.2 Ratpack
- •9.6.3 Grails and REST
- •9.7 Summary
- •10 Building and testing web applications
- •10.1 Groovy servlets and ServletCategory
- •10.2 Easy server-side development with groovlets
- •10.2.1 A “Hello, World!” groovlet
- •10.2.2 Implicit variables in groovlets
- •10.3.2 Integration testing with Gradle
- •10.3.3 Automating Jetty in the Gradle build
- •10.4 Grails: the Groovy “killer app”
- •10.4.1 The quest for the holy Grails
- •10.5 Summary
- •A.1 Installing a JDK
- •A.2 Installing Groovy
- •A.3 Testing your installation
- •A.4 IDE support
- •A.5 Installing other projects in the Groovy ecosystem
- •B.1 Scripts and the traditional example
- •B.2 Variables, numbers, and strings
- •B.2.1 Numbers
- •B.2.2 Strings and Groovy strings
- •B.3 Plain Old Groovy Objects
- •B.4 Collections
- •B.4.1 Ranges
- •B.4.2 Lists
- •B.4.3 Maps
- •B.5 Closures
- •B.6 Loops and conditionals
- •B.6.1 Loops
- •B.6.2 Conditionals
- •B.6.3 Elvis
- •B.6.4 Safe de-reference
- •B.7 File I/O
- •B.8.1 Parsing and slurping XML
- •B.8.2 Generating XML
- •B.8.3 Validation
- •B.9 JSON support
- •B.9.1 Slurping JSON
- •B.9.2 Building JSON
- •index
- •Symbols
288 |
CHAPTER 10 Building and testing web applications |
10.5 Summary
This chapter examined ways Groovy helps with testing and building web applications. Unit tests are similar to their Java counterparts, but the Gradle build framework provides a great way to do integration testing of deployed applications.
The Groovy JDK includes classes like ServletCategory, which simplify the implementation of web components. Groovy also has a built-in web scripting engine called groovlets, which makes it easy to work with requests, responses, sessions, and input parameters in a web application.
Finally, this chapter includes a brief discussion of Grails, possibly the biggest Java/ Groovy integration success story of them all.
www.it-ebooks.info

appendix A Installing Groovy
Installing Groovy is easy. This appendix shows you how to do it, with a review of the various options involved.
A.1 Installing a JDK
Groovy generates Java bytecodes that are interpreted by a Java virtual machine. This means you have to have Java installed in order to install Groovy. You need a full Java Development Kit (JDK), rather than a Java Runtime Environment (JRE). You only need the Standard Edition (SE) of Java, rather than the Enterprise1 Edition.
The official JDK for Java SE is available from Oracle at http://mng.bz/83Ct. At the time of this writing, the current version is Java SE 7u25 (Java 7, update 25), but Groovy works on any version of Java 1.5 and above.
Be sure to set an environment variable called JAVA_HOME to point to the installation directory. You also probably want to add the bin folder under JAVA_HOME to your path.
On Windows that will look like this:
C:\> set JAVA_HOME="C:\Program Files\Java\jdk1.7.0"
C:\> set PATH=%JAVA_HOME%\bin;%PATH%
Those commands will set the JAVA_HOME and PATH properties in the local shell. To set them everywhere, right-click My Computer, select Properties, click Advanced, and then click Environment Variables. Add them as System variables, and start a new shell.2
1Just as an aside, when did the word business get deprecated in favor of the word enterprise? Is it a Star Trek thing? Does being an Enterprise Architect mean you design starships for a living? Are Enterprise Java Beans used when making coffee on a starship?
2The specifics of the process will be slightly different on different Windows versions, but the concepts are the same. Set the variables as System environment variables, and start a new shell because Windows won’t update an existing one.
289
www.it-ebooks.info
290 |
APPENDIX A Installing Groovy |
On Macs or Unix flavors, the same settings are
$ export JAVA_HOME=/Library/Java/… $ export PATH=$PATH:$JAVA_HOME/bin
There are too many variations on these statements to count, depending on directory structure and version numbers, but the principles are always the same: install Java, set the JAVA_HOME variable to point to it, and add its bin subdirectory to your path.
A.2 Installing Groovy
Assuming you’ve installed Java, installing Groovy is easy. Again, there are several options, but the basic process comes down to downloading and unzipping the distribution, setting a GROOVY_HOME environment variable, and adding its bin subdirectory to your path.
If you’re not a fan of automated installers or you don’t have root privileges on your machine, you can download a zipped, binary distribution of Groovy directly. The current version can always be found at http://groovy.codehaus.org/Download. You can get either the binary release or the source release (or both). Either way, unzip the download into a directory of your choice.
On Windows, following the same pattern as the Java installation, it’s
C:\> set GROOVY_HOME=C:\Groovy\groovy-2.1.6
C:\> set PATH=%GROOVY_HOME%\bin;%PATH%
On a Mac or Unix, the same process is
$ export GROOVY_HOME=...
$ export PATH=$PATH:$GROOVY_HOME/bin
If you don’t mind installers, a good one is available for Windows. An EXE installer is available on the same download page, which will install Groovy to a directory of your choice, set the GROOVY_HOME variable for you, and add the bin folder underneath it to your path. It also offers to install some optional libraries for you, which are useful and do not interfere with your regular installation in any way. I’ve been using the Windows installer at client sites for years and have never had a problem with it. It will, by the way, notify you if you don’t have a JAVA_HOME environment variable set.
If you’re on a Mac or other Unix box, you have other convenient alternatives available. First, there’s a MacPorts (www.macports.org) option. Run
$ sudo port install groovy
That will download and install the latest version. If you prefer HomeBrew (http:// mxcl.github.io/homebrew), the relevant command is
$ brew install groovy
That, too, will download the latest version, install it, and create soft links to the executable scripts in your path.
www.it-ebooks.info
Testing your installation |
291 |
The other major alternative is to use GVM, the Groovy enVironment Manager (http://gvmtool.net). This is the best option if you plan to switch versions at any time. GVM is installed using curl, with this command:
$ curl –s get.gvmtool.net | bash
GVM assumes you are using a bash shell, but the same process works for most Unix flavors. It also works on Windows if you install Cygwin. See the web page for details.
The great advantage of GVM is that it makes switching versions almost trivially easy. If you have GVM installed, you can find out which versions of Groovy are available by typing
$ gvm list groovy
You can install the latest one like this:
$ gvm install groovy
If you supply a version number to the install command, you can select which version of Groovy to install. You can switch from one version of Groovy to another using
$ gvm use groovy [version]
If the version you request isn’t installed, GVM will download and install it for you. In my own work, I don’t switch Groovy versions that often, but I switch Grails versions frequently and the same tool works for Groovy, Grails, Griffon, and a few other software distributions. GVM installs software under a .gvm folder in your home directory, so you should set the GROOVY_HOME variable to point there. For example, on my Mac, I have
$ export GROOVY_HOME=/Users/kousen/.gvm/groovy/current
That’s useful because switching versions through GVM updates the current link. I don’t have to explicitly add that folder to my path, though, because the tool adds soft links to a bin folder already in my path.
A.3 Testing your installation
The easiest way to see if your Groovy installation is working is to try out the Groovy shell or the Groovy console. If you type
$ groovysh
you should get a response like this:
Groovy Shell (2.1.5, JVM: 1.7.0_11) Type 'help' or '\h' for help.
------------------------------------
groovy:000> println 'Hello, World!' Hello, World!
===> null groovy:000>
www.it-ebooks.info