Posts mit dem Label Groovy werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Groovy werden angezeigt. Alle Posts anzeigen

Samstag, 27. September 2014

Spock Old Feature

Yesterday I watched the Idiomatic Spock presentation with Rob Fletcher on YouTube, a great talk about testing with Spock. A small feature I didn't know that this exits in Spock is the old method. With this static method from the Spock framework you can get a value from the test subject before the actions in the "when:" block are executed. This is done in Spock by some byte code magic (AST transformation), in the debugger you will see that the statement in the old method is executed before the statements above. This feature helps to get a clean and stable test logic and it can be use for example to verify that a size of a collection is increased, here a simple Spock specification which verify that the stack size is increased by using the Spock old Feature.

More Spock magic, patterns and anti patterns can be found in idiomatic-spock demo project on GitHub from Rob Fletcher and in his presentation on YouTube.

Sonntag, 16. März 2014

Spring Context the Grails Way

Since Spring Framework Version 4.0, there is a way to define a spring context in a groovy DSL, like the spring configuration can be done in Grails. A short description of this DSL can be found in the JavaDoc of the class GroovyBeanDefinitionReader and on the spring.io blog there is a good post that covers the DSL features.

Today I played around with the DSL, my context configuration covers a lot of the DSL feature and is shown in the listing.
If you like to use the groovy context in Spring JUnit test a ContextLoader must be defined, a simple test that uses the groovy context is show in the listing.
The whole demo project can be found on my github account here.

Sonntag, 16. Februar 2014

Project Templates with Lazybones

Lazybones is a cool command line tool to manage project templates. The tool can be used for example to create a ratpack project. But you can also manage your own project templates, to create any kind of project with lazybones from your own template.

Install lazybones

Before you can use lazybones you should install it. To install lazybones you can use gvm the Groovy enVironment Manager.

gvm install lazybones

More options how to install lazybones can be found in the project README.

How to create your own template

It is easy to create an own project template. For your own templates project you should use the lazybones template. To create a simple templates project, the following steps should be done:
  1. Create a workspace folder for the template project
  2. Switch to the workspace folder
  3. Create the templates project from the lazybones template: 
    • lazybones create lazybones-project simple-templates-project
  4. Switch into the templates project: cd simple-templates-project
  5. Create a template in the templates folder:
    • mkdir templates/simple-groovy/
    • Add a VERSION file in the folder "templates/simple-groovy". The VERSION file should contain the template version.
    • Add a README.md file in the same folder, the README file should contain the template project description.
    • Add a build.gradle file in the folder to build the template project.
  6. Now you can publish the template by running a gradle task. To install the template project local run: 
    • gradle installAllTemplate
  7. The new template now can be used from the lazybones command line tool:
    • lazybones create simple-groovy 0.0.1 my-app
More details how to publish the template project see the lazybones template development documentation.

Template Logic

If you need some user interaction or template logic add a "lazybones.groovy" file in the template project. Here simple "lazybones.groovy" script:

My simple templates demo project can be found here on github. More details about lazybones see the project documentation and the templates projects.

Dienstag, 18. Juni 2013

Gradle and Continuous Delivery

Here my slides from the SEITENBAU Developer Convention 2013. The slides provide an introduction to Gradle Basics and cover how to use Gradle for Continuous Delivery.

Mittwoch, 24. April 2013

Simple Table DSL in Groovy

Sometimes the best way to model data is as a table. A good example where you could use tables is the spock framework. Spock is a great testing framework for Java and Groovy. In spock the test parameters can be defined as a table. The spock framework use Groovy AST transformations to implement the DSL for the tables. I think about if there is a simpler way to parse an embedded table DSL in groovy. Not with the same features then the spock table DSL. My requirements for the DSL are to define a table and to have support for variables in the table.

Here a short example of such a table:

To implement the DSL parser in groovy I use two groovy languages feature:
  1. The category feature to define the OR operators, for the different types of the table values 
  2. The dynamic of groovy for the variable support, the getProperty is overwritten and creates a object of type Var for a undefined groovy variable
The code below contains a simple groovy implementation of a table DSL parser. The simple parser now can be used to parse the table DSL from the example above. How the DSL can be use in JUnit is show in the parameterized test in the listing below. Not a perfect example, because for testing you should use Spock instead.

Freitag, 8. Juni 2012

Groovy Closures and Annotation Parameters

Since Groovy 1.8 there is support to have a closure as a parameter in annotations. A nice example how to use this feature is the GContracts project, a groovy design by contract framework, which use closure annotation parameters, to define pre and post conditions of a method.

The support of closures in annotations provides a simple way to have much more dynamic in annotations. This is cool and provides new areas where you now could use annotations. Here a simple example how a closure as annotation parameter could be used to group properties.


More details about closures in annotation see the groovy 1.8 release notes.

Links:

Dienstag, 5. Juni 2012

Groovy Declared Fields Filter Synthetic

Groovy generates some "synthetic" field to a class, that means the reflection method getDeclaredFields returns more fields than fields which are declared in the Groovy source of a class. To filter the "synthetic" fields the Groovy grep method could be used e.g. "anyClass. declaredFields.grep { !it.synthetic }".

Links

Sonntag, 20. Mai 2012

Grails and Spring @Async Annotation

If some logic e.g. in a grails service should be invoked asynchronous, in Grails 2 the spring annotation @Async can be used. For that the spring annotation driven task support must be activated in the spring configuration. To activate the task support, the task namespace should be added to the spring configuration. Here a sample spring configuration ("resources.groovy").


Now the spring annotation can be used to mark e.g. service logic which should be invoked asynchronous. Here a sample to send mails asynchronous (for sending the mails the grails mail plugin is used).



For more details see also the spring framework documentation:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async

Sonntag, 5. Juni 2011

Convert a simple CSV File with Groovy, Java, Scala, awk, Ruby, Python, PHP or Bash?

Change Log:
  • 05.06.2011 1:30 pm - Initial created Post with  Groovy, Java, Scala, awk, Ruby, Python Implementation
  • 05.06.2011 4:00 pm - Add PHP implementation and update voting (now you can vote for PHP).
  • 06.06.2011 4:00 pm - Add Bash implementation and update voting (now you can also vote for Bash)

Which is the best programming language for converting a simple CSV into another format?

First I blogged three Java VM based solutions written in Groovy, Java and Scala to convert a simple CSV file into another format. Rainer sends me the Java based solution, yesterday Axel Knauf sends an awk based solution, Niko sends Ruby based solution, Hendrik sends a Python based solution, Sebastian sends me a PHP implementation and Julien sends a Bash version. Now there are a Groovy, Java, Scala, awk, Ruby, Python, PHP and Bash implementation.

Now here again a complete overview of the different implementations:

The Groovy Implementation:

The Java Implementation:

The Scala Implementation:

Here the shell command and awk script:

The pure Ruby Implementation:

The Python Implementation:

The pure PHP Implementation:

The Bash Implementation:


I'm curious whether there are other implementation proposals (Clojure, Perl, PHP, …), if you have one you could send me the script via Twitter or leave a comment here…

I am also curious which implementation Groovy, Java, Scala, awk or Ruby you like and why? I have create voting here:


Thanks Rainer, Axel Knauf, Niko Dittmann, Hendrik Heimbuerger S.Barthenheier and Julien Guitton for the Java, awk, Ruby Python, PHP and Bash implementation.

Links:

Samstag, 4. Juni 2011

Convert a CSV File in Groovy, Java or Scala?

Last week I have simple task I must convert a simple CSV file into another CSV format. My first solution was a simple Groovy script. Then inforw sends me a Java solution, to show me that with Java it is no much more code then the Groovy implementation is. Today I wrote just for fun a solution in Scala, to see how the code looks in Scala. My favorite of the three implementations is at the moment the Groovy one. But I think the Scala implementation has the best readability. Below you see the three implementations.

I'm curious what you like, feel free for comments? And I would be glad if someone contributes even further implementation in Clojure, Python, Perl,… or even a better Scala, Java or Groovy implementation.

The Groovy Implementation:

The Java Implementation:

The Scala Implementation:

Thanks @inforw for the discussion and the Java implementation.