openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From awh...@apache.org
Subject svn commit: r686325 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/ openjpa-persistence/src/main/java/org/apache/openjpa/p...
Date Fri, 15 Aug 2008 19:04:26 GMT
Author: awhite
Date: Fri Aug 15 12:04:25 2008
New Revision: 686325

URL: http://svn.apache.org/viewvc?rev=686325&view=rev
Log:
Eagerly compile JPQL queries to catch JPQL errors and throw proper exception as
required by spec.  OPENJPA-678


Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java?rev=686325&r1=686324&r2=686325&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/exception/TestException.java
Fri Aug 15 12:04:25 2008
@@ -160,22 +160,14 @@
 	}
 	
 	/**
-	 * Invalid query does not throw IllegalArgumentException on construction 
-	 * as per JPA spec. The exception is thrown during execution.
-	 * 
-	 * A patch
-	 * <A HREF="http://issues.apache.org/jira/browse/OPENJPA-678">OPENJPA-678</A>
-	 * by Xiaoqin Feng has proposed eager compilation of the query to raise the
-	 * error before execution and as per JPA spec. 
-	 * However, this patch has not yet been applied as eagerly compiling query
-	 * has other side-effects. 
-	 * 
+	 * Invalid query throws IllegalArgumentException on construction 
+	 * as per JPA spec.
 	 */
 	public void testIllegalArgumennExceptionOnInvalidQuery() {
 	    EntityManager em = emf.createEntityManager();
-	    Query query = em.createQuery("This is not a valid JPQL query");
 	    try {
-		   query.getResultList();
+	      em.createQuery("This is not a valid JPQL query");
+        fail("Did not throw IllegalArgumentException for invalid query.");
 	    } catch (Throwable t) {
 		   assertException(t, IllegalArgumentException.class);
 	    }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java?rev=686325&r1=686324&r2=686325&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
Fri Aug 15 12:04:25 2008
@@ -18,6 +18,11 @@
  */
 package org.apache.openjpa.persistence.test;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.lang.reflect.Modifier;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -302,6 +307,20 @@
         else if (o1.equals(o2))
             fail("expected args to be different; compared equal.");
     }
+
+    /**
+     * Round-trip a serializable object to bytes.
+     */
+    public static Object roundtrip(Object o) 
+        throws ClassNotFoundException, IOException {
+        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream(bytes);
+        out.writeObject(o);
+        out.flush();
+        ObjectInputStream in = new ObjectInputStream(
+            new ByteArrayInputStream(bytes.toByteArray()));
+        return in.readObject();
+    }
     
     // ================================================ 
     // Utility methods for exception handling

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java?rev=686325&r1=686324&r2=686325&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
Fri Aug 15 12:04:25 2008
@@ -87,7 +87,7 @@
     private Map<FetchConfiguration,FetchPlan> _plans =
         new IdentityHashMap<FetchConfiguration,FetchPlan>(1);
 
-    private RuntimeExceptionTranslator ret =
+    private RuntimeExceptionTranslator _ret =
         PersistenceExceptions.getRollbackTranslator(this);
 
     public EntityManagerImpl() {
@@ -104,8 +104,8 @@
 
     private void initialize(EntityManagerFactoryImpl factory, Broker broker) {
         _emf = factory;
-        _broker = new DelegatingBroker(broker, ret);
-        _broker.setImplicitBehavior(this, ret);
+        _broker = new DelegatingBroker(broker, _ret);
+        _broker.setImplicitBehavior(this, _ret);
     }
 
     /**
@@ -867,7 +867,16 @@
 
     public OpenJPAQuery createQuery(String language, String query) {
         assertNotCloseInvoked();
-        return new QueryImpl(this, ret, _broker.newQuery(language, query));
+        try {
+            org.apache.openjpa.kernel.Query q = _broker.newQuery(language, 
+                query);
+            // have to validate JPQL according to spec
+            if (JPQLParser.LANG_JPQL.equals(language))
+                q.compile(); 
+            return new QueryImpl(this, _ret, q);
+        } catch (RuntimeException re) {
+            throw PersistenceExceptions.toPersistenceException(re);
+        }
     }
 
     public OpenJPAQuery createQuery(Query query) {
@@ -875,7 +884,7 @@
             return createQuery((String) null);
         assertNotCloseInvoked();
         org.apache.openjpa.kernel.Query q = ((QueryImpl) query).getDelegate();
-        return new QueryImpl(this, ret, _broker.newQuery(q.getLanguage(),
+        return new QueryImpl(this, _ret, _broker.newQuery(q.getLanguage(),
             q));
     }
 
@@ -891,7 +900,7 @@
             meta.setInto(del);
             del.compile();
 
-            OpenJPAQuery q = new QueryImpl(this, ret, del);
+            OpenJPAQuery q = new QueryImpl(this, _ret, del);
             String[] hints = meta.getHintKeys();
             Object[] values = meta.getHintValues();
             for (int i = 0; i < hints.length; i++)
@@ -917,7 +926,7 @@
         org.apache.openjpa.kernel.Query kernelQuery = _broker.newQuery(
             QueryLanguages.LANG_SQL, query);
         kernelQuery.setResultMapping(null, mappingName);
-        return new QueryImpl(this, ret, kernelQuery);
+        return new QueryImpl(this, _ret, kernelQuery);
     }
 
     /**
@@ -1235,7 +1244,7 @@
     public void readExternal(ObjectInput in)
         throws IOException, ClassNotFoundException {
         try {
-            ret = PersistenceExceptions.getRollbackTranslator(this);
+            _ret = PersistenceExceptions.getRollbackTranslator(this);
 
             // this assumes that serialized Brokers are from something
             // that extends AbstractBrokerFactory.
@@ -1254,7 +1263,7 @@
             initialize(emf, broker);
         } catch (RuntimeException re) {
             try {
-                re = ret.translate(re);
+                re = _ret.translate(re);
             } catch (Exception e) {
                 // ignore
             }
@@ -1276,7 +1285,7 @@
             out.writeObject(baos.toByteArray());
         } catch (RuntimeException re) {
             try {
-                re = ret.translate(re);
+                re = _ret.translate(re);
             } catch (Exception e) {
                 // ignore
             }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=686325&r1=686324&r2=686325&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Fri Aug 15 12:04:25 2008
@@ -79,12 +79,9 @@
 	/**
 	 * Constructor; supply factory exception translator and delegate.
 	 * 
-	 * @param em
-	 *            The EntityManager which created this query
-	 * @param ret
-	 *            Exception translater for this query
-	 * @param query
-	 *            The underlying "kernel" query.
+	 * @param em  The EntityManager which created this query
+	 * @param ret Exception translater for this query
+	 * @param query The underlying "kernel" query.
 	 */
 	public QueryImpl(EntityManagerImpl em, RuntimeExceptionTranslator ret,
 			org.apache.openjpa.kernel.Query query) {



Mime
View raw message