details:   https://code.openbravo.com/erp/devel/pi/rev/c919c3df8538
changeset: 32676:c919c3df8538
user:      Carlos Aristu <carlos.aristu <at> openbravo.com>
date:      Wed Sep 13 09:59:56 2017 +0200
summary:   related to issue 36834: Added test case for OBBindings

diffstat:

 src-test/src/org/openbravo/test/AllAntTaskTests.java           |    4 +-
 src-test/src/org/openbravo/test/AllTests.java                  |    6 +-
 src-test/src/org/openbravo/test/AntTaskTests.java              |    6 +-
 src-test/src/org/openbravo/test/expression/OBBindingsTest.java |  143 
++++++++++
 4 files changed, 154 insertions(+), 5 deletions(-)

diffs (228 lines):

diff -r 29adad2927d4 -r c919c3df8538 
src-test/src/org/openbravo/test/AllAntTaskTests.java
--- a/src-test/src/org/openbravo/test/AllAntTaskTests.java      Wed Sep 13 
09:26:50 2017 +0200
+++ b/src-test/src/org/openbravo/test/AllAntTaskTests.java      Wed Sep 13 
09:59:56 2017 +0200
@@ -25,6 +25,7 @@
 import org.openbravo.base.weld.test.testinfrastructure.CdiInfrastructure;
 import org.openbravo.base.weld.test.testinfrastructure.DalPersistanceEventTest;
 import org.openbravo.base.weld.test.testinfrastructure.ParameterizedCdi;
+import org.openbravo.client.application.OBBindings;
 import org.openbravo.client.application.test.ADCSInitialization;
 import org.openbravo.client.application.test.ApplicationTest;
 import org.openbravo.client.application.test.DisplayLogicAtServerLevelTest;
@@ -150,7 +151,8 @@
     PersistanceObserver.class, //
 
     // expression
-    EvaluationTest.class,
+    EvaluationTest.class, //
+    OBBindings.class,
 
     // model
     RuntimeModelTest.class, //
diff -r 29adad2927d4 -r c919c3df8538 
src-test/src/org/openbravo/test/AllTests.java
--- a/src-test/src/org/openbravo/test/AllTests.java     Wed Sep 13 09:26:50 
2017 +0200
+++ b/src-test/src/org/openbravo/test/AllTests.java     Wed Sep 13 09:59:56 
2017 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2008-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2008-2017 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -23,6 +23,7 @@
 import org.junit.runners.Suite;
 import org.openbravo.advpaymentmngt.test.FundsTransferTest;
 import org.openbravo.base.weld.test.testinfrastructure.CdiInfrastructure;
+import org.openbravo.client.application.OBBindings;
 import org.openbravo.erpCommon.info.ClassicSelectorTest;
 import org.openbravo.test.centralbroker.CentralBrokerTest;
 import org.openbravo.test.dal.ComputedColumnsTest;
@@ -91,7 +92,8 @@
     TrlColumnsOraTypeTest.class,
 
     // expression
-    EvaluationTest.class,
+    EvaluationTest.class, //
+    OBBindings.class,
 
     // xml
     EntityXMLImportTestBusinessObject.class, //
diff -r 29adad2927d4 -r c919c3df8538 
src-test/src/org/openbravo/test/AntTaskTests.java
--- a/src-test/src/org/openbravo/test/AntTaskTests.java Wed Sep 13 09:26:50 
2017 +0200
+++ b/src-test/src/org/openbravo/test/AntTaskTests.java Wed Sep 13 09:59:56 
2017 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2017 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -22,6 +22,7 @@
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
 import org.openbravo.base.weld.test.testinfrastructure.CdiInfrastructure;
+import org.openbravo.client.application.OBBindings;
 import org.openbravo.erpCommon.info.ClassicSelectorTest;
 import org.openbravo.test.dal.ComputedColumnsTest;
 import org.openbravo.test.dal.DalComplexQueryRequisitionTest;
@@ -104,7 +105,8 @@
     ComputedColumnsTest.class,
 
     // expression
-    EvaluationTest.class,
+    EvaluationTest.class, //
+    OBBindings.class,
 
     // model
     RuntimeModelTest.class, OneToManyTest.class, //
diff -r 29adad2927d4 -r c919c3df8538 
src-test/src/org/openbravo/test/expression/OBBindingsTest.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/expression/OBBindingsTest.java    Wed Sep 
13 09:59:56 2017 +0200
@@ -0,0 +1,143 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2017 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.expression;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.openbravo.client.application.OBBindings;
+import org.openbravo.client.application.OBBindingsConstants;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.test.base.OBBaseTest;
+import org.openbravo.test.base.mock.HttpServletRequestMock;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test the result of evaluating JavaScript expressions configuring {@link 
OBBindings} as the Java
+ * binding to be used by the engine in the execution of the scripts.
+ */
+public class OBBindingsTest extends OBBaseTest {
+  private static final Logger log = 
LoggerFactory.getLogger(OBBindingsTest.class);
+  private static final String DEFAULT = "DEFAULT";
+  private static final String YES = "Y";
+  private ScriptEngine engine;
+  private HttpServletRequestMock request;
+
+  @Before
+  public void initScriptEngine() throws Exception {
+    request = new HttpServletRequestMock();
+    Map<String, String> parameters = new HashMap<>();
+    parameters.put(OBBindingsConstants.COMMAND_TYPE_PARAM, DEFAULT);
+    parameters.put(OBBindingsConstants.POSTED_PARAM, YES);
+    // initialize Javascript engine
+    engine = new ScriptEngineManager().getEngineByName("js");
+    engine.put("OB", new OBBindings(OBContext.getOBContext(), parameters, 
request.getSession()));
+    log.info("Using script engine {}", engine.getClass().getSimpleName());
+  }
+
+  @Test
+  public void formatNowDate() throws ScriptException {
+    Object result = engine.eval("OB.formatDate(new Date())");
+    String expectedDate = new SimpleDateFormat("dd-MM-yyyy").format(new 
Date());
+    assertThat(result.toString(), equalTo(expectedDate));
+  }
+
+  @Test
+  public void formatDate() throws ScriptException {
+    Object result = engine.eval("OB.formatDate(new Date('12/29/2017'))");
+    assertThat(result.toString(), equalTo("29-12-2017"));
+  }
+
+  @Test
+  public void customFormatDate() throws ScriptException {
+    Object result = engine.eval("OB.formatDate(new 
Date('12/29/2017'),'yyyy/MM/dd')");
+    assertThat(result.toString(), equalTo("2017/12/29"));
+  }
+
+  @Test
+  public void formatDateTime() throws ScriptException {
+    Object result = engine.eval("OB.formatDateTime(new Date('12/29/2017 
16:29:21'))");
+    assertThat(result.toString(), equalTo("29-12-2017 16:29:21"));
+  }
+
+  @Test
+  public void parseDate() throws ScriptException, ParseException {
+    Date result = (Date) engine.eval("OB.parseDate('29-12-2017')");
+    Date expectedDate = new SimpleDateFormat("dd-MM-yyyy").parse("29-12-2017");
+    assertThat(result, equalTo(expectedDate));
+  }
+
+  @Test
+  public void customParseDate() throws ScriptException, ParseException {
+    Date result = (Date) 
engine.eval("OB.parseDate('2017/12/29','yyyy/MM/dd')");
+    Date expectedDate = new SimpleDateFormat("dd-MM-yyyy").parse("29-12-2017");
+    assertThat(result, equalTo(expectedDate));
+  }
+
+  @Test
+  public void parseDateTime() throws ScriptException, ParseException {
+    // parseDateTime is only called from the client and assumes that the dates 
that arrive are in
+    // the UTC timezone and transforms it into the local time
+    Date result = (Date) 
engine.eval("OB.parseDateTime('2017-12-29T16:29:21')");
+    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
+    Date expectedDate = convertToLocalTime(sdf.parse("29-12-2017 16:29:21"));
+    assertThat(result, equalTo(expectedDate));
+  }
+
+  private Date convertToLocalTime(Date UTCTime) {
+    Calendar localTime = Calendar.getInstance();
+    localTime.setTime(UTCTime);
+    int gmtMillisecondOffset = (localTime.get(Calendar.ZONE_OFFSET) + localTime
+        .get(Calendar.DST_OFFSET));
+    localTime.add(Calendar.MILLISECOND, gmtMillisecondOffset);
+    return localTime.getTime();
+  }
+
+  @Test
+  public void getContextInfo() throws ScriptException {
+    Object result = engine.eval("OB.getContext().getCurrentClient().id");
+    assertThat(result.toString(), equalTo(TEST_CLIENT_ID));
+  }
+
+  @Test
+  public void getStringValueFromRequestMap() throws ScriptException {
+    String result = (String) engine.eval("OB.getCommandType()");
+    assertThat(result, equalTo(DEFAULT));
+  }
+
+  @Test
+  public void getBooleanValueFromRequestMap() throws ScriptException {
+    Boolean result = (Boolean) engine.eval("OB.isPosted()");
+    assertThat(result, equalTo(Boolean.TRUE));
+  }
+}

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to