A document has been updated:
http://cocoon.zones.apache.org/daisy/documentation/636.html
Document ID: 636
Branch: main
Language: default
Name: Apache Cocoon Tests (unchanged)
Document Type: Cocoon Document (unchanged)
Updated on: 7/3/07 1:58:19 PM
Updated by: Grzegorz Kossakowski
A new version has been created, state: publish
Parts
=====
Content
-------
This part has been updated.
Mime type: text/xml (unchanged)
File name: (unchanged)
Size: 7071 bytes (previous version: 2874 bytes)
Content diff:
(24 equal lines skipped)
etc.</li>
</ul>
--- <h2>How to add more JUnit tests</h2>
---
--- <p>As there is already a nice junit framework available, it is not that
--- difficult to add junit tests for any transformer. Let's assume that we
create
--- test for <tt>org.apache.cocoon.transformation.XYTrasnformer</tt> class.</p>
---
<p class="note">It is important to pay attention to class'es package and
name
because test's configuration setup seekes for files that follow convention.
In
--- order to setup all needed components and execute test as expected you need
to
+++ order to set up all needed components and execute test as expected you need
to
follow proper directory structure that is shown in an example below.</p>
--- <p>In short:</p>
+++ <h2>How to add more JUnit tests for Avalon-managed class</h2>
--- <ul>
--- <li>Create <tt>XYTransformerTestCase.java</tt> and put it in
--- <tt>src/test/java/org/apache/cocoon/transformation</tt></li>
--- <li>Create <tt>XYTransformerTestCase.xtest</tt> that contains configuration
for
--- components tested transformer needs to work with and put it in
--- <tt>src/test/resources/og/apache/cocoon/transformation</tt></li>
--- <li>Add testABC() methods to XYTransformerTestCase.java</li>
--- </ul>
+++ <p>As there is already a nice junit framework available, it is not that
+++ difficult to add junit tests for any class. The process will be explained
+++ step-by-step getting as an example
+++ <tt>org.apache.cocoon.template.JXTemplateGenerator</tt> class from
+++ <tt>cocoon-template-impl</tt> module.</p>
+++ <h3>Component configuration</h3>
+++
+++ <p>You must create <tt>JXTemplateGeneratorTestCase.xtest</tt> file and put
it at
+++ <tt>src/test/resources/og/apache/cocoon/template</tt>. The file will contain
+++ configuration for components that tested generator needs to work with.
Example
+++ file may look like this:</p>
+++
+++ <pre><testcase>
+++ <roles>
+++ <role name="org.apache.cocoon.generation.GeneratorSelector"
shorthand="generators"
default-class="org.apache.cocoon.core.container.DefaultServiceSelector"/>
+++ <role name="org.apache.excalibur.store.Store/TransientStore"
shorthand="store"
default-class="org.apache.excalibur.store.impl.MemoryStore"/>
+++ <role
name="org.apache.cocoon.template.expression.StringTemplateParserSelector"
shorthand="string-template-parsers"
default-class="org.apache.cocoon.core.container.DefaultServiceSelector"/>
+++ </roles>
+++ <components>
+++ <generators logger="test">
+++ <component-instance
class="org.apache.cocoon.template.JXTemplateGenerator" name="jx"/>
+++ </generators>
+++ <component
role="org.apache.cocoon.template.script.ScriptManager"
class="org.apache.cocoon.template.script.DefaultScriptManager"/>
+++ <component
role="org.apache.cocoon.template.script.InstructionFactory"
class="org.apache.cocoon.template.script.DefaultInstructionFactory"/>
+++ <string-template-parsers>
+++ <component-instance
class="org.apache.cocoon.template.expression.JXTGStringTemplateParser"
name="jxtg"/>
+++ <component-instance
class="org.apache.cocoon.template.expression.DefaultStringTemplateParser"
name="default"/>
+++ </string-template-parsers>
+++ </components>
+++ </testcase></pre>
+++
+++ <p>As you can see, several components and generator itself are set up.</p>
+++
+++ <h3>Java test-case</h3>
+++
+++ <p>Now you must create <tt>JXTemplateGeneratorTestCase.java</tt> file and
put it
+++ at <tt>src/test/java/org/apache/cocoon/template</tt>. The class should
extend
+++ <tt>org.apache.cocoon.SitemapComponentTestCase</tt> from
<tt>cocoon-core</tt>
+++ module so the Avalon's ServiceManager is set up properly and you can use
lots of
+++ helper methods. Next step is writing actual testing methods, example method
+++ would look like:</p>
+++
+++ <pre>public void testGenerate() throws Exception {
+++ String inputURI = docBase + "generate.xml";
+++
+++ assertEqual(load(inputURI), generate(JX, inputURI, EMPTY_PARAMS));
+++ }
+++ </pre>
+++
+++ <p>This simple method tests if generator reads simple input file
(generate.xml)
+++ and emits all SAX events properly.</p>
+++
+++ <p class="note">It useful to check
<tt>S</tt><tt>itemapComponentTestCase</tt>
+++ and <tt>CocoonTestCase</tt> source code (or Javadocs) to find out what
methods
+++ are already provided for your convenience.</p>
+++
+++ <h3>Execute the test</h3>
+++
+++ <p>As stated earlier Maven will execute all tests every time the build is
+++ performed so if you want to check if everything is working you can go to
root
+++ directory of <tt>cocoon-template-impl</tt> and execute:</p>
+++
+++ <pre>mvn clean install
+++ </pre>
+++
+++ <p>As the output you should get something like this:</p>
+++
+++ <pre>-------------------------------------------------------
+++ T E S T S
+++ -------------------------------------------------------
+++ Running org.apache.cocoon.template.jxtg.JXTemplateGeneratorTestCase
+++ [DEBUG] XMLizer: Default parser is 'org.apache.excalibur.xml.sax.SAXParser'.
+++ [DEBUG] Resolved to systemID :
resource://org/apache/cocoon/template/jxtg/generate.xml
+++ [DEBUG] Creating source object for
resource://org/apache/cocoon/template/jxtg/generate.xml
+++ [DEBUG] JaxpParser: validating: false, namespace-prefixes: false, reuse
parser: true, stop on warning: true, stop on recoverable-error: true,
saxParserFactory: javax.xml.parsers.SAXParserFactory, documentBuilderFactory:
javax.xml.parsers.DocumentBuilderFactory, resolver hint: null
+++ [DEBUG] Releasing source object for
resource://org/apache/cocoon/template/jxtg/generate.xml
+++ [DEBUG] Resolving 'resource://org/apache/cocoon/template/jxtg/generate.xml'
with base 'null' in context
'file:/home/grek/asf/cocoon-trunk/blocks/cocoon-template/cocoon-template-impl/'
+++ [DEBUG] Resolved to systemID :
resource://org/apache/cocoon/template/jxtg/generate.xml
+++ [DEBUG] Creating source object for
resource://org/apache/cocoon/template/jxtg/generate.xml
+++ [DEBUG] Releasing source object for
resource://org/apache/cocoon/template/jxtg/generate.xml
+++ Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.524 sec
+++ </pre>
+++
+++ <p>Which indicates that everything went just fine.</p>
+++
<h1>htmlunit Tests</h1>
<p>Run <tt>build htmlunit-tests</tt> to execute all html the unit functional
(22 equal lines skipped)