openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1342328 - in /openjpa/branches/2.2.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Date Thu, 24 May 2012 16:37:24 GMT
Author: hthomann
Date: Thu May 24 16:37:24 2012
New Revision: 1342328

URL: http://svn.apache.org/viewvc?rev=1342328&view=rev
Log:
OPENJPA-2118: Provide fix to avoid possible 'division by zero' error.

Modified:
    openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
    openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java

Modified: openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java?rev=1342328&r1=1342327&r2=1342328&view=diff
==============================================================================
--- openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
(original)
+++ openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java
Thu May 24 16:37:24 2012
@@ -393,7 +393,7 @@ public class PreparedQueryImpl implement
         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)));
         }

Modified: openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java?rev=1342328&r1=1342327&r2=1342328&view=diff
==============================================================================
--- openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
(original)
+++ openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Thu May 24 16:37:24 2012
@@ -41,6 +41,7 @@ import org.apache.openjpa.kernel.jpql.JP
 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;
@@ -1157,6 +1158,37 @@ public class TestPreparedQueryCache exte
         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");



Mime
View raw message