/tests/com/google/appengine/datanucleus/query/JDOQLDeleteTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 171 lines · 127 code · 25 blank · 19 comment · 0 complexity · 5ec745cb1fd1ea73bf6636dfcf75cf96 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.query;
  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.Utils;
  19. import com.google.appengine.datanucleus.jdo.JDOTestCase;
  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 javax.jdo.JDOFatalUserException;
  24. import javax.jdo.Query;
  25. /**
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. public class JDOQLDeleteTest extends JDOTestCase {
  29. public void testDelete_Txn_MultipleEntityGroups() {
  30. ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24, 25));
  31. ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24, 25));
  32. Query q = pm.newQuery(Flight.class);
  33. beginTxn();
  34. try {
  35. q.deletePersistentAll();
  36. fail("expected exception");
  37. } catch (JDOFatalUserException e) {
  38. // good - can't delete books from multiple entity groups in a txn
  39. }
  40. rollbackTxn();
  41. assertEquals(2, countForClass(Flight.class));
  42. }
  43. public void testDelete_Txn_OneEntityGroup() {
  44. Key parentKey = KeyFactory.createKey("yar", 23);
  45. ds.put(Flight.newFlightEntity(parentKey, null, "jimmy", "bos", "mia", 23, 24, 25));
  46. ds.put(Flight.newFlightEntity(parentKey, null, "jimmy", "bos", "mia", 23, 24, 25));
  47. Query q = pm.newQuery(Flight.class);
  48. beginTxn();
  49. assertEquals(2, q.deletePersistentAll());
  50. assertEquals(2, countForClass(Flight.class));
  51. commitTxn();
  52. assertEquals(0, countForClass(Flight.class));
  53. }
  54. public void testDelete_NoTxn() {
  55. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  56. ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24));
  57. ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24));
  58. Query q = pm.newQuery(Flight.class);
  59. assertEquals(2, q.deletePersistentAll());
  60. assertEquals(0, countForClass(Flight.class));
  61. }
  62. public void testDeleteAncestorQuery_Txn() {
  63. Key parentKey = KeyFactory.createKey("yar", 23);
  64. Entity pojo1 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
  65. Entity pojo2 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
  66. ds.put(pojo1);
  67. ds.put(pojo2);
  68. Query q = pm.newQuery(HasKeyAncestorKeyPkJDO.class, "ancestorKey == :p1");
  69. beginTxn();
  70. assertEquals(2, q.deletePersistentAll(parentKey));
  71. commitTxn();
  72. assertEquals(0, countForClass(HasKeyAncestorKeyPkJDO.class));
  73. }
  74. public void testDeleteAncestorQuery_TxnRollback() throws EntityNotFoundException {
  75. Key parentKey = KeyFactory.createKey("yar", 23);
  76. Entity pojo1 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
  77. Entity pojo2 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
  78. ds.put(pojo1);
  79. ds.put(pojo2);
  80. Query q = pm.newQuery(HasKeyAncestorKeyPkJDO.class, "ancestorKey == :p1");
  81. beginTxn();
  82. assertEquals(2, q.deletePersistentAll(parentKey));
  83. rollbackTxn();
  84. assertEquals(2, countForClass(HasKeyAncestorKeyPkJDO.class));
  85. }
  86. public void testDeleteAncestorQuery_NoTxn() {
  87. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  88. Key parentKey = KeyFactory.createKey("yar", 23);
  89. Entity pojo1 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
  90. Entity pojo2 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
  91. ds.put(pojo1);
  92. ds.put(pojo2);
  93. Query q = pm.newQuery(HasKeyAncestorKeyPkJDO.class, "ancestorKey == :p1");
  94. assertEquals(2, q.deletePersistentAll(parentKey));
  95. assertEquals(0, countForClass(HasKeyAncestorKeyPkJDO.class));
  96. }
  97. public void testBatchDelete_NoTxn() {
  98. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  99. Entity e1 = Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24);
  100. ds.put(e1);
  101. Entity e2 = Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24);
  102. ds.put(e2);
  103. Entity e3 = Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24);
  104. ds.put(e3);
  105. Key key = KeyFactory.createKey("yar", "does not exist");
  106. Query q = pm.newQuery(Flight.class, "id == :ids");
  107. assertEquals(2, q.deletePersistentAll(Utils.newArrayList(key, e1.getKey(), e2.getKey())));
  108. assertEquals(1, countForClass(Flight.class));
  109. }
  110. public void testBatchDelete_Txn() {
  111. Key parent = KeyFactory.createKey("yar", 23);
  112. Entity e1 = Flight.newFlightEntity(parent, null, "jimmy", "bos", "mia", 23, 24, 25);
  113. ds.put(e1);
  114. Entity e2 = Flight.newFlightEntity(parent, null, "jimmy", "bos", "mia", 23, 24, 25);
  115. ds.put(e2);
  116. Entity e3 = Flight.newFlightEntity(parent, null, "jimmy", "bos", "mia", 23, 24, 25);
  117. ds.put(e3);
  118. beginTxn();
  119. Query q = pm.newQuery(Flight.class, "id == :ids");
  120. assertEquals(2, q.deletePersistentAll(Utils.newArrayList(parent, e1.getKey(), e2.getKey())));
  121. assertEquals(3, countForClass(Flight.class));
  122. commitTxn();
  123. assertEquals(1, countForClass(Flight.class));
  124. }
  125. public void testDeleteCascades() {
  126. HasOneToManyListJDO parent = new HasOneToManyListJDO();
  127. Flight f = new Flight();
  128. parent.getFlights().add(f);
  129. beginTxn();
  130. pm.makePersistent(parent);
  131. commitTxn();
  132. assertEquals(1, countForClass(Flight.class));
  133. assertEquals(1, countForClass(HasOneToManyListJDO.class));
  134. beginTxn();
  135. Query q = pm.newQuery(HasOneToManyListJDO.class);
  136. assertEquals(1, q.deletePersistentAll());
  137. assertEquals(1, countForClass(Flight.class));
  138. assertEquals(1, countForClass(HasOneToManyListJDO.class));
  139. commitTxn();
  140. assertEquals(0, countForClass(Flight.class));
  141. assertEquals(0, countForClass(HasOneToManyListJDO.class));
  142. }
  143. }