Project

General

Profile

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

Henning Blohm, 07.09.2021 19:14

1 2 Udo Offermann
h1. Sample Groovy in Z2
2 3 Udo Offermann
3 28 Henning Blohm
This sample shows how to use the Groovy support, as implemented by the [[Groovy_Add-on|Groovy Add-on]].
4 13 Henning Blohm
5 28 Henning Blohm
This sample is stored in the repository "z2-samples-groovy":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy<notextile></notextile>.
6 21 Henning Blohm
7 29 Henning Blohm
*NOTE:* This sample has been discontinued as of v2.9.
8
9 3 Udo Offermann
h2. Prerequisites
10
11 20 Henning Blohm
{{include(Java_Version_Requirements)}}
12 17 Henning Blohm
13 3 Udo Offermann
h2. Setting up the sample
14
15 1 Udo Offermann
Here's the really fast version:
16
17 29 Henning Blohm
<pre><code class="ruby">
18
mkdir install
19
cd install
20
</code></pre>
21
22
{{include(Install z2-base-v28)}}
23 3 Udo Offermann
24 28 Henning Blohm
<pre><code>
25
git clone -b v2.8 https://www.z2-environment.net/git/z2-samples.groovy
26 3 Udo Offermann
</code></pre>
27 23 Henning Blohm
28
{{include(Install_sample_postfix)}}
29 4 Udo Offermann
30 14 Henning Blohm
The first time you launch the sample, it will take a while to download all required resources.
31
32
This sample highlights three things
33
34
h2. Using Groovy or Java or any mix of the two
35
36 16 Henning Blohm
When declaring to use the groovy compiler as in "/com.zfabrik.samples.groovy_and_java.web/java/z.properties":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy/revisions/master/entry/com.zfabrik.samples.groovy_and_java.web/java/z.properties you can mix Java and Groovy as you like. 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.
37 14 Henning Blohm
38 1 Udo Offermann
<pre>
39 14 Henning Blohm
com.zfabrik.component.type=com.zfabrik.java
40 1 Udo Offermann
41 14 Henning Blohm
java.privateReferences=\
42 28 Henning Blohm
    com.zfabrik.servletjsp,\
43
    com.zfabrik.groovy
44
45 14 Henning Blohm
java.compile.order = groovy
46 1 Udo Offermann
</pre>
47 28 Henning Blohm
48 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.
49 1 Udo Offermann
50 28 Henning Blohm
The sample apps just print the HTTP request header - the 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/ 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.
51 1 Udo Offermann
52 28 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]]).
53 1 Udo Offermann
54 18 Henning Blohm
Make sure to have "Groovy support":https://github.com/groovy/groovy-eclipse/wiki installed with Eclipse (otherwise you will not have much fun with Groovy sources)!
55 1 Udo Offermann
56 28 Henning Blohm
Check [[Groovy_Add-on#Trouble-Shooting|Groovy_Add-on]], if you have trouble running code in Eclipse.
57 19 Henning Blohm
58 14 Henning Blohm
h2. Using Groovlets and Groovy Template Pages
59
60 28 Henning Blohm
Generally, the mere fact that groovy-all is available implies that generally speaking all features described in
61 14 Henning Blohm
62 28 Henning Blohm
* http://docs.groovy-lang.org/latest/html/documentation/servlet-userguide.html and
63
* http://docs.groovy-lang.org/latest/html/documentation/template-engines.html
64 14 Henning Blohm
65 28 Henning Blohm
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 to see it running.
66 14 Henning Blohm
67 28 Henning Blohm
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 to see it running.
68 14 Henning Blohm
69
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 :
70
71 28 Henning Blohm
<pre>
72 14 Henning Blohm
    <servlet>
73 1 Udo Offermann
            <servlet-name>Groovy</servlet-name>
74 14 Henning Blohm
            <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class>
75
    </servlet>
76 1 Udo Offermann
    <servlet>
77
           <servlet-name>GroovyTemplate</servlet-name>
78 14 Henning Blohm
           <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
79
    </servlet>
80 1 Udo Offermann
    <servlet-mapping>
81 14 Henning Blohm
            <servlet-name>Groovy</servlet-name>
82
            <url-pattern>*.groovy</url-pattern>
83
    </servlet-mapping>
84 1 Udo Offermann
    <servlet-mapping>
85 14 Henning Blohm
            <servlet-name>GroovyTemplate</servlet-name>
86
            <url-pattern>*.gsp</url-pattern>
87
    </servlet-mapping>
88
</pre>
89
90 28 Henning Blohm
*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 )
91 14 Henning Blohm
92
h2. Using Spock Tests
93
94 28 Henning Blohm
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|How to z2Unit]]) to allow server-side unit tests.
95 14 Henning Blohm
96 28 Henning Blohm
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.
97 14 Henning Blohm
98
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:
99
100 28 Henning Blohm
<pre>
101 14 Henning Blohm
@RunWith(Z2UnitTestRunner.class)
102
@Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class)
103
class HelloSpockZ2 extends Specification {
104
105 28 Henning Blohm
    def "A first test that should pass"() {
106
        setup:
107
            def x = new ArrayList<String>();
108
        when:
109
            x.add("Hello")
110
        then:
111
            x.size() == 1
112
    }
113 14 Henning Blohm
114 28 Henning Blohm
    def "a second test that should fail"() {
115
        setup:
116
            def x = new ArrayList<String>();
117
        when:
118
            x.add("Hello")
119
        then:
120
            x.size() == 2
121
    }
122 14 Henning Blohm
}
123
</pre>
124
125
If you have an Eclipse setup for this sample, as outlined above, you can run these tests directly from your IDE.