Fix test cases running in DB2 due to 1) dict.forUpdateClause must be obtained after emf is created because specific dictionary may override the value of this attribute, 2) The forUpdateClause in DB2 is LockMode type dependent, (i.e. WITH RR/RS) therefore can not depends on this attribute for positive test case comparison, negative test ok.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1073542 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Albert Lee 2011-02-22 23:15:01 +00:00
parent 78198b67e7
commit e3be88bf7e
3 changed files with 17 additions and 8 deletions

View File

@ -34,7 +34,7 @@ import javax.persistence.Version;
@NamedQuery(name = "findEmployeeById", @NamedQuery(name = "findEmployeeById",
query = "SELECT c FROM LockEmployee c WHERE c.id = :id"), query = "SELECT c FROM LockEmployee c WHERE c.id = :id"),
@NamedQuery(name = "findEmployeeByIdWithLock", @NamedQuery(name = "findEmployeeByIdWithLock",
query = "SELECT c FROM LockEmployee c WHERE c.id = :id", lockMode = LockModeType.PESSIMISTIC_READ), query = "SELECT c FROM LockEmployee c WHERE c.id = :id", lockMode = LockModeType.PESSIMISTIC_WRITE),
@NamedQuery(name = "findEmployeeByIdWithNoLock", @NamedQuery(name = "findEmployeeByIdWithNoLock",
query = "SELECT c FROM LockEmployee c WHERE c.id = :id", lockMode = LockModeType.NONE) }) query = "SELECT c FROM LockEmployee c WHERE c.id = :id", lockMode = LockModeType.NONE) })
@Entity @Entity

View File

@ -22,6 +22,7 @@ import javax.persistence.EntityManager;
import javax.persistence.LockModeType; import javax.persistence.LockModeType;
import javax.persistence.Query; import javax.persistence.Query;
import org.apache.openjpa.jdbc.sql.DB2Dictionary;
import org.apache.openjpa.persistence.FetchPlan; import org.apache.openjpa.persistence.FetchPlan;
import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
@ -44,12 +45,12 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
"openjpa.LockManager", "pessimistic", "openjpa.LockManager", "pessimistic",
"openjpa.Optimistic", "false" "openjpa.Optimistic", "false"
); );
lockClause = getForUpdateClause();
} }
public void testForUpdateClausePresentInNamedQueryWithLockMode() { public void testForUpdateClausePresentInNamedQueryWithLockMode() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
lockClause = getForUpdateClause();
assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0));
em.getTransaction().rollback(); em.getTransaction().rollback();
em.getTransaction().begin(); em.getTransaction().begin();
@ -75,8 +76,13 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClausePresentInQueryWithDefault() { public void testForUpdateClausePresentInQueryWithDefault() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); lockClause = getForUpdateClause();
assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); if (!DB2Dictionary.class.isAssignableFrom(getDBDictionary().getClass())) {
// Skip test because "for update" clause in DB2 depends on LockMode type (i.e. WITH RR/RS)
// plus dict.forUpdateClause is not sensitive to this implementation.
assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
}
OpenJPAEntityManager oem = (OpenJPAEntityManager)em; OpenJPAEntityManager oem = (OpenJPAEntityManager)em;
OpenJPAQuery<?> q = oem.createNamedQuery("findEmployeeById").setParameter("id", 0); OpenJPAQuery<?> q = oem.createNamedQuery("findEmployeeById").setParameter("id", 0);
@ -88,8 +94,8 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClauseAbsentInQueryWithFetchPlanNoneLockMode() { public void testForUpdateClauseAbsentInQueryWithFetchPlanNoneLockMode() {
OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAEntityManagerSPI em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
lockClause = getForUpdateClause();
OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0); OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0);
FetchPlan fp = q.getFetchPlan(); FetchPlan fp = q.getFetchPlan();
fp.setReadLockMode(LockModeType.NONE); fp.setReadLockMode(LockModeType.NONE);
@ -107,6 +113,7 @@ public class TestNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClauseAbsentInQueryWithExplictNoLock() { public void testForUpdateClauseAbsentInQueryWithExplictNoLock() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
lockClause = getForUpdateClause();
assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
em.getTransaction().commit(); em.getTransaction().commit();

View File

@ -39,7 +39,6 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
super.setUp(CLEAR_TABLES, LockEmployee.class, super.setUp(CLEAR_TABLES, LockEmployee.class,
"openjpa.LockManager", "pessimistic" "openjpa.LockManager", "pessimistic"
); );
lockClause = getForUpdateClause();
} }
/* /*
@ -47,6 +46,7 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
*/ */
public void testForUpdateClausePresentInNamedQueryWithLockMode() { public void testForUpdateClausePresentInNamedQueryWithLockMode() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
lockClause = getForUpdateClause();
em.getTransaction().begin(); em.getTransaction().begin();
assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0)); assertClausePresentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithLock").setParameter("id", 0));
em.getTransaction().rollback(); em.getTransaction().rollback();
@ -65,6 +65,7 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClausePresentInQueryWithDefault() { public void testForUpdateClausePresentInQueryWithDefault() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
lockClause = getForUpdateClause();
assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeById").setParameter("id", 0));
@ -83,7 +84,7 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClauseAbsentInQueryWithFetchPlanNoneLockMode() { public void testForUpdateClauseAbsentInQueryWithFetchPlanNoneLockMode() {
OpenJPAEntityManagerSPI em = emf.createEntityManager(); OpenJPAEntityManagerSPI em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
lockClause = getForUpdateClause();
OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0); OpenJPAQuery<?> q = em.createNamedQuery("findEmployeeById").setParameter("id", 0);
FetchPlan fp = q.getFetchPlan(); FetchPlan fp = q.getFetchPlan();
fp.setReadLockMode(LockModeType.NONE); fp.setReadLockMode(LockModeType.NONE);
@ -104,6 +105,7 @@ public class TestOptimisticNamedQueryLockMode extends SQLListenerTestCase {
public void testForUpdateClauseAbsentInQueryWithExplictNoLock() { public void testForUpdateClauseAbsentInQueryWithExplictNoLock() {
EntityManager em = emf.createEntityManager(); EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
lockClause = getForUpdateClause();
assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0)); assertClauseAbsentInSQL(lockClause, em.createNamedQuery("findEmployeeByIdWithNoLock").setParameter("id", 0));
em.getTransaction().commit(); em.getTransaction().commit();