From ce107f3678beed71837993a2e70635208042b7a8 Mon Sep 17 00:00:00 2001 From: hengsin Date: Thu, 14 May 2020 21:41:39 +0800 Subject: [PATCH] =?UTF-8?q?IDEMPIERE-2698=20Performance:=20Report=20checki?= =?UTF-8?q?ng=20for=20zoom=20condition=20on=20dat=E2=80=A6=20(#59)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * IDEMPIERE-2698 Performance: Report checking for zoom condition on database - must be cached Add unit testing * IDEMPIERE-2699 Performance: Constant visits to database for MTable not cached Should usually access from tenant instead system client. --- .../org/idempiere/test/AbstractTestCase.java | 47 +++++++++++++-- .../idempiere/test/performance/CacheTest.java | 60 +++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java diff --git a/org.idempiere.test/src/org/idempiere/test/AbstractTestCase.java b/org.idempiere.test/src/org/idempiere/test/AbstractTestCase.java index d4dc3ae079..b22e8a8b6d 100644 --- a/org.idempiere.test/src/org/idempiere/test/AbstractTestCase.java +++ b/org.idempiere.test/src/org/idempiere/test/AbstractTestCase.java @@ -40,6 +40,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestInfo; /** * @author hengsin @@ -57,26 +58,42 @@ public abstract class AbstractTestCase { protected final int GARDEN_WORLD_HQ_WAREHOUSE = 103; @BeforeAll + /** + * setup for class + */ static void setup() { Adempiere.startup(false); } @BeforeEach - protected void init() { + /** + * Init for each test method + * @param testInfo + */ + protected void init(TestInfo testInfo) { String trxName = Trx.createTrxName(getClass().getName()+"_"); trx = Trx.get(trxName, true); trx.start(); - initContext(); + initContext(testInfo); } - protected LoginDetails newLoginDetails() { + /** + * Create the login context for each test method + * @param testInfo + * @return LoginDetails + */ + protected LoginDetails newLoginDetails(TestInfo testInfo) { return new LoginDetails(GARDEN_WORLD_CLIENT, GARDEN_WORLD_HQ_ORG, GARDEN_WORLD_ADMIN_USER, GARDEN_WORLD_ADMIN_ROLE, GARDEN_WORLD_HQ_WAREHOUSE, new Timestamp(System.currentTimeMillis()), Language.getLanguage("en_US")); } - protected void initContext() { - loginDetails = newLoginDetails(); + /** + * Init environment context for each test method + * @param testInfo + */ + protected void initContext(TestInfo testInfo) { + loginDetails = newLoginDetails(testInfo); Env.setContext(Env.getCtx(), Env.AD_CLIENT_ID, loginDetails.getClientId()); Env.setContext(Env.getCtx(), Env.AD_ORG_ID, loginDetails.getOrganizationId()); @@ -125,6 +142,9 @@ public abstract class AbstractTestCase { } @AfterEach + /** + * tear down for each test method + */ protected void tearDown() { if (trx != null && trx.isActive()) { trx.rollback(); @@ -132,10 +152,17 @@ public abstract class AbstractTestCase { } } + /** + * + * @return current transaction + */ protected Trx getTrx() { return trx; } + /** + * commit current transaction + */ protected void commit() { if (trx != null && trx.isActive()) { try { @@ -146,6 +173,9 @@ public abstract class AbstractTestCase { } } + /** + * rollback current transaction + */ protected void rollback() { if (trx != null && trx.isActive()) { trx.rollback(); @@ -180,11 +210,18 @@ public abstract class AbstractTestCase { return loginDetails.getLoginDate(); } + /** + * + * @return current transaction name + */ protected String getTrxName() { return trx.getTrxName(); } @AfterAll + /** + * shutdown for class + */ static void shutdown() { Adempiere.stop(); } diff --git a/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java b/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java new file mode 100644 index 0000000000..999cdfad48 --- /dev/null +++ b/org.idempiere.test/src/org/idempiere/test/performance/CacheTest.java @@ -0,0 +1,60 @@ +/*********************************************************************** + * This file is part of iDempiere ERP Open Source * + * http://www.idempiere.org * + * * + * Copyright (C) Contributors * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * + * MA 02110-1301, USA. * + * * + * Contributors: * + * - hengsin * + **********************************************************************/ +package org.idempiere.test.performance; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.compiere.model.MOrder; +import org.compiere.model.MZoomCondition; +import org.compiere.util.CacheMgt; +import org.idempiere.test.AbstractTestCase; +import org.junit.jupiter.api.Test; + +/** + * + * @author hengsin + * + */ +public class CacheTest extends AbstractTestCase { + + public CacheTest() { + } + + @Test + /** + * https://idempiere.atlassian.net/browse/IDEMPIERE-2698 + */ + public void testZoomConditionCache() { + MZoomCondition[] conditions1 = MZoomCondition.getConditions(MOrder.Table_ID); + assertTrue(conditions1 != null && conditions1.length > 0); + MZoomCondition[] conditions2 = MZoomCondition.getConditions(MOrder.Table_ID); + assertTrue(conditions2 != null && conditions2.length > 0); + assertTrue(conditions1 == conditions2); + CacheMgt.get().reset(); + MZoomCondition[] conditions3 = MZoomCondition.getConditions(MOrder.Table_ID); + assertTrue(conditions3 != null && conditions3.length == conditions1.length); + assertTrue(conditions1 != conditions3); + } +}