<?xml version="1.0" encoding="UTF-8"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<chapter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="5.0" xmlns:xl="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude"
    xsi:schemaLocation="http://docbook.org/ns/docbook ../../applications/content/dtd/docbook.xsd"
    xmlns="http://docbook.org/ns/docbook">
    <title><anchor xml:id="TheOFBizUnitTest"/>The OFBiz Unit Test (Using JUnit)</title>
    <section>
        <title>How to define a unit test?</title>
        <orderedlist>
            <listitem>
                <para>
                    Define test-suite in ofbiz-component.xml like this:
                </para>
                <programlisting>
                    &lt;test-suite loader="main" location="testdef/servicetests.xml"/&gt;
                </programlisting>
            </listitem>
            <listitem>
                <para>
                    Create test-case in test-suite file.
                </para>
                <programlisting>
                    &lt;test-suite suite-name="servicetests"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:noNamespaceSchemaLocation="https://ofbiz.apache.org/dtds/test-suite.xsd"&gt;
                        ....
                        ....
                        &lt;test-case case-name="test-case-name"&gt;
                        ...
                        &lt;/test-case&gt;
                        ....
                        ....
                    &lt;/test-suite&gt;
                </programlisting>
            </listitem>
        </orderedlist>
    </section>
    <section>
        <title>How to create a test case?</title>
        <para>
            Define a test-case in an XML test-suite file like this:
            <programlisting>
                &lt;test-case case-name="test-case-name"&gt;
                    ...
                &lt;/test-case&gt;
            </programlisting>
            The test-case tag contains details of each test type:
        </para>
        <section>
            <title>1. Entity XML</title>
            <para>
                Specific entity xml and its action would like to be tested in entity-xml-url attribute and action attribute respectively like this:
                <programlisting>
                    &lt;test-case case-name="service-dead-lock-retry-assert-data"&gt;
                        &lt;entity-xml action="assert" entity-xml-url="component://service/testdef/data/ServiceDeadLockRetryAssertData.xml"/&gt;
                    &lt;/test-case&gt;
                </programlisting>
            </para>
        </section>
        <section>
            <title>2. JUnit</title>
            <para>
                Specific class's name which will be tested, in a class-name attribute like this:
                <programlisting>
                    &lt;test-case case-name="service-tests"&gt;
                        &lt;junit-test-suite class-name="org.apache.ofbiz.service.test.ServiceEngineTests"/&gt;
                    &lt;/test-case&gt;
                </programlisting>
            </para>
        </section>
        <section>
            <title>3. Service</title>
            <para>
                Specific service's name which will be tested in a service-name attribute like this:
                <programlisting>
                    &lt;test-case case-name="service-lock-wait-timeout-retry-test"&gt;
                        &lt;service-test service-name="testServiceLockWaitTimeoutRetry"/&gt;
                    &lt;/test-case&gt;
                </programlisting>
            </para>
        </section>
        <section>
            <title>4. Simple Method</title>
            <para>
                Specific simple method's location and name which will be tested in a location and a name attribute respectively like this:
                <programlisting>
                    &lt;test-case case-name="auto-accounting-transaction-tests-PoReceipt"&gt;
                        &lt;simple-method-test location="component://accounting/minilang/test/AutoAcctgTransTests.xml" name="testAcctgTransOnPoReceipts"/&gt;
                    &lt;/test-case&gt;
                </programlisting>
            </para>
        </section>
    </section>
    <section>
        <title>How to run a unit test?</title>
        <para>
            You can run unit test by run 'ant' with following target:
        </para>
        <section>
            <title>1. run-tests</title>
            <para>
                Run OFBiz default tests.
            </para>
        </section>
        <section>
            <title>2. run-test-list</title>
            <para>
                Run all configured tests, stopping/starting ofbiz between each test.
            </para>
        </section>
        <section>
            <title>3. run-single-test</title>
            <para>
                Run a single test, require two arguments:
                <orderedlist>
                    <listitem>
                        <para>
                            'test.component' is a name of test component.
                        </para>
                    </listitem>
                    <listitem>
                        <para>
                            'test.case' is a name of test case.
                        </para>
                    </listitem>
                </orderedlist>
            </para>
        </section>
        <section>
            <title>4. run-single-test-suite</title>
            <para>
                Run a single test suite, requiring two arguments:
                <orderedlist>
                    <listitem>
                        <para>
                            'test.component' is a name of test component.
                        </para>
                    </listitem>
                    <listitem>
                        <para>
                            'test.suiteName' is a name of test suite.
                        </para>
                    </listitem>
                </orderedlist>
            </para>
        </section>
    </section>
    <section>
        <title>Possible error messages</title>
        <para>
            Some error messages in the log are not important and often caused by the test.
        </para>
        <section>
            <title>1. Roll back</title>
            <para>
                Roll back error message occurred when transaction roll back data. This error message will be in between starting and finished test cass line like this:
                <programlisting>
                    [java] 2009-12-22 16:05:28,349 (main) [   TestRunContainer.java:238:INFO ] [JUNIT] : [test case's name] starting...
                    [java] 2009-12-22 16:05:28,355 (main) [    TransactionUtil.java:336:ERROR]
                    ....
                    ....
                    ....
                    [java] ---- exception report ----------------------------------------------------------
                    [java] [TransactionUtil.rollback]
                    [java] Exception: java.lang.Exception
                    [java] Message: Stack Trace
                    [java] ---- stack trace ---------------------------------------------------------------
                    [java] java.lang.Exception: Stack Trace
                    [java] org.apache.ofbiz.entity.transaction.TransactionUtil.rollback(TransactionUtil.java:335)
                    [java] org.apache.ofbiz.entity.transaction.TransactionUtil.rollback(TransactionUtil.java:317)
                    [java] org.apache.ofbiz.entity.test.EntityTestSuite.testTransactionUtilRollback(EntityTestSuite.java:437)
                    [java] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                    [java] sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                    [java] sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                    [java] java.lang.reflect.Method.invoke(Method.java:597)
                    [java] junit.framework.TestCase.runTest(TestCase.java:154)
                    [java] junit.framework.TestCase.runBare(TestCase.java:127)
                    [java] junit.framework.TestResult$1.protect(TestResult.java:106)
                    [java] junit.framework.TestResult.runProtected(TestResult.java:124)
                    [java] junit.framework.TestResult.run(TestResult.java:109)
                    [java] junit.framework.TestCase.run(TestCase.java:118)
                    [java] junit.framework.TestSuite.runTest(TestSuite.java:208)
                    [java] junit.framework.TestSuite.run(TestSuite.java:203)
                    [java] junit.framework.TestSuite.runTest(TestSuite.java:208)
                    [java] junit.framework.TestSuite.run(TestSuite.java:203)
                    [java] org.apache.ofbiz.testtools.TestRunContainer.start(TestRunContainer.java:146)
                    [java] org.apache.ofbiz.base.container.ContainerLoader.start(ContainerLoader.java:100)
                    [java] org.apache.ofbiz.base.start.Start.startStartLoaders(Start.java:272)
                    [java] org.apache.ofbiz.base.start.Start.startServer(Start.java:322)
                    [java] org.apache.ofbiz.base.start.Start.start(Start.java:326)
                    [java] org.apache.ofbiz.base.start.Start.main(Start.java:411)
                    [java] --------------------------------------------------------------------------------
                    [java]
                    ....
                    ....
                    ....
                    [java] 2009-12-22 16:05:28,366 (main) [    TransactionUtil.java:346:INFO ] [TransactionUtil.rollback] transaction rolled back
                    [java] 2009-12-22 16:05:28,370 (main) [   TestRunContainer.java:234:INFO ] [JUNIT] : [test case's name] finished.
                </programlisting>
            </para>
        </section>
    </section>
    <section>
        <title>Test result</title>
        <para>
            After you run unit test, you can see the result of the testing. If you use run-tests target, you can see test result web page by view runtime/logs/test-results/html/index.html file in web browser
            and see JUnit Test Result files for each test suite in runtime/logs/test-results directory. If you use other target you only see JUnit Test Result file in runtime/logs/test-results.
        </para>
    </section>
</chapter>
