You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2012/05/06 11:28:14 UTC
svn commit: r1334579 - in /ofbiz/trunk/framework/minilang:
dtd/simple-methods-v2.xsd
src/org/ofbiz/minilang/method/conditional/Assert.java
Author: adrianc
Date: Sun May 6 09:28:14 2012
New Revision: 1334579
URL: http://svn.apache.org/viewvc?rev=1334579&view=rev
Log:
Overhauled Mini-language <assert> element.
Modified:
ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Assert.java
Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd?rev=1334579&r1=1334578&r2=1334579&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Sun May 6 09:28:14 2012
@@ -3592,37 +3592,32 @@ under the License.
<xs:element name="assert" substitutionGroup="IfOtherOperations">
<xs:annotation>
<xs:documentation>
- Each condition under the assert element will be checked and if it fails an error will be added to the given error list.
- Note that while the definitions for the if-* operations are used, the tags should be empty because of the differing semantics.
-
- This is mainly used for testing, and for writing simple-methods that are meant to be used as part of a test suite.
-
- This is mostly useful for testing because the messages are targeted at a programmer, and not really at an end user.
+ Adds an error to the error list for each condition that evaluates to false.
</xs:documentation>
</xs:annotation>
<xs:complexType>
- <xs:group maxOccurs="unbounded" ref="IfConditions"/>
- <xs:attributeGroup ref="attlist.assert"/>
+ <xs:group maxOccurs="unbounded" ref="IfConditions" />
+ <xs:attribute type="xs:string" name="title">
+ <xs:annotation>
+ <xs:documentation>
+ The title of the assert operation. The title is used in automated test reports.
+ <br/><br/>
+ Optional. Attribute type: constant
+ </xs:documentation>
+ </xs:annotation>
+ </xs:attribute>
+ <xs:attribute type="xs:string" name="error-list-name">
+ <xs:annotation>
+ <xs:documentation>
+ The name of the list in the method environment to check for error messages.
+ Defaults to "error_list".
+ <br/><br/>
+ Optional. Attribute type: constant
+ </xs:documentation>
+ </xs:annotation>
+ </xs:attribute>
</xs:complexType>
</xs:element>
- <xs:attributeGroup name="attlist.assert">
- <xs:attribute type="xs:string" name="title">
- <xs:annotation>
- <xs:documentation>
- Each assert operation have a title that can be used in the report for the testing.
- These can be used in the normal code.
- </xs:documentation>
- </xs:annotation>
- </xs:attribute>
- <xs:attribute type="xs:string" name="error-list-name" default="error_list">
- <xs:annotation>
- <xs:documentation>
- The name of the list in the method environment to check for error messages.
- Defaults to "error_list".
- </xs:documentation>
- </xs:annotation>
- </xs:attribute>
- </xs:attributeGroup>
<!--
Operations that conditionally execute blocks within them; they mimic the
Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Assert.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Assert.java?rev=1334579&r1=1334578&r2=1334579&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Assert.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Assert.java Sun May 6 09:28:14 2012
@@ -18,57 +18,64 @@
*******************************************************************************/
package org.ofbiz.minilang.method.conditional;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import javolution.util.FastList;
-import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilXml;
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
import org.ofbiz.base.util.string.FlexibleStringExpander;
import org.ofbiz.minilang.MiniLangException;
+import org.ofbiz.minilang.MiniLangValidate;
import org.ofbiz.minilang.SimpleMethod;
-import org.ofbiz.minilang.method.ContextAccessor;
import org.ofbiz.minilang.method.MethodContext;
import org.ofbiz.minilang.method.MethodOperation;
import org.w3c.dom.Element;
/**
- * Operation used to check each sub-condition independently and for each one that fails (does not evaluate to true), adds an error to the error message list.
+ * Adds an error to the error list for each condition that evaluates to false.
*/
-public class Assert extends MethodOperation {
+public final class Assert extends MethodOperation {
public static final String module = Assert.class.getName();
- /** List of Conditional objects */
- protected List<Conditional> conditionalList = FastList.newInstance();
- protected ContextAccessor<List<Object>> errorListAcsr;
- protected FlexibleStringExpander titleExdr;
+ private final List<Conditional> conditionalList;
+ private final FlexibleMapAccessor<List<Object>> errorListFma;
+ private final FlexibleStringExpander titleExdr;
public Assert(Element element, SimpleMethod simpleMethod) throws MiniLangException {
super(element, simpleMethod);
- errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list");
+ if (MiniLangValidate.validationOn()) {
+ MiniLangValidate.attributeNames(simpleMethod, element, "title", "error-list-name");
+ MiniLangValidate.constantAttributes(simpleMethod, element, "title", "error-list-name");
+ }
+ errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
titleExdr = FlexibleStringExpander.getInstance(element.getAttribute("title"));
+ List<? extends Element> childElements = UtilXml.childElementList(element);
+ if (MiniLangValidate.validationOn() && childElements.isEmpty()) {
+ MiniLangValidate.handleError("No conditional elements.", simpleMethod, element);
+ }
+ List<Conditional> conditionalList = new ArrayList<Conditional>(childElements.size());
for (Element conditionalElement : UtilXml.childElementList(element)) {
- this.conditionalList.add(ConditionalFactory.makeConditional(conditionalElement, simpleMethod));
+ conditionalList.add(ConditionalFactory.makeConditional(conditionalElement, simpleMethod));
}
+ this.conditionalList = Collections.unmodifiableList(conditionalList);
}
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
- List<Object> messages = errorListAcsr.get(methodContext);
+ List<Object> messages = errorListFma.get(methodContext.getEnvMap());
if (messages == null) {
messages = FastList.newInstance();
- errorListAcsr.put(methodContext, messages);
+ errorListFma.put(methodContext.getEnvMap(), messages);
}
- String title = this.titleExdr.expandString(methodContext.getEnvMap());
- // check each conditional and if fails generate a message to add to the error list
+ String title = titleExdr.expandString(methodContext.getEnvMap());
for (Conditional condition : conditionalList) {
- boolean conditionTrue = condition.checkCondition(methodContext);
- if (!conditionTrue) {
- // pretty print condition
- StringBuilder messageBuffer = new StringBuilder();
- messageBuffer.append("Assertion ");
- if (UtilValidate.isNotEmpty(title)) {
+ if (!condition.checkCondition(methodContext)) {
+ StringBuilder messageBuffer = new StringBuilder("Assertion ");
+ if (!title.isEmpty()) {
messageBuffer.append("[");
messageBuffer.append(title);
messageBuffer.append("] ");
@@ -83,10 +90,9 @@ public class Assert extends MethodOperat
@Override
public String expandedString(MethodContext methodContext) {
- String title = this.titleExdr.expandString(methodContext.getEnvMap());
- StringBuilder messageBuf = new StringBuilder();
- messageBuf.append("<assert");
- if (UtilValidate.isNotEmpty(title)) {
+ String title = titleExdr.expandString(methodContext.getEnvMap());
+ StringBuilder messageBuf = new StringBuilder("<assert");
+ if (!title.isEmpty()) {
messageBuf.append(" title=\"");
messageBuf.append(title);
messageBuf.append("\"");