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
This commit is contained in:
Albert Lee 2009-10-12 16:17:34 +00:00
parent 60b8e64ec9
commit 87e07a9d47
2 changed files with 51 additions and 0 deletions

View File

@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.lockmgr;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.LockModeType; import javax.persistence.LockModeType;
import javax.persistence.TransactionRequiredException;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl; import org.apache.openjpa.jdbc.kernel.JDBCFetchConfigurationImpl;
@ -261,4 +262,52 @@ public class TestEmLockMode extends SequencedActionsTest {
assertEquals(beforeIsolation, fConfig.getIsolation()); 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();
}
} }

View File

@ -1124,6 +1124,8 @@ public class EntityManagerImpl
public LockModeType getLockMode(Object entity) { public LockModeType getLockMode(Object entity) {
assertNotCloseInvoked(); assertNotCloseInvoked();
_broker.assertActiveTransaction();
assertValidAttchedEntity(entity);
return MixedLockLevelsHelper.fromLockLevel( return MixedLockLevelsHelper.fromLockLevel(
_broker.getLockLevel(entity)); _broker.getLockLevel(entity));
} }