IDEMPIERE-5359 Unit test not working with Oracle (#1419)

* IDEMPIERE-5359 Unit test not working with Oracle

- Fix local trx not rollback for SQLException
- Add test for getSQLValueObjectsEx and getSQLArrayObjectsEx
This commit is contained in:
hengsin 2022-08-04 16:21:29 +08:00 committed by GitHub
parent 5931f6ce8f
commit 58813e8e83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 6 deletions

View File

@ -1291,6 +1291,10 @@ public final class DB
} }
catch (SQLException e) catch (SQLException e)
{ {
if (trx != null)
{
trx.rollback();
}
throw new DBException(e, sql); throw new DBException(e, sql);
} }
finally finally
@ -1386,6 +1390,10 @@ public final class DB
} }
catch (SQLException e) catch (SQLException e)
{ {
if (trx != null)
{
trx.rollback();
}
throw new DBException(e, sql); throw new DBException(e, sql);
} }
finally finally
@ -1481,7 +1489,10 @@ public final class DB
} }
catch (SQLException e) catch (SQLException e)
{ {
//log.log(Level.SEVERE, sql, getSQLException(e)); if (trx != null)
{
trx.rollback();
}
throw new DBException(e, sql); throw new DBException(e, sql);
} }
finally finally
@ -1578,6 +1589,10 @@ public final class DB
} }
catch (SQLException e) catch (SQLException e)
{ {
if (trx != null)
{
trx.rollback();
}
throw new DBException(e, sql); throw new DBException(e, sql);
} }
finally finally
@ -2478,8 +2493,18 @@ public final class DB
List<Object> retValue = new ArrayList<Object>(); List<Object> retValue = new ArrayList<Object>();
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
Trx trx = null;
if (trxName == null)
{
trxName = Trx.createTrxName("getSQLValueObjectsEx");
trx = Trx.get(trxName, true);
}
try try
{ {
if (trx != null)
{
trx.getConnection().setReadOnly(true);
}
pstmt = prepareStatement(sql, trxName); pstmt = prepareStatement(sql, trxName);
setParameters(pstmt, params); setParameters(pstmt, params);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
@ -2498,12 +2523,20 @@ public final class DB
} }
catch (SQLException e) catch (SQLException e)
{ {
if (trx != null)
{
trx.rollback();
}
throw new DBException(e, sql); throw new DBException(e, sql);
} }
finally finally
{ {
close(rs, pstmt); close(rs, pstmt);
rs = null; pstmt = null; rs = null; pstmt = null;
if (trx != null)
{
trx.close();
}
} }
return retValue; return retValue;
} }
@ -2521,8 +2554,18 @@ public final class DB
List<List<Object>> rowsArray = new ArrayList<List<Object>>(); List<List<Object>> rowsArray = new ArrayList<List<Object>>();
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
Trx trx = null;
if (trxName == null)
{
trxName = Trx.createTrxName("getSQLArrayObjectsEx");
trx = Trx.get(trxName, true);
}
try try
{ {
if (trx != null)
{
trx.getConnection().setReadOnly(true);
}
pstmt = prepareStatement(sql, trxName); pstmt = prepareStatement(sql, trxName);
setParameters(pstmt, params); setParameters(pstmt, params);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
@ -2541,12 +2584,20 @@ public final class DB
} }
catch (SQLException e) catch (SQLException e)
{ {
if (trx != null)
{
trx.rollback();
}
throw new DBException(e, sql); throw new DBException(e, sql);
} }
finally finally
{ {
close(rs, pstmt); close(rs, pstmt);
rs = null; pstmt = null; rs = null; pstmt = null;
if (trx != null)
{
trx.close();
}
} }
if (rowsArray.size() == 0) if (rowsArray.size() == 0)
return null; return null;

View File

@ -21,14 +21,18 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.adempiere.exceptions.DBException; import org.adempiere.exceptions.DBException;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.X_Test;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair; import org.compiere.util.KeyNamePair;
import org.compiere.util.TimeUtil; import org.compiere.util.TimeUtil;
import org.compiere.util.ValueNamePair; import org.compiere.util.ValueNamePair;
import org.idempiere.test.AbstractTestCase; import org.idempiere.test.AbstractTestCase;
import org.idempiere.test.DictionaryIDs;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -38,6 +42,8 @@ import org.junit.jupiter.api.Test;
*/ */
public class DBTest extends AbstractTestCase public class DBTest extends AbstractTestCase
{ {
private static final int TEST_RECORD_ID = 103;
@Test @Test
public void test_getSQLValueEx() throws Exception public void test_getSQLValueEx() throws Exception
{ {
@ -51,18 +57,18 @@ public class DBTest extends AbstractTestCase
DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE"); DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
}); });
int t_integer = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", 103); int t_integer = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", TEST_RECORD_ID);
assertThrows(DBException.class, () -> { assertThrows(DBException.class, () -> {
DB.getSQLValueEx(null, "update test set t_integer=1 where test_id=?", 103); DB.getSQLValueEx(null, "update test set t_integer=1 where test_id=?", TEST_RECORD_ID);
}); });
int t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", 103); int t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", TEST_RECORD_ID);
assertEquals(t_integer, t_integer1, "test.t_integer wrongly updated"); assertEquals(t_integer, t_integer1, "test.t_integer wrongly updated");
assertThrows(DBException.class, () -> { assertThrows(DBException.class, () -> {
DB.getSQLValueEx(getTrxName(), "update test set t_integer=1 where test_id=?;select t_integer from test where test_id=?", 103); DB.getSQLValueEx(getTrxName(), "update test set t_integer=1 where test_id=?;select t_integer from test where test_id=?", TEST_RECORD_ID);
}); });
rollback(); rollback();
t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", 103); t_integer1 = DB.getSQLValueEx(null, "select t_integer from test where test_id=?", TEST_RECORD_ID);
assertEquals(t_integer, t_integer1, "test.t_integer wrongly updated"); assertEquals(t_integer, t_integer1, "test.t_integer wrongly updated");
} }
@ -218,4 +224,54 @@ public class DBTest extends AbstractTestCase
assertEquals(arr[5].getName(), "7"); assertEquals(arr[5].getName(), "7");
} }
@Test
public void test_getSQLValueObjectsEx()
{
StringBuilder sql = new StringBuilder("SELECT ")
.append(X_Test.COLUMNNAME_Test_ID)
.append(", ")
.append(X_Test.COLUMNNAME_Test_UU)
.append(" FROM Test WHERE Test_ID=?");
List<Object> objects = DB.getSQLValueObjectsEx(null, sql.toString(), TEST_RECORD_ID);
assertEquals(2, objects.size());
X_Test test = new X_Test(Env.getCtx(), TEST_RECORD_ID, getTrxName());
assertEquals(test.get_ID(), ((Number)objects.get(0)).intValue());
assertEquals(test.getTest_UU(), objects.get(1));
objects = DB.getSQLValueObjectsEx(getTrxName(), sql.toString(), TEST_RECORD_ID);
assertEquals(2, objects.size());
assertEquals(test.get_ID(), ((Number)objects.get(0)).intValue());
assertEquals(test.getTest_UU(), objects.get(1));
}
@Test
public void test_getSQLArrayObjectsEx()
{
String sql = "SELECT M_Product_ID, Name FROM M_Product WHERE M_Product_ID IN (?, ?, ?)";
List<List<Object>> rows = DB.getSQLArrayObjectsEx(null, sql, DictionaryIDs.M_Product.AZALEA_BUSH.id, DictionaryIDs.M_Product.MULCH.id, DictionaryIDs.M_Product.FERTILIZER_50.id);
assertEquals(3, rows.size());
int match = 0;
for(List<Object> row : rows) {
assertEquals(2, row.size());
Number id = (Number)row.get(0);
if (id.intValue() == DictionaryIDs.M_Product.AZALEA_BUSH.id ||
id.intValue() == DictionaryIDs.M_Product.MULCH.id ||
id.intValue() == DictionaryIDs.M_Product.FERTILIZER_50.id)
match++;
}
assertEquals(3, match);
rows = DB.getSQLArrayObjectsEx(getTrxName(), sql, DictionaryIDs.M_Product.AZALEA_BUSH.id, DictionaryIDs.M_Product.MULCH.id, DictionaryIDs.M_Product.FERTILIZER_50.id);
assertEquals(3, rows.size());
match = 0;
for(List<Object> row : rows) {
assertEquals(2, row.size());
Number id = (Number)row.get(0);
if (id.intValue() == DictionaryIDs.M_Product.AZALEA_BUSH.id ||
id.intValue() == DictionaryIDs.M_Product.MULCH.id ||
id.intValue() == DictionaryIDs.M_Product.FERTILIZER_50.id)
match++;
}
assertEquals(3, match);
}
} }