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

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1344498 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Heath Thomann 2012-05-30 23:09:15 +00:00
parent 8200a351a7
commit 11cd0d5fd4
2 changed files with 32 additions and 1 deletions

View File

@ -393,7 +393,7 @@ public class PreparedQueryImpl implements PreparedQuery {
Collection values, Integer[] 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

@ -39,6 +39,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;
@ -316,6 +317,36 @@ public class TestPreparedQueryCache extends AbstractPersistenceTestCase {
}
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 void testRepeatedParameterInSubqueryInDifferentOrderSubQLast() {
OpenJPAEntityManager em = emf.createEntityManager();