Sample-groovy-in-Z2 » History » Version 20
Henning Blohm, 22.09.2015 21:07
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 | 20 | Henning Blohm | {{include(Java_Version_Requirements)}} |
8 | 17 | Henning Blohm | |
9 | 3 | Udo Offermann | 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 | 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. |
35 | 14 | Henning Blohm | |
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 | 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)! |
52 | |||
53 | 19 | Henning Blohm | Check [[Groovy_Add-on#Trouble-Shooting]], if you have trouble running code in Eclipse. |
54 | |||
55 | 14 | Henning Blohm | h2. Using Groovlets and Groovy Template Pages |
56 | |||
57 | Generally, the mere fact that groovy-all is available implies that generally speaking all features described in |
||
58 | |||
59 | * "http://groovy.codehaus.org/Groovlets":http://groovy.codehaus.org/Groovlets and |
||
60 | * "http://groovy.codehaus.org/Groovy+Templates":http://groovy.codehaus.org/Groovy+Templates |
||
61 | |||
62 | 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. |
||
63 | |||
64 | 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. |
||
65 | |||
66 | 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 : |
||
67 | |||
68 | <pre class="xml"> |
||
69 | <servlet> |
||
70 | <servlet-name>Groovy</servlet-name> |
||
71 | <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class> |
||
72 | </servlet> |
||
73 | <servlet> |
||
74 | <servlet-name>GroovyTemplate</servlet-name> |
||
75 | <servlet-class>groovy.servlet.TemplateServlet</servlet-class> |
||
76 | </servlet> |
||
77 | <servlet-mapping> |
||
78 | <servlet-name>Groovy</servlet-name> |
||
79 | <url-pattern>*.groovy</url-pattern> |
||
80 | </servlet-mapping> |
||
81 | <servlet-mapping> |
||
82 | <servlet-name>GroovyTemplate</servlet-name> |
||
83 | <url-pattern>*.gsp</url-pattern> |
||
84 | </servlet-mapping> |
||
85 | </pre> |
||
86 | |||
87 | *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) |
||
88 | |||
89 | h2. Using Spock Tests |
||
90 | |||
91 | 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. |
||
92 | |||
93 | 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. |
||
94 | |||
95 | 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: |
||
96 | |||
97 | <pre class="groovy"> |
||
98 | @RunWith(Z2UnitTestRunner.class) |
||
99 | @Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class) |
||
100 | class HelloSpockZ2 extends Specification { |
||
101 | |||
102 | def "A first test that should pass"() { |
||
103 | setup: |
||
104 | def x = new ArrayList<String>(); |
||
105 | when: |
||
106 | x.add("Hello") |
||
107 | then: |
||
108 | x.size() == 1 |
||
109 | } |
||
110 | |||
111 | |||
112 | def "a second test that should fail"() { |
||
113 | setup: |
||
114 | def x = new ArrayList<String>(); |
||
115 | when: |
||
116 | x.add("Hello") |
||
117 | then: |
||
118 | x.size() == 2 |
||
119 | } |
||
120 | } |
||
121 | </pre> |
||
122 | |||
123 | If you have an Eclipse setup for this sample, as outlined above, you can run these tests directly from your IDE. |
||
124 | </pre> |