Real Life Groovy
-
Upload
paul-bakker -
Category
Technology
-
view
1.351 -
download
0
description
Transcript of Real Life Groovy
Real Life Groovy
Paul Bakker
What I wish this talk is about
Introduction
Introduction
What this talk actually is about
About me
Paul Bakker• Trainer Info Support• Java Magazine author• Big time Groovy fan ;-)
EJB 3JSFSeamJavaFXSpringHibernateFlex
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
• A dynamic language • Compiles to byte code• Running in the JVM• Completely compatible with Java
So what was Groovy again?
Lists
Maps
The Groovy 101Closures
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 all just Java
Generating test data
• Populate a database with test data• Automated & Repeatable• You might use DBUnit
–That’s a lot of XML...
Generating test data
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
The schema
MESSAGEmessage_idsubjectbodyfirstnamelastnameusernameattachment
MESSAGEmessage_idsubjectbodyuser_id
USERuser_idusernamefirstnamelastnameemail
FILEfile_idfilenamedata
ATTACHMENTmessage_idfile_id
*
1
1*
1*
A Groovy SQL script
Running the script
• Compile the script• Jar the class files• Add Groovy.jar dependency• java -jar GroovyDBScript.jar
Unit Testing
• Mocking is really useful–But it’s quite a lot of work
• Groovy’s dynamic nature and Meta Programming helps
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
Java Class Java Service delegate
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 EJB3 Session bean
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
Groovy domain class
ActionScript class
Serialize
MusthavemetaClassproperty!
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 value
Should we ever write Java code?
• More readable code• Better unit testing• Advanced frameworks
• Not everybody knows Groovy• No static code analysis