| [Up] |
Tutorial
These tutorials try to explain how to use some new features of the new web module(version 1.1). This version extends the previous one by adding some new active tags.
The new tags:
- <web:start>
- <web:get>
- <web:post>
Hello world !
<?xml version="1.0" encoding="iso-8859-1"?>
<xcl:active-sheet xmlns:sys="http://www.inria.fr/xml/active-tags/sys" xmlns:xcl="http://www.inria.fr/xml/active-tags/xcl" xmlns:web="http://www.inria.fr/xml/active-tags/web" xmlns:exp="http://www.inria.fr/xml/active-tags/exp"> <exp:unmarshal name="myWebapp" source="file:///path/to/hello-world.web"/> <web:start service="{$myWebapp}"> </web:start> <!-- the xcl params are "send" to the xcl:mapping matching the url --> <web:post name="reponse" url="/index.html" service="{ $myWebapp }"> <xcl:param name="who" value="toto"/> </web:post> </xcl:active-sheet>
Thanks to the tags <web:post> we are able to transmit informations to hello-world.web without server. The params between these tags are send.
<?xml version="1.0" encoding="iso-8859-1"?>
<web:service xmlns:web="http://www.inria.fr/xml/active-tags/web" xmlns:xcl="http://www.inria.fr/xml/active-tags/xcl"> <!-- [webapp]/index.xml --> <web:mapping match="^/index\.xml$" mime-type="application/xml"> <!--get the parameter from the query string : ...index.xml?who=John+Doe --> <xcl:set name="who" value="{ string( $web:request/who ) }"/> <!--set a default value if the parameter was missing--> <xcl:set xcl:if="{ not( $who ) }" name="who" value="world"/> <!--create an XML document--> <xcl:document name="xml"> <!--the XML content as litterals--> <example> <!--{ $who } will be substituted by its value--> <title>Hello { $who } !</title> <para>This example shows how to create dynamically an XML document.</para> <a href="http://reflex.gforge.inria.fr">Powered by RefleX.</a> </example> </xcl:document> <!--serialize the XML document to the HTTP output stream ; as the transformation doesn't involve a stylesheet, a copy is performed--> <xcl:transform source="{ $xml }" output="{ value( $web:response/@web:output ) }"/> </web:mapping> </web:service>
We are using the $web:request and $web:response properties send to this formulaire with <web:post> (we can do the same thing with get). There is no httpservlet involved in this operation because <web:post> is emulating the server.
Running the batch script (linux, windows) from the RefleX home directory :
- Download the Web application hello-world.web
- Download the whole tests hello-world.xcl
linux$ hello-world.sh "/path/to/webapp" -c /path/to/moduleweb1.1.cat
windows:\> hello-world.bat "/path/to/webapp" -c /path/to/moduleweb1.1.cat
Web Application and Xunit
Here is an exemple showing how to use the new tags and Xunit. Thanks to the new web module you will be able to test your own web application without any server.
<!--...--> <exp:element name="test:all-web"> <test:web-post-good name="report/report-post-good" output-expected="expected/output-expected-post-good.xml" label="WEB: envoi de requete POST qui doit marcher:"/> <xunit:merge-reports name="resume des tests xunit" source="{io:file('report/')}" output="{io:file('web-merge-err.xml')}"/> </exp:element> <exp:element name="test:web-post-good""> <!-- building a new testcase --> <xunit:test-case name="{ value( $exp:params/@name ) }" label="{ value( $exp:params/@label ) }""> <exp:unmarshalname="myWebapp" source="file://{string($sys:env/applieweb)} /WEB-INF/general.web" /> <web:start service="{ $myWebapp }"> <xcl:param name="output-dir" value="{$applieweb}/"/> </web:start> <!-- emulating a post request --> <web:post name="reponse" url="/enregistrer.html" service="{ $myWebapp }"> <xcl:param name="fichier" value="file1"/> <xcl:param name="nom" value="name1"/> <xcl:param name="age" value="1"/> <xcl:param name="mail" value="mail1"/> </web:post> <xcl:set name="numFile" value="{ string( value( $reponse/@web:body ) //DIV[@name='fichier']/@id)}" /> <xcl:parse name="resultat-attendu" source="{ value( $exp:params/@output-expected ) }"/> <xcl:parse name="resultat-obtenu" source="file://{string($sys:env/applieweb)}/build/{$numFile}.xml"/> <xunit:assert-node-equals result="{ $resultat-obtenu }" expected="{ $resultat-attendu }" recurse="true"/> </xunit:test-case> </exp:element> <!--...-->
With this exemple the 2 modules(web,xunit) are involved. You can use the $web:body and $web:head in order to use the emulated server's answer.
Running the batch script (linux, windows) from the RefleX home directory :
- Your Reflex home must contain 1.1.jar, moduleweb1.1.cat, reflex.0.2.0.jar, and directory lib with all jar needed.
- Your XUnit directory (where you launch tests) must contain run-tests.xcl, web-xunit.(sh|bat), web-use-cases.cat, and test-web.exp
- Download the Web application Application web
- Download the whole tests Les tests
linux$ web-xunit.sh "/path/to/webapp" -c /path/to/moduleweb1.1.cat
windows:\> web-xunit.bat "/path/to/webapp" -c /path/to/moduleweb1.1.cat

