Sample-groovy-in-Z2 » History » Revision 14
Revision 13 (Henning Blohm, 28.04.2013 17:06) → Revision 14/29 (Henning Blohm, 28.04.2013 18:50)
h1. Sample Groovy in Z2 This sample shows how to use the Groovy support, as implemented by the [[Groovy Add-on]]. h2. Prerequisites All you need is a JDK 6 or JDK 7 distribution as described in [[Step_2_-_Install_and_run_in_5_minutes]]. h2. Setting up the sample There is no further pre-requisite to running this sample, and you may proceed as described in [[How to run a sample]]. Here's the really fast version: <pre><code class="ruby"> mkdir install cd install git clone -b master http://git.z2-environment.net/z2-base.core git clone -b master http://git.z2-environment.net/z2-samples.groovy # on Linux / Mac OS: cd z2-base.core/run/bin ./gui.sh # on Windows: cd z2-base.core\run\bin gui.bat </code></pre> The first time you launch the sample, it sample will take a while to download all required resources. This sample highlights three things h2. Using Groovy or Java or any mix of while. Watch the two When declaring to use the groovy compiler as in <b>/com.zfabrik.samples.groovy_and_java.web/java/z.properties</b> you can mix Java log output and Groovy as you will. The Groovy compiler support will figure keep an eye 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. for the following lines: <pre> com.zfabrik.component.type=com.zfabrik.java java.privateReferences=\ com.zfabrik.servletjsp,\ com.zfabrik.groovy java.compile.order = groovy ...pp.WebAppResource [800]: Starting WebApp: com.zfabrik.samples.groovy.web/web </pre> ...er.GroovyCompiler [800]: GROOVYC: Version 2.1.2 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. The sample apps just print the HTTP request header - the "http://localhost:8080/plain-groovy-sample":http://localhost:8080/plain-groovy-sample is ...er.GroovyCompiler [800]: GROOVYC: Compiling 'com.zfabrik.samples.groovy.web/java' 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 Compiler... ...er.GroovyCompiler [800]: GROOVYC: 'com.zfabrik.samples.groovy.web/java' successfully compiled. ...er.GroovyCompiler [800]: GROOVYC: Total 613ms ...pp.WebAppResource [800]: Done starting Web App (/plain-groovy-sample): com.zfabrik.samples.groovy.web/web ...pp.WebAppResource [800]: Starting WebApp: com.zfabrik.admin/web ...pp.WebAppResource [800]: Done starting Web App (/adm): com.zfabrik.admin/web ...pp.WebAppResource [800]: Starting WebApp: com.zfabrik.samples.groovy_and_java.web/web ...er.GroovyCompiler [800]: GROOVYC: Version 2.1.2 ...er.GroovyCompiler [800]: GROOVYC: Compiling 'com.zfabrik.samples.groovy_and_java.web/java' 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. Joint-Compiler... ...er.GroovyCompiler [800]: GROOVYC: 'com.zfabrik.samples.groovy_and_java.web/java' successfully compiled. ...er.GroovyCompiler [800]: GROOVYC: Total 608ms ...pp.WebAppResource [800]: Done starting Web App (/groovy-java-sample): com.zfabrik.samples.groovy_and_java.web/web </pre> 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_ _environemnt_ and _com.zfabrik.samples.groovy.web_ from z2-samples.groovy (see also [[Step_3_-_First_steps_with_Z2_on_Git|First steps]]). h2. Using Groovlets and Groovy Template Pages Generally, The sample apps just print the mere fact that groovy-all is available implies that generally speaking all features described in * "http://groovy.codehaus.org/Groovlets":http://groovy.codehaus.org/Groovlets and * "http://groovy.codehaus.org/Groovy+Templates":http://groovy.codehaus.org/Groovy+Templates work. Groovlets are groovy scripts that are turned into Servlets on HTTP request header - the fly. The same header list as above "first":http://localhost:8080/plain-groovy-sample 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. Similarly, the using plain 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. To turn on support for Groovlets (see project @com.zfabrik.samples.groovy.web@) 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 : <pre class="xml"> <servlet> <servlet-name>Groovy</servlet-name> <servlet-class>com.zfabrik.groovy.servlet.ContextAwareGroovyServlet</servlet-class> </servlet> <servlet> <servlet-name>GroovyTemplate</servlet-name> <servlet-class>groovy.servlet.TemplateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Groovy</servlet-name> <url-pattern>*.groovy</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GroovyTemplate</servlet-name> <url-pattern>*.gsp</url-pattern> </servlet-mapping> </pre> *Note:* Instead of "second":http://localhost:8080/groovy-java-sample/ is using the standard Groovy Servlet (implementing Groovlets support), we use a specialized version that is part mixture 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) h2. Using Spock Tests 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 sources (see [[How to z2Unit]]) to allow server-side unit tests. Quite elegantly, the only declaration project @com.zfabrik.samples.groovy_and_java.web@). Note that differentiates a Spock test from any old JUnit test is while the mentioning of Spock's JUnit runner called Sputnik. When you write a local Spock tests, this former project is implicitly applied via compiled using the Spock test super class @Specification@. Now z2Unit uses a JUnit runner itself to shift test execution from plain Groovy compiler the invoking VM to the Z2 VM. The solution to that seeming conflict latter is indeed straight-forward: Declare compiled using the z2Unit test runner Joint Groovy/Java Compiler. Please refer to have execution handed over, [[Groovy Add-on]] for more details and tell z2Unit to how-to use Sputnik when executing a test class within Z2. 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: <pre class="groovy"> @RunWith(Z2UnitTestRunner.class) @Z2UnitTest(componentName="com.zfabrik.samples.spock", runWith=Sputnik.class) class HelloSpockZ2 extends Specification { def "A first test that should pass"() { setup: def x = new ArrayList<String>(); when: x.add("Hello") then: x.size() == 1 } def "a second test that should fail"() { setup: def x = new ArrayList<String>(); when: x.add("Hello") then: x.size() == 2 } } </pre> If you have an Eclipse setup it for this sample, as outlined above, you can run these tests directly from your IDE. </pre> own projects.