/tests/com/google/appengine/datanucleus/jdo/JDODeleteTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 241 lines · 180 code · 27 blank · 34 comment · 0 complexity · fee6a5915dbd76e5e8408dc185b9b3c9 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.jdo;
  14. import com.google.appengine.api.datastore.Entity;
  15. import com.google.appengine.api.datastore.EntityNotFoundException;
  16. import com.google.appengine.api.datastore.Key;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.datanucleus.test.jdo.Flight;
  19. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
  20. import com.google.appengine.datanucleus.test.jdo.HasVersionWithFieldJDO;
  21. import com.google.appengine.datanucleus.test.jdo.KitchenSink;
  22. import javax.jdo.JDOHelper;
  23. import javax.jdo.JDOObjectNotFoundException;
  24. import javax.jdo.JDOOptimisticVerificationException;
  25. /**
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. public class JDODeleteTest extends JDOTestCase {
  29. private static final String DEFAULT_VERSION_PROPERTY_NAME = "VERSION";
  30. public void testSimpleDelete() {
  31. Key key = ds.put(KitchenSink.newKitchenSinkEntity(null));
  32. String keyStr = KeyFactory.keyToString(key);
  33. beginTxn();
  34. KitchenSink ks = pm.getObjectById(KitchenSink.class, keyStr);
  35. assertNotNull(ks);
  36. pm.deletePersistent(ks);
  37. commitTxn();
  38. beginTxn();
  39. try {
  40. pm.getObjectById(KitchenSink.class, keyStr);
  41. fail("expected onfe");
  42. } catch (JDOObjectNotFoundException onfe) {
  43. // good
  44. } finally {
  45. rollbackTxn();
  46. }
  47. }
  48. public void testSimpleDelete_NamedKey() {
  49. Key key = ds.put(KitchenSink.newKitchenSinkEntity("named key", null));
  50. String keyStr = KeyFactory.keyToString(key);
  51. beginTxn();
  52. KitchenSink ks = pm.getObjectById(KitchenSink.class, keyStr);
  53. assertNotNull(ks);
  54. assertEquals("named key", KeyFactory.stringToKey(ks.key).getName());
  55. pm.deletePersistent(ks);
  56. commitTxn();
  57. beginTxn();
  58. try {
  59. pm.getObjectById(KitchenSink.class, keyStr);
  60. fail("expected onfe");
  61. } catch (JDOObjectNotFoundException onfe) {
  62. // good
  63. } finally {
  64. rollbackTxn();
  65. }
  66. }
  67. public void testOptimisticLocking_Update_NoField() {
  68. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  69. Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
  70. Key key = ds.put(flightEntity);
  71. String keyStr = KeyFactory.keyToString(key);
  72. beginTxn();
  73. Flight flight = pm.getObjectById(Flight.class, keyStr);
  74. flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
  75. // we update the flight directly in the datastore right before we delete
  76. ds.put(flightEntity);
  77. try {
  78. pm.deletePersistent(flight);
  79. fail("expected optimistic exception");
  80. } catch (JDOOptimisticVerificationException jove) {
  81. // good
  82. } finally {
  83. rollbackTxn();
  84. }
  85. }
  86. public void testOptimisticLocking_Delete_NoField() {
  87. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  88. Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
  89. Key key = ds.put(flightEntity);
  90. String keyStr = KeyFactory.keyToString(key);
  91. beginTxn();
  92. Flight flight = pm.getObjectById(Flight.class, keyStr);
  93. flight.setName("2");
  94. flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
  95. // we remove the flight from the datastore right before delete
  96. ds.delete(key);
  97. try {
  98. pm.deletePersistent(flight);
  99. fail("expected optimistic exception");
  100. } catch (JDOOptimisticVerificationException jove) {
  101. // good
  102. } finally {
  103. rollbackTxn();
  104. }
  105. }
  106. public void testOptimisticLocking_Update_HasVersionField() {
  107. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  108. Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
  109. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
  110. Key key = ds.put(entity);
  111. String keyStr = KeyFactory.keyToString(key);
  112. beginTxn();
  113. HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
  114. hvwf.setValue("value");
  115. commitTxn();
  116. beginTxn();
  117. hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
  118. assertEquals(2L, hvwf.getVersion());
  119. // make sure the version gets bumped
  120. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 3L);
  121. // we update the entity directly in the datastore right before delete
  122. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 7L);
  123. ds.put(entity);
  124. try {
  125. pm.deletePersistent(hvwf);
  126. fail("expected optimistic exception");
  127. } catch (JDOOptimisticVerificationException jove) {
  128. // good
  129. } finally {
  130. rollbackTxn();
  131. }
  132. // make sure the version didn't change on the model object
  133. assertEquals(2L, JDOHelper.getVersion(hvwf));
  134. }
  135. public void testOptimisticLocking_Delete_HasVersionField() {
  136. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  137. Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
  138. entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
  139. Key key = ds.put(entity);
  140. String keyStr = KeyFactory.keyToString(key);
  141. beginTxn();
  142. HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
  143. // delete the entity in the datastore right before we delete
  144. ds.delete(key);
  145. try {
  146. pm.deletePersistent(hvwf);
  147. fail("expected optimistic exception");
  148. } catch (JDOOptimisticVerificationException jove) {
  149. // good
  150. } finally {
  151. rollbackTxn();
  152. }
  153. // make sure the version didn't change on the model object
  154. assertEquals(1L, JDOHelper.getVersion(hvwf));
  155. }
  156. public void testNonTransactionalDelete() throws EntityNotFoundException {
  157. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  158. Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
  159. Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
  160. pm.deletePersistent(f);
  161. pm.close();
  162. try {
  163. ds.get(key);
  164. fail("expected enfe");
  165. } catch (EntityNotFoundException enfe) {
  166. // good
  167. }
  168. pm = pmf.getPersistenceManager();
  169. }
  170. public void testDeleteHasAncestorPkField() {
  171. Entity e =
  172. new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), KeyFactory.createKey("Yam", 24));
  173. ds.put(e);
  174. beginTxn();
  175. HasKeyAncestorKeyPkJDO pojo = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, e.getKey());
  176. pm.deletePersistent(pojo);
  177. commitTxn();
  178. }
  179. public void testDeletePersistentNew() {
  180. beginTxn();
  181. KitchenSink ks = KitchenSink.newKitchenSink();
  182. pm.makePersistent(ks);
  183. String keyStr = ks.key;
  184. pm.deletePersistent(ks);
  185. commitTxn();
  186. beginTxn();
  187. try {
  188. pm.getObjectById(KitchenSink.class, keyStr);
  189. fail("expected onfe");
  190. } catch (JDOObjectNotFoundException onfe) {
  191. // good
  192. } finally {
  193. rollbackTxn();
  194. }
  195. }
  196. public void testDeletePersistentNew_NoTxn() {
  197. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  198. KitchenSink ks = KitchenSink.newKitchenSink();
  199. pm.makePersistent(ks);
  200. String keyStr = ks.key;
  201. pm.deletePersistent(ks);
  202. pm.close();
  203. pm = pmf.getPersistenceManager();
  204. try {
  205. pm.getObjectById(KitchenSink.class, keyStr);
  206. fail("expected onfe");
  207. } catch (JDOObjectNotFoundException onfe) {
  208. // good
  209. }
  210. }
  211. }