groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J. David Beutel" <l...@getsu.com>
Subject Re: Wanted: Help understanding Groovy Syntax for DBUnit Example
Date Thu, 02 Jul 2015 23:47:16 GMT
On 2015-07-02 12:00 , Richard Heintze wrote:
> I love this example and it even works!
>
> https://gist.github.com/kiy0taka/272110
>
> Can someone help me understand the syntax on lines 29-33?
> (1) What is dataset? Are we passing it a lambda?
>

That dataSet is a call to the method defined at the bottom of 
DbUnitSampleTest, and we are passing it a Closure (like a lambda). 
Parentheses are not required for unambiguous method calls in Groovy, and 
conventionally not used around a Closure when passing it as the last 
parameter.

> OK, so semi-colons are optional. Hmmm... Apparently we have a list of 
> emp constructors. Is this correct?
>

Actually, those emp are method calls, too, passing a Map as the single 
parameter.  The parentheses and brackets are both optional there.  There 
is no emp class.  This is Groovy's builder pattern.

> (2) How is it that the "{}" are constructing a list of emps?
>
> Why is there no code like "[emp empno:7369, ename:'SMITH', job:'CLERK'
> emp empno:7499, ename:'ALLEN', job:'SALESMAN'
> emp empno:7521, ename:'WARD', job:'SALESMAN'] as List"?
>

That Closure, c, is passed by the dataSet method to 
StreamingMarkupBuilder's bind method, wrapped in another Closure to add 
a "dataset" root element.  The bind method uses a BaseMarkupBuilder to 
call the Closure and create XML.  The emp method doesn't exist, so each 
time the Closure tries to call it, the builder uses Groovy's 
invokeMethod to add another element to the XML.

> (3) And what about emp? Who defines the class emp and its constructor? 
> Where?
>

DbUnit just needs the XML to match the db schema that was created in the 
static block.  There are no domain classes in that example.

> (4) And what is this business with the grape.grab inside the static 
> {}? I've never seen the keyword static in groovy code before.  Why 
> static? Why not "@Grab" instead of grapes.grab (inside the static)?
>

I'm not sure about that, but would guess that it's an hsqldb convention, 
and @Grab might work too.

> Thanks
> siegfried

Cheers,
11011011

Mime
View raw message