From 87e07a9d47a05cb6ee72c80d932a20023a296a15 Mon Sep 17 00:00:00 2001 From: Albert Lee Date: Mon, 12 Oct 2009 16:17:34 +0000 Subject: [PATCH] OPENJPA-1344 Add checks for needed exception conditions. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@824406 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/lockmgr/TestEmLockMode.java | 49 +++++++++++++++++++ .../persistence/EntityManagerImpl.java | 2 + 2 files changed, 51 insertions(+) diff --git a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java index e9913c523..521d9e407 100644 --- a/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java +++ b/openjpa-persistence-locking/src/test/java/org/apache/openjpa/persistence/lockmgr/TestEmLockMode.java @@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.lockmgr; import javax.persistence.EntityManager; import javax.persistence.LockModeType; +import javax.persistence.TransactionRequiredException; import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl; @@ -261,4 +262,52 @@ public class TestEmLockMode extends SequencedActionsTest { assertEquals(beforeIsolation, fConfig.getIsolation()); } + + /* + * Test em.getLockMode(); + */ + public void testGetLockMode() { + EntityManager em = emf.createEntityManager(); + + LockEmployee employee = em.find(LockEmployee.class, 1); + try { + em.getLockMode(employee); + fail("Expecting TransactionRequiredException."); + } catch (TransactionRequiredException tre) { + } catch (Exception e){ + fail("Expecting TransactionRequiredException."); + } + + em.getTransaction().begin(); + try { + assertEquals("getLockMode only allows in transaction.",LockModeType.NONE, em.getLockMode(employee)); + } catch (Exception e){ + fail("Do not expecting any exception."); + } + em.getTransaction().rollback(); + + em.clear(); + em.getTransaction().begin(); + try { + // getLockMode on a detached entity; + em.getLockMode(employee); + fail("Expecting IllegalArgumentException for getLockMode on a detached entity in an active transaction."); + } catch (IllegalArgumentException iae) { + } catch (Exception e){ + fail("Expecting IllegalArgumentException for getLockMode on a detached entity in an active transaction."); + } + em.getTransaction().rollback(); + + em.getTransaction().begin(); + try { + employee = em.find(LockEmployee.class, 1, LockModeType.PESSIMISTIC_WRITE); + assertEquals("Test getLockMode on non-NONE lock mode type.", LockModeType.PESSIMISTIC_WRITE, em + .getLockMode(employee)); + } catch (Exception e){ + fail("Do not expecting any exception."); + } + em.getTransaction().rollback(); + + em.close(); + } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java index 3673d2b35..bb58a7aaf 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java @@ -1124,6 +1124,8 @@ public class EntityManagerImpl public LockModeType getLockMode(Object entity) { assertNotCloseInvoked(); + _broker.assertActiveTransaction(); + assertValidAttchedEntity(entity); return MixedLockLevelsHelper.fromLockLevel( _broker.getLockLevel(entity)); }