You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2010/04/08 22:47:46 UTC
svn commit: r932105 - in
/openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr:
LockEmployee.java TestNamedQueryLockMode.java
Author: curtisr7
Date: Thu Apr 8 20:47:45 2010
New Revision: 932105
URL: http://svn.apache.org/viewvc?rev=932105&view=rev
Log:
OPENJPA-1604: Committing test changes. Code contributed by Pinaki Poddar and Rick Curtis
Added:
openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java (with props)
Modified:
openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/LockEmployee.java
Modified: openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/LockEmployee.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/LockEmployee.java?rev=932105&r1=932104&r2=932105&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/LockEmployee.java (original)
+++ openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/LockEmployee.java Thu Apr 8 20:47:45 2010
@@ -25,14 +25,18 @@ import java.io.ObjectOutput;
import javax.persistence.Entity;
import javax.persistence.Id;
+import javax.persistence.LockModeType;
+import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Version;
-@NamedQuery(
- name="findEmployeeById"
- , query="SELECT c FROM LockEmployee c WHERE c.id = :id"
- )
-
+@NamedQueries( {
+ @NamedQuery(name = "findEmployeeById",
+ query = "SELECT c FROM LockEmployee c WHERE c.id = :id"),
+ @NamedQuery(name = "findEmployeeByIdWithLock",
+ query = "SELECT c FROM LockEmployee c WHERE c.id = :id", lockMode = LockModeType.PESSIMISTIC_READ),
+ @NamedQuery(name = "findEmployeeByIdWithNoLock",
+ query = "SELECT c FROM LockEmployee c WHERE c.id = :id", lockMode = LockModeType.NONE) })
@Entity
public class LockEmployee implements Externalizable {
Added: openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java?rev=932105&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java (added)
+++ openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java Thu Apr 8 20:47:45 2010
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.lockmgr;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.TransactionRequiredException;
+import org.apache.openjpa.persistence.test.AllowFailure;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+/**
+ * Tests the lock mode on named query emits a FOR UPDATE clause in target SQL
+ * query.
+ *
+ *
+ */
+public class TestNamedQueryLockMode extends SQLListenerTestCase {
+ public void setUp() {
+ super.setUp(CLEAR_TABLES, LockEmployee.class,
+ "openjpa.LockManager", "pessimistic",
+ "openjpa.Optimistic", "false"
+ );
+ }
+
+ public void testForUpdateClausePresentInNamedQueryWithLockMode() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0));
+ em.getTransaction().rollback();
+ em.getTransaction().begin();
+ assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0));
+ em.getTransaction().rollback();
+ em.getTransaction().begin();
+ assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0));
+ em.getTransaction().rollback();
+ }
+
+ @AllowFailure
+ public void testNamedQueryWithLockModeMustExecuteInTransaction() {
+ EntityManager em = emf.createEntityManager();
+ // execute without a transaction
+ try {
+ em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id",
+ 0).getResultList();
+ fail("Expected " + TransactionRequiredException.class.getName());
+ } catch (TransactionRequiredException e) {
+ // Expected
+ }
+ }
+
+ public void testForUpdateClausePresentInQueryWithDefault() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0));
+ assertClausePresentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeById").setParameter("id", 0));
+ em.getTransaction().commit();
+ }
+
+ @AllowFailure
+ public void testForUpdateClauseAbsentInQueryWithExplictNoLock() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
+ assertClauseAbsentInSQL("FOR UPDATE", em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
+ em.getTransaction().commit();
+ }
+
+ String getLastSQL() {
+ String last = sql.get(getSQLCount() - 1);
+ assertNotNull("No last sql found", last);
+ return last;
+ }
+
+ void assertClausePresentInSQL(String clause, Query q) {
+ q.getResultList();
+ String last = getLastSQL();
+ assertTrue(clause + " is not present in " + last, last.toUpperCase().indexOf(clause) != -1);
+ }
+
+ void assertClauseAbsentInSQL(String clause, Query q) {
+ q.getResultList();
+ String last = getLastSQL();
+ assertTrue(clause + " is not absent in " + last, last.toUpperCase().indexOf(clause) == -1);
+ }
+}
Propchange: openjpa/trunk/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestNamedQueryLockMode.java
------------------------------------------------------------------------------
svn:eol-style = native