Real Life Groovy

   Paul Bakker
Introduction




What I wish this talk is about
Introduction




What this talk actually is about
About me


  Paul Bakker
                            EJB 3
• Trainer Info Support
                            JSF
• Java M...
Outline


• Freshing up your Groovy knowledge
• Real life Groovy examples
  –Test Data
  –Database conversion
  –Unit Test...
So what was Groovy again?



•   A dynamic language
•   Compiles to byte code
•   Running in the JVM
•   Completely compat...
Lists
Maps
The Groovy 101
Closures
Meta Programming:
The Expando
Meta Programming:
Changing behavior at runtime
Groovy’s strong points


•   Working with collections
•   Changing behavior at runtime
•   The builder pattern
•   It’s al...
Generating test data


• Populate a database with test data
• Automated & Repeatable
• You might use DBUnit
  –That’s a lo...
Generating test data
The problem:
           Database conversion

                 Old legacy schema




      Redesigned schema for new applic...
The schema

                          MESSAGE              ATTACHMENT
                                       1
MESSAGE    ...
A Groovy SQL script
Running the script



•   Compile the script
•   Jar the class files
•   Add Groovy.jar dependency
•   java -jar GroovyDBS...
Unit Testing


• Mocking is really useful
  –But it’s quite a lot of work
• Groovy’s dynamic nature and Meta
  Programming...
The Java class to test
The Java Unit Test
The Groovy Unit Test
Making untestable code testable

• Replace Java code at joint points
  –Replace direct service calls with a Groovy proxy

...
A Groovy Service Proxy
Building




Uses
           Implements
Groovy Joint compiler

groovyc
*.groovy
*.java
‐j
‐Jsource=1.4
‐Jtarget=1.4
A leap of faith

• Replace some Java code by more readable
  Groovy code
• XML Converters
• A Groovy web service client
• ...
A Groovy web service client
Using the Groovy Service



  getTitles(“Java”)


  [“Effective Java”, ...]
A Groovy Session Bean


Replace any Java code
A warning about domain classes

Don’t forget about the metaClass property


                 Serialize
 Groovy            ...
GANT


• ANT without XML
• More natural build definition
• Use control structures

• Works in every build environment
Just get the Groove

• Groovy makes a lot of tasks easier
• Fits in most environments
• Use Groovy where it really adds va...
Should we ever write Java code?

          • More readable code
          • Better unit testing
          • Advanced frame...
Upcoming SlideShare
Loading in …5
×

Real Life Groovy

1,648 views

Published on

Presentation about using Groovy in real Java projects.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,648
On SlideShare
0
From Embeds
0
Number of Embeds
56
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Real Life Groovy

  1. 1. Real Life Groovy Paul Bakker
  2. 2. Introduction What I wish this talk is about
  3. 3. Introduction What this talk actually is about
  4. 4. About me Paul Bakker EJB 3 • Trainer Info Support JSF • Java Magazine author Seam • Big time Groovy fan ;-) JavaFX Spring Hibernate Flex
  5. 5. Outline • Freshing up your Groovy knowledge • Real life Groovy examples –Test Data –Database conversion –Unit Testing • A leap of faith –A Groovy web service client –Generating XML –A Groovy session bean
  6. 6. So what was Groovy again? • A dynamic language • Compiles to byte code • Running in the JVM • Completely compatible with Java
  7. 7. Lists
  8. 8. Maps
  9. 9. The Groovy 101 Closures
  10. 10. Meta Programming: The Expando
  11. 11. Meta Programming: Changing behavior at runtime
  12. 12. Groovy’s strong points • Working with collections • Changing behavior at runtime • The builder pattern • It’s all just Java
  13. 13. Generating test data • Populate a database with test data • Automated & Repeatable • You might use DBUnit –That’s a lot of XML...
  14. 14. Generating test data
  15. 15. The problem: Database conversion Old legacy schema Redesigned schema for new application • Reuse most of the data • Don’t loose references • But don’t limit new mappings
  16. 16. The schema MESSAGE ATTACHMENT 1 MESSAGE message_id message_id * subject file_id message_id body subject * * user_id body 1 firstname lastname FILE username USER attachment file_id user_id 1 filename username data firstname lastname email
  17. 17. A Groovy SQL script
  18. 18. Running the script • Compile the script • Jar the class files • Add Groovy.jar dependency • java -jar GroovyDBScript.jar
  19. 19. Unit Testing • Mocking is really useful –But it’s quite a lot of work • Groovy’s dynamic nature and Meta Programming helps
  20. 20. The Java class to test
  21. 21. The Java Unit Test
  22. 22. The Groovy Unit Test
  23. 23. Making untestable code testable • Replace Java code at joint points –Replace direct service calls with a Groovy proxy Groovy Java Service Java Class Proxy delegate
  24. 24. A Groovy Service Proxy
  25. 25. Building Uses Implements
  26. 26. Groovy Joint compiler groovyc
*.groovy
*.java
‐j
‐Jsource=1.4
‐Jtarget=1.4
  27. 27. A leap of faith • Replace some Java code by more readable Groovy code • XML Converters • A Groovy web service client • A Groovy EJB3 Session bean
  28. 28. A Groovy web service client
  29. 29. Using the Groovy Service getTitles(“Java”) [“Effective Java”, ...]
  30. 30. A Groovy Session Bean Replace any Java code
  31. 31. A warning about domain classes Don’t forget about the metaClass property Serialize Groovy ActionScript domain class class Must
have
 metaClass
 property!
  32. 32. GANT • ANT without XML • More natural build definition • Use control structures • Works in every build environment
  33. 33. Just get the Groove • Groovy makes a lot of tasks easier • Fits in most environments • Use Groovy where it really adds value
  34. 34. Should we ever write Java code? • More readable code • Better unit testing • Advanced frameworks • Not everybody knows Groovy • No static code analysis

×