PageRenderTime 33ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 390 lines | 321 code | 42 blank | 27 comment | 5 complexity | bc190a5e0fe078cf0c4762a23811e8d4 MD5 | raw file
Possible License(s): Apache-2.0
  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.DatastoreServiceConfig;
  15. import com.google.appengine.api.datastore.Entity;
  16. import com.google.appengine.api.datastore.Key;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.datanucleus.Utils;
  19. import com.google.appengine.datanucleus.test.jdo.BidirectionalChildListJDO;
  20. import com.google.appengine.datanucleus.test.jdo.Flight;
  21. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
  22. import com.google.appengine.datanucleus.test.jdo.HasOneToManyListJDO;
  23. import com.google.appengine.datanucleus.test.jdo.HasOneToOneJDO;
  24. import java.lang.reflect.Method;
  25. import javax.jdo.JDOUserException;
  26. /**
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. public class JDOBatchDeleteTest extends JDOBatchTestCase {
  30. private static Entity newFlightEntity() {
  31. return Flight.newFlightEntity("Harold", "BOS", "MIA", 4, 2);
  32. }
  33. private static Entity newFlightEntity(Key parent, int childIndex) {
  34. Entity e = Flight.newFlightEntity(parent, null, "Harold", "BOS", "MIA", 4, 2, 23);
  35. e.setProperty("flights_INTEGER_IDX", childIndex);
  36. return e;
  37. }
  38. private static Entity newBidirChildEntity(Key parent, int childIndex) {
  39. Entity e = new Entity(BidirectionalChildListJDO.class.getSimpleName(), parent);
  40. e.setProperty("bidirChildren_INTEGER_IDX", childIndex);
  41. return e;
  42. }
  43. BatchRecorder newBatchRecorder() {
  44. DatastoreServiceConfig config = getStoreManager().getDefaultDatastoreServiceConfigForReads();
  45. return new BatchRecorder(config) {
  46. boolean isBatchMethod(Method method) {
  47. return method.getName().equals("delete") &&
  48. (method.getParameterTypes().length == 1 && Iterable.class.isAssignableFrom(method.getParameterTypes()[0])) ||
  49. (method.getParameterTypes().length == 2 && Iterable.class.isAssignableFrom(method.getParameterTypes()[1]));
  50. }
  51. };
  52. }
  53. public void testDeletePersistentAll_NoTxn() {
  54. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  55. Key k1 = ds.put(newFlightEntity());
  56. Key k2 = ds.put(newFlightEntity());
  57. Flight f1 = pm.getObjectById(Flight.class, k1);
  58. Flight f2 = pm.getObjectById(Flight.class, k2);
  59. pm.deletePersistentAll(f1, f2);
  60. assertEquals(0, countForClass(Flight.class));
  61. assertEquals(1, batchRecorder.batchOps);
  62. Key k3 = ds.put(newFlightEntity());
  63. Key k4 = ds.put(newFlightEntity());
  64. Flight f3 = pm.getObjectById(Flight.class, k3);
  65. Flight f4 = pm.getObjectById(Flight.class, k4);
  66. pm.deletePersistentAll(Utils.newArrayList(f3, f4));
  67. assertEquals(0, countForClass(Flight.class));
  68. assertEquals(2, batchRecorder.batchOps);
  69. }
  70. public void testDeletePersistentAll_OneEntity_NoTxn() {
  71. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  72. Key k1 = ds.put(newFlightEntity());
  73. Flight f1 = pm.getObjectById(Flight.class, k1);
  74. pm.deletePersistentAll(f1);
  75. assertEquals(0, countForClass(Flight.class));
  76. assertEquals(0, batchRecorder.batchOps);
  77. Key k2 = ds.put(newFlightEntity());
  78. Flight f2 = pm.getObjectById(Flight.class, k2);
  79. pm.deletePersistentAll(Utils.newArrayList(f2));
  80. assertEquals(0, countForClass(Flight.class));
  81. assertEquals(0, batchRecorder.batchOps);
  82. }
  83. public void testDeletePersistentAll_Txn_MultipleEntityGroups() {
  84. switchDatasource(PersistenceManagerFactoryName.transactional);
  85. Key k1 = ds.put(newFlightEntity());
  86. Key k2 = ds.put(newFlightEntity());
  87. beginTxn();
  88. Flight f1 = pm.getObjectById(Flight.class, k1);
  89. commitTxn();
  90. beginTxn();
  91. Flight f2 = pm.getObjectById(Flight.class, k2);
  92. commitTxn();
  93. beginTxn();
  94. try {
  95. pm.deletePersistentAll(f1, f2);
  96. fail("expected exception");
  97. } catch (JDOUserException e) {
  98. // good
  99. }
  100. rollbackTxn();
  101. assertEquals(2, countForClass(Flight.class));
  102. // We don't get to the batch delete because we blow up
  103. // before we get there.
  104. assertEquals(0, batchRecorder.batchOps);
  105. Key k3 = ds.put(newFlightEntity());
  106. Key k4 = ds.put(newFlightEntity());
  107. beginTxn();
  108. Flight f3 = pm.getObjectById(Flight.class, k3);
  109. commitTxn();
  110. beginTxn();
  111. Flight f4 = pm.getObjectById(Flight.class, k4);
  112. commitTxn();
  113. beginTxn();
  114. try {
  115. pm.deletePersistentAll(Utils.newArrayList(f3, f4));
  116. fail("expected exception");
  117. } catch (JDOUserException e) {
  118. // good
  119. }
  120. rollbackTxn();
  121. assertEquals(4, countForClass(Flight.class));
  122. assertEquals(0, batchRecorder.batchOps);
  123. // cleanup outside tx
  124. pm.deletePersistentAll(Utils.newArrayList(f1, f2, f3, f4));
  125. }
  126. public void testDeletePersistentAll_Txn_OneEntityGroup() {
  127. switchDatasource(PersistenceManagerFactoryName.transactional);
  128. Key parentKey = KeyFactory.createKey("yar", 24);
  129. Key k1 = ds.put(new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey));
  130. Key k2 = ds.put(new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey));
  131. beginTxn();
  132. HasKeyAncestorKeyPkJDO child1 = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, k1);
  133. HasKeyAncestorKeyPkJDO child2 = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, k2);
  134. pm.deletePersistentAll(child1, child2);
  135. commitTxn();
  136. assertEquals(0, countForClass(HasKeyAncestorKeyPkJDO.class));
  137. assertEquals(1, batchRecorder.batchOps);
  138. }
  139. public void testDeletePersistentAll_OneEntity_Txn() {
  140. switchDatasource(PersistenceManagerFactoryName.transactional);
  141. Key k1 = ds.put(newFlightEntity());
  142. beginTxn();
  143. Flight f1 = pm.getObjectById(Flight.class, k1);
  144. pm.deletePersistentAll(f1);
  145. commitTxn();
  146. assertEquals(0, countForClass(Flight.class));
  147. assertEquals(0, batchRecorder.batchOps);
  148. Key k2 = ds.put(newFlightEntity());
  149. beginTxn();
  150. Flight f2 = pm.getObjectById(Flight.class, k2);
  151. pm.deletePersistentAll(Utils.newArrayList(f2));
  152. commitTxn();
  153. assertEquals(0, countForClass(Flight.class));
  154. assertEquals(0, batchRecorder.batchOps);
  155. }
  156. public void testDeletePersistentAll_CascadeDelete_OneToOne_NoTxn() {
  157. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  158. Key k1 = ds.put(new Entity(HasOneToOneJDO.class.getSimpleName()));
  159. Key k2 = ds.put(new Entity(HasOneToOneJDO.class.getSimpleName()));
  160. ds.put(newFlightEntity(k1, 1));
  161. ds.put(newFlightEntity(k2, 1));
  162. HasOneToOneJDO parent1 = pm.getObjectById(HasOneToOneJDO.class, k1);
  163. HasOneToOneJDO parent2 = pm.getObjectById(HasOneToOneJDO.class, k2);
  164. pm.deletePersistentAll(parent1, parent2);
  165. assertEquals(0, countForClass(HasOneToOneJDO.class));
  166. assertEquals(0, countForClass(Flight.class));
  167. assertEquals(1, batchRecorder.batchOps);
  168. }
  169. public void testDeletePersistentAll_CascadeDelete_OneToOne_MultipleEntityGroups_Txn() {
  170. switchDatasource(PersistenceManagerFactoryName.transactional);
  171. Key k1 = ds.put(new Entity(HasOneToOneJDO.class.getSimpleName()));
  172. Key k2 = ds.put(new Entity(HasOneToOneJDO.class.getSimpleName()));
  173. ds.put(newFlightEntity(k1, 1));
  174. ds.put(newFlightEntity(k2, 1));
  175. beginTxn();
  176. HasOneToOneJDO parent1 = pm.getObjectById(HasOneToOneJDO.class, k1);
  177. commitTxn();
  178. beginTxn();
  179. HasOneToOneJDO parent2 = pm.getObjectById(HasOneToOneJDO.class, k2);
  180. commitTxn();
  181. beginTxn();
  182. try {
  183. pm.deletePersistentAll(parent1, parent2);
  184. fail("expected exception");
  185. } catch (JDOUserException e) {
  186. // good
  187. }
  188. rollbackTxn();
  189. assertEquals(2, countForClass(HasOneToOneJDO.class));
  190. assertEquals(2, countForClass(Flight.class));
  191. assertEquals(1, batchRecorder.batchOps);
  192. // cleanup outside tx
  193. pm.deletePersistentAll(parent1, parent2);
  194. }
  195. public void testDeletePersistentAll_CascadeDelete_OneToOne_OneEntityGroup_Txn() {
  196. switchDatasource(PersistenceManagerFactoryName.transactional);
  197. Key k1 = ds.put(new Entity(HasOneToOneJDO.class.getSimpleName(), KeyFactory.createKey("Yar", 43)));
  198. Key k2 = ds.put(new Entity(HasOneToOneJDO.class.getSimpleName(), KeyFactory.createKey("Yar", 43)));
  199. ds.put(newFlightEntity(k1, 1));
  200. ds.put(newFlightEntity(k2, 1));
  201. beginTxn();
  202. HasOneToOneJDO parent1 = pm.getObjectById(HasOneToOneJDO.class, k1);
  203. HasOneToOneJDO parent2 = pm.getObjectById(HasOneToOneJDO.class, k2);
  204. pm.deletePersistentAll(parent1, parent2);
  205. commitTxn();
  206. assertEquals(0, countForClass(HasOneToOneJDO.class));
  207. assertEquals(0, countForClass(Flight.class));
  208. assertEquals(1, batchRecorder.batchOps);
  209. }
  210. public void testDeletePersistentAll_CascadeDelete_OneToMany_NoTxn() {
  211. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  212. Entity owner1Entity = new Entity(HasOneToManyListJDO.class.getSimpleName());
  213. Key k1 = ds.put(owner1Entity);
  214. Entity owner2Entity = new Entity(HasOneToManyListJDO.class.getSimpleName());
  215. Key k2 = ds.put(owner2Entity);
  216. Entity fl1 = newFlightEntity(k1, 1);
  217. Entity fl2 = newFlightEntity(k1, 2);
  218. Entity fl3 = newFlightEntity(k2, 1);
  219. Entity fl4 = newFlightEntity(k2, 2);
  220. ds.put(fl1);
  221. ds.put(fl2);
  222. ds.put(fl3);
  223. ds.put(fl4);
  224. Entity bi1 = newBidirChildEntity(k1, 1);
  225. Entity bi2 = newBidirChildEntity(k1, 2);
  226. Entity bi3 = newBidirChildEntity(k2, 1);
  227. Entity bi4 = newBidirChildEntity(k2, 2);
  228. ds.put(bi1);
  229. ds.put(bi2);
  230. ds.put(bi3);
  231. ds.put(bi4);
  232. owner1Entity.setProperty("flights", Utils.newArrayList(fl1.getKey(), fl2.getKey()));
  233. owner1Entity.setProperty("bidirChildren", Utils.newArrayList(bi1.getKey(), bi2.getKey()));
  234. ds.put(owner1Entity);
  235. owner2Entity.setProperty("flights", Utils.newArrayList(fl3.getKey(), fl4.getKey()));
  236. owner2Entity.setProperty("bidirChildren", Utils.newArrayList(bi3.getKey(), bi4.getKey()));
  237. ds.put(owner2Entity);
  238. HasOneToManyListJDO parent1 = pm.getObjectById(HasOneToManyListJDO.class, k1);
  239. assertEquals(2, parent1.getFlights().size());
  240. assertEquals(2, parent1.getBidirChildren().size());
  241. HasOneToManyListJDO parent2 = pm.getObjectById(HasOneToManyListJDO.class, k2);
  242. assertEquals(2, parent2.getFlights().size());
  243. assertEquals(2, parent2.getBidirChildren().size());
  244. pm.deletePersistentAll(parent1, parent2);
  245. assertEquals(0, countForClass(HasOneToManyListJDO.class));
  246. assertEquals(0, countForClass(Flight.class));
  247. assertEquals(0, countForClass(BidirectionalChildListJDO.class));
  248. assertEquals(1, batchRecorder.batchOps);
  249. }
  250. public void testDeletePersistentAll_CascadeDelete_OneToMany_MultipleEntityGroups_Txn() {
  251. switchDatasource(PersistenceManagerFactoryName.transactional);
  252. Entity owner1Entity = new Entity(HasOneToManyListJDO.class.getSimpleName());
  253. Key k1 = ds.put(owner1Entity);
  254. Entity owner2Entity = new Entity(HasOneToManyListJDO.class.getSimpleName());
  255. Key k2 = ds.put(owner2Entity);
  256. Entity fl1 = newFlightEntity(k1, 1);
  257. Entity fl2 = newFlightEntity(k1, 2);
  258. Entity fl3 = newFlightEntity(k2, 1);
  259. Entity fl4 = newFlightEntity(k2, 2);
  260. ds.put(fl1);
  261. ds.put(fl2);
  262. ds.put(fl3);
  263. ds.put(fl4);
  264. Entity bi1 = newBidirChildEntity(k1, 1);
  265. Entity bi2 = newBidirChildEntity(k1, 2);
  266. Entity bi3 = newBidirChildEntity(k2, 1);
  267. Entity bi4 = newBidirChildEntity(k2, 2);
  268. ds.put(bi1);
  269. ds.put(bi2);
  270. ds.put(bi3);
  271. ds.put(bi4);
  272. owner1Entity.setProperty("flights", Utils.newArrayList(fl1.getKey(), fl2.getKey()));
  273. owner1Entity.setProperty("bidirChildren", Utils.newArrayList(bi1.getKey(), bi2.getKey()));
  274. ds.put(owner1Entity);
  275. owner2Entity.setProperty("flights", Utils.newArrayList(fl3.getKey(), fl4.getKey()));
  276. owner2Entity.setProperty("bidirChildren", Utils.newArrayList(bi3.getKey(), bi4.getKey()));
  277. ds.put(owner2Entity);
  278. beginTxn();
  279. HasOneToManyListJDO parent1 = pm.getObjectById(HasOneToManyListJDO.class, k1);
  280. assertEquals(2, parent1.getFlights().size());
  281. assertEquals(2, parent1.getBidirChildren().size());
  282. commitTxn();
  283. beginTxn();
  284. HasOneToManyListJDO parent2 = pm.getObjectById(HasOneToManyListJDO.class, k2);
  285. assertEquals(2, parent2.getFlights().size());
  286. assertEquals(2, parent2.getBidirChildren().size());
  287. commitTxn();
  288. beginTxn();
  289. try {
  290. pm.deletePersistentAll(parent1, parent2);
  291. fail("expected exception");
  292. } catch (JDOUserException e) {
  293. // good
  294. }
  295. rollbackTxn();
  296. assertEquals(2, countForClass(HasOneToManyListJDO.class));
  297. assertEquals(4, countForClass(Flight.class));
  298. assertEquals(4, countForClass(BidirectionalChildListJDO.class));
  299. assertEquals(1, batchRecorder.batchOps);
  300. // cleanup outside tx
  301. pm.deletePersistentAll(parent1, parent2);
  302. }
  303. public void testDeletePersistentAll_CascadeDelete_OneToMany_OneEntityGroup_Txn() {
  304. switchDatasource(PersistenceManagerFactoryName.transactional);
  305. Entity owner1Entity = new Entity(HasOneToManyListJDO.class.getSimpleName(), KeyFactory.createKey("yar", 43));
  306. Entity owner2Entity = new Entity(HasOneToManyListJDO.class.getSimpleName(), KeyFactory.createKey("yar", 43));
  307. Key k1 = ds.put(owner1Entity);
  308. Key k2 = ds.put(owner2Entity);
  309. Entity fl1 = newFlightEntity(k1, 1);
  310. Entity fl2 = newFlightEntity(k1, 2);
  311. Entity fl3 = newFlightEntity(k2, 1);
  312. Entity fl4 = newFlightEntity(k2, 2);
  313. ds.put(fl1);
  314. ds.put(fl2);
  315. ds.put(fl3);
  316. ds.put(fl4);
  317. Entity bi1 = newBidirChildEntity(k1, 1);
  318. Entity bi2 = newBidirChildEntity(k1, 2);
  319. Entity bi3 = newBidirChildEntity(k2, 1);
  320. Entity bi4 = newBidirChildEntity(k2, 2);
  321. ds.put(bi1);
  322. ds.put(bi2);
  323. ds.put(bi3);
  324. ds.put(bi4);
  325. owner1Entity.setProperty("flights", Utils.newArrayList(fl1.getKey(), fl2.getKey()));
  326. owner1Entity.setProperty("bidirChildren", Utils.newArrayList(bi1.getKey(), bi2.getKey()));
  327. ds.put(owner1Entity);
  328. owner2Entity.setProperty("flights", Utils.newArrayList(fl3.getKey(), fl4.getKey()));
  329. owner2Entity.setProperty("bidirChildren", Utils.newArrayList(bi3.getKey(), bi4.getKey()));
  330. ds.put(owner2Entity);
  331. beginTxn();
  332. HasOneToManyListJDO parent1 = pm.getObjectById(HasOneToManyListJDO.class, k1);
  333. assertEquals(2, parent1.getFlights().size());
  334. assertEquals(2, parent1.getBidirChildren().size());
  335. HasOneToManyListJDO parent2 = pm.getObjectById(HasOneToManyListJDO.class, k2);
  336. assertEquals(2, parent2.getFlights().size());
  337. assertEquals(2, parent2.getBidirChildren().size());
  338. pm.deletePersistentAll(parent1, parent2);
  339. commitTxn();
  340. assertEquals(0, countForClass(HasOneToManyListJDO.class));
  341. assertEquals(0, countForClass(Flight.class));
  342. assertEquals(0, countForClass(BidirectionalChildListJDO.class));
  343. assertEquals(1, batchRecorder.batchOps);
  344. }
  345. }