mirror of https://github.com/apache/openjpa.git
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:
parent
8200a351a7
commit
11cd0d5fd4
|
@ -393,7 +393,7 @@ public class PreparedQueryImpl implements PreparedQuery {
|
||||||
Collection values, Integer[] indices, Object param, Broker broker) {
|
Collection values, Integer[] indices, Object param, Broker broker) {
|
||||||
int n = values.size();
|
int n = values.size();
|
||||||
Object[] array = values.toArray();
|
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,
|
throw new UserException(_loc.get("uparam-coll-size", param, values,
|
||||||
Arrays.toString(indices)));
|
Arrays.toString(indices)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.apache.openjpa.kernel.jpql.JPQLParser;
|
||||||
import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
|
import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
|
||||||
import org.apache.openjpa.lib.jdbc.JDBCEvent;
|
import org.apache.openjpa.lib.jdbc.JDBCEvent;
|
||||||
import org.apache.openjpa.lib.jdbc.JDBCListener;
|
import org.apache.openjpa.lib.jdbc.JDBCListener;
|
||||||
|
import org.apache.openjpa.persistence.ArgumentException;
|
||||||
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
||||||
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
|
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() {
|
public void testRepeatedParameterInSubqueryInDifferentOrderSubQLast() {
|
||||||
OpenJPAEntityManager em = emf.createEntityManager();
|
OpenJPAEntityManager em = emf.createEntityManager();
|
||||||
|
|
Loading…
Reference in New Issue