/tests/com/google/appengine/datanucleus/jpa/JPAImplicitEntityGroupTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 255 lines · 185 code · 45 blank · 25 comment · 0 complexity · e6418c015b47ad718b74bfa125e94d61 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.jpa;
  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.jpa.HasKeyAncestorKeyPkJPA;
  18. import com.google.appengine.datanucleus.test.jpa.HasOneToOneJPA;
  19. import com.google.appengine.datanucleus.test.jpa.HasOneToOneParentJPA;
  20. import com.google.appengine.datanucleus.test.jpa.HasOneToOnesWithDifferentCascadesJPA;
  21. import com.google.appengine.datanucleus.test.jpa.HasStringAncestorKeyPkJPA;
  22. import com.google.appengine.datanucleus.test.jpa.HasStringAncestorStringPkJPA;
  23. import static com.google.appengine.datanucleus.TestUtils.assertKeyParentEquals;
  24. import static com.google.appengine.datanucleus.TestUtils.assertKeyParentNull;
  25. import javax.persistence.PersistenceException;
  26. /**
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. public class JPAImplicitEntityGroupTest extends JPATestCase {
  30. public void testOneToOnePersistCascadeAll() throws EntityNotFoundException {
  31. HasOneToOneJPA parent = new HasOneToOneJPA();
  32. HasOneToOneParentJPA child = new HasOneToOneParentJPA();
  33. parent.setHasParent(child);
  34. child.setParent(parent);
  35. beginTxn();
  36. em.persist(parent);
  37. commitTxn();
  38. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
  39. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  40. ds.get(KeyFactory.stringToKey(parent.getId()));
  41. }
  42. public void testOneToOnePersistChildWithAncestorCascadeAll() throws EntityNotFoundException {
  43. HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
  44. HasStringAncestorStringPkJPA child = new HasStringAncestorStringPkJPA();
  45. parent.setCascadeAllChild(child);
  46. beginTxn();
  47. em.persist(parent);
  48. commitTxn();
  49. assertEquals(parent.getId(), child.getAncestorId());
  50. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
  51. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  52. ds.get(KeyFactory.stringToKey(parent.getId()));
  53. }
  54. public void testOneToOnePersistCascadePersist() throws EntityNotFoundException {
  55. HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
  56. HasStringAncestorKeyPkJPA child = new HasStringAncestorKeyPkJPA();
  57. parent.setCascadePersistChild(child);
  58. beginTxn();
  59. em.persist(parent);
  60. assertEquals(parent.getId(), child.getAncestorKey());
  61. commitTxn();
  62. Entity childEntity = ds.get(child.getKey());
  63. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  64. }
  65. public void testOneToOnePersistCascadeRemove() throws EntityNotFoundException {
  66. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_not_allowed);
  67. HasKeyAncestorKeyPkJPA child = new HasKeyAncestorKeyPkJPA();
  68. beginTxn();
  69. em.persist(child);
  70. commitTxn();
  71. HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
  72. parent.setCascadeRemoveChild(child);
  73. beginTxn();
  74. em.persist(parent);
  75. try {
  76. commitTxn();
  77. fail("expected exception");
  78. } catch (PersistenceException e) {
  79. // good
  80. rollbackTxn();
  81. }
  82. Entity childEntity = ds.get(child.getKey());
  83. assertKeyParentNull(childEntity, childEntity.getKey());
  84. }
  85. public void testOneToOnePersistCascadeAll_OverrideParentManually() {
  86. HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
  87. HasOneToOnesWithDifferentCascadesJPA anotherParent = new HasOneToOnesWithDifferentCascadesJPA();
  88. beginTxn();
  89. em.persist(anotherParent);
  90. commitTxn();
  91. HasStringAncestorStringPkJPA child = new HasStringAncestorStringPkJPA(anotherParent.getId());
  92. parent.setCascadeAllChild(child);
  93. beginTxn();
  94. em.persist(parent);
  95. try {
  96. commitTxn();
  97. fail("expected exception");
  98. } catch (PersistenceException e) {
  99. // good
  100. }
  101. }
  102. public void testOneToOnePersistCascadePersist_OverrideParentManually() {
  103. HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
  104. HasOneToOnesWithDifferentCascadesJPA anotherParent = new HasOneToOnesWithDifferentCascadesJPA();
  105. beginTxn();
  106. em.persist(anotherParent);
  107. commitTxn();
  108. HasStringAncestorKeyPkJPA child = new HasStringAncestorKeyPkJPA();
  109. child.setAncestorKey(anotherParent.getId());
  110. parent.setCascadePersistChild(child);
  111. beginTxn();
  112. em.persist(parent);
  113. try {
  114. commitTxn();
  115. fail("expected exception");
  116. } catch (PersistenceException e) {
  117. // good
  118. }
  119. }
  120. public void testOneToOneRemoveParentCascadeAll() {
  121. Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJPA.class.getSimpleName());
  122. ds.put(parentEntity);
  123. Entity childEntity = new Entity(HasStringAncestorStringPkJPA.class.getSimpleName(), parentEntity.getKey());
  124. ds.put(childEntity);
  125. parentEntity.setProperty("cascadeall", childEntity.getKey());
  126. ds.put(parentEntity);
  127. beginTxn();
  128. HasOneToOnesWithDifferentCascadesJPA parent = em.find(
  129. HasOneToOnesWithDifferentCascadesJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
  130. assertNotNull(parent.getCascadeAllChild());
  131. em.remove(parent);
  132. commitTxn();
  133. try {
  134. ds.get(parentEntity.getKey());
  135. fail("expected enfe");
  136. } catch (EntityNotFoundException e) {
  137. // good
  138. }
  139. try {
  140. ds.get(childEntity.getKey());
  141. fail("expected enfe");
  142. } catch (EntityNotFoundException e) {
  143. // good
  144. }
  145. }
  146. public void testOneToOneRemoveChildCascadeAll() throws EntityNotFoundException {
  147. Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJPA.class.getSimpleName());
  148. ds.put(parentEntity);
  149. Entity childEntity = new Entity(HasStringAncestorStringPkJPA.class.getSimpleName(), parentEntity.getKey());
  150. ds.put(childEntity);
  151. parentEntity.setProperty("cascadeall", childEntity.getKey());
  152. ds.put(parentEntity);
  153. beginTxn();
  154. HasOneToOnesWithDifferentCascadesJPA parent = em.find(
  155. HasOneToOnesWithDifferentCascadesJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
  156. assertNotNull(parent.getCascadeAllChild());
  157. assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeAllChild().getId());
  158. em.remove(parent.getCascadeAllChild());
  159. commitTxn();
  160. parentEntity = ds.get(parentEntity.getKey());
  161. assertEquals(childEntity.getKey(), parentEntity.getProperty("cascadeall"));
  162. try {
  163. ds.get(childEntity.getKey());
  164. fail("expected enfe");
  165. } catch (EntityNotFoundException e) {
  166. // good
  167. }
  168. }
  169. public void testOneToOneRemoveChildCascadeRemove() throws EntityNotFoundException {
  170. Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJPA.class.getSimpleName());
  171. ds.put(parentEntity);
  172. Entity childEntity = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentEntity.getKey());
  173. ds.put(childEntity);
  174. parentEntity.setProperty("cascaderemove", childEntity.getKey());
  175. ds.put(parentEntity);
  176. beginTxn();
  177. HasOneToOnesWithDifferentCascadesJPA parent = em.find(
  178. HasOneToOnesWithDifferentCascadesJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
  179. assertNotNull(parent.getCascadeRemoveChild());
  180. assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeRemoveChild().getKey());
  181. em.remove(parent.getCascadeRemoveChild());
  182. commitTxn();
  183. parentEntity = ds.get(parentEntity.getKey());
  184. assertEquals(childEntity.getKey(), parentEntity.getProperty("cascaderemove"));
  185. try {
  186. ds.get(childEntity.getKey());
  187. fail("expected enfe");
  188. } catch (EntityNotFoundException e) {
  189. // good
  190. }
  191. }
  192. public void testChildGoesIntoEntityGroupOfExistingParent() throws EntityNotFoundException {
  193. HasOneToOneJPA parent = new HasOneToOneJPA();
  194. beginTxn();
  195. em.persist(parent);
  196. commitTxn();
  197. HasOneToOneParentJPA child = new HasOneToOneParentJPA();
  198. parent.setHasParent(child);
  199. child.setParent(parent);
  200. beginTxn();
  201. em.merge(parent);
  202. commitTxn();
  203. Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
  204. assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
  205. ds.get(KeyFactory.stringToKey(parent.getId()));
  206. }
  207. }