details:   https://code.openbravo.com/erp/devel/pi/rev/253508d68cbf
changeset: 33651:253508d68cbf
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Wed Mar 07 14:26:09 2018 +0100
summary:   fixed bug 38066: no view cache in browser for windows with server 
display logic

  Window having at least a field with server side display logic were not cached
  in browser if there was no preference configuring that display logic.

  The problem was cuased by an incorrect eTag calculation that if not preference
  was set a date in future was set including current hour.

  The fix:
   * Simplifies eTag calculation for this case: if no preference defined, no 
need
     to add any date.
   * Uses for StringBuilder for String concatenation to slightly improve 
performance

details:   https://code.openbravo.com/erp/devel/pi/rev/92b12ce052c6
changeset: 33652:92b12ce052c6
user:      Asier Lostalé <asier.lostale <at> openbravo.com>
date:      Thu Mar 08 10:10:30 2018 +0100
summary:   related to bug 38066: added test case

diffstat:

 
modules/org.openbravo.client.application/src/org/openbravo/client/application/ViewComponent.java
 |   43 +--
 src-test/src/org/openbravo/test/AllWebserviceTests.java                        
                  |    4 +-
 src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java          
                  |   22 +-
 src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java             
                  |   13 +-
 src-test/src/org/openbravo/test/views/ETagGeneration.java                      
                  |  113 ++++++++++
 5 files changed, 154 insertions(+), 41 deletions(-)

diffs (truncated from 325 to 300 lines):

diff -r 2a79f597da83 -r 92b12ce052c6 
modules/org.openbravo.client.application/src/org/openbravo/client/application/ViewComponent.java
--- 
a/modules/org.openbravo.client.application/src/org/openbravo/client/application/ViewComponent.java
  Wed Mar 07 12:40:14 2018 +0100
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/ViewComponent.java
  Thu Mar 08 10:10:30 2018 +0100
@@ -18,7 +18,6 @@
  */
 package org.openbravo.client.application;
 
-import java.util.Calendar;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
@@ -294,30 +293,27 @@
   }
 
   private synchronized String getViewVersionHash() {
-    String viewVersionHash = "";
-    String viewVersions = "";
     final String viewId = getParameter("viewId");
     OBContext.setAdminMode();
     try {
       Window window = OBDal.getInstance().get(Window.class, 
correctViewId(viewId));
       if (window == null) {
-        return viewVersionHash;
+        return "";
       }
+
+      StringBuilder viewVersions = new StringBuilder();
       for (Tab t : window.getADTabList()) {
-        viewVersions += t.getTable().isFullyAudited() + "|";
+        viewVersions.append(t.getTable().isFullyAudited()).append("|");
       }
-      viewVersions += getLastGridConfigurationChange(window) + "|";
-      viewVersions += getLastSystemPrefrenceChage(window) + "|";
-      viewVersionHash = DigestUtils.md5Hex(viewVersions);
+      viewVersions.append(getLastGridConfigurationChange(window)).append("|");
+      viewVersions.append(getLastSystemPrefrenceChange(window)).append("|");
+      return DigestUtils.md5Hex(viewVersions.toString());
     } finally {
       OBContext.restorePreviousMode();
     }
-    return viewVersionHash;
   }
 
-  private String getLastSystemPrefrenceChage(Window window) {
-    Date lastModification = new Date(0);
-
+  private String getLastSystemPrefrenceChange(Window window) {
     Set<String> preferences = new HashSet<String>();
 
     Pattern p = 
Pattern.compile(DynamicExpressionParser.REPLACE_DISPLAY_LOGIC_SERVER_PATTERN);
@@ -328,17 +324,8 @@
       }
     }
 
-    Calendar cal = Calendar.getInstance();
-    cal.set(9999, 9, 9);
-    Date updated = new Date(cal.getTimeInMillis());
-    if (!preferences.isEmpty()) {
-      updated = getLastUpdated(preferences, updated);
-    }
-    if (lastModification.compareTo(updated) < 0) {
-      lastModification = updated;
-    }
-
-    return lastModification.toString();
+    Date updated = !preferences.isEmpty() ? getLastUpdated(preferences) : null;
+    return updated == null ? "" : updated.toString();
   }
 
   @SuppressWarnings("unchecked")
@@ -355,11 +342,10 @@
     Query query = session.createQuery(where.toString());
     query.setParameter("windowId", windowID);
 
-    return (List<String>) query.list();
+    return query.list();
   }
 
-  private Date getLastUpdated(Set<String> preferenceSet, Date 
lastUpdatedParam) {
-
+  private Date getLastUpdated(Set<String> preferenceSet) {
     StringBuilder where = new StringBuilder();
     where.append(" select max(p.updated)");
     where.append(" from ADPreference p");
@@ -374,10 +360,7 @@
     Query query = session.createQuery(where.toString());
     query.setParameterList("properties", preferenceSet);
     Date lastUpdated = (Date) query.uniqueResult();
-    if (lastUpdated == null) {
-      lastUpdated = lastUpdatedParam;
-    }
+
     return lastUpdated;
-
   }
 }
diff -r 2a79f597da83 -r 92b12ce052c6 
src-test/src/org/openbravo/test/AllWebserviceTests.java
--- a/src-test/src/org/openbravo/test/AllWebserviceTests.java   Wed Mar 07 
12:40:14 2018 +0100
+++ b/src-test/src/org/openbravo/test/AllWebserviceTests.java   Thu Mar 08 
10:10:30 2018 +0100
@@ -43,6 +43,7 @@
 import org.openbravo.test.datasource.TestNoteDatasource;
 import org.openbravo.test.security.ExplicitCrossOrganizationReference;
 import org.openbravo.test.security.UserInfoSessionDataTest;
+import org.openbravo.test.views.ETagGeneration;
 import org.openbravo.test.webservice.JSONWebServices;
 import org.openbravo.test.webservice.JSONWebServicesWhereParameter;
 import org.openbravo.test.webservice.PerformanceTest;
@@ -92,7 +93,8 @@
     LinkToParentTreeDataSourceTest.class, //
     OtherDatasourceRequests.class, //
     NonIdForeignKeyFilters.class, //
-    ResetCookieOnLogin.class //
+    ResetCookieOnLogin.class, //
+    ETagGeneration.class //
 })
 public class AllWebserviceTests {
 }
diff -r 2a79f597da83 -r 92b12ce052c6 
src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java
--- a/src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java     
Wed Mar 07 12:40:14 2018 +0100
+++ b/src-test/src/org/openbravo/test/datasource/BaseDataSourceTestDal.java     
Thu Mar 08 10:10:30 2018 +0100
@@ -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) 2014-2017 Openbravo SLU 
+ * All portions are Copyright (C) 2014-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -68,15 +68,29 @@
    * Performs a request to Openbravo returning its response and asserting the 
response code matches
    * expectedResponse.
    */
+  /**
+   * Performs a request to Openbravo returning its response and asserting the 
response code matches
+   * expectedResponse.
+   */
   protected String doRequest(String wsPart, String content, int 
expectedResponse, String method,
       String contentType) throws Exception {
+    authenticate();
+
+    return DatasourceTestUtil.request(getOpenbravoURL(), wsPart, method, 
content, cookie, 200,
+        contentType);
+  }
+
+  /**
+   * Performs a request to authenticate with current settings if already not 
authenticated.
+   * 
+   * @return Sting with cookie with authenticated session id
+   * */
+  protected String authenticate() throws Exception {
     if (!authenticated) {
       cookie = DatasourceTestUtil.authenticate(getOpenbravoURL(), getLogin(), 
getPassword());
       authenticated = true;
     }
-
-    return DatasourceTestUtil.request(getOpenbravoURL(), wsPart, method, 
content, cookie, 200,
-        contentType);
+    return cookie;
   }
 
   /**
diff -r 2a79f597da83 -r 92b12ce052c6 
src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java
--- a/src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java        
Wed Mar 07 12:40:14 2018 +0100
+++ b/src-test/src/org/openbravo/test/datasource/DatasourceTestUtil.java        
Thu Mar 08 10:10:30 2018 +0100
@@ -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) 2014-2015 Openbravo SLU 
+ * All portions are Copyright (C) 2014-2018 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -26,8 +26,6 @@
 import java.util.Map;
 import java.util.Properties;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.codehaus.jettison.json.JSONObject;
@@ -36,18 +34,21 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import junit.framework.TestCase;
+
 /**
  * Utility methods to deal with datasource calls.
  * 
  * @author alostale
  * 
  */
-class DatasourceTestUtil {
+public class DatasourceTestUtil {
   private static final Logger log = 
LoggerFactory.getLogger(DatasourceTestUtil.class);
   private static final String CONTEXT_PROPERTY = "context.url";
 
-  static HttpURLConnection createConnection(String url, String wsPart, String 
method, String cookie)
-      throws Exception {
+  /** Creates a connection to a given URL without processing it. */
+  public static HttpURLConnection createConnection(String url, String wsPart, 
String method,
+      String cookie) throws Exception {
 
     String completeUrl = url + wsPart;
     log.debug("Create conntection URL: {}, method {}", completeUrl, method);
diff -r 2a79f597da83 -r 92b12ce052c6 
src-test/src/org/openbravo/test/views/ETagGeneration.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src-test/src/org/openbravo/test/views/ETagGeneration.java Thu Mar 08 
10:10:30 2018 +0100
@@ -0,0 +1,113 @@
+/*
+ *************************************************************************
+ * 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) 2018 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.views;
+
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assume.assumeThat;
+
+import java.net.HttpURLConnection;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.hibernate.Query;
+import org.junit.Test;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.businessUtility.Preferences;
+import org.openbravo.model.ad.domain.Preference;
+import org.openbravo.model.ad.system.Client;
+import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.test.datasource.BaseDataSourceTestDal;
+import org.openbravo.test.datasource.DatasourceTestUtil;
+
+/** Test cases covering ETag management for generated views */
+public class ETagGeneration extends BaseDataSourceTestDal {
+  private static final String SALES_ORDER_WINDOW = "143";
+
+  @Test
+  public void eTagShouldBeStable() throws Exception {
+    assumeThat("Has modules in development", hasModulesInDevelopment(), 
is(false));
+
+    String eTag = getEtag(SALES_ORDER_WINDOW);
+
+    assertResponseCode("Response without changes", SALES_ORDER_WINDOW, eTag,
+        HttpServletResponse.SC_NOT_MODIFIED);
+  }
+
+  @Test
+  public void serverDisplayLogicConfigShouldChangeETag() throws Exception {
+    assumeThat("Has modules in development", hasModulesInDevelopment(), 
is(false));
+
+    Preference newPref = null;
+    setSystemAdministratorContext();
+    try {
+      String oldEtag = getEtag(SALES_ORDER_WINDOW);
+
+      if (!Preferences.existsPreference("UomManagement", true, "0", "0", null, 
null, null)) {
+        newPref = Preferences.setPreferenceValue("UomManagement", "Y", true, 
OBDal.getInstance()
+            .getProxy(Client.class, "0"), 
OBDal.getInstance().getProxy(Organization.class, "0"),
+            null, null, null, null);
+      }
+      OBDal.getInstance().commitAndClose();
+
+      String newEtag = getEtag(SALES_ORDER_WINDOW);
+      assertThat("ETag should change", newEtag, is(not(oldEtag)));
+
+      assertResponseCode("Response after adding server dl config", 
SALES_ORDER_WINDOW, oldEtag,
+          HttpServletResponse.SC_OK);
+      assertResponseCode("Response on 2nd request after change in dl server 
config",
+          SALES_ORDER_WINDOW, newEtag, HttpServletResponse.SC_NOT_MODIFIED);
+    } finally {
+      if (newPref != null) {
+        OBDal.getInstance().remove(newPref);
+      }
+    }
+  }
+
+  private Boolean hasModulesInDevelopment() {
+    final Query indevelMods = OBDal.getInstance().getSession()
+        .createQuery("select 1 from ADModule m where m.inDevelopment=true");
+    indevelMods.setMaxResults(1);

------------------------------------------------------------------------------
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