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

http://datanucleus-appengine.googlecode.com/ · Java · 287 lines · 215 code · 45 blank · 27 comment · 0 complexity · 485643e78f1e161dc30f82112d0fdab2 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.KeyFactory;
  17. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
  18. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorStringPkJDO;
  19. import com.google.appengine.datanucleus.test.jdo.HasOneToOneJDO;
  20. import com.google.appengine.datanucleus.test.jdo.HasOneToOneParentJDO;
  21. import com.google.appengine.datanucleus.test.jdo.HasOneToOnesWithDifferentCascadesJDO;
  22. import com.google.appengine.datanucleus.test.jdo.HasStringAncestorKeyPkJDO;
  23. import com.google.appengine.datanucleus.test.jdo.HasStringAncestorStringPkJDO;
  24. import static com.google.appengine.datanucleus.TestUtils.assertKeyParentEquals;
  25. import javax.jdo.JDOFatalUserException;
  26. /**
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. public class JDOImplicitEntityGroupTest extends JDOTestCase {
  30. public void testOneToOnePersistCascadeAll() throws EntityNotFoundException {
  31. HasOneToOneJDO parent = new HasOneToOneJDO();
  32. HasOneToOneParentJDO child = new HasOneToOneParentJDO();
  33. parent.setHasParent(child);
  34. child.setParent(parent);
  35. beginTxn();
  36. pm.makePersistent(parent);
  37. commitTxn();
  38. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getKey()));
  39. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  40. beginTxn();
  41. parent = pm.getObjectById(HasOneToOneJDO.class, parent.getId());
  42. assertNotNull(parent.getHasParent());
  43. assertNotNull(parent.getHasParent().getParent());
  44. commitTxn();
  45. }
  46. public void testOneToOnePersistChildWithAncestorCascadeAll() throws EntityNotFoundException {
  47. HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
  48. HasStringAncestorStringPkJDO child = new HasStringAncestorStringPkJDO();
  49. parent.setCascadeAllChild(child);
  50. beginTxn();
  51. pm.makePersistent(parent);
  52. commitTxn();
  53. beginTxn();
  54. parent = pm.getObjectById(HasOneToOnesWithDifferentCascadesJDO.class, parent.getId());
  55. assertNotNull(parent.getCascadeAllChild());
  56. child = pm.getObjectById(HasStringAncestorStringPkJDO.class, child.getId());
  57. assertEquals(parent.getId(), child.getAncestorId());
  58. commitTxn();
  59. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
  60. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  61. }
  62. public void testOneToOnePersistChildWithKeyAncestorCascadeAll() throws EntityNotFoundException {
  63. HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
  64. HasKeyAncestorStringPkJDO child = new HasKeyAncestorStringPkJDO();
  65. parent.setCascadeAllChildWithKeyAncestor(child);
  66. beginTxn();
  67. pm.makePersistent(parent);
  68. commitTxn();
  69. beginTxn();
  70. parent = pm.getObjectById(HasOneToOnesWithDifferentCascadesJDO.class, parent.getId());
  71. child = pm.getObjectById(HasKeyAncestorStringPkJDO.class, child.getKey());
  72. assertEquals(parent.getId(), KeyFactory.keyToString(child.getAncestorKey()));
  73. assertNotNull(parent.getCascadeAllChildWithKeyAncestor());
  74. commitTxn();
  75. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getKey()));
  76. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  77. }
  78. public void testOneToOnePersistCascadePersist() throws EntityNotFoundException {
  79. HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
  80. HasStringAncestorKeyPkJDO child = new HasStringAncestorKeyPkJDO();
  81. parent.setCascadePersistChild(child);
  82. beginTxn();
  83. pm.makePersistent(parent);
  84. commitTxn();
  85. beginTxn();
  86. parent = pm.getObjectById(HasOneToOnesWithDifferentCascadesJDO.class, parent.getId());
  87. child = pm.getObjectById(HasStringAncestorKeyPkJDO.class, child.getKey());
  88. assertEquals(parent.getId(), child.getAncestorKey());
  89. assertNotNull(parent.getCascadePersistChild());
  90. commitTxn();
  91. Entity childEntity = ds.get(child.getKey());
  92. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  93. }
  94. public void testOneToOnePersistCascadeRemove() throws EntityNotFoundException {
  95. HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
  96. HasKeyAncestorKeyPkJDO child = new HasKeyAncestorKeyPkJDO();
  97. parent.setCascadeRemoveChild(child);
  98. beginTxn();
  99. pm.makePersistent(child);
  100. commitTxn();
  101. beginTxn();
  102. try {
  103. // this fails because it attempts to establish a parent
  104. // for an entity that was originally persisted without a parent.
  105. pm.makePersistent(parent);
  106. fail("expected exception");
  107. } catch (JDOFatalUserException e) {
  108. // good
  109. rollbackTxn();
  110. }
  111. }
  112. public void testOneToOnePersistCascadeAll_OverrideParentManually() {
  113. HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
  114. HasOneToOnesWithDifferentCascadesJDO anotherParent = new HasOneToOnesWithDifferentCascadesJDO();
  115. beginTxn();
  116. pm.makePersistent(anotherParent);
  117. commitTxn();
  118. HasStringAncestorStringPkJDO child = new HasStringAncestorStringPkJDO(anotherParent.getId());
  119. parent.setCascadeAllChild(child);
  120. beginTxn();
  121. try {
  122. pm.makePersistent(parent);
  123. fail("expected exception");
  124. } catch (JDOFatalUserException e) {
  125. // good
  126. } finally {
  127. rollbackTxn();
  128. }
  129. }
  130. public void testOneToOnePersistCascadePersist_OverrideParentManually() {
  131. HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
  132. HasOneToOnesWithDifferentCascadesJDO anotherParent = new HasOneToOnesWithDifferentCascadesJDO();
  133. beginTxn();
  134. pm.makePersistent(anotherParent);
  135. commitTxn();
  136. HasStringAncestorKeyPkJDO child = new HasStringAncestorKeyPkJDO();
  137. child.setAncestorKey(anotherParent.getId());
  138. parent.setCascadePersistChild(child);
  139. beginTxn();
  140. try {
  141. pm.makePersistent(parent);
  142. fail("expected exception");
  143. } catch (JDOFatalUserException e) {
  144. // good
  145. } finally {
  146. rollbackTxn();
  147. }
  148. }
  149. public void testOneToOneRemoveParentCascadeAll() {
  150. Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJDO.class.getSimpleName());
  151. ds.put(parentEntity);
  152. Entity childEntity = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentEntity.getKey());
  153. ds.put(childEntity);
  154. parentEntity.setProperty("cascadeall", childEntity.getKey());
  155. ds.put(parentEntity);
  156. beginTxn();
  157. HasOneToOnesWithDifferentCascadesJDO parent = pm.getObjectById(
  158. HasOneToOnesWithDifferentCascadesJDO.class, KeyFactory.keyToString(parentEntity.getKey()));
  159. assertNotNull(parent.getCascadeAllChild());
  160. pm.deletePersistent(parent);
  161. commitTxn();
  162. try {
  163. ds.get(parentEntity.getKey());
  164. fail("expected enfe");
  165. } catch (EntityNotFoundException e) {
  166. // good
  167. }
  168. try {
  169. ds.get(childEntity.getKey());
  170. fail("expected enfe");
  171. } catch (EntityNotFoundException e) {
  172. // good
  173. }
  174. }
  175. public void testOneToOneRemoveChildCascadeAll() throws EntityNotFoundException {
  176. Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJDO.class.getSimpleName());
  177. ds.put(parentEntity);
  178. Entity childEntity = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentEntity.getKey());
  179. ds.put(childEntity);
  180. parentEntity.setProperty("cascadeall", childEntity.getKey());
  181. ds.put(parentEntity);
  182. beginTxn();
  183. HasOneToOnesWithDifferentCascadesJDO parent = pm.getObjectById(
  184. HasOneToOnesWithDifferentCascadesJDO.class, KeyFactory.keyToString(parentEntity.getKey()));
  185. assertNotNull(parent.getCascadeAllChild());
  186. assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeAllChild().getId());
  187. pm.deletePersistent(parent.getCascadeAllChild());
  188. commitTxn();
  189. parentEntity = ds.get(parentEntity.getKey());
  190. assertEquals(childEntity.getKey(), parentEntity.getProperty("cascadeall"));
  191. try {
  192. ds.get(childEntity.getKey());
  193. fail("expected enfe");
  194. } catch (EntityNotFoundException e) {
  195. // good
  196. }
  197. }
  198. public void testOneToOneRemoveChildCascadeRemove() throws EntityNotFoundException {
  199. Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJDO.class.getSimpleName());
  200. ds.put(parentEntity);
  201. Entity childEntity = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentEntity.getKey());
  202. ds.put(childEntity);
  203. parentEntity.setProperty("cascaderemove", childEntity.getKey());
  204. ds.put(parentEntity);
  205. beginTxn();
  206. HasOneToOnesWithDifferentCascadesJDO parent = pm.getObjectById(
  207. HasOneToOnesWithDifferentCascadesJDO.class, KeyFactory.keyToString(parentEntity.getKey()));
  208. assertNotNull(parent.getCascadeRemoveChild());
  209. assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeRemoveChild().getKey());
  210. pm.deletePersistent(parent.getCascadeRemoveChild());
  211. commitTxn();
  212. parentEntity = ds.get(parentEntity.getKey());
  213. assertEquals(childEntity.getKey(), parentEntity.getProperty("cascaderemove"));
  214. try {
  215. ds.get(childEntity.getKey());
  216. fail("expected enfe");
  217. } catch (EntityNotFoundException e) {
  218. // good
  219. }
  220. }
  221. public void testChildGoesIntoEntityGroupOfExistingParent() throws EntityNotFoundException {
  222. HasOneToOneJDO parent = new HasOneToOneJDO();
  223. beginTxn();
  224. pm.makePersistent(parent);
  225. commitTxn();
  226. beginTxn();
  227. HasOneToOneParentJDO child = new HasOneToOneParentJDO();
  228. parent.setHasParent(child);
  229. child.setParent(parent);
  230. pm.makePersistent(parent);
  231. commitTxn();
  232. beginTxn();
  233. parent = pm.getObjectById(HasOneToOneJDO.class, parent.getId());
  234. assertNotNull(parent.getHasParent());
  235. commitTxn();
  236. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getKey()));
  237. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  238. }
  239. }