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

http://datanucleus-appengine.googlecode.com/ · Java · 308 lines · 223 code · 47 blank · 38 comment · 0 complexity · 7f799bf3d3cb141f76008245fadcc1b8 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.BidirectionalOneToOneSubclassesJDO.Example1;
  21. import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToOneSubclassesJDO.Example2;
  22. import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToOneSubclassesJDO.Example3;
  23. /**
  24. * @author Max Ross <max.ross@gmail.com>
  25. */
  26. public class JDOBidirectionalOneToOneSubclassTest extends JDOTestCase {
  27. public void testExample1Subclass() throws EntityNotFoundException {
  28. // insertion
  29. Example1.B parent = new Example1.B();
  30. parent.setAString("a string");
  31. parent.setBString("b string");
  32. Example1.X child = new Example1.X();
  33. child.setXString("x string");
  34. parent.setChild(child);
  35. beginTxn();
  36. pm.makePersistent(parent);
  37. commitTxn();
  38. Entity parentEntity =
  39. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  40. Entity childEntity = ds.get(child.getId());
  41. assertEquals(3, parentEntity.getProperties().size());
  42. assertEquals("a string", parentEntity.getProperty("aString"));
  43. assertEquals("b string", parentEntity.getProperty("bString"));
  44. assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
  45. assertEquals(1, childEntity.getProperties().size());
  46. assertEquals("x string", childEntity.getProperty("xString"));
  47. // lookup
  48. beginTxn();
  49. parent = pm.getObjectById(parent.getClass(), parent.getId());
  50. assertEquals("a string", parent.getAString());
  51. assertEquals("b string", parent.getBString());
  52. assertEquals(child.getId(), parent.getChild().getId());
  53. commitTxn();
  54. beginTxn();
  55. child = pm.getObjectById(child.getClass(), child.getId());
  56. assertEquals("x string", child.getXString());
  57. commitTxn();
  58. // cascade delete
  59. beginTxn();
  60. pm.deletePersistent(parent);
  61. commitTxn();
  62. assertEquals(0, countForClass(parent.getClass()));
  63. assertEquals(0, countForClass(child.getClass()));
  64. }
  65. public void testExample1Superclass() throws EntityNotFoundException {
  66. // insertion
  67. Example1.A parent = new Example1.A();
  68. parent.setAString("a string");
  69. Example1.X child = new Example1.X();
  70. child.setXString("x string");
  71. parent.setChild(child);
  72. beginTxn();
  73. pm.makePersistent(parent);
  74. commitTxn();
  75. Entity parentEntity =
  76. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  77. Entity childEntity = ds.get(child.getId());
  78. assertEquals(2, parentEntity.getProperties().size());
  79. assertEquals("a string", parentEntity.getProperty("aString"));
  80. assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
  81. assertEquals(1, childEntity.getProperties().size());
  82. assertEquals("x string", childEntity.getProperty("xString"));
  83. // lookup
  84. beginTxn();
  85. parent = pm.getObjectById(parent.getClass(), parent.getId());
  86. assertEquals("a string", parent.getAString());
  87. assertEquals(child.getId(), parent.getChild().getId());
  88. commitTxn();
  89. beginTxn();
  90. child = pm.getObjectById(child.getClass(), child.getId());
  91. assertEquals("x string", child.getXString());
  92. commitTxn();
  93. // cascade delete
  94. beginTxn();
  95. pm.deletePersistent(parent);
  96. commitTxn();
  97. assertEquals(0, countForClass(parent.getClass()));
  98. assertEquals(0, countForClass(child.getClass()));
  99. }
  100. public void testExample3Subclass() throws EntityNotFoundException {
  101. // insertion
  102. Example2.B parent = new Example2.B();
  103. parent.setAString("a string");
  104. parent.setBString("b string");
  105. Example2.Y child = new Example2.Y();
  106. child.setXString("x string");
  107. child.setYString("y string");
  108. parent.setChild(child);
  109. beginTxn();
  110. pm.makePersistent(parent);
  111. commitTxn();
  112. Entity parentEntity =
  113. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  114. Entity childEntity = ds.get(child.getId());
  115. assertEquals(3, parentEntity.getProperties().size());
  116. assertEquals("a string", parentEntity.getProperty("aString"));
  117. assertEquals("b string", parentEntity.getProperty("bString"));
  118. assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
  119. assertEquals(2, childEntity.getProperties().size());
  120. assertEquals("x string", childEntity.getProperty("xString"));
  121. assertEquals("y string", childEntity.getProperty("yString"));
  122. // lookup
  123. beginTxn();
  124. parent = pm.getObjectById(parent.getClass(), parent.getId());
  125. assertEquals("a string", parent.getAString());
  126. assertEquals("b string", parent.getBString());
  127. assertEquals(child.getId(), parent.getChild().getId());
  128. commitTxn();
  129. beginTxn();
  130. child = pm.getObjectById(child.getClass(), child.getId());
  131. assertEquals("x string", child.getXString());
  132. assertEquals("y string", child.getYString());
  133. commitTxn();
  134. // cascade delete
  135. beginTxn();
  136. pm.deletePersistent(parent);
  137. commitTxn();
  138. assertEquals(0, countForClass(parent.getClass()));
  139. assertEquals(0, countForClass(child.getClass()));
  140. }
  141. public void testExample3Superclass() throws EntityNotFoundException {
  142. // insertion
  143. Example2.A parent = new Example2.A();
  144. parent.setAString("a string");
  145. beginTxn();
  146. pm.makePersistent(parent);
  147. commitTxn();
  148. Entity parentEntity =
  149. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  150. assertEquals(1, parentEntity.getProperties().size());
  151. assertEquals("a string", parentEntity.getProperty("aString"));
  152. // lookup
  153. beginTxn();
  154. parent = pm.getObjectById(parent.getClass(), parent.getId());
  155. assertEquals("a string", parent.getAString());
  156. commitTxn();
  157. // delete
  158. beginTxn();
  159. pm.deletePersistent(parent);
  160. commitTxn();
  161. assertEquals(0, countForClass(parent.getClass()));
  162. }
  163. public void testExample4Subclass() throws EntityNotFoundException {
  164. // insertion
  165. Example3.B parent = new Example3.B();
  166. parent.setAString("a string");
  167. parent.setBString("b string");
  168. Example3.Y child = new Example3.Y();
  169. child.setXString("x string");
  170. child.setYString("y string");
  171. parent.setChild(child);
  172. beginTxn();
  173. pm.makePersistent(parent);
  174. commitTxn();
  175. Entity parentEntity =
  176. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  177. Entity childEntity = ds.get(child.getId());
  178. assertEquals(3, parentEntity.getProperties().size());
  179. assertEquals("a string", parentEntity.getProperty("aString"));
  180. assertEquals("b string", parentEntity.getProperty("bString"));
  181. assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
  182. assertEquals(2, childEntity.getProperties().size());
  183. assertEquals("x string", childEntity.getProperty("xString"));
  184. assertEquals("y string", childEntity.getProperty("yString"));
  185. // lookup
  186. beginTxn();
  187. parent = pm.getObjectById(parent.getClass(), parent.getId());
  188. assertEquals("a string", parent.getAString());
  189. assertEquals("b string", parent.getBString());
  190. assertEquals(child.getId(), parent.getChild().getId());
  191. commitTxn();
  192. beginTxn();
  193. child = pm.getObjectById(child.getClass(), child.getId());
  194. assertEquals("x string", child.getXString());
  195. assertEquals("y string", child.getYString());
  196. commitTxn();
  197. // cascade delete
  198. beginTxn();
  199. pm.deletePersistent(parent);
  200. commitTxn();
  201. assertEquals(0, countForClass(parent.getClass()));
  202. assertEquals(0, countForClass(child.getClass()));
  203. }
  204. public void testExample4Superclass() throws EntityNotFoundException {
  205. // insertion
  206. Example3.A parent = new Example3.A();
  207. parent.setAString("a string");
  208. beginTxn();
  209. pm.makePersistent(parent);
  210. commitTxn();
  211. Entity parentEntity =
  212. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  213. assertEquals(2, parentEntity.getProperties().size());
  214. assertEquals("a string", parentEntity.getProperty("aString"));
  215. assertTrue(parentEntity.hasProperty("child_id_OID"));
  216. assertNull(parentEntity.getProperty("child_id_OID"));
  217. // lookup
  218. beginTxn();
  219. parent = pm.getObjectById(parent.getClass(), parent.getId());
  220. assertEquals("a string", parent.getAString());
  221. commitTxn();
  222. // delete
  223. beginTxn();
  224. pm.deletePersistent(parent);
  225. commitTxn();
  226. assertEquals(0, countForClass(parent.getClass()));
  227. }
  228. public void testWrongChildType() {
  229. Example1.A parent = new Example1.A();
  230. parent.setAString("a string");
  231. Example1.Y child = new Example1.Y();
  232. child.setXString("x string");
  233. parent.setChild(child);
  234. beginTxn();
  235. try {
  236. pm.makePersistent(parent);
  237. fail("expected exception");
  238. } catch (UnsupportedOperationException uoe) {
  239. // good
  240. }
  241. rollbackTxn();
  242. }
  243. public void testWrongChildType_Update() {
  244. Example1.A parent = new Example1.A();
  245. parent.setAString("a string");
  246. beginTxn();
  247. pm.makePersistent(parent);
  248. commitTxn();
  249. beginTxn();
  250. parent = pm.getObjectById(parent.getClass(), parent.getId());
  251. Example1.Y child = new Example1.Y();
  252. child.setXString("x string");
  253. parent.setChild(child);
  254. try {
  255. commitTxn();
  256. fail("expected exception");
  257. } catch (UnsupportedOperationException uoe) {
  258. // good
  259. }
  260. }
  261. }