OPENJPA-1562 Committing code and test updates contributed by Dianne Richards. Refresh after remove was not throwing exception dictated by spec.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@923849 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2010-03-16 16:32:14 +00:00
parent 845f9b5018
commit 6d87bbdbb0
3 changed files with 35 additions and 14 deletions

View File

@ -116,6 +116,23 @@ public class TestRefresh extends SingleEMTestCase {
assertEquals(mode, CacheStoreMode.USE); assertEquals(mode, CacheStoreMode.USE);
} }
public void testRefreshAfterRemove() {
try {
em.getTransaction().begin();
Item item = new Item();
item.setItemData("Test Data");
em.persist(item);
em.flush();
em.remove(item);
em.flush();
em.refresh(item);
em.getTransaction().commit();
fail("Did not catch expected IllegalArgumentException for refresh() of removed entity");
} catch (IllegalArgumentException e) {
// Expected exception
}
}
void assertCached(Class<?> cls, Object oid) { void assertCached(Class<?> cls, Object oid) {
assertTrue(cls + ":" + oid + " should be in L2 cache, but not", emf.getCache().contains(cls, oid)); assertTrue(cls + ":" + oid + " should be in L2 cache, but not", emf.getCache().contains(cls, oid));

View File

@ -255,28 +255,28 @@ public class TestMixedLockManagerRefreshException extends SequencedActionsTest {
{Act.Remove}, {Act.Remove},
{Act.Refresh, 1, LockModeType.NONE }, {Act.Refresh, 1, LockModeType.NONE },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.READ }, {Act.Refresh, 1, LockModeType.READ },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.WRITE }, {Act.Refresh, 1, LockModeType.WRITE },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.OPTIMISTIC }, {Act.Refresh, 1, LockModeType.OPTIMISTIC },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.OPTIMISTIC_FORCE_INCREMENT }, {Act.Refresh, 1, LockModeType.OPTIMISTIC_FORCE_INCREMENT },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.PESSIMISTIC_READ }, {Act.Refresh, 1, LockModeType.PESSIMISTIC_READ },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.PESSIMISTIC_WRITE }, {Act.Refresh, 1, LockModeType.PESSIMISTIC_WRITE },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.Refresh, 1, LockModeType.PESSIMISTIC_FORCE_INCREMENT }, {Act.Refresh, 1, LockModeType.PESSIMISTIC_FORCE_INCREMENT },
{Act.TestException}, {Act.TestException, 1, IllegalArgumentException.class },
{Act.RollbackTx}, {Act.RollbackTx},
}; };

View File

@ -101,6 +101,10 @@ public class EntityManagerImpl
private static final Localizer _loc = Localizer.forPackage(EntityManagerImpl.class); private static final Localizer _loc = Localizer.forPackage(EntityManagerImpl.class);
private static final Object[] EMPTY_OBJECTS = new Object[0]; private static final Object[] EMPTY_OBJECTS = new Object[0];
private static final String GET_LOCK_MODE = "getLockMode";
private static final String LOCK = "lock";
private static final String REFRESH = "refresh";
private DelegatingBroker _broker; private DelegatingBroker _broker;
private EntityManagerFactoryImpl _emf; private EntityManagerFactoryImpl _emf;
@ -742,7 +746,7 @@ public class EntityManagerImpl
public void refresh(Object entity, LockModeType mode, Map<String, Object> properties) { public void refresh(Object entity, LockModeType mode, Map<String, Object> properties) {
assertNotCloseInvoked(); assertNotCloseInvoked();
assertValidAttchedEntity("refresh", entity); assertValidAttchedEntity(REFRESH, entity);
_broker.assertWriteOperation(); _broker.assertWriteOperation();
configureCurrentFetchPlan(pushFetchPlan(), properties, mode, true); configureCurrentFetchPlan(pushFetchPlan(), properties, mode, true);
@ -1144,7 +1148,7 @@ public class EntityManagerImpl
public LockModeType getLockMode(Object entity) { public LockModeType getLockMode(Object entity) {
assertNotCloseInvoked(); assertNotCloseInvoked();
_broker.assertActiveTransaction(); _broker.assertActiveTransaction();
assertValidAttchedEntity("getLockMode", entity); assertValidAttchedEntity(GET_LOCK_MODE, entity);
return MixedLockLevelsHelper.fromLockLevel( return MixedLockLevelsHelper.fromLockLevel(
_broker.getLockLevel(entity)); _broker.getLockLevel(entity));
} }
@ -1155,13 +1159,13 @@ public class EntityManagerImpl
public void lock(Object entity) { public void lock(Object entity) {
assertNotCloseInvoked(); assertNotCloseInvoked();
assertValidAttchedEntity("lock", entity); assertValidAttchedEntity(LOCK, entity);
_broker.lock(entity, this); _broker.lock(entity, this);
} }
public void lock(Object entity, LockModeType mode, int timeout) { public void lock(Object entity, LockModeType mode, int timeout) {
assertNotCloseInvoked(); assertNotCloseInvoked();
assertValidAttchedEntity("lock", entity); assertValidAttchedEntity(LOCK, entity);
configureCurrentFetchPlan(pushFetchPlan(), null, mode, false); configureCurrentFetchPlan(pushFetchPlan(), null, mode, false);
try { try {
@ -1173,7 +1177,7 @@ public class EntityManagerImpl
public void lock(Object entity, LockModeType mode, Map<String, Object> properties) { public void lock(Object entity, LockModeType mode, Map<String, Object> properties) {
assertNotCloseInvoked(); assertNotCloseInvoked();
assertValidAttchedEntity("lock", entity); assertValidAttchedEntity(LOCK, entity);
_broker.assertActiveTransaction(); _broker.assertActiveTransaction();
configureCurrentCacheModes(getFetchPlan(), properties); configureCurrentCacheModes(getFetchPlan(), properties);
configureCurrentFetchPlan(pushFetchPlan(), properties, mode, false); configureCurrentFetchPlan(pushFetchPlan(), properties, mode, false);
@ -1341,7 +1345,7 @@ public class EntityManagerImpl
*/ */
void assertValidAttchedEntity(String call, Object entity) { void assertValidAttchedEntity(String call, Object entity) {
OpenJPAStateManager sm = _broker.getStateManager(entity); OpenJPAStateManager sm = _broker.getStateManager(entity);
if (sm == null || !sm.isPersistent() || sm.isDetached()) { if (sm == null || !sm.isPersistent() || sm.isDetached() || (call.equals(REFRESH) && sm.isDeleted())) {
throw new IllegalArgumentException(_loc.get("invalid_entity_argument", throw new IllegalArgumentException(_loc.get("invalid_entity_argument",
call, entity == null ? "null" : Exceptions.toString(entity)).getMessage()); call, entity == null ? "null" : Exceptions.toString(entity)).getMessage());
} }