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 2021/09/10 08:18:22 UTC

[ofbiz-framework] branch trunk updated: Improved: Improve run test from artefact info (OFBIZ-12312)

This is an automated email from the ASF dual-hosted git repository.

nmalin pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new fa3e357  Improved: Improve run test from artefact info (OFBIZ-12312)
fa3e357 is described below

commit fa3e3573dcde4fab74a238472edb1e78c0873625
Author: Nicolas Malin <ni...@nereide.fr>
AuthorDate: Fri Sep 10 10:17:23 2021 +0200

    Improved: Improve run test from artefact info (OFBIZ-12312)
    
    From https://localhost:8443/webtools/control/ViewComponents we have te possibility to run each test case or test suite.
    
    We improve it with call with rest schema (like https://localhost:8443/webtools/control/RunTest/product/producttagtests) and display the result below
    
    By this way, we also improve the groovy syntax to slim the code and extend xml form to CommonBasicGrid
    
    Thanks to Gil Portenseigne for is support
---
 .../artifactinfo/TestSuiteInfo.groovy              | 31 +++-------
 .../artifactinfo/TestSuiteResults.groovy           | 66 ++++++++++++++++++++++
 .../ofbiz/webtools/artifactinfo/RunTestEvents.java | 23 +++-----
 .../webapp/webtools/WEB-INF/controller.xml         | 15 +++--
 framework/webtools/widget/ArtifactInfoForms.xml    | 34 +++++++----
 framework/webtools/widget/ArtifactInfoScreens.xml  |  8 ++-
 6 files changed, 123 insertions(+), 54 deletions(-)

diff --git a/framework/webtools/groovyScripts/artifactinfo/TestSuiteInfo.groovy b/framework/webtools/groovyScripts/artifactinfo/TestSuiteInfo.groovy
index d33ebe1..ae6538e 100644
--- a/framework/webtools/groovyScripts/artifactinfo/TestSuiteInfo.groovy
+++ b/framework/webtools/groovyScripts/artifactinfo/TestSuiteInfo.groovy
@@ -21,40 +21,27 @@ import org.apache.ofbiz.base.component.ComponentConfig
 import org.apache.ofbiz.base.config.GenericConfigException
 import org.apache.ofbiz.base.config.ResourceHandler
 import org.apache.ofbiz.base.util.UtilXml
-import org.apache.ofbiz.base.util.UtilMisc
-
-import org.w3c.dom.Document
 import org.w3c.dom.Element
 
 List testList = []
 for (ComponentConfig.TestSuiteInfo testSuiteInfo: ComponentConfig.getAllTestSuiteInfos(parameters.compName)) {
-    String componentName = testSuiteInfo.componentConfig.getComponentName()
     ResourceHandler testSuiteResource = testSuiteInfo.createResourceHandler()
 
     try {
-        Document testSuiteDocument = testSuiteResource.getDocument()
-        Element documentElement = testSuiteDocument.getDocumentElement()
-        suiteName =  documentElement.getAttribute("suite-name")
-        firstLine = true
-        for (Element testCaseElement : UtilXml.childElementList(documentElement, UtilMisc.toSet("test-case", "test-group"))) {
-            testMap = [:]
-            String caseName = testCaseElement.getAttribute("case-name")
-            if (firstLine == true) {
-                testMap = UtilMisc.toMap("suiteName", suiteName, "suiteNameSave", suiteName, "caseName", caseName)
-                firstLine = false
-            } else {
-                testMap = UtilMisc.toMap("suiteNameSave", suiteName, "caseName", caseName)
-            }
-            testList.add(testMap)
+        Element documentElement = testSuiteResource.getDocument().getDocumentElement()
+        String suiteName = testSuiteResource.getDocument().getDocumentElement().getAttribute("suite-name")
+        boolean firstLine = true
+        for (Element testCaseElement : UtilXml.childElementList(documentElement, ["test-case", "test-group"] as Set)) {
+            testList << [suiteName     : suiteName,
+                         caseName      : testCaseElement.getAttribute("case-name"),
+                         firstSuiteLine: firstLine ? 'Y' : 'N']
+            firstLine = false
         }
     } catch (GenericConfigException e) {
-        String errMsg = "Error reading XML document from ResourceHandler for loader [" + testSuiteResource.getLoaderName() + "] and location [" + testSuiteResource.getLocation() + "]"
+        String errMsg = "Error reading XML document from ResourceHandler for loader [${testSuiteResource.getLoaderName()}] and location [${testSuiteResource.getLocation()}]"
         logError(e, errMsg)
         throw new IllegalArgumentException(errMsg)
     }
-
-
-
 }
 
 context.suits = testList
diff --git a/framework/webtools/groovyScripts/artifactinfo/TestSuiteResults.groovy b/framework/webtools/groovyScripts/artifactinfo/TestSuiteResults.groovy
new file mode 100644
index 0000000..b0f7813
--- /dev/null
+++ b/framework/webtools/groovyScripts/artifactinfo/TestSuiteResults.groovy
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+
+import org.apache.ofbiz.base.component.ComponentConfig
+import org.apache.ofbiz.base.util.UtilXml
+import org.apache.ofbiz.testtools.TestRunContainer
+import org.w3c.dom.Document
+import org.w3c.dom.Element
+
+List testList = []
+
+for (ComponentConfig.TestSuiteInfo testSuiteInfo : ComponentConfig.getAllTestSuiteInfos(parameters.compName)) {
+    String suiteName = getTestSuiteName(testSuiteInfo)
+    // if a suiteName has been requested, limit result to it.
+    if (parameters.suiteName && suiteName != parameters.suiteName) continue
+
+    boolean firstLine = true
+    for (Element testCaseElement : getTestCaseResultsForSuite(suiteName)) {
+        List<Element> children = UtilXml.childElementList(testCaseElement)
+        Element child = children ? children[0] : null
+        String details = ""
+        if (child) {
+             details = UtilXml.getAttributeValueIgnorePrefix(child, 'message') ?: child.getNodeValue()
+        }
+        testList << [testName        : testCaseElement.getAttribute('name'),
+                     success         : child == null,
+                     details         : details,
+                     suiteName       : suiteName,
+                     displaySuiteName: firstLine,
+                     time            : testCaseElement.getAttribute('time')]
+        firstLine = false
+    }
+}
+context.results = testList
+
+private List<? extends Element> getTestCaseResultsForSuite(String suiteName) {
+    File xmlFile = new File(TestRunContainer.LOG_DIR + suiteName + ".xml")
+    if (xmlFile.exists()) {
+        Document results = UtilXml.readXmlDocument(xmlFile.getText())
+        Element resultElement = results.getDocumentElement()
+        return UtilXml.childElementList(resultElement, ["testcase"] as Set)
+    }
+    return new ArrayList<Element>()
+}
+
+private String getTestSuiteName(ComponentConfig.TestSuiteInfo testSuiteInfo) {
+    Element documentElement = testSuiteInfo.createResourceHandler().getDocument().getDocumentElement()
+    return documentElement.getAttribute("suite-name")
+}
diff --git a/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/RunTestEvents.java b/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/RunTestEvents.java
index ee88219..1a7443d 100644
--- a/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/RunTestEvents.java
+++ b/framework/webtools/src/main/java/org/apache/ofbiz/webtools/artifactinfo/RunTestEvents.java
@@ -21,6 +21,7 @@ package org.apache.ofbiz.webtools.artifactinfo;
 import java.util.ArrayList;
 import java.util.List;
 
+import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -38,28 +39,22 @@ public class RunTestEvents {
 
     public static String runTest(HttpServletRequest request, HttpServletResponse response) throws ContainerException {
 
-        String component = request.getParameter("compName");
-        String suiteName = request.getParameter("suiteName");
-        String caseName = request.getParameter("caseName");
-        String result = null;
+        String component = (String) request.getAttribute("compName");
+        String suiteName = (String) request.getAttribute("suiteName");
+        String caseName = (String) request.getAttribute("caseName");
 
         List<StartupCommand> ofbizCommands = new ArrayList<>();
+        Map cmdArgs = UtilMisc.toMap("component", component, "suitename", suiteName);
         if (caseName == null) {
-            ofbizCommands.add(new StartupCommand.Builder("test").properties(
-                    UtilMisc.toMap("component", component, "suitename", suiteName)).build());
-        } else {
-            ofbizCommands.add(new StartupCommand.Builder("test").properties(
-                    UtilMisc.toMap("component", component, "suitename", suiteName, "case", caseName)).build());
+            cmdArgs.put("case", caseName);
         }
+        ofbizCommands.add(new StartupCommand.Builder("test").properties(cmdArgs).build());
 
         TestRunContainer testRunContainer = new TestRunContainer();
         testRunContainer.init(ofbizCommands, "frontend test run", "   ");
         if (!testRunContainer.start()) {
-            result = "error";
-        } else {
-            result = "success";
+            return "error";
         }
-
-        return result;
+        return "success";
     }
 }
diff --git a/framework/webtools/webapp/webtools/WEB-INF/controller.xml b/framework/webtools/webapp/webtools/WEB-INF/controller.xml
index 0f8bf4b..43683ad 100644
--- a/framework/webtools/webapp/webtools/WEB-INF/controller.xml
+++ b/framework/webtools/webapp/webtools/WEB-INF/controller.xml
@@ -423,15 +423,20 @@ under the License.
         <response name="success" type="view" value="ViewComponents"/>
     </request-map>
 
-    <request-map uri="TestSuiteInfo">
+    <request-map uri="TestSuiteInfo"><security https="true" auth="true"/><response name="success" type="view" value="TestSuiteInfo"/></request-map>
+    <request-map uri="TestSuiteInfo/{compName}"><security https="true" auth="true"/><response name="success" type="view" value="TestSuiteInfo"/></request-map>
+
+    <request-map uri="RunTest/{compName}/{suiteName}">
         <security https="true" auth="true"/>
-        <response name="success" type="view" value="TestSuiteInfo"/>
+        <event type="java" path="org.apache.ofbiz.webtools.artifactinfo.RunTestEvents" invoke="runTest"/>
+        <response name="success" type="request" value="TestSuiteInfo"/>
+        <response name="error" type="request" value="TestSuiteInfo"/>
     </request-map>
-
-    <request-map uri="RunTest">
+    <request-map uri="RunTest/{compName}/{suiteName}/{caseName}">
         <security https="true" auth="true"/>
         <event type="java" path="org.apache.ofbiz.webtools.artifactinfo.RunTestEvents" invoke="runTest"/>
-        <response name="success" type="request-redirect" value="TestSuiteInfo"/>
+        <response name="success" type="request" value="TestSuiteInfo"/>
+        <response name="error" type="request" value="TestSuiteInfo"/>
     </request-map>
     <!-- EntitySQL requests -->
     <request-map uri="EntitySQLProcessor">
diff --git a/framework/webtools/widget/ArtifactInfoForms.xml b/framework/webtools/widget/ArtifactInfoForms.xml
index 97179a4..d00c88b 100644
--- a/framework/webtools/widget/ArtifactInfoForms.xml
+++ b/framework/webtools/widget/ArtifactInfoForms.xml
@@ -20,28 +20,40 @@
 
 <forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://ofbiz.apache.org/Widget-Form" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Form http://ofbiz.apache.org/dtds/widget-form.xsd">
-        <form name="ComponentList" type="list" separate-columns="true" title="Component List" list-name="componentList" target=""
-        odd-row-style="alternate-row" header-row-style="header-row-2" default-table-style="basic-table hover-bar">
+    <grid name="ComponentList" separate-columns="true" title="Component List" list-name="componentList" target=""
+          extends="CommonBasicGrid" extends-resource="component://common/widget/CommonForms.xml">
         <field name="compName">
-            <hyperlink description="${compName}" target="TestSuiteInfo?compName=${compName}" ></hyperlink>
+            <hyperlink description="${compName}" target="TestSuiteInfo/${compName}" ></hyperlink>
         </field>
         <field name="rootLocation"><display/></field>
         <field name="enabled"><display/></field>
         <field name="webAppName"><display/></field>
         <field name="contextRoot"><display/></field>
         <field name="location"><display/></field>
-    </form>
+    </grid>
 
-    <form name="TestSuiteInfo" type="list" separate-columns="true" title="Component List" list-name="suits" target=""
-        odd-row-style="alternate-row" header-row-style="header-row-2" default-table-style="basic-table hover-bar">
+    <grid name="TestSuiteInfo" separate-columns="true" title="Component List" list-name="suits" target=""
+          extends="CommonBasicGrid" extends-resource="component://common/widget/CommonForms.xml">
         <field name="compName"><hidden value="${parameters.compName}"/></field>
-        <field name="suiteName"><display/></field>
-        <field name="runSuite" use-when="suiteName!=void">
-            <hyperlink description="run suite" target="RunTest?compName=${parameters.compName}&amp;suiteName=${suiteNameSave}"/>
+        <field name="suiteName" use-when="firstSuiteLine == 'Y'"><display/></field>
+        <field name="runSuite" use-when="firstSuiteLine == 'Y'">
+            <hyperlink description="run suite" target="RunTest/${parameters.compName}/${suiteName}"/>
         </field>
         <field name="caseName"><display/></field>
         <field name="runCase">
-            <hyperlink description="run case" target="RunTest?compName=${parameters.compName}&amp;suiteName=${suiteNameSave}&amp;caseName=${caseName}"/>
+            <hyperlink description="run case" target="RunTest/${parameters.compName}/${suiteName}/${caseName}"/>
         </field>
-    </form>
+    </grid>
+
+    <grid name="TestSuiteResults" title="Component Suite Results"
+          list-name="results" separate-columns="true" paginate="false"
+          extends="CommonBasicGrid" extends-resource="component://common/widget/CommonForms.xml">
+        <alt-row-style use-when="!success" style="error"/>
+        <field name="compName"><hidden value="${parameters.compName}"/></field>
+        <field name="suiteName"><display description="${groovy: displaySuiteName ? suiteName : ''}"/></field>
+        <field name="testName"><display/></field>
+        <field name="success"><display/></field>
+        <field name="time"><display/></field>
+        <field name="details"><display/></field>
+    </grid>
 </forms>
diff --git a/framework/webtools/widget/ArtifactInfoScreens.xml b/framework/webtools/widget/ArtifactInfoScreens.xml
index b13debd..fe957f8 100644
--- a/framework/webtools/widget/ArtifactInfoScreens.xml
+++ b/framework/webtools/widget/ArtifactInfoScreens.xml
@@ -57,7 +57,7 @@ under the License.
                 <decorator-screen name="CommonArtifactDecorator" location="${parameters.mainDecoratorLocation}">
                     <decorator-section name="body">
                         <screenlet>
-                            <include-form name="ComponentList" location="component://webtools/widget/ArtifactInfoForms.xml"/>
+                            <include-grid name="ComponentList" location="component://webtools/widget/ArtifactInfoForms.xml"/>
                         </screenlet>
                     </decorator-section>
                 </decorator-screen>
@@ -71,12 +71,16 @@ under the License.
                 <set field="titleProperty" value="WebtoolsViewComponents"/>
                 <set field="tabButtonItem" value="viewents"/>
                 <script location="component://webtools/groovyScripts/artifactinfo/TestSuiteInfo.groovy"/>
+                <script location="component://webtools/groovyScripts/artifactinfo/TestSuiteResults.groovy"/>
             </actions>
             <widgets>
                 <decorator-screen name="CommonArtifactDecorator" location="${parameters.mainDecoratorLocation}">
                     <decorator-section name="body">
                         <screenlet title="Component: ${parameters.compName}">
-                            <include-form name="TestSuiteInfo" location="component://webtools/widget/ArtifactInfoForms.xml"/>
+                            <include-grid name="TestSuiteInfo" location="component://webtools/widget/ArtifactInfoForms.xml"/>
+                        </screenlet>
+                        <screenlet title="Last Results: ${parameters.suiteName}">
+                            <include-grid name="TestSuiteResults" location="component://webtools/widget/ArtifactInfoForms.xml"/>
                         </screenlet>
                     </decorator-section>
                 </decorator-screen>