You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by nm...@apache.org on 2017/11/25 17:35:20 UTC

svn commit: r1816335 - in /ofbiz/ofbiz-framework/trunk/framework: base/testdef/basetests.xml testtools/dtd/test-suite.xsd testtools/src/main/java/org/apache/ofbiz/testtools/ModelTestSuite.java

Author: nmalin
Date: Sat Nov 25 17:35:20 2017
New Revision: 1816335

URL: http://svn.apache.org/viewvc?rev=1816335&view=rev
Log:
Implemented: Implement Groovy test in testtools (OFBIZ-9996)

With the minilang deprecation and the migration to groovy, I implemented on the ofbiz testtools the possibility to use groovy for write new integration test or convert current simple xml test.
Now you can define a new element as child of test-group on your xml test-suite file like
        <groovy-test-suite name=simple location=component://base/groovyScript/test/SimpleTests.groovy/>

To Write your test, you need to create a Groovy class that extends GroovyScriptTestCase class :
    import org.apache.ofbiz.testtools.GroovyScriptTestCase
    class BaseTest extends GroovyScriptTestCase {
        void testTrue() {
            assert 1, 1
        }
    }
All your function will execute as TestCase.

For more information on groovy specific testing language see http://groovy-lang.org/testing.html

At this time the OFBiz Groovy DSL isn't supported to write groovy test case but you have access to the delegator and the disphatcher as native class variable.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml
    ofbiz/ofbiz-framework/trunk/framework/testtools/dtd/test-suite.xsd
    ofbiz/ofbiz-framework/trunk/framework/testtools/src/main/java/org/apache/ofbiz/testtools/ModelTestSuite.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml?rev=1816335&r1=1816334&r2=1816335&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/testdef/basetests.xml Sat Nov 25 17:35:20 2017
@@ -43,5 +43,6 @@
         <junit-test-suite class-name="org.apache.ofbiz.base.test.BaseUnitTests"/>
         <junit-test-suite class-name="org.apache.ofbiz.base.util.test.UtilPropertiesTests"/>
         <junit-test-suite class-name="org.apache.ofbiz.base.util.test.UtilXmlTests"/>
+        <groovy-test-suite name="simple" location="component://base/groovyScript/test/SimpleTests.groovy"/>
     </test-group>
 </test-suite>

Modified: ofbiz/ofbiz-framework/trunk/framework/testtools/dtd/test-suite.xsd
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/testtools/dtd/test-suite.xsd?rev=1816335&r1=1816334&r2=1816335&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/testtools/dtd/test-suite.xsd (original)
+++ ofbiz/ofbiz-framework/trunk/framework/testtools/dtd/test-suite.xsd Sat Nov 25 17:35:20 2017
@@ -1,3 +1,4 @@
+
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
 Licensed to the Apache Software Foundation (ASF) under one
@@ -86,6 +87,29 @@ under the License.
         </xs:attribute>
     </xs:attributeGroup>
 
+    <xs:element name="groovy-test-suite" substitutionGroup="TestCaseTypes">
+        <xs:annotation>
+            <xs:documentation>
+                Used for JUnit test suites written as a Groovy class. See http://groovy-lang.org/testing.html
+            </xs:documentation>
+        </xs:annotation>
+        <xs:complexType>
+            <xs:attributeGroup ref="attlist.groovy-test-suite"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:attributeGroup name="attlist.groovy-test-suite">
+        <xs:attribute type="xs:string" name="name"/>
+    </xs:attributeGroup>
+    <xs:attributeGroup name="attlist.groovy-test-suite">
+        <xs:attribute type="xs:string" name="location" use="required"/>
+            <xs:annotation>
+                <xs:documentation>
+                    Give the location where is groovy file that contaions the test suite.
+                    You can use a flexible url location like component://mycomponent/groovyScript/test/MySuiteTest.groovy
+                </xs:documentation>
+            </xs:annotation>
+    </xs:attributeGroup>
+
     <xs:element name="service-test" substitutionGroup="TestCaseTypes">
         <xs:complexType>
             <xs:attributeGroup ref="attlist.service-test"/>

Modified: ofbiz/ofbiz-framework/trunk/framework/testtools/src/main/java/org/apache/ofbiz/testtools/ModelTestSuite.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/testtools/src/main/java/org/apache/ofbiz/testtools/ModelTestSuite.java?rev=1816335&r1=1816334&r2=1816335&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/testtools/src/main/java/org/apache/ofbiz/testtools/ModelTestSuite.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/testtools/src/main/java/org/apache/ofbiz/testtools/ModelTestSuite.java Sat Nov 25 17:35:20 2017
@@ -25,6 +25,8 @@ import java.util.List;
 
 import org.apache.commons.lang.RandomStringUtils;
 import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.GeneralException;
+import org.apache.ofbiz.base.util.GroovyUtil;
 import org.apache.ofbiz.base.util.ObjectType;
 import org.apache.ofbiz.base.util.UtilGenerics;
 import org.apache.ofbiz.base.util.UtilMisc;
@@ -115,6 +117,14 @@ public class ModelTestSuite {
                 String errMsg = "Unable to load test suite class : " + className;
                 Debug.logError(e, errMsg, module);
             }
+        } else if ("groovy-test-suite".equals(nodeName)) {
+            try {
+                Class testClass = GroovyUtil.getScriptClassFromLocation(testElement.getAttribute("location"));
+                TestCase groovyTestCase = (GroovyScriptTestCase) testClass.newInstance();
+                this.testList.add(new TestSuite(testClass, testElement.getAttribute("name")));
+            } catch (GeneralException|InstantiationException|IllegalAccessException e) {
+                Debug.logError(e, module);
+            }
         } else if ("service-test".equals(nodeName)) {
             this.testList.add(new ServiceTest(caseName, testElement));
         } else if ("simple-method-test".equals(nodeName)) {
@@ -189,6 +199,9 @@ public class ModelTestSuite {
             if (test instanceof OFBizTestCase) {
                 ((OFBizTestCase)test).setDispatcher(dispatcher);
             }
+        } else if (test instanceof GroovyScriptTestCase) {
+            ((GroovyScriptTestCase)test).setDelegator(delegator);
+            ((GroovyScriptTestCase)test).setDispatcher(dispatcher);
         }
     }
 }