Sample-groovy-in-Z2 » History » Version 21
Henning Blohm, 12.06.2017 09:43
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 | 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: |
14 | 3 | Udo Offermann | |
15 | <pre><code class="ruby"> |
||
16 | mkdir install |
||
17 | cd install |
||
18 | git clone -b master http://git.z2-environment.net/z2-base.core |
||
19 | git clone -b master http://git.z2-environment.net/z2-samples.groovy |
||
20 | |||
21 | # on Linux / Mac OS: |
||
22 | cd z2-base.core/run/bin |
||
23 | ./gui.sh |
||
24 | |||
25 | # on Windows: |
||
26 | cd z2-base.core\run\bin |
||
27 | gui.bat |
||
28 | </code></pre> |
||
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 | |||
41 | java.privateReferences=\ |
||
42 | com.zfabrik.servletjsp,\ |
||
43 | com.zfabrik.groovy |
||
44 | |||
45 | java.compile.order = groovy |
||
46 | 1 | Udo Offermann | </pre> |
47 | 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. |
48 | 1 | Udo Offermann | |
49 | 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. |
50 | 1 | Udo Offermann | |
51 | 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]]). |
52 | 1 | Udo Offermann | |
53 | 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)! |
54 | |||
55 | 19 | Henning Blohm | Check [[Groovy_Add-on#Trouble-Shooting]], if you have trouble running code in Eclipse. |
56 | |||
57 | 14 | Henning Blohm | h2. Using Groovlets and Groovy Template Pages |
58 | |||
59 | Generally, the mere fact that groovy-all is available implies that generally speaking all features described in |
||
60 | |||
61 | * "http://groovy.codehaus.org/Groovlets":http://groovy.codehaus.org/Groovlets and |
||
62 | * "http://groovy.codehaus.org/Groovy+Templates":http://groovy.codehaus.org/Groovy+Templates |
||
63 | |||
64 | 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. |
||
65 | |||
66 | 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. |
||
67 | |||
68 | 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 : |
||
69 | |||
70 | <pre class="xml"> |
||
71 | <servlet> |
||
72 | <servlet-name>Groovy</servlet-name> |
||
73 | <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class> |
||
74 | </servlet> |
||
75 | <servlet> |
||
76 | <servlet-name>GroovyTemplate</servlet-name> |
||
77 | <servlet-class>groovy.servlet.TemplateServlet</servlet-class> |
||
78 | </servlet> |
||
79 | <servlet-mapping> |
||
80 | <servlet-name>Groovy</servlet-name> |
||
81 | <url-pattern>*.groovy</url-pattern> |
||
82 | </servlet-mapping> |
||
83 | <servlet-mapping> |
||
84 | <servlet-name>GroovyTemplate</servlet-name> |
||
85 | <url-pattern>*.gsp</url-pattern> |
||
86 | </servlet-mapping> |
||
87 | </pre> |
||
88 | |||
89 | *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) |
||
90 | |||
91 | h2. Using Spock Tests |
||
92 | |||
93 | 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. |
||
94 | |||
95 | 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. |
||
96 | |||
97 | 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: |
||
98 | |||
99 | <pre class="groovy"> |
||
100 | @RunWith(Z2UnitTestRunner.class) |
||
101 | @Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class) |
||
102 | class HelloSpockZ2 extends Specification { |
||
103 | |||
104 | def "A first test that should pass"() { |
||
105 | setup: |
||
106 | def x = new ArrayList<String>(); |
||
107 | when: |
||
108 | x.add("Hello") |
||
109 | then: |
||
110 | x.size() == 1 |
||
111 | } |
||
112 | |||
113 | |||
114 | 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 | } |
||
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. |
||
126 | </pre> |