Project

General

Profile

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

Henning Blohm, 24.02.2020 14:40

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 3 Udo Offermann
h2. Prerequisites
8
9 20 Henning Blohm
{{include(Java_Version_Requirements)}}
10 17 Henning Blohm
11 3 Udo Offermann
h2. Setting up the sample
12
13 23 Henning Blohm
Here's the really fast version:
14 1 Udo Offermann
15 23 Henning Blohm
{{include(Install_sample_prefix)}}
16 3 Udo Offermann
17 28 Henning Blohm
<pre><code>
18
git clone -b v2.8 https://www.z2-environment.net/git/z2-samples.groovy
19 3 Udo Offermann
</code></pre>
20 23 Henning Blohm
21
{{include(Install_sample_postfix)}}
22 4 Udo Offermann
23 14 Henning Blohm
The first time you launch the sample, it will take a while to download all required resources.
24
25
This sample highlights three things
26
27
h2. Using Groovy or Java or any mix of the two
28
29 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.
30 14 Henning Blohm
31 1 Udo Offermann
<pre>
32 14 Henning Blohm
com.zfabrik.component.type=com.zfabrik.java
33 1 Udo Offermann
34 14 Henning Blohm
java.privateReferences=\
35 28 Henning Blohm
    com.zfabrik.servletjsp,\
36
    com.zfabrik.groovy
37
38 14 Henning Blohm
java.compile.order = groovy
39 1 Udo Offermann
</pre>
40 28 Henning Blohm
41 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.
42 1 Udo Offermann
43 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.
44 1 Udo Offermann
45 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]]).
46 1 Udo Offermann
47 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)!
48 1 Udo Offermann
49 28 Henning Blohm
Check [[Groovy_Add-on#Trouble-Shooting|Groovy_Add-on]], if you have trouble running code in Eclipse.
50 19 Henning Blohm
51 14 Henning Blohm
h2. Using Groovlets and Groovy Template Pages
52
53 28 Henning Blohm
Generally, the mere fact that groovy-all is available implies that generally speaking all features described in
54 14 Henning Blohm
55 28 Henning Blohm
* http://docs.groovy-lang.org/latest/html/documentation/servlet-userguide.html and
56
* http://docs.groovy-lang.org/latest/html/documentation/template-engines.html
57 14 Henning Blohm
58 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.
59 14 Henning Blohm
60 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.
61 14 Henning Blohm
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 28 Henning Blohm
<pre>
65 14 Henning Blohm
    <servlet>
66 1 Udo Offermann
            <servlet-name>Groovy</servlet-name>
67 14 Henning Blohm
            <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class>
68
    </servlet>
69 1 Udo Offermann
    <servlet>
70
           <servlet-name>GroovyTemplate</servlet-name>
71 14 Henning Blohm
           <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
72
    </servlet>
73 1 Udo Offermann
    <servlet-mapping>
74 14 Henning Blohm
            <servlet-name>Groovy</servlet-name>
75
            <url-pattern>*.groovy</url-pattern>
76
    </servlet-mapping>
77 1 Udo Offermann
    <servlet-mapping>
78 14 Henning Blohm
            <servlet-name>GroovyTemplate</servlet-name>
79
            <url-pattern>*.gsp</url-pattern>
80
    </servlet-mapping>
81
</pre>
82
83 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 )
84 14 Henning Blohm
85
h2. Using Spock Tests
86
87 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.
88 14 Henning Blohm
89 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.
90 14 Henning Blohm
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 28 Henning Blohm
<pre>
94 14 Henning Blohm
@RunWith(Z2UnitTestRunner.class)
95
@Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class)
96
class HelloSpockZ2 extends Specification {
97
98 28 Henning Blohm
    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 14 Henning Blohm
107 28 Henning Blohm
    def "a second test that should fail"() {
108
        setup:
109
            def x = new ArrayList<String>();
110
        when:
111
            x.add("Hello")
112
        then:
113
            x.size() == 2
114
    }
115 14 Henning Blohm
}
116
</pre>
117
118
If you have an Eclipse setup for this sample, as outlined above, you can run these tests directly from your IDE.