OPENJPA-2118: Provide fix to avoid possible 'division by zero' error.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@1341941 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2012-05-23 16:29:53 +00:00
parent ff83975992
commit 9ae3d06e07
2 changed files with 33 additions and 1 deletions

View File

@ -389,7 +389,7 @@ public class PreparedQueryImpl implements PreparedQuery {
Collection values, int[] indices, Object param, Broker broker) {
int n = values.size();
Object[] array = values.toArray();
if (n > indices.length || indices.length%n != 0) {
if (n == 0 || n > indices.length || indices.length%n != 0) {
throw new UserException(_loc.get("uparam-coll-size", param, values,
Arrays.toString(indices)));
}

View File

@ -41,6 +41,7 @@ import org.apache.openjpa.kernel.jpql.JPQLParser;
import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
import org.apache.openjpa.lib.jdbc.JDBCEvent;
import org.apache.openjpa.lib.jdbc.JDBCListener;
import org.apache.openjpa.persistence.ArgumentException;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
@ -1141,6 +1142,37 @@ public class TestPreparedQueryCache extends TestCase {
assertEquals(2010, l.get(0).getStartYear());
}
public void testCollectionValuedParameterOfEntitiesWithEmptyList() {
OpenJPAEntityManager em = emf.createEntityManager();
String jpql1 =
"select d from Department d where d.name in ('Marketing', 'Sales') order by d.name";
List<Department> param1 =
(List<Department>) em.createQuery(jpql1).getResultList();
em.clear();
String jpql = "select e from Employee e where e.department in :param";
List<Employee> rs1 =
em.createQuery(jpql).setParameter("param", param1).getResultList();
for (int i = 0; i < rs1.size(); i++) {
Employee e = (Employee) rs1.get(i);
assertFalse(e.getDepartment().getName().equals("Engineering"));
}
// Prior to OPENJPA-2118, the following query would yeild a
// 'ArithmeticException: divide
// by zero' exception (see JIRA for details).
try {
// Pass an empty list to 'param'.
em.createQuery(jpql).setParameter("param",
new ArrayList<Department>()).getResultList();
} catch (ArgumentException ae) {
assertEquals(ae.getCause().getMessage(),
"Input parameter \"param\" is empty.");
}
}
public List<Company> getAllCompaniesPaged(int start, int max) {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select p from Company p order by p.startYear");