Project

General

Profile

Sample-groovy-in-Z2 » History » Version 15

Henning Blohm, 29.04.2013 11:55

1 2 Udo Offermann
h1. Sample Groovy in Z2
2 3 Udo Offermann
3 13 Henning Blohm
This sample shows how to use the Groovy support, as implemented by the [[Groovy Add-on]].
4
5 3 Udo Offermann
h2. Prerequisites
6
7 12 Henning Blohm
All you need is a JDK 6 or JDK 7 distribution as described in [[Step_2_-_Install_and_run_in_5_minutes]].
8 3 Udo Offermann
9
h2. Setting up the sample
10
11 15 Henning Blohm
There is no further pre-requisite to running this sample, and you may proceed as described in [[How to run a sample]] (in particular, if you want to use Subversion). Here's the really fast version:
12 3 Udo Offermann
13
<pre><code class="ruby">
14
mkdir install
15
cd install 
16
git clone -b master http://git.z2-environment.net/z2-base.core
17
git clone -b master http://git.z2-environment.net/z2-samples.groovy
18
19
# on Linux / Mac OS:
20
cd z2-base.core/run/bin
21
./gui.sh
22
23
# on Windows:
24
cd z2-base.core\run\bin
25
gui.bat
26
</code></pre>
27 4 Udo Offermann
28 14 Henning Blohm
The first time you launch the sample, it will take a while to download all required resources.
29
30
This sample highlights three things
31
32
h2. Using Groovy or Java or any mix of the two
33
34
When declaring to use the groovy compiler as in <b>/com.zfabrik.samples.groovy_and_java.web/java/z.properties</b> you can mix Java and Groovy as you will. The Groovy compiler support will figure out whether a Java component (which may then be not Java only anymore - strictly speaking) contains only Java sources, only Groovy sources, or a mix of both.
35
36 1 Udo Offermann
<pre>
37 14 Henning Blohm
com.zfabrik.component.type=com.zfabrik.java
38
39
java.privateReferences=\
40
	com.zfabrik.servletjsp,\
41
	com.zfabrik.groovy
42
	
43
java.compile.order = groovy
44 1 Udo Offermann
</pre>
45 14 Henning Blohm
As in the sample, it is mandatory to reference *com.zfabrik.groovy* (a module providing groovy-all and some more) for any part that contains groovy sources.
46 1 Udo Offermann
47 14 Henning Blohm
The sample apps just print the HTTP request header - the "http://localhost:8080/plain-groovy-sample":http://localhost:8080/plain-groovy-sample is using plain Groovy (see project @com.zfabrik.samples.groovy.web@) and the "http://localhost:8080/groovy-java-sample/":http://localhost:8080/groovy-java-sample/ is using a mixture of Groovy and Java sources (see project @com.zfabrik.samples.groovy_and_java.web@). Note that while the former project is compiled using the plain Groovy compiler the latter is compiled using the Joint Groovy/Java Compiler.
48 1 Udo Offermann
49 14 Henning Blohm
If you want to inspect the code using Eclipse, please create a workspace in install (i.e. @install/workspace@) and import the Git repositories and the following  projects into your workspace: _core_ from z2-base.core, _environment_ and _com.zfabrik.samples.groovy.web_ from z2-samples.groovy (see also [[Step_3_-_First_steps_with_Z2_on_Git|First steps]]).
50 1 Udo Offermann
51 14 Henning Blohm
h2. Using Groovlets and Groovy Template Pages
52
53
Generally, the mere fact that groovy-all is available implies that generally speaking all features described in 
54
55
* "http://groovy.codehaus.org/Groovlets":http://groovy.codehaus.org/Groovlets and
56
* "http://groovy.codehaus.org/Groovy+Templates":http://groovy.codehaus.org/Groovy+Templates
57
58
work. Groovlets are groovy scripts that are turned into Servlets on the fly. The same header list as above is created by the "headers.groovy":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy/revisions/master/entry/com.zfabrik.samples.groovy.web/web/WebContent/headers.groovy script. Go to "http://localhost:8080/plain-groovy-sample/headers.groovy":http://localhost:8080/plain-groovy-sample/headers.groovy to see it running.
59
60
Similarly, the Groovy equivalent of Java server pages, Groovy Template Pages are supported. The author has not looked deeply into this. But check out "index.gsp":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy/revisions/master/entry/com.zfabrik.samples.groovy.web/web/WebContent/index.gsp for a trivial sample. Go "http://localhost:8080/plain-groovy-sample/index.gsp":http://localhost:8080/plain-groovy-sample/index.gsp to see it running.
61
62
To turn on support for Groovlets and GSPs, corresponding servlets and resource mappings have to be defined in the web app's "web.xml:https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy/revisions/master/entry/com.zfabrik.samples.groovy.web/web/WebContent/WEB-INF/web.xml :
63
64
<pre  class="xml">
65
    <servlet>
66
            <servlet-name>Groovy</servlet-name>
67
            <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class>
68
    </servlet>
69
    <servlet>
70
           <servlet-name>GroovyTemplate</servlet-name>
71
           <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
72
    </servlet>
73
    <servlet-mapping>
74
            <servlet-name>Groovy</servlet-name>
75
            <url-pattern>*.groovy</url-pattern>
76
    </servlet-mapping>
77
    <servlet-mapping>
78
            <servlet-name>GroovyTemplate</servlet-name>
79
            <url-pattern>*.gsp</url-pattern>
80
    </servlet-mapping>
81
</pre>
82
83
*Note:* Instead of using the standard Groovy Servlet (implementing Groovlets support), we use a specialized version that is part of the @com.zfabrik.groovy@ module. This is so that application types will be found correctly. Inquiry with the Groovy community in under way (see also #1042)
84
85
h2. Using Spock Tests
86
87
The "Spock Test Specification framework":http://code.google.com/p/spock/ provides an elegant way to specify and implement test cases over - in the end - the JUnit framework that is well integrated in virtually any Java capable development environment. Z2 integrates with JUnit via z2Unit (see [[How to z2Unit]]) to allow server-side unit tests. 
88
89
Quite elegantly, the only declaration that differentiates a Spock test from any old JUnit test is the mentioning of Spock's JUnit runner called Sputnik. When you write a local Spock tests, this is implicitly applied via the Spock test super class @Specification@. Now z2Unit uses a JUnit runner itself to shift test execution from the invoking VM to the Z2 VM. The solution to that seeming conflict is indeed straight-forward: Declare the z2Unit test runner to have execution handed over, and tell z2Unit to use Sputnik when executing a test class within Z2. 
90
91
The sample module @com.zfabrik.samples.spock@ contains a test class "HelloSpockZ2":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy/revisions/master/entry/com.zfabrik.samples.spock/java/src.test/com/zfabrik/samples/spock/tests/HelloSpockZ2.groovy that illustrates this combination:
92
93
<pre  class="groovy">
94
@RunWith(Z2UnitTestRunner.class)
95
@Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class)
96
class HelloSpockZ2 extends Specification {
97
98
	def "A first test that should pass"() {
99
		setup:
100
			def x = new ArrayList<String>();
101
		when:
102
			x.add("Hello")
103
		then:
104
			x.size() == 1
105
	}
106
107
	
108
	def "a second test that should fail"() {
109
		setup:
110
			def x = new ArrayList<String>();
111
		when:
112
			x.add("Hello")
113
		then:
114
			x.size() == 2
115
	}
116
}
117
</pre>
118
119
If you have an Eclipse setup for this sample, as outlined above, you can run these tests directly from your IDE.
120
</pre>