* add getSQLValueEx method (same as getSQLValue but throws DBException)
* add getSQLValueBDEx method (same as getSQLValueBD but throws DBException)
* method setParameter(PreparedStatement pstmt, int index, Object param) should throw DBException if param is not supported
* added DBTest JUnit Test Case
This commit is contained in:
teo_sarca 2008-12-08 10:44:06 +00:00
parent 848e81fbf2
commit 533d0763b9
4 changed files with 193 additions and 39 deletions

View File

@ -825,6 +825,8 @@ public final class DB
pstmt.setTimestamp(index, (Timestamp)param);
else if (param instanceof Boolean)
pstmt.setString(index, ((Boolean)param).booleanValue() ? "Y" : "N");
else
throw new DBException("Unknown parameter type "+index+" - "+param);
}
/**
@ -1130,9 +1132,10 @@ public final class DB
* @param trxName trx
* @param sql sql
* @param params array of parameters
* @return first value or -1
* @return first value or -1 if not found
* @throws DBException if there is any SQLException
*/
public static int getSQLValue (String trxName, String sql, Object... params)
public static int getSQLValueEx (String trxName, String sql, Object... params) throws DBException
{
int retValue = -1;
PreparedStatement pstmt = null;
@ -1147,9 +1150,9 @@ public final class DB
else
log.info("No Value " + sql);
}
catch (Exception e)
catch (SQLException e)
{
log.log(Level.SEVERE, sql, getSQLException(e));
throw new DBException(e, sql);
}
finally
{
@ -1159,6 +1162,27 @@ public final class DB
return retValue;
}
/**
* Get int Value from sql
* @param trxName trx
* @param sql sql
* @param params array of parameters
* @return first value or -1 if not found or error
*/
public static int getSQLValue (String trxName, String sql, Object... params)
{
int retValue = -1;
try
{
retValue = getSQLValueEx(trxName, sql, params);
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, getSQLException(e));
}
return retValue;
}
/**
* Get int Value from sql
* @param trxName trx
@ -1226,9 +1250,10 @@ public final class DB
* @param trxName trx
* @param sql sql
* @param params array of parameters
* @return first value or null
* @return first value or null if not found
* @throws DBException if there is any SQLException
*/
public static BigDecimal getSQLValueBD (String trxName, String sql, Object... params)
public static BigDecimal getSQLValueBDEx (String trxName, String sql, Object... params) throws DBException
{
BigDecimal retValue = null;
PreparedStatement pstmt = null;
@ -1243,9 +1268,10 @@ public final class DB
else
log.info("No Value " + sql);
}
catch (Exception e)
catch (SQLException e)
{
log.log(Level.SEVERE, sql, getSQLException(e));
//log.log(Level.SEVERE, sql, getSQLException(e));
throw new DBException(e, sql);
}
finally
{
@ -1254,6 +1280,27 @@ public final class DB
}
return retValue;
}
/**
* Get BigDecimal Value from sql
* @param trxName trx
* @param sql sql
* @param params array of parameters
* @return first value or null
*/
public static BigDecimal getSQLValueBD (String trxName, String sql, Object... params)
{
try
{
return getSQLValueBDEx(trxName, sql, params);
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, getSQLException(e));
}
return null;
}
/**
* Get BigDecimal Value from sql

View File

@ -0,0 +1,82 @@
/**
*
*/
package test.functional;
import java.math.BigDecimal;
import org.adempiere.exceptions.DBException;
import org.compiere.util.DB;
import test.AdempiereTestCase;
/**
* Test {@link org.compiere.util.DB} class
* @author Teo Sarca, www.arhipac.ro
*/
public class DBTest extends AdempiereTestCase
{
public void test_getSQLValueEx() throws Exception
{
int result = DB.getSQLValueEx(null, "SELECT 10 FROM DUAL");
assertEquals(10, result);
//
result = DB.getSQLValue(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
assertEquals("No value should be returned", -1, result);
//
DBException ex = null;
try
{
DB.getSQLValueEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
}
catch (DBException e)
{
ex = e;
}
assertNotNull("No DBException Was Throwed", ex);
}
public void test_getSQLValue() throws Exception
{
int result = DB.getSQLValue(null, "SELECT 10 FROM DUAL");
assertEquals(10, result);
//
result = DB.getSQLValue(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
assertEquals("No value should be returned", -1, result);
//
result = DB.getSQLValue(null, "SELECT 10 FROM INEXISTENT_TABLE");
assertEquals("Error should be signaled", -1, result);
}
public void test_getSQLValueBDEx() throws Exception
{
BigDecimal result = DB.getSQLValueBDEx(null, "SELECT 10 FROM DUAL");
assertEquals(BigDecimal.TEN, result);
//
result = DB.getSQLValueBD(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
assertNull("No value should be returned", result);
//
DBException ex = null;
try
{
DB.getSQLValueBDEx(null, "SELECT 10 FROM INEXISTENT_TABLE");
}
catch (DBException e)
{
ex = e;
}
assertNotNull("No DBException Was Throwed", ex);
}
public void test_getSQLValueBD() throws Exception
{
BigDecimal result = DB.getSQLValueBD(null, "SELECT 10 FROM DUAL");
assertEquals(BigDecimal.TEN, result);
//
result = DB.getSQLValueBD(null, "SELECT 10 FROM AD_SYSTEM WHERE 1=2");
assertNull("No value should be returned", result);
//
result = DB.getSQLValueBD(null, "SELECT 10 FROM INEXISTENT_TABLE");
assertNull("Error should be signaled", result);
}
}

View File

@ -18,6 +18,7 @@ public class FunctionalTestSuite {
suite.addTestSuite(MStorageTest.class);
suite.addTestSuite(MSysConfigTest.class);
suite.addTestSuite(QueryTest.class);
suite.addTestSuite(DBTest.class);
suite.addTestSuite(TrxTest.class);
suite.addTestSuite(MRefListTest.class);
suite.addTestSuite(MUOMTest.class);

View File

@ -10,6 +10,8 @@ import org.adempiere.exceptions.DBException;
import org.compiere.model.MTable;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
import org.compiere.util.DB;
import org.compiere.util.Env;
import test.AdempiereTestCase;
@ -17,20 +19,25 @@ import test.AdempiereTestCase;
* Test {@link org.compiere.model.Query} class
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
*/
public class QueryTest extends AdempiereTestCase {
public void testQuery_NoTable() throws Exception {
public class QueryTest extends AdempiereTestCase
{
public void testQuery_NoTable() throws Exception
{
boolean exThrowed = false;
try {
try
{
new Query(getCtx(), "NO_TABLE_DEFINED", null, getTrxName());
}
catch (RuntimeException e) {
catch (RuntimeException e)
{
exThrowed = true;
//e.printStackTrace();
}
assertTrue("No Error Was Throwed", exThrowed);
}
public void testList() throws Exception {
public void testList() throws Exception
{
List<MTable> list = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")
@ -40,50 +47,61 @@ public class QueryTest extends AdempiereTestCase {
assertEquals("Invalid object 2", list.get(1).getTableName(), "M_InOut");
}
public void testScroll() throws Exception {
POResultSet<MTable> rs = null;
try {
rs = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")
.scroll();
public void testScroll() throws Exception
{
POResultSet<MTable> rs = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")
.scroll();
try
{
int i = 0;
for(MTable t = rs.next(); t != null; t = rs.next()) {
if (i == 0) {
while (rs.hasNext())
{
MTable t = rs.next();
if (i == 0)
{
assertEquals("Invalid object "+i, "C_Invoice", t.getTableName());
}
else if (i == 1) {
else if (i == 1)
{
assertEquals("Invalid object "+i, "M_InOut", t.getTableName());
}
else {
else
{
assertFalse("More objects retrived than expected", true);
}
i++;
}
}
finally {
if (rs != null)
rs.close();
finally
{
DB.close(rs);
rs = null;
}
}
public void testIterate() throws Exception {
public void testIterate() throws Exception
{
Iterator<MTable> it = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")
.iterate();
int i = 0;
while(it.hasNext()) {
while(it.hasNext())
{
MTable t = it.next();
if (i == 0) {
if (i == 0)
{
assertEquals("Invalid object "+i, "C_Invoice", t.getTableName());
}
else if (i == 1) {
else if (i == 1)
{
assertEquals("Invalid object "+i, "M_InOut", t.getTableName());
}
else {
else
{
assertFalse("More objects retrived than expected", true);
}
i++;
@ -91,7 +109,8 @@ public class QueryTest extends AdempiereTestCase {
}
public void testCount() throws Exception {
public void testCount() throws Exception
{
int count = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")
@ -99,26 +118,31 @@ public class QueryTest extends AdempiereTestCase {
assertEquals("Invalid count", 2, count);
}
public void testCount_BadSQL() throws Exception {
public void testCount_BadSQL() throws Exception
{
boolean exThrowed = false;
try {
try
{
new Query(getCtx(), "AD_Table", "TableName IN (?,?) AND BAD_SQL", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")
.count();
}
catch (DBException e) {
catch (DBException e)
{
exThrowed = true;
}
assertTrue("No Error Was Throwed", exThrowed);
}
public void testCount_NoValues() throws Exception {
public void testCount_NoValues() throws Exception
{
int count = new Query(getCtx(), "AD_Table", "1=2", getTrxName()).count();
assertEquals("Counter should be ZERO", 0, count);
}
public void testFirst() throws Exception {
public void testFirst() throws Exception
{
MTable t = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName())
.setParameters(new Object[]{"C_Invoice", "M_InOut"})
.setOrderBy("TableName")