details:   https://code.openbravo.com/erp/devel/pi/rev/6c488ffc5162
changeset: 34005:6c488ffc5162
user:      Carlos Aristu <carlos.aristu <at> openbravo.com>
date:      Tue May 22 17:51:09 2018 +0200
summary:   related to issue 38520: fix test case problems

 - Add check to avoid DAL layer re-initialization if the SQL functions are 
already registered
 - Now WeldBaseTests by default register all the SQL functions provided by the 
SQLFunctionRegister instances

diffstat:

 
modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/WeldBaseTest.java
 |  28 +++++++++-
 src-test/src/org/openbravo/test/base/OBBaseTest.java                           
         |  22 +++++++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diffs (129 lines):

diff -r 0974038fc78f -r 6c488ffc5162 
modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/WeldBaseTest.java
--- 
a/modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/WeldBaseTest.java
   Tue May 22 15:34:57 2018 +0000
+++ 
b/modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/WeldBaseTest.java
   Tue May 22 17:51:09 2018 +0200
@@ -11,19 +11,24 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2010-2017 Openbravo SLU
+ * All portions are Copyright (C) 2010-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
  */
 package org.openbravo.base.weld.test;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.util.AnnotationLiteral;
 import javax.inject.Inject;
 
+import org.hibernate.dialect.function.SQLFunction;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -38,6 +43,7 @@
 import org.openbravo.base.weld.WeldUtils;
 import org.openbravo.client.kernel.KernelInitializer;
 import org.openbravo.dal.core.OBInterceptor;
+import org.openbravo.dal.core.SQLFunctionRegister;
 import org.openbravo.test.base.OBBaseTest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -99,6 +105,10 @@
   @Inject
   private KernelInitializer kernelInitializer;
 
+  @Inject
+  @Any
+  private Instance<SQLFunctionRegister> sqlFunctionRegisters;
+
   /**
    * Sets static instance bean manager in WeldUtils so it is globally 
accessible and initializes
    * kernel.
@@ -111,6 +121,7 @@
   @Before
   public void setUp() throws Exception {
     if (!initialized) {
+      initializeDalLayer(getSqlFunctions());
       WeldUtils.setStaticInstanceBeanManager(beanManager);
       kernelInitializer.setInterceptor();
       weldUtils.setBeanManager(beanManager);
@@ -119,6 +130,21 @@
     super.setUp();
   }
 
+  private Map<String, SQLFunction> getSqlFunctions() {
+    Map<String, SQLFunction> sqlFunctions = new HashMap<>();
+    if (sqlFunctionRegisters == null) {
+      return sqlFunctions;
+    }
+    for (SQLFunctionRegister register : sqlFunctionRegisters) {
+      Map<String, SQLFunction> registeredSqlFunctions = 
register.getSQLFunctions();
+      if (registeredSqlFunctions == null) {
+        continue;
+      }
+      sqlFunctions.putAll(registeredSqlFunctions);
+    }
+    return sqlFunctions;
+  }
+
   /**
    * Once we are done with the class execution, OBInterceptor needs to be 
reset other case when
    * executing a suite it will reuse the container created for the previous 
classes instead of the
diff -r 0974038fc78f -r 6c488ffc5162 
src-test/src/org/openbravo/test/base/OBBaseTest.java
--- a/src-test/src/org/openbravo/test/base/OBBaseTest.java      Tue May 22 
15:34:57 2018 +0000
+++ b/src-test/src/org/openbravo/test/base/OBBaseTest.java      Tue May 22 
17:51:09 2018 +0200
@@ -51,6 +51,7 @@
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.provider.OBConfigFileProvider;
 import org.openbravo.base.session.OBPropertiesProvider;
+import org.openbravo.base.session.SessionFactoryController;
 import org.openbravo.base.structure.BaseOBObject;
 import org.openbravo.dal.core.DalContextListener;
 import org.openbravo.dal.core.DalLayerInitializer;
@@ -375,7 +376,7 @@
   }
 
   /**
-   * Initializes the DALLayer, can be overridden to add specific 
initialization behavior.
+   * Initializes the DAL layer, can be overridden to add specific 
initialization behavior.
    * 
    * @param sqlFunctions
    *          a Map with SQL functions to be registered in Hibernate during 
the DAL layer
@@ -383,11 +384,30 @@
    * @throws Exception
    */
   protected void initializeDalLayer(Map<String, SQLFunction> sqlFunctions) 
throws Exception {
+    if (areAllSqlFunctionsRegistered(sqlFunctions)) {
+      // do not re-initialize the DAL layer, as the provided SQL functions are 
already registered
+      return;
+    }
     DalLayerInitializer.getInstance().setInitialized(false);
     log.info("Creating custom DAL layer initialization...");
     staticInitializeDalLayer(sqlFunctions);
   }
 
+  private boolean areAllSqlFunctionsRegistered(Map<String, SQLFunction> 
sqlFunctions) {
+    if (sqlFunctions == null || sqlFunctions.isEmpty()) {
+      return true;
+    }
+    @SuppressWarnings("unchecked")
+    Map<String, SQLFunction> registeredFunctions = 
SessionFactoryController.getInstance()
+        .getConfiguration().getSqlFunctions();
+    for (String sqlFunction : sqlFunctions.keySet()) {
+      if (!registeredFunctions.containsKey(sqlFunction)) {
+        return false;
+      }
+    }
+    return true;
+  }
+
   private static void staticInitializeDalLayer() throws Exception {
     staticInitializeDalLayer(null);
   }

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