Related to issue 19539: Improve DynamicExpressionParser test
authorAugusto Mauch <augusto.mauch@openbravo.com>
Tue, 28 Feb 2012 17:46:59 +0100
changeset 15641 1ac5c5ca6841
parent 15640 d4d191fb59ab
child 15642 3afb79bd3730
Related to issue 19539: Improve DynamicExpressionParser test

The DynamicExpressionParserTest has been improved, so it can extend BaseTest instead of TestCase. This way, there is no need to duplicate the code modified in the transformValue function.
The class DynamicExpressionParser has been adapted to be able to be used in a BaseTest. Now, if weld can not be used the get the list of fields and auxiliary inputs, that info will be taken directly from the tab.
modules/org.openbravo.client.application/src-test/org/openbravo/client/application/test/DynamicExpressionParserTest.java
modules/org.openbravo.client.application/src/org/openbravo/client/application/DynamicExpressionParser.java
--- a/modules/org.openbravo.client.application/src-test/org/openbravo/client/application/test/DynamicExpressionParserTest.java	Tue Feb 28 17:45:56 2012 +0100
+++ b/modules/org.openbravo.client.application/src-test/org/openbravo/client/application/test/DynamicExpressionParserTest.java	Tue Feb 28 17:46:59 2012 +0100
@@ -18,66 +18,30 @@
  */
 package org.openbravo.client.application.test;
 
-import java.util.HashMap;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import org.junit.Test;
+import org.openbravo.client.application.DynamicExpressionParser;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.ui.Tab;
+import org.openbravo.test.base.BaseTest;
 
-import junit.framework.Assert;
-import junit.framework.TestCase;
+public class DynamicExpressionParserTest extends BaseTest {
 
-public class DynamicExpressionParserTest extends TestCase {
+  @Test
+  public void testDynamicExpressionParser() throws Exception {
+    setSystemAdministratorContext();
+    String displayLogic = "((@Financial_Invoice_Line@='N'))";
+    String expectedResult = "((OB.Utilities.getValue(currentValues,'financialInvoiceLine') === false))";
+    Tab tab = OBDal.getInstance().get(Tab.class, "270");
 
-  private static Map<String, String> exprToJSMap;
-  static {
-    exprToJSMap = new HashMap<String, String>();
-    exprToJSMap.put("'Y'", "true");
-    exprToJSMap.put("'N'", "false");
-  }
+    DynamicExpressionParser parser = new DynamicExpressionParser(displayLogic, tab);
+    assertTrue(expectedResult.equals(parser.getJSExpression()));
 
-  private String transformValue(String value) {
-    if (value == null) {
-      return null;
-    }
-    String removeBracketsRegExp = "[\\[\\(]*(.*?)[\\)\\]]*";
-    Pattern pattern = Pattern.compile(removeBracketsRegExp);
-    Matcher matcher = pattern.matcher(value);
-    String transformedValueWithBrackets = null;
-    // It is always matched: zero or plus opening brackets, followed by any string, follow by zero
-    // or plus closing brackets
-    if (matcher.matches()) {
-      // Extracts the value
-      String valueWithoutBrackets = matcher.group(1);
-      // Transforms the value
-      String transformedValueWithoutBrackets = exprToJSMap.get(valueWithoutBrackets) != null ? exprToJSMap
-          .get(valueWithoutBrackets) : valueWithoutBrackets;
-      // Re-encloses the value
-      transformedValueWithBrackets = value.replace(valueWithoutBrackets,
-          transformedValueWithoutBrackets);
-    }
-    return transformedValueWithBrackets;
-  }
+    displayLogic = "@Financial_Invoice_Line@='Y'";
+    expectedResult = "OB.Utilities.getValue(currentValues,'financialInvoiceLine') === true";
 
-  public void testRegularExpression() {
-    String value = "'Y'";
-    String transformedValue = transformValue(value);
-    Assert.assertEquals(transformedValue, "true");
+    parser = new DynamicExpressionParser(displayLogic, tab);
+    assertTrue(expectedResult.equals(parser.getJSExpression()));
 
-    value = "(['Y'";
-    transformedValue = transformValue(value);
-    Assert.assertEquals(transformedValue, "([true");
-
-    value = "'Y')";
-    transformedValue = transformValue(value);
-    Assert.assertEquals(transformedValue, "true)");
-
-    value = "(['Y'])";
-    transformedValue = transformValue(value);
-    Assert.assertEquals(transformedValue, "([true])");
-
-    value = "('NotBoolean')";
-    transformedValue = transformValue(value);
-    Assert.assertEquals(transformedValue, "('NotBoolean')");
   }
 
 }
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/DynamicExpressionParser.java	Tue Feb 28 17:45:56 2012 +0100
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/DynamicExpressionParser.java	Tue Feb 28 17:46:59 2012 +0100
@@ -219,9 +219,18 @@
   private DisplayLogicElement getDisplayLogicTextTranslate(String token) {
     if (token == null || token.trim().equals(""))
       return new DisplayLogicElement("", false);
-    ApplicationDictionaryCachedStructures cachedStructures = WeldUtils
-        .getInstanceFromStaticBeanManager(ApplicationDictionaryCachedStructures.class);
-    for (Field field : cachedStructures.getFieldsOfTab(tab.getId())) {
+    List<Field> fields;
+    List<AuxiliaryInput> auxIns;
+    try {
+      ApplicationDictionaryCachedStructures cachedStructures = WeldUtils
+          .getInstanceFromStaticBeanManager(ApplicationDictionaryCachedStructures.class);
+      fields = cachedStructures.getFieldsOfTab(tab.getId());
+      auxIns = cachedStructures.getAuxiliarInputList(tab.getId());
+    } catch (NullPointerException e) {
+      fields = tab.getADFieldList();
+      auxIns = tab.getADAuxiliaryInputList();
+    }
+    for (Field field : fields) {
       if (field.getColumn() == null) {
         continue;
       }
@@ -237,7 +246,7 @@
             uiDef instanceof YesNoUIDefinition);
       }
     }
-    for (AuxiliaryInput auxIn : cachedStructures.getAuxiliarInputList(tab.getId())) {
+    for (AuxiliaryInput auxIn : auxIns) {
       if (token.equalsIgnoreCase(auxIn.getName())) {
         auxInputsInExpression.add(auxIn);
         return new DisplayLogicElement(TOKEN_PREFIX + auxIn.getName(), false);