modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/WeldBaseTest.java
author Javier Armendáriz <javier.armendariz@openbravo.com>
Mon, 11 Feb 2019 11:01:09 +0100
changeset 35440 9275bf6ed1ff
parent 35352 4590a05d86f8
parent 35439 3a11d791ddab
permissions -rw-r--r--
Fixed issue 34821: High memory usage on tests using arquillian.
harpreet@9076
     1
/*
harpreet@9076
     2
 *************************************************************************
harpreet@9076
     3
 * The contents of this file are subject to the Openbravo  Public  License
harpreet@9076
     4
 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
harpreet@9076
     5
 * Version 1.1  with a permitted attribution clause; you may not  use this
harpreet@9076
     6
 * file except in compliance with the License. You  may  obtain  a copy of
harpreet@9076
     7
 * the License at http://www.openbravo.com/legal/license.html 
harpreet@9076
     8
 * Software distributed under the License  is  distributed  on  an "AS IS"
harpreet@9076
     9
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
harpreet@9076
    10
 * License for the specific  language  governing  rights  and  limitations
harpreet@9076
    11
 * under the License.
harpreet@9076
    12
 * The Original Code is Openbravo ERP.
harpreet@9076
    13
 * The Initial Developer of the Original Code is Openbravo SLU
javier@35440
    14
 * All portions are Copyright (C) 2010-2019 Openbravo SLU
harpreet@9076
    15
 * All Rights Reserved.
harpreet@9076
    16
 * Contributor(s):  ______________________________________.
harpreet@9076
    17
 ************************************************************************
harpreet@9076
    18
 */
harpreet@9076
    19
package org.openbravo.base.weld.test;
harpreet@9076
    20
carlos@34005
    21
import java.util.HashMap;
carlos@34005
    22
import java.util.Map;
carlos@34005
    23
harpreet@9076
    24
import javax.enterprise.inject.Any;
carlos@34005
    25
import javax.enterprise.inject.Instance;
harpreet@9076
    26
import javax.enterprise.inject.spi.Bean;
harpreet@9076
    27
import javax.enterprise.inject.spi.BeanManager;
harpreet@9076
    28
import javax.enterprise.util.AnnotationLiteral;
harpreet@9076
    29
import javax.inject.Inject;
harpreet@9076
    30
javier@35439
    31
import org.eu.ingwar.tools.arquillian.extension.suite.annotations.ArquillianSuiteDeployment;
asier@35352
    32
import org.apache.logging.log4j.LogManager;
asier@35352
    33
import org.apache.logging.log4j.Logger;
carlos@34005
    34
import org.hibernate.dialect.function.SQLFunction;
asier@27093
    35
import org.jboss.arquillian.container.test.api.Deployment;
inigo@24236
    36
import org.jboss.arquillian.junit.Arquillian;
inigo@24236
    37
import org.jboss.shrinkwrap.api.ShrinkWrap;
asier@27093
    38
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
inigo@24236
    39
import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
inigo@24236
    40
import org.jboss.shrinkwrap.api.spec.JavaArchive;
asier@27116
    41
import org.junit.AfterClass;
asier@27094
    42
import org.junit.Before;
harpreet@9076
    43
import org.junit.runner.RunWith;
harpreet@9076
    44
import org.openbravo.base.session.OBPropertiesProvider;
asier@27116
    45
import org.openbravo.base.session.SessionFactoryController;
asier@27094
    46
import org.openbravo.base.weld.WeldUtils;
asier@27111
    47
import org.openbravo.client.kernel.KernelInitializer;
asier@27116
    48
import org.openbravo.dal.core.OBInterceptor;
carlos@34005
    49
import org.openbravo.dal.core.SQLFunctionRegister;
inigo@24235
    50
import org.openbravo.test.base.OBBaseTest;
harpreet@9076
    51
harpreet@9076
    52
/**
harpreet@9076
    53
 * Base test for weld, provides access to the weld container.
harpreet@9076
    54
 * 
harpreet@9076
    55
 * @author mtaal
harpreet@9076
    56
 */
harpreet@9076
    57
@RunWith(Arquillian.class)
javier@35439
    58
@ArquillianSuiteDeployment
inigo@24235
    59
public class WeldBaseTest extends OBBaseTest {
javier@35032
    60
  private static final Logger log = LogManager.getLogger();
harpreet@9076
    61
asier@27116
    62
  private static boolean initialized = false;
asier@27116
    63
  private static JavaArchive archive = null;
asier@27116
    64
harpreet@9076
    65
  @Deployment
harpreet@9076
    66
  public static JavaArchive createTestArchive() {
asier@27116
    67
    if (archive == null) {
inigo@29508
    68
inigo@29508
    69
      // Setting this property to "true" is avoided throwing an error when this class is used with
inigo@29508
    70
      // analytics module. It is a workaround for Weld proxy bug. See issue:
inigo@29508
    71
      // https://issues.openbravo.com/view.php?id=32704
inigo@29508
    72
      System.setProperty("com.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager", "true");
inigo@29508
    73
asier@27116
    74
      log.info("Creating cdi archive...");
asier@35352
    75
      final String sourcePath = OBPropertiesProvider.getInstance()
asier@35352
    76
          .getOpenbravoProperties()
asier@27116
    77
          .getProperty("source.path");
asier@27116
    78
      archive = ShrinkWrap.create(JavaArchive.class);
asier@27093
    79
asier@27116
    80
      // add all beans without exclusions so cdi can also be used for *test* packages
asier@27116
    81
      archive.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
asier@27093
    82
asier@27116
    83
      // include all classes deployed in webapp container
asier@27116
    84
      archive.as(ExplodedImporter.class).importDirectory(sourcePath + "/build/classes/");
asier@27093
    85
asier@27116
    86
      // ...and all the jUnit ones
asier@27116
    87
      archive.as(ExplodedImporter.class).importDirectory(sourcePath + "/src-test/build/classes/");
asier@27093
    88
asier@27116
    89
      // include all libraries deployed in webapp container
asier@27116
    90
      archive.addAsDirectory(sourcePath + "/WebContent/WEB-INF/lib");
asier@27093
    91
asier@27116
    92
      log.debug(archive.toString(true));
asier@27116
    93
      log.info("... cdi archive created");
asier@27116
    94
    }
harpreet@9076
    95
    return archive;
harpreet@9076
    96
  }
harpreet@9076
    97
harpreet@9076
    98
  @SuppressWarnings("serial")
harpreet@9076
    99
  private static final AnnotationLiteral<Any> ANY = new AnnotationLiteral<Any>() {
harpreet@9076
   100
  };
harpreet@9076
   101
harpreet@9076
   102
  @Inject
harpreet@9076
   103
  private BeanManager beanManager;
harpreet@9076
   104
asier@27116
   105
  @Inject
asier@31980
   106
  private WeldUtils weldUtils;
asier@31980
   107
asier@31980
   108
  @Inject
asier@27116
   109
  private KernelInitializer kernelInitializer;
asier@27116
   110
carlos@34005
   111
  @Inject
carlos@34005
   112
  @Any
carlos@34005
   113
  private Instance<SQLFunctionRegister> sqlFunctionRegisters;
carlos@34005
   114
asier@27111
   115
  /**
asier@27116
   116
   * Sets static instance bean manager in WeldUtils so it is globally accessible and initializes
asier@27116
   117
   * kernel.
asier@27116
   118
   * 
asier@27116
   119
   * Arquillian creates a new cdi container for each test class but keeps existent one for all tests
asier@27116
   120
   * within same class, let's initialize it once per class but we cannot use @BeforeClass at this
asier@27116
   121
   * point because we require of beanManager to be injected.
asier@27111
   122
   */
asier@31311
   123
  @Override
asier@27094
   124
  @Before
asier@31311
   125
  public void setUp() throws Exception {
asier@27116
   126
    if (!initialized) {
carlos@34005
   127
      initializeDalLayer(getSqlFunctions());
asier@27116
   128
      WeldUtils.setStaticInstanceBeanManager(beanManager);
asier@27116
   129
      kernelInitializer.setInterceptor();
asier@31980
   130
      weldUtils.setBeanManager(beanManager);
asier@27116
   131
      initialized = true;
asier@27116
   132
    }
asier@31311
   133
    super.setUp();
asier@27116
   134
  }
asier@27116
   135
carlos@34005
   136
  private Map<String, SQLFunction> getSqlFunctions() {
carlos@34005
   137
    Map<String, SQLFunction> sqlFunctions = new HashMap<>();
carlos@34005
   138
    if (sqlFunctionRegisters == null) {
carlos@34005
   139
      return sqlFunctions;
carlos@34005
   140
    }
carlos@34005
   141
    for (SQLFunctionRegister register : sqlFunctionRegisters) {
carlos@34005
   142
      Map<String, SQLFunction> registeredSqlFunctions = register.getSQLFunctions();
carlos@34005
   143
      if (registeredSqlFunctions == null) {
carlos@34005
   144
        continue;
carlos@34005
   145
      }
carlos@34005
   146
      sqlFunctions.putAll(registeredSqlFunctions);
carlos@34005
   147
    }
carlos@34005
   148
    return sqlFunctions;
carlos@34005
   149
  }
carlos@34005
   150
asier@27116
   151
  /**
asier@27116
   152
   * Once we are done with the class execution, OBInterceptor needs to be reset other case when
asier@27116
   153
   * executing a suite it will reuse the container created for the previous classes instead of the
asier@27116
   154
   * new one.
asier@27116
   155
   */
asier@27116
   156
  @AfterClass
asier@27116
   157
  public static void resetOBInterceptors() {
asier@27116
   158
    final OBInterceptor interceptor = (OBInterceptor) SessionFactoryController.getInstance()
asier@35352
   159
        .getConfiguration()
asier@35352
   160
        .getInterceptor();
asier@27116
   161
    interceptor.setInterceptorListener(null);
asier@27116
   162
    initialized = false;
asier@27094
   163
  }
asier@27094
   164
harpreet@9076
   165
  @SuppressWarnings("unchecked")
harpreet@9076
   166
  protected <U extends Object> U getWeldComponent(Class<U> clz) {
harpreet@9076
   167
harpreet@9076
   168
    final Bean<?> bean = beanManager.getBeans(clz, ANY).iterator().next();
harpreet@9076
   169
harpreet@9076
   170
    return (U) beanManager.getReference(bean, clz, beanManager.createCreationalContext(bean));
harpreet@9076
   171
  }
harpreet@9076
   172
}