List of Listings – Making Java Groovy: Foreword by Guillaume Laforge

List of Listings

Chapter 1. Why add Groovy to Java?

Listing 1.1. Sorting strings using the Collections.sort method

Listing 1.2. A Java class to sort strings

Listing 1.3. A Java class representing a task

Listing 1.4. A Spock test that checks each Java sorting method

Chapter 2. Groovy by example

Listing 2.1. Building a Swing UI 3D pie chart using Google Chart

Listing 2.2. Stadium.groovy: a POGO to hold stadium information

Listing 2.3. Geocoder.groovy, which uses the Google geocoder to compute lat and lng

Listing 2.4. populate_stadium_data.groovy

Listing 2.5. A portion of GetGameData, showing the attributes and initialization

Listing 2.6. boxscore.xml: the box score from game 4 of the 2007 World Series

Listing 2.7. The getGame method in GetGameData.groovy

Listing 2.8. The getGames method from GetGameData

Listing 2.9. GetGameDataTests.groovy: a JUnit 4 test case

Listing 2.10. GameServlet.groovy: a groovlet for Groovy Baseball

Chapter 3. Code-level integration

Listing 3.1. Finding all the available script engine factories

Listing 3.2. Using the ScriptEngine to execute a simple Groovy script

Listing 3.3. A JUnit test case to check the JSR 223 script engine results

Listing 3.4. JUnit 4 test class verifying results of calling Eval methods from Java

Listing 3.5. Using GroovyShell and Binding to invoke the Google geocoder

Listing 3.6. A Groovy class for geocoding

Chapter 4. Using Groovy features in Java

Listing 4.1. A simple POJO representing a person

Listing 4.2. A Department with a map of Employees and operator overriding

Listing 4.3. A Spock test to check the operator overloading methods in a Java class

Listing 4.4. Base 64 encoding and decoding username/password information

Listing 4.5. GroundHog Day—an example of Date and Calendar in the Groovy JDK

Listing 4.6. A JUnit test in Groovy to demonstrate the delegated methods

Listing 4.7. Testing the ImmutablePoint class

Listing 4.8. A Spock test for the ImmutableLine class

Listing 4.9. A Spock test for the ImmutablePath class

Listing 4.10. Converting songs to XML and back

Listing 4.11. chuck_norris.groovy, which processes data from ICNDB

Chapter 5. Build processes

Listing 5.1. build.xml: A simple Ant build file for a “Hello, World” Java application

Listing 5.2. Execution of the default task in the “Hello, World” Ant build

Listing 5.3. A trivial Ant build that executes Groovy code in a task

Listing 5.4. Extending the “Hello, World” build to mix Java and Groovy sources

Listing 5.5. antbuilder.groovy, which copies itself

Listing 5.6. Simplifying the build script using the with method

Listing 5.7. A Groovy AntBuilder script port of the build.xml file from listing 5.1

Listing 5.8. The Maven pom.xml file for a standard Java project

Listing 5.9. Weather.groovy, a POGO to hold weather results from the web service

Listing 5.10. YahooParser.groovy, which accesses and parses the weather service

Listing 5.11. The Maven pom.xml file produced by the GMaven project

Listing 5.12. Part of the Java stub generated from Weather.groovy

Listing 5.13. Using Apache Commons Math and a database driver together

Listing 5.14. A Greeting POJO to demonstrate a Gradle build

Listing 5.15. A JUnit test for the Greeting POJO

Listing 5.16. A build.gradle file for the POJO application with testing

Listing 5.17. A Groovy test for the POJO, making this a mixed Java/Groovy project

Listing 5.18. A build.gradle file for a mixed Java/Groovy project

Listing 5.19. Inputs/outputs example from the incrementalBuilds Gradle sample

Chapter 6. Testing Groovy and Java projects

Listing 6.1. A Java interface with three methods

Listing 6.2. The Java implementation of the UtilityMethods interface

Listing 6.3. The Groovy implementation of the UtilityMethods interface

Listing 6.4. A Java JUnit test to check the Groovy implementation

Listing 6.5. A Groovy JUnit test for a Java implementation

Listing 6.6. A GroovyTestCase test for the Java implementation

Listing 6.7. A class to hold all the script tests

Listing 6.8. The “Hello, World!” script

Listing 6.9. A test that captures script output

Listing 6.10. A script with a binding variable

Listing 6.11. Setting a binding variable to test a script

Listing 6.12. Testing Groovy scripts using GroovyShellTestCase

Listing 6.13. A test for the addition script, calc.groovy

Listing 6.14. A script that uses a logger

Listing 6.15. Capturing log output in a test case

Listing 6.16. AccountService (in Java): uses the AccountDAO to find Accounts

Listing 6.17. The AccountDAO interface, with CRUD methods for the Account class

Listing 6.18. A JUnit 4 test case for the AccountService, in Groovy, with a stubbed DAO

Listing 6.19. Using a map of closures to implement an interface

Listing 6.20. The complete FileAccountDAO implementation, in Groovy

Listing 6.21. FileAccountDAO unit test, using an Expando to stub the File

Listing 6.22. The Groovy Baseball Geocoder class, revisited

Listing 6.23. GeocoderIntegrationTests.groovy: a JUnit 4 test for the geocoder

Listing 6.24. GeocoderUnitTest.groovy: tests geocoder even if not online

Listing 6.25. Building and testing with Spock using Gradle

Listing 6.26. A specification verifying basic java.lang.String behavior

Listing 6.27. Another spec, illustrating the old method

Listing 6.28. Data-driven Spock test

Listing 6.29. A Tribble class in Groovy

Chapter 7. The Spring framework

Listing 7.1. An Account POGO in Groovy that uses BigDecimal

Listing 7.2. The AccountDAO interface, in Java

Listing 7.3. Implementing the AccountDAO using JdbcTemplate, in Groovy

Listing 7.4. A portion of the AccountService class in Java

Listing 7.5. The Gradle build file for the account application

Listing 7.6. Spock tests for the JdbcAccountDAO implementation

Listing 7.7. Spock tests for the service class

Listing 7.8. A trivial mortgage application class in Groovy

Listing 7.9. The Evaluator interface in Java

Listing 7.10. A Java evaluator, with a rather lenient loan policy

Listing 7.11. A Groovy mortgage evaluator deployed as source code

Listing 7.12. The bean configuration file with the refreshable Groovy evaluator bean

Listing 7.13. A demo application that loads the Groovy bean and calls approve 10 times

Listing 7.14. A Java aspect that logs changes to properties

Listing 7.15. A simple POJO with three set methods

Listing 7.16. The Spring bean configuration file for AOP

Listing 7.17. A JUnit 4 test case to exercise the POJO

Listing 7.18. A Groovy aspect for printing property values before they are changed

Listing 7.19. An aspect tracking methods in the Account POGO

Listing 7.20. Additions to bean configuration file for an inline scripted aspect

Listing 7.21. An account processor that debits each account by one dollar

Listing 7.22. A Java configuration file that declares the AccountProcessor bean

Listing 7.23. A Spock test to check the behavior of the AccountProcessor

Listing 7.24. A Spring configuration file in Groovy as a factory for Accounts

Listing 7.25. A Spock test for the prototype Accounts

Listing 7.26. The complete Gradle build file, including Grails-Spring dependencies

Listing 7.27. The BeanBuilderTests class with its first test case

Listing 7.28. Setting bean properties in the BeanBuilder, from BeanBuilderTests

Listing 7.29. Defining several related beans with the BeanBuilder

Listing 7.30. Defining beans at different scopes

Listing 7.31. Defining an aspect using BeanBuilder

Chapter 8. Database access

Listing 8.1. The Product class, a POJO mapped to a database table

Listing 8.2. A DAO interface for the Product class

Listing 8.3. A JDBC implementation of the DAO interface

Listing 8.4. Adding creation and population of the Product table to the DAO

Listing 8.5. The findProductById method with all the required ceremony

Listing 8.6. A ProductDAO implementation using the groovy.sql.Sql class

Listing 8.7. The complete SqlProductDAO class, except for the parts already shown

Listing 8.8. The JpaProductDAO class, which uses JPA classes to implement the DAO

Listing 8.9. A Spring test case for the JPA DAO implementation

Listing 8.10. The Product class, this time as a POGO in a Grails application

Listing 8.11. The Customer class. Customers have many orders (hopefully).

Listing 8.12. The Order class, which has many orders and belongs to a customer

Listing 8.13. The OrderLine POGO, which is assembled to build an Order

Listing 8.14. Initialization code in BootStrap.groovy

Listing 8.15. A portion of the JSON object representing the movie Blazing Saddles

Listing 8.16. Movie.groovy, which wraps the JSON data

Listing 8.17. A portion of the method that converts JSON movies to Movie instances

Listing 8.18. A JUnit test to verify the JSON conversion

Chapter 9. RESTful web services

Listing 9.1. A Person POGO, used in the RESTful web service

Listing 9.2. The DAO interface with the CRUD methods for Person

Listing 9.3. The Spock test method to insert and delete a new Person

Listing 9.4. Java resource class for Person POJO

Listing 9.5. A Spock test for the PersonResource with a convenient test server

Listing 9.6. A Groovy implementation of the PersonResource class

Listing 9.7. Setting prev, self, and next link headers for each person

Listing 9.8. Walking through the data using link headers

Listing 9.9. Using a JsonBuilder to produce nested links

Listing 9.10. Parsing a Person instance from a string

Listing 9.11. A groovlet that processes and produces XML

Chapter 10. Building and testing web applications

Listing 10.1. A simple servlet implemented in Groovy

Listing 10.2. Methods for HttpSession from groovy.servlet.ServletCategory

Listing 10.3. The HelloNameServlet class, which uses the ServletCategory

Listing 10.4. The HelloNameServletTest class, which uses Spring’s mock objects

Listing 10.5. HelloGroovletTest, an integration test for the hello groovlet

Listing 10.6. A unit test for the groovlet using GroovyShell and Binding

Listing 10.7. Using GroovyShellTestCase to simplify unit-testing groovlets

Listing 10.8. The GameService groovlet from the Groovy Baseball application

Listing 10.9. HelloServletJavaTest: a servlet test class using mock objects

Listing 10.10. Gradle build file for web application, using the war plugin

Listing 10.11. ServletIntegrationTests.groovy: accessing deployed servlets

Listing 10.12. The Quest domain class

Listing 10.13. Tasks belong to a Quest

Listing 10.14. The Knight class, which is associated with a Quest and a Castle

Listing 10.15. The Castle, which stores location information

Listing 10.16. The static Castle controller class

Listing 10.17. The Geocoder, yet again, which works on Castles this time

Listing 10.18. The unit test for the GeocoderService

Listing 10.19. Dependency injection of a service into a controller

Listing 10.20. Methods added to the GeocoderService to support the mapping plugin

Listing 10.21. Modifications to list.gsp to display a Google Map of castles

Appendix B. Groovy by feature

Listing B.1. Running “Hello, World!” in the Groovy shell

Listing B.2. numbers.groovy, showing method calls on numeric literals

Listing B.3. Using dates in a range with Java’s Calendar class

Listing B.4., an application to read a line of numbers and add them

Listing B.5. Slurping XML

Listing B.6. Generating XML using a MarkupBuilder

Listing B.7. An XML schema for the library XML