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

http://datanucleus-appengine.googlecode.com/ · Java · 261 lines · 190 code · 38 blank · 33 comment · 0 complexity · 33e65d666004870d4a9e378a6ac2a5f1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus.jdo;
  17. import com.google.appengine.api.datastore.Entity;
  18. import com.google.appengine.api.datastore.EntityNotFoundException;
  19. import com.google.appengine.api.datastore.KeyFactory;
  20. import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SubChild;
  21. import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SubParentWithSubChild;
  22. import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SubParentWithSuperChild;
  23. import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SuperChild;
  24. import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SuperParentWithSubChild;
  25. import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SuperParentWithSuperChild;
  26. import java.util.Collections;
  27. /**
  28. * @author Max Ross <max.ross@gmail.com>
  29. */
  30. public class JDOUnidirectionalOneToManySubclassTest extends JDOTestCase {
  31. public void testSubParentWithSubChild() throws EntityNotFoundException {
  32. // insertion
  33. SubParentWithSubChild parent = new SubParentWithSubChild();
  34. parent.setSuperParentString("super parent string");
  35. parent.setSubParentString("sub parent string");
  36. SubChild subChild = new SubChild();
  37. subChild.setAString("a string");
  38. subChild.setBString("b string");
  39. parent.getSuperParentSubChildren().add(subChild);
  40. beginTxn();
  41. pm.makePersistent(parent);
  42. commitTxn();
  43. Entity parentEntity =
  44. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  45. Entity superParentSubChildEntity = ds.get(subChild.getId());
  46. assertEquals(3, parentEntity.getProperties().size());
  47. assertEquals("super parent string", parentEntity.getProperty("superParentString"));
  48. assertEquals("sub parent string", parentEntity.getProperty("subParentString"));
  49. assertEquals(Collections.singletonList(superParentSubChildEntity.getKey()),
  50. parentEntity.getProperty("subChildren"));
  51. assertEquals(2, superParentSubChildEntity.getProperties().size());
  52. assertEquals("a string", superParentSubChildEntity.getProperty("aString"));
  53. assertEquals("b string", superParentSubChildEntity.getProperty("bString"));
  54. // lookup
  55. beginTxn();
  56. parent = pm.getObjectById(parent.getClass(), parent.getId());
  57. assertEquals("super parent string", parent.getSuperParentString());
  58. assertEquals("sub parent string", parent.getSubParentString());
  59. assertEquals(1, parent.getSuperParentSubChildren().size());
  60. assertEquals(subChild.getId(), parent.getSuperParentSubChildren().get(0).getId());
  61. commitTxn();
  62. beginTxn();
  63. subChild = pm.getObjectById(subChild.getClass(), subChild.getId());
  64. assertEquals("a string", subChild.getAString());
  65. assertEquals("b string", subChild.getBString());
  66. commitTxn();
  67. // cascade delete
  68. beginTxn();
  69. pm.deletePersistent(parent);
  70. commitTxn();
  71. assertEquals(0, countForClass(parent.getClass()));
  72. assertEquals(0, countForClass(subChild.getClass()));
  73. }
  74. public void testSubParentWithSuperChild() throws EntityNotFoundException {
  75. // insertion
  76. SubParentWithSuperChild parent = new SubParentWithSuperChild();
  77. parent.setSuperParentString("super parent string");
  78. parent.setSubParentString("sub parent string");
  79. SuperChild superChild = new SuperChild();
  80. superChild.setAString("a string");
  81. parent.getSuperParentSuperChildren().add(superChild);
  82. beginTxn();
  83. pm.makePersistent(parent);
  84. commitTxn();
  85. Entity parentEntity =
  86. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  87. Entity superParentSuperChildEntity = ds.get(superChild.getId());
  88. assertEquals(3, parentEntity.getProperties().size());
  89. assertEquals("super parent string", parentEntity.getProperty("superParentString"));
  90. assertEquals("sub parent string", parentEntity.getProperty("subParentString"));
  91. assertEquals(Collections.singletonList(superParentSuperChildEntity.getKey()),
  92. parentEntity.getProperty("superChildren"));
  93. assertEquals(1, superParentSuperChildEntity.getProperties().size());
  94. assertEquals("a string", superParentSuperChildEntity.getProperty("aString"));
  95. // lookup
  96. beginTxn();
  97. parent = pm.getObjectById(parent.getClass(), parent.getId());
  98. assertEquals("super parent string", parent.getSuperParentString());
  99. assertEquals("sub parent string", parent.getSubParentString());
  100. assertEquals(1, parent.getSuperParentSuperChildren().size());
  101. assertEquals(superChild.getId(), parent.getSuperParentSuperChildren().get(0).getId());
  102. commitTxn();
  103. beginTxn();
  104. superChild = pm.getObjectById(superChild.getClass(), superChild.getId());
  105. assertEquals("a string", superChild.getAString());
  106. commitTxn();
  107. // cascade delete
  108. beginTxn();
  109. pm.deletePersistent(parent);
  110. commitTxn();
  111. assertEquals(0, countForClass(parent.getClass()));
  112. assertEquals(0, countForClass(superChild.getClass()));
  113. }
  114. public void testSuperParentWithSuperChild() throws EntityNotFoundException {
  115. // insertion
  116. SuperParentWithSuperChild parent = new SuperParentWithSuperChild();
  117. parent.setSuperParentString("super parent string");
  118. SuperChild superChild = new SuperChild();
  119. superChild.setAString("a string");
  120. parent.getSuperParentSuperChildren().add(superChild);
  121. beginTxn();
  122. pm.makePersistent(parent);
  123. commitTxn();
  124. Entity parentEntity =
  125. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  126. Entity superParentSuperChildEntity = ds.get(superChild.getId());
  127. assertEquals(2, parentEntity.getProperties().size());
  128. assertEquals("super parent string", parentEntity.getProperty("superParentString"));
  129. assertEquals(Collections.singletonList(superParentSuperChildEntity.getKey()),
  130. parentEntity.getProperty("superChildren"));
  131. assertEquals(1, superParentSuperChildEntity.getProperties().size());
  132. assertEquals("a string", superParentSuperChildEntity.getProperty("aString"));
  133. // lookup
  134. beginTxn();
  135. parent = pm.getObjectById(parent.getClass(), parent.getId());
  136. assertEquals("super parent string", parent.getSuperParentString());
  137. assertEquals(1, parent.getSuperParentSuperChildren().size());
  138. assertEquals(superChild.getId(), parent.getSuperParentSuperChildren().get(0).getId());
  139. commitTxn();
  140. beginTxn();
  141. superChild = pm.getObjectById(superChild.getClass(), superChild.getId());
  142. assertEquals("a string", superChild.getAString());
  143. commitTxn();
  144. // cascade delete
  145. beginTxn();
  146. pm.deletePersistent(parent);
  147. commitTxn();
  148. assertEquals(0, countForClass(parent.getClass()));
  149. assertEquals(0, countForClass(superChild.getClass()));
  150. }
  151. public void testSuperParentWithSubChild() throws EntityNotFoundException {
  152. // insertion
  153. SuperParentWithSubChild parent = new SuperParentWithSubChild();
  154. parent.setSuperParentString("super parent string");
  155. SubChild subChild = new SubChild();
  156. subChild.setAString("a string");
  157. subChild.setBString("b string");
  158. parent.getSuperParentSubChildren().add(subChild);
  159. beginTxn();
  160. pm.makePersistent(parent);
  161. commitTxn();
  162. Entity parentEntity =
  163. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  164. Entity superParentSubChildEntity = ds.get(subChild.getId());
  165. assertEquals(2, parentEntity.getProperties().size());
  166. assertEquals("super parent string", parentEntity.getProperty("superParentString"));
  167. assertEquals(Collections.singletonList(superParentSubChildEntity.getKey()),
  168. parentEntity.getProperty("subChildren"));
  169. assertEquals(2, superParentSubChildEntity.getProperties().size());
  170. assertEquals("a string", superParentSubChildEntity.getProperty("aString"));
  171. assertEquals("b string", superParentSubChildEntity.getProperty("bString"));
  172. // lookup
  173. beginTxn();
  174. parent = pm.getObjectById(parent.getClass(), parent.getId());
  175. assertEquals("super parent string", parent.getSuperParentString());
  176. assertEquals(1, parent.getSuperParentSubChildren().size());
  177. assertEquals(subChild.getId(), parent.getSuperParentSubChildren().get(0).getId());
  178. commitTxn();
  179. beginTxn();
  180. subChild = pm.getObjectById(subChild.getClass(), subChild.getId());
  181. assertEquals("a string", subChild.getAString());
  182. assertEquals("b string", subChild.getBString());
  183. commitTxn();
  184. // cascade delete
  185. beginTxn();
  186. pm.deletePersistent(parent);
  187. commitTxn();
  188. assertEquals(0, countForClass(parent.getClass()));
  189. assertEquals(0, countForClass(subChild.getClass()));
  190. }
  191. public void testWrongChildType() {
  192. SuperParentWithSuperChild parent = new SuperParentWithSuperChild();
  193. parent.setSuperParentString("a string");
  194. SubChild child = new SubChild();
  195. parent.getSuperParentSuperChildren().add(child);
  196. beginTxn();
  197. try {
  198. pm.makePersistent(parent);
  199. fail("expected exception");
  200. } catch (UnsupportedOperationException uoe) {
  201. // good
  202. }
  203. rollbackTxn();
  204. }
  205. public void testWrongChildType_Update() throws InstantiationException, IllegalAccessException {
  206. // need a non-txn datasource so we can access multiple entity groups
  207. switchDatasource(PersistenceManagerFactoryName.nontransactional);
  208. SubChild child = SubChild.class.newInstance();
  209. SuperParentWithSuperChild parent = new SuperParentWithSuperChild();
  210. parent.setSuperParentString("a string");
  211. beginTxn();
  212. pm.makePersistent(parent);
  213. commitTxn();
  214. parent = pm.getObjectById(parent.getClass(), parent.getId());
  215. try {
  216. parent.getSuperParentSuperChildren().add(child);
  217. pm.close();
  218. fail("expected exception");
  219. } catch (UnsupportedOperationException uoe) {
  220. // good
  221. }
  222. }
  223. }