Tag Archives: Groovy

Injecto: Mixins for Groovy

Yesterday, as a follow up on some post on Artima, I have mentioned some fundamental difference between Mixins and Traits.

What I’ve forgot to mention is that mixins are available in Groovy through Injecto (a module initially developed by Luke Daley.

A couple of weeks ago I have started to think about some advanced features that would fit into Injecto. Unfortunately, meanwhile I got sidetracked by my daily job and a couple of complex technical aspects (f.e. supporting overloaded methods, different lifecycle mixins, etc.) that I wanted to solve for the new features. I hope to be able to get to it in the upcoming weeks, so that early next year to come out with a revamped mixin support for Groovy.

Posted by: Alex Popescu (aka the_mindstorm)

Advertisement

1 Comment

Filed under personalog, technolog

Groovy gets the best meta programming elements

The second time I have taken a deeper look at Groovy (this happened sometime last year), there were a couple of things about its meta programming capabilities that even if looking good were not so easy and not so advanced as the offerings in other languages.

At that time, I’ve looked at what other languages (like Ruby, Python, etc.) were offering in this field and I have tried to sell them to the Groovy dev team. But, due to the lack of time I had to stop after a while.

Fortunately enough, Graeme Rocher, project lead of Grails, have been constantly pushing for most of those proposals and even for more advanced/useful features. And now, with the release of Groovy beta3 I can say that Groovy has just got the best meta programming elements.

Well, I am not gonna present them here because they are already very well documented on the Groovy site:

The whole Groovy team is wishing you: Happy Groovying!

PS: Yes, I agree that using the word “best” is gonna make my statement arguable (and probably it is 🙂 ), but I truely believe that Groovy offers now a wonderful meta programming environment.

Update: I have posted a while ago a couple of links about meta object protocol.

1 Comment

Filed under technolog

Groovy Mocks

Groovy supports “natively” mocks and stubs. I will not go into this topic as it is quite well covered here. However, basically what you can do is:

/* the invocation params are passed to the closure */
mock.demand.real_method(params) { params -> 
    /* some code that will be executed when method is called */
}
mock.use {
  /* code that uses the mock */
}

Exactly the same can be done with stubs. Groovy in Action explains the conceptual difference between mocks and stubs:

Stubs enable your CUT [n.b. class under test] to run in isolation and allow you to make assertions about state changes of the CUT. With mocks, the test focus moves to the interplay of the CUT and its collaborators. What gets asserted is whether the CUT follows a specified protocol.

There are other behavioral differences between mocks and stubs, so I would encourage you to get a copy of GinA for more details. The advantages and ease of usage are evident. As for TDD adepts I think they already know that using a dynlang will ease their life.

Another very cool feature of Groovy is the possibility to define properties. So, instead of having to write a java bean like:

class JavaBean {
  private String name;

   public String getName() { return name; }

   public void setName(String n) { name = n; }
}

you just need to write:

class GroovyBean {
  String name;
  // Well... this is all !
}

By this time you are already wondering what is the reason for this post and what is the link between Groovy properties and mocking (beside maybe advertising the coolness of Groovy mocks). Well, if you would be using Groovy mocks for a while you would have noticed that you cannot actually mock the property access (there was an workaround: explicit use of the getter, but this would have defended the whole idea around properties, didn’t it?). So, fresh on the Groovy trunk there is support for mocking property access:

class Caller {
  def printTwo = { new Collaborator().one }
}
def mock = new MockFor(Collaborator)

mock.demand.getOne { "two" }
mock.use {
   def c = new Caller()
   assert    c.printTwo() == "two"
}

Considering that I got started on mocks, my next post will be about JMock2 and the (many) things I don’t like about it.

Update: it looks like there is still one limitation for Groovy mocks/stubs: they cannot be used yet for constructors (GROOVY-1823). Code like this will not run:

def dateStub = new StubFor( Date )
dateStub.demand.Date{ aControlCurrentTime } // this syntax is however ugly

Leave a comment

Filed under technolog

Ohh my GINA (Groovy in Action)

Groovy in Action
I have already noticed a couple of GINA‘s reviews out there, but I still thought to post mine. Yes, I know this one can be considered a bit more subjective than others having in mind that lately I played more and more with Groovy, I have implemented support for annotations in Groovy and already released the 1.0 of Test’N’Groove (integration of TestNG and Groovy — but I will write more about it some other time).

During the last months I have been reading a lot about different programming languages: Common LISP, Python, Scala, etc. So, I would say that my brain was in a new programming language mood. And, even if some may hate me for this, I would say that writing a book about a PL is an easy job. Depending on the target you set and on your experience and knowledge, you pick the subjects you are best in and develop around those. The hard part is to make your book the Bible of that programming language, the first book people will be looking for when starting to learn that language. And even harder is to make it attractive and fun and entertaining for your readers. At this point I must confess that I read GINA in less than 3 days, even if it has more than 600 pages (oke, I confess I haven’t read it all, but for sure more than 80% of it). And not only that I got that feeling that it is so fun that I cannot stop myself, but I think it fulfills all the characteristics of great and absolutely enjoyable PL book.

For the beginning here is a short description of Groovy’s power:

Some languages may have a few more features than Groovy. Some languages may claim to integrate better with Java. None can currently touch Groovy when you consider both aspects together: Nothing provides a better combination of Java friendliness ”and” a complete range of modern language features.

The Groovy in Action authors, many of them part of the Groovy specification and development team, will walk you through almost all interesting features of Groovy (closures, builders, dynamic object orientation, Groovy Standard Library) — and I still believe they haven’t left many out :-). The reader is introduced step by step in the world of Groovy, getting more and more knowledge by every page. The book doesn’t stop at theoretical aspects of Groovy language, but it is also presenting the reader with pragmatic aspects as working with databases, working with XML, integration with Spring and integration in Mustang, performing Windows specific tasks. The book has also a special place for Grails one of the most important (if not the most) framework based on Groovy.

Finally, without any fear, I would say that Groovy in Action is not just a language guide, but represents the clear, readable and enjoyable specification of Groovy (and you should definitely read it and start playing with Groovy [blink/] ).

2 Comments

Filed under personalog, technolog

The definitive guide to Grails

You may say that Grails is just yet another web framework, but this would definitely not be fair. Even if it is a quite young web framework – being now at version 0.4.1 – Grails has learnt a lot from existing Java web frameworks but also from frameworks outside Java space like RoR , Django, etc. I would even say that the fact that Grails is based on Groovy makes it unique to the Java space and I think it may prove its usability quite immediate. These were my reasons that made me start reading the book.

Grails forms just one framework that is driving the movement toward dynamic language-based frameworks. In this sense, anyone who is interested in the dynamic language field, whether you are from a Perl, Ruby, or Python background, would gain something from reading this book, if just to acquire insight into what the alternatives are.
(Graeme Rocher)

Graeme Rocher, the Grails project lead, took the opportunity to write The Definitive Guide to Grails, so that everybody can benefit of his knowledge in developing apps and become an expert in Grails.

As the book starts, you are already told about the rationale behind Grails:

Grails definitely has an ambitious name for being the Holy Grails all application developers have sought so far. But more than having mere ambition, Grails fulfills its promises by letting you be more productive than you could have ever thought.
(Guillaume Laforge)

and a bit further:

The goal of Grails was to go beyond what other languages and their associated frameworks could offer in the web application space. Grails aimed to do the following:

  • Integrate tightly with the Java platform
  • Be simple on the surface but retain flexibility to access the powerfull underlying Java frameworks and features
  • Learn from the mistakes already made on the mature Java platform

However, it is not my intention to take away your pleasure to read the book, but just to point out a couple of things that kept up my interest while scanning it.

The book gets you started with Grails even from the first chapter where you set up your environment and the example application. It is impressive how quick things are working:

  1. downloaded the binary distribution and unarchived it in a directory
  2. configured the environment (just an environment variable)
  3. run grails run-app
  4. launch your brower: http://localhost:8080/bookmarks

All these steps and more details about Grails project, project organization are presented in the first chapters of the book. These are meant to make you feel comfortable creating new projects and handling the command line tools, but also to give you a quick introduction to Groovy.

Then the author delves into each Grails details walking you through building the domain and its persistence using GORM, creating web controllers, data binding and control flow, writing Groovy Server Pages using Groovy scriptlets and the extensive set of Grails tags.

One point about Grails controllers that I want to mention its the framework capability to define action interceptors, something that other web frameworks have proved to be a nice approach (see WebWork). Grails benefits of a large number of smart tags, some of them making usage of Ajax very easily. The Chapter 9 talks extensively about this, introducing the reader to before and after call interception, event handling, remote linking, applying effects with the help of sciptaculous JS library.

Aside of detailed each Grails Dynamic Tags, Chapter 8 is presenting the usefulness and usage of layouts and templates talking also about Layout-by-Convention.

Another thing I liked about Grails is the pagination tag, this being introduce in the same Chapter 8: Groovy Server Pages.

The last two chapters of the book are talking about more advanced topics like jobs scheduling, server-side Java integration, Hibernate, Spring, Acegi usage.

The whole book walks you through an interesting and nice app that will make things even easier to understand. All in all, I would say that The Definitive Guide to Grails was a very nice reading and I hope others will find it the same.

Leave a comment

Filed under personalog, technolog

Groovy support for JMeter

After watching the excellent presentation on performance testing given by Goranka Bjedov (QA Engineer at Google), my interest on JMeter increased exponentially. Planning for some testing of InfoQ upcoming features (oh yes, we have some surprises on our bag for this beginning of the year) I have giving it a try.
It took me a while to identify the elements involved while creating a scenario (by the way: JMeter name for scenarios: Test plan is not quite correct) and the components to be used, but the online documentation is not so bad.
However, while playing around I have felt the need to create some monitoring scripts (n.b. I am still working on this part). JMeter offers support for BSF and BSH out of the box, but having in mind the release of Groovy 1.0 I thought to try to add Groovy support to JMeter.
So, last night, armed with the Groovy intro chapter from The Definitive Guide to Grails and Groovy in Action [note], I have started my attempt. The task wasn’t easy, but not because of some obvious reasons: different parts of JMeter are using different approaches (like being contributed by completely unrelated people that never looked at the existing code), so deciding how should I write it took me a while. However, in a couple of hours I am having an working version: 2 main classes that are reused for different UI parts and respectively for Groovy integration, an interface to allow extensions and a couple of almost empty classes for each component : PreProcessor, PostProcessor, Listener and Sampler. Total size: 12 classes, aprox. 300 usefull LOC, cca. 500 TLOC. The UI is not so nice but follows the existing JMeter UI (I haven’t done any GUI work in the last couple of years so I am a bit rusty [smile/]). After doing this job, I think most pieces are pretty reusable so integrating other scripting solution like JRuby or even the JDK6 scripting support would be a very easy task.

I am thinking to contribute this code to either JMeter or a Groovy module, but I still need to decide. Meanwhile, let me know if you are interested in this integration.

PS: I am still wondering how can I get my hands on any of the Manning books. I have pinged them a couple of times, submitted a complaint but never heard back from them.

Update: thanks to Dierk Koenig (author of Groovy in Action) and Olivia (from Manning) the above problem seems to start having a solution. Thanks.

10 Comments

Filed under technolog

Groove ’bout Groovy

If you still want to evaluate Groovy maybe this will help you ONJava.com: Groovy, Java’s New Scripting Language.
Then come back and give me a groovy scenario for groovy. Thanks anticipated [blink/].

Leave a comment

Filed under links, technolog