Sample-groovy-in-Z2 » History » Version 27
Henning Blohm, 22.04.2019 23:42
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 | 21 | Henning Blohm | This sample is stored in the repository "z2-samples-groovy":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-groovy. |
6 | |||
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 | 27 | Henning Blohm | <pre><code class="bash"> |
18 | 26 | Henning Blohm | git clone -b v2.7 http://git.z2-environment.net/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 | |||
34 | java.privateReferences=\ |
||
35 | com.zfabrik.servletjsp,\ |
||
36 | com.zfabrik.groovy |
||
37 | |||
38 | java.compile.order = groovy |
||
39 | 1 | Udo Offermann | </pre> |
40 | 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. |
41 | 1 | Udo Offermann | |
42 | 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. |
43 | 1 | Udo Offermann | |
44 | 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]]). |
45 | 1 | Udo Offermann | |
46 | 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)! |
47 | |||
48 | 19 | Henning Blohm | Check [[Groovy_Add-on#Trouble-Shooting]], if you have trouble running code in Eclipse. |
49 | |||
50 | 14 | Henning Blohm | h2. Using Groovlets and Groovy Template Pages |
51 | |||
52 | Generally, the mere fact that groovy-all is available implies that generally speaking all features described in |
||
53 | |||
54 | 24 | Henning Blohm | * "http://docs.groovy-lang.org/latest/html/documentation/servlet-userguide.html":http://docs.groovy-lang.org/latest/html/documentation/servlet-userguide.html and |
55 | * "http://docs.groovy-lang.org/latest/html/documentation/template-engines.html":http://docs.groovy-lang.org/latest/html/documentation/template-engines.html |
||
56 | 14 | Henning Blohm | |
57 | 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. |
||
58 | |||
59 | 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. |
||
60 | |||
61 | 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 : |
||
62 | |||
63 | <pre class="xml"> |
||
64 | <servlet> |
||
65 | <servlet-name>Groovy</servlet-name> |
||
66 | <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class> |
||
67 | </servlet> |
||
68 | <servlet> |
||
69 | <servlet-name>GroovyTemplate</servlet-name> |
||
70 | <servlet-class>groovy.servlet.TemplateServlet</servlet-class> |
||
71 | </servlet> |
||
72 | <servlet-mapping> |
||
73 | <servlet-name>Groovy</servlet-name> |
||
74 | <url-pattern>*.groovy</url-pattern> |
||
75 | </servlet-mapping> |
||
76 | <servlet-mapping> |
||
77 | <servlet-name>GroovyTemplate</servlet-name> |
||
78 | <url-pattern>*.gsp</url-pattern> |
||
79 | </servlet-mapping> |
||
80 | </pre> |
||
81 | |||
82 | *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) |
||
83 | |||
84 | h2. Using Spock Tests |
||
85 | |||
86 | 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. |
||
87 | |||
88 | 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. |
||
89 | |||
90 | 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: |
||
91 | |||
92 | <pre class="groovy"> |
||
93 | @RunWith(Z2UnitTestRunner.class) |
||
94 | @Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class) |
||
95 | class HelloSpockZ2 extends Specification { |
||
96 | |||
97 | def "A first test that should pass"() { |
||
98 | setup: |
||
99 | def x = new ArrayList<String>(); |
||
100 | when: |
||
101 | x.add("Hello") |
||
102 | then: |
||
103 | x.size() == 1 |
||
104 | } |
||
105 | |||
106 | |||
107 | 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 | } |
||
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. |
||
119 | </pre> |