MRefList:

* BF [ 1874419 ] JDBC Statement not close in a finally block
* organized imports
* added test case
This commit is contained in:
teo_sarca 2008-07-22 10:00:21 +00:00
parent f3cabb1af5
commit 5364036b18
3 changed files with 68 additions and 57 deletions

View File

@ -16,11 +16,18 @@
*****************************************************************************/
package org.compiere.model;
import java.sql.*;
import java.util.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.*;
import org.compiere.util.*;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.ValueNamePair;
/**
* Reference List Value
@ -42,37 +49,9 @@ public class MRefList extends X_AD_Ref_List
*/
public static MRefList get (Properties ctx, int AD_Reference_ID, String Value, String trxName)
{
MRefList retValue = null;
String sql = "SELECT * FROM AD_Ref_List "
+ "WHERE AD_Reference_ID=? AND Value=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, trxName);
pstmt.setInt (1, AD_Reference_ID);
pstmt.setString (2, Value);
ResultSet rs = pstmt.executeQuery ();
if (rs.next ())
retValue = new MRefList (ctx, rs, trxName);
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (SQLException ex)
{
s_log.log(Level.SEVERE, sql, ex);
}
try
{
if (pstmt != null)
pstmt.close ();
}
catch (SQLException ex1)
{
}
pstmt = null;
return retValue;
return new Query(ctx, Table_Name, "AD_Reference_ID=? AND Value=?", trxName)
.setParameters(new Object[]{AD_Reference_ID, Value})
.first();
} // get
/**
@ -98,6 +77,7 @@ public class MRefList extends X_AD_Ref_List
+ " INNER JOIN AD_Ref_List r ON (r.AD_Ref_List_ID=t.AD_Ref_List_ID) "
+ "WHERE r.AD_Reference_ID=? AND r.Value=? AND t.AD_Language=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, null);
@ -105,26 +85,18 @@ public class MRefList extends X_AD_Ref_List
pstmt.setString(2, Value);
if (!isBaseLanguage)
pstmt.setString(3, AD_Language);
ResultSet rs = pstmt.executeQuery ();
rs = pstmt.executeQuery ();
if (rs.next ())
retValue = rs.getString(1);
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (SQLException ex)
{
s_log.log(Level.SEVERE, sql + " - " + key, ex);
}
try
{
if (pstmt != null)
pstmt.close ();
finally {
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
catch (SQLException ex1)
{
}
pstmt = null;
// Save into Cache
if (retValue == null)
@ -158,6 +130,7 @@ public class MRefList extends X_AD_Ref_List
+ " ORDER BY t.Name"
;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<ValueNamePair> list = new ArrayList<ValueNamePair>();
if (optional)
list.add(new ValueNamePair("", ""));
@ -167,26 +140,21 @@ public class MRefList extends X_AD_Ref_List
pstmt.setInt(1, AD_Reference_ID);
if (!isBaseLanguage)
pstmt.setString(2, ad_language);
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
while (rs.next())
list.add(new ValueNamePair(rs.getString(1), rs.getString(2)));
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
catch (SQLException e)
{
s_log.log(Level.SEVERE, sql, e);
}
try
finally
{
if (pstmt != null)
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
ValueNamePair[] retValue = new ValueNamePair[list.size()];
list.toArray(retValue);

View File

@ -18,6 +18,7 @@ public class FunctionalTestSuite {
suite.addTestSuite(MStorageTest.class);
suite.addTestSuite(MSysConfigTest.class);
suite.addTestSuite(QueryTest.class);
suite.addTestSuite(MRefListTest.class);
//$JUnit-END$
return suite;
}

View File

@ -0,0 +1,42 @@
/******************************************************************************
* Product: XERP Core *
* Copyright (C) 2008 SC ARHIPAC SERVICE SRL. All Rights Reserved. *
*****************************************************************************/
package test.functional;
import org.compiere.model.MRefList;
import org.compiere.model.X_AD_Table;
import org.compiere.util.Env;
import test.AdempiereTestCase;
/**
* Test MRefList class
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
*
*/
public class MRefListTest extends AdempiereTestCase {
public void testGet() throws Exception {
//
// Should be found
MRefList rl = MRefList.get(getCtx(),
X_AD_Table.ACCESSLEVEL_AD_Reference_ID,
X_AD_Table.ACCESSLEVEL_Organization,
getTrxName());
assertNotNull(rl);
assertEquals("Should be found", X_AD_Table.ACCESSLEVEL_Organization, rl.getValue());
//
// Should not be found
rl = MRefList.get(getCtx(), 7654321, "7654321", getTrxName());
assertNull("Should not be found", rl);
}
public void testGetListName() throws Exception {
Env.setContext(getCtx(), Env.LANGUAGE, "en_US");
String name = MRefList.getListName(getCtx(),
X_AD_Table.ACCESSLEVEL_AD_Reference_ID,
X_AD_Table.ACCESSLEVEL_All);
assertEquals("All", name);
}
}