From d734cafd9bf327e56556126d44078f8b1b0c16c9 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Tue, 16 May 2023 10:32:15 +0200 Subject: [PATCH] IDEMPIERE-5712 Tests are leaving records in the database (#1827) * IDEMPIERE-5712 Tests are leaving records in the database * - add transaction * - add cleanup for records committed out of transaction --- .../src/org/compiere/model/MTest.java | 14 +++++++++- .../src/org/idempiere/test/base/POTest.java | 28 +++++++++++++------ .../org/idempiere/test/base/QueryTest.java | 4 +-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MTest.java b/org.adempiere.base/src/org/compiere/model/MTest.java index f722529af5..64411e9443 100644 --- a/org.adempiere.base/src/org/compiere/model/MTest.java +++ b/org.adempiere.base/src/org/compiere/model/MTest.java @@ -80,7 +80,19 @@ public class MTest extends X_Test */ public MTest (Properties ctx, String testString, int testNo) { - super(ctx, 0, null); + this(ctx, testString, testNo, null); + } + + /** + * Test Object Constructor + * @param ctx context + * @param testString test string + * @param testNo test no + * @param trxName + */ + public MTest (Properties ctx, String testString, int testNo, String trxName) + { + super(ctx, 0, trxName); testString = testString + "_" + testNo; setName(testString); setDescription(testString + " " + testString + " " + testString); diff --git a/org.idempiere.test/src/org/idempiere/test/base/POTest.java b/org.idempiere.test/src/org/idempiere/test/base/POTest.java index 75483b3905..2595b43fe9 100644 --- a/org.idempiere.test/src/org/idempiere/test/base/POTest.java +++ b/org.idempiere.test/src/org/idempiere/test/base/POTest.java @@ -90,8 +90,7 @@ public class POTest extends AbstractTestCase public MyTestPO(Properties ctx, boolean failOnSave, String trxName) { - super(ctx, "Test_"+System.currentTimeMillis(), 10); - this.set_TrxName(trxName); + super(ctx, "Test_"+System.currentTimeMillis(), 10, trxName); this.setDescription(""+getClass()); this.failOnSave = failOnSave; } @@ -140,8 +139,7 @@ public class POTest extends AbstractTestCase "test", }; // Create the test PO and save - MTest testPO = new MTest(Env.getCtx(), getClass().getName(), 1); - testPO.set_TrxName(getTrxName()); + MTest testPO = new MTest(Env.getCtx(), getClass().getName(), 1, getTrxName()); for (String str : testStrings) { @@ -186,8 +184,7 @@ public class POTest extends AbstractTestCase String bigString = sb.toString(); // // Create the test PO: - MTest testPO = new MTest(Env.getCtx(), getClass().getName(), 1); - testPO.set_TrxName(getTrxName()); + MTest testPO = new MTest(Env.getCtx(), getClass().getName(), 1, getTrxName()); // // Getting Max Length: POInfo info = POInfo.getPOInfo(Env.getCtx(), MTest.Table_ID); @@ -240,8 +237,10 @@ public class POTest extends AbstractTestCase } // // Test for old objects + MyTestPO test = null; + try { - MyTestPO test = new MyTestPO(Env.getCtx(), false, null); + test = new MyTestPO(Env.getCtx(), false, null); assertTrue(test.save(), "Object *should* be saved -- "+test); // MyTestPO test2 = new MyTestPO(Env.getCtx(), test.get_ID(), null); @@ -253,6 +252,19 @@ public class POTest extends AbstractTestCase String name = MyTestPO.getName(test2.get_ID(), null); assertEquals(test.getName(), name, "Object should not be modified(2) -- id="+test2); } + finally + { + // cleanup + if (test != null) + { + if (test.getDependent_ID() > 0) + { + MyTestPO testDependent = new MyTestPO(Env.getCtx(), test.getDependent_ID(), null); + testDependent.deleteEx(true); + } + test.deleteEx(true); + } + } } /** @@ -475,7 +487,7 @@ public class POTest extends AbstractTestCase @Test public void testVirtualColumnLoad() { - MTest testPo = new MTest(Env.getCtx(), getClass().getName(), 1); + MTest testPo = new MTest(Env.getCtx(), getClass().getName(), 1, getTrxName()); testPo.save(); // asynchronous (default) virtual column loading diff --git a/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java b/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java index f62e6f6359..0070a95caa 100644 --- a/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java +++ b/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java @@ -255,7 +255,7 @@ public class QueryTest extends AbstractTestCase { public void testPaging() { DB.executeUpdateEx("DELETE FROM Test WHERE Name LIKE 'QueryTest%'", getTrxName()); for (int i=101; i<=130; i++) { - PO testPo = new MTest(Env.getCtx(), "QueryTest", i); + PO testPo = new MTest(Env.getCtx(), "QueryTest", i, getTrxName()); testPo.save(); } Query query = new Query(Env.getCtx(), MTest.Table_Name, "Name LIKE 'QueryTest%'", getTrxName()) @@ -397,7 +397,7 @@ public class QueryTest extends AbstractTestCase { @Test public void testVirtualColumnLoad() { // create bogus record - PO testPo = new MTest(Env.getCtx(), getClass().getName(), 1); + PO testPo = new MTest(Env.getCtx(), getClass().getName(), 1, getTrxName()); testPo.save(); BigDecimal expected = new BigDecimal("123.45");