Skip to content

psramkumar/play-groovy

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Groovy Plugin for Play Framework

This is a Groovy plugin for the Play Framework -- it'll allow you to write your Play code in Groovy rather than Java (or Scala) and test it with Spock and Geb.

Done

  • Groovy (1.8.3) compilation (with recompile on reload)
  • can cross-compile java and groovy sources
  • can put multiple classes in files (classes not tied to filenames)
  • Spock tests
  • Treated as normal unit tests but with nicer syntax and better error output
  • Geb tests (unfinished)
  • Can be used to do functional or selenium type testing (htmlunit)
  • can also visit external pages as part of a test

Installation

This module isn't on the Play repo yet, so if you want to use it you'll need to install it manually.

  • clone the repository and run play build-module inside it
  • this will give you a dist/play-groovy-0.1.zip
  • unzip this into {play-home}/modules/groovy-0.1
  • create a new play groovy app: play new groovyapp --with groovy and wait for the dependencies to download
  • enjoy your play groovy application! go forth and find some bugs

Usage

This isn't as radical as the Play Scala plugin. You can still use JPA and structure your models in the same way as you would in Java... but you can cut down on line noise using Groovy's syntax, and exploit its dynamic nature when it's useful. Because it supports cross-compilation, you can also keep most of your app Java and add a few Groovy files (such as Spock tests) when you want.

Any work you need to do with collections in particular will benefit from Groovy's expressive syntax and methods. The Differences from Java page on the Groovy website has a great list of differences.

Take a look at the samples/blog app, which is a Groovyified version of the Play 'yabe' app.

Accessors/mutators

Play has you set your model fields as public and then autogenerates get and set methods for them. In Groovy, you should not set a visibility modifier for your fields. The Groovy compiler will default these fields to private but create a get and set method for them, as Play does.

So while in standard Play you'd do this:

public class Post extends Model {
	public String title;
}
...
Post p = new Post();
p.title = 'Hello';
System.out.println(p.title);

In Groovy, do this:

class Post extends Model {
	String title
}
...
Post p = new Post()
p.title = 'Hello'
println(p.title)

In both cases, you can override the get/set methods with your own.

Constructors

If you like, you can leave your models without a constructor and then instantiate them using the Groovy bean constructor, like so:

class Post extends Model {
	String title
	String content
}
...
Post p = new Post(title: 'Hello World', content: 'Lorem ipsum...')

But be aware this will reduce the amount of compile-time checking that can happen.

Tests

Perhaps the most useful thing about this is the built-in Spock and Geb support. Spock lets you write your unit and functional tests in a really expressive way, and formats errors in a monospaced, readable fashion.

You can also use Geb to do functional/selenium type testing. I'm still not sure exactly how useful it is, but it's there for you to play with.

Here's an example of a Spock test in play:

def 'try to connect as a user'() {
	given:
	new User(email: '[email protected]', password: 'secret', fullname: 'Bob').save()

	expect:
	User.connect('[email protected]', 'secret') != null
	User.connect('[email protected]', 'wrong-password') == null
	User.connect('[email protected]', 'secret') == null
}

Todo

  • More robust sample/testing apps for better test coverage
  • Geb support is not finished yet
  • only supports GET requests
  • doesn't support cookies
  • doesn't return js/css yet, or get content types
  • IDE support (might work now, I haven't tried yet)

Future

  • JPA is great but doesn't look so Groovy. I wonder if I could get Grails' GORM to work...

About

groovy plugin for Play Framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 56.8%
  • Groovy 31.3%
  • Java 8.7%
  • Python 3.2%