Top Banner
Real Life Groovy Paul Bakker
34

Real Life Groovy

May 11, 2015

Download

Technology

Paul Bakker

Presentation about using Groovy in real Java projects.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Real Life Groovy

Real Life Groovy

Paul Bakker

Page 2: Real Life Groovy

What I wish this talk is about

Introduction

Page 3: Real Life Groovy

Introduction

What this talk actually is about

Page 4: Real Life Groovy

About me

Paul Bakker• Trainer Info Support• Java Magazine author• Big time Groovy fan ;-)

EJB 3JSFSeamJavaFXSpringHibernateFlex

Page 5: Real Life Groovy

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

• 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

Lists

Page 8: Real Life Groovy

Maps

Page 9: Real Life Groovy

The Groovy 101Closures

Page 10: Real Life Groovy

Meta Programming:The Expando

Page 11: Real Life Groovy

Meta Programming:Changing behavior at runtime

Page 12: Real Life Groovy

Groovy’s strong points

• Working with collections• Changing behavior at runtime• The builder pattern• It’s all just Java

Page 13: Real Life Groovy

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

Generating test data

Page 15: Real Life Groovy

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

The schema

MESSAGEmessage_idsubjectbodyfirstnamelastnameusernameattachment

MESSAGEmessage_idsubjectbodyuser_id

USERuser_idusernamefirstnamelastnameemail

FILEfile_idfilenamedata

ATTACHMENTmessage_idfile_id

*

1

1*

1*

Page 17: Real Life Groovy

A Groovy SQL script

Page 18: Real Life Groovy

Running the script

• Compile the script• Jar the class files• Add Groovy.jar dependency• java -jar GroovyDBScript.jar

Page 19: Real Life Groovy

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

The Java class to test

Page 21: Real Life Groovy

The Java Unit Test

Page 22: Real Life Groovy

The Groovy Unit Test

Page 23: Real Life Groovy

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

A Groovy Service Proxy

Page 25: Real Life Groovy

Building

Uses Implements

Page 26: Real Life Groovy

Groovy Joint compiler

groovyc*.groovy*.java‐j‐Jsource=1.4‐Jtarget=1.4

Page 27: Real Life Groovy

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

A Groovy web service client

Page 29: Real Life Groovy

Using the Groovy Service

getTitles(“Java”)

[“Effective Java”, ...]

Page 30: Real Life Groovy

A Groovy Session Bean

Replace any Java code

Page 31: Real Life Groovy

A warning about domain classes

Don’t forget about the metaClass property

Groovy domain class

ActionScript class

Serialize

MusthavemetaClassproperty!

Page 32: Real Life Groovy

GANT

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

• Works in every build environment

Page 33: Real Life Groovy

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

Should we ever write Java code?

• More readable code• Better unit testing• Advanced frameworks

• Not everybody knows Groovy• No static code analysis