real life groovy
DESCRIPTION
Presentation about using Groovy in real Java projects.TRANSCRIPT
![Page 1: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/1.jpg)
Real Life Groovy
Paul Bakker
![Page 2: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/2.jpg)
What I wish this talk is about
Introduction
![Page 3: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/3.jpg)
Introduction
What this talk actually is about
![Page 4: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/4.jpg)
About me
Paul Bakker• Trainer Info Support• Java Magazine author• Big time Groovy fan ;-)
EJB 3JSFSeamJavaFXSpringHibernateFlex
![Page 5: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/5.jpg)
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
![Page 6: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/6.jpg)
• A dynamic language • Compiles to byte code• Running in the JVM• Completely compatible with Java
So what was Groovy again?
![Page 7: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/7.jpg)
Lists
![Page 8: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/8.jpg)
Maps
![Page 9: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/9.jpg)
The Groovy 101Closures
![Page 10: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/10.jpg)
Meta Programming:The Expando
![Page 11: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/11.jpg)
Meta Programming:Changing behavior at runtime
![Page 12: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/12.jpg)
Groovy’s strong points
• Working with collections• Changing behavior at runtime• The builder pattern• It’s all just Java
![Page 13: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/13.jpg)
Generating test data
• Populate a database with test data• Automated & Repeatable• You might use DBUnit
–That’s a lot of XML...
![Page 14: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/14.jpg)
Generating test data
![Page 15: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/15.jpg)
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
![Page 16: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/16.jpg)
The schema
MESSAGEmessage_idsubjectbodyfirstnamelastnameusernameattachment
MESSAGEmessage_idsubjectbodyuser_id
USERuser_idusernamefirstnamelastnameemail
FILEfile_idfilenamedata
ATTACHMENTmessage_idfile_id
*
1
1*
1*
![Page 17: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/17.jpg)
A Groovy SQL script
![Page 18: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/18.jpg)
Running the script
• Compile the script• Jar the class files• Add Groovy.jar dependency• java -jar GroovyDBScript.jar
![Page 19: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/19.jpg)
Unit Testing
• Mocking is really useful–But it’s quite a lot of work
• Groovy’s dynamic nature and Meta Programming helps
![Page 20: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/20.jpg)
The Java class to test
![Page 21: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/21.jpg)
The Java Unit Test
![Page 22: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/22.jpg)
The Groovy Unit Test
![Page 23: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/23.jpg)
Making untestable code testable
• Replace Java code at joint points–Replace direct service calls with a Groovy proxy
Java Class Java Service delegate
Groovy Proxy
![Page 24: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/24.jpg)
A Groovy Service Proxy
![Page 25: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/25.jpg)
Building
Uses Implements
![Page 26: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/26.jpg)
Groovy Joint compiler
groovyc*.groovy*.java‐j‐Jsource=1.4‐Jtarget=1.4
![Page 27: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/27.jpg)
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
![Page 28: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/28.jpg)
A Groovy web service client
![Page 29: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/29.jpg)
Using the Groovy Service
getTitles(“Java”)
[“Effective Java”, ...]
![Page 30: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/30.jpg)
A Groovy Session Bean
Replace any Java code
![Page 31: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/31.jpg)
A warning about domain classes
Don’t forget about the metaClass property
Groovy domain class
ActionScript class
Serialize
MusthavemetaClassproperty!
![Page 32: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/32.jpg)
GANT
• ANT without XML• More natural build definition• Use control structures
• Works in every build environment
![Page 33: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/33.jpg)
Just get the Groove
• Groovy makes a lot of tasks easier• Fits in most environments• Use Groovy where it really adds value
![Page 34: Real Life Groovy](https://reader034.vdocument.in/reader034/viewer/2022052619/5550bc4bb4c905fa618b4f1f/html5/thumbnails/34.jpg)
Should we ever write Java code?
• More readable code• Better unit testing• Advanced frameworks
• Not everybody knows Groovy• No static code analysis