PageRenderTime 24ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 389 lines | 287 code | 58 blank | 44 comment | 0 complexity | 9f60c215fc55c6e362c479b5503bda41 MD5 | raw file
Possible License(s): Apache-2.0
  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.jpa;
  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.jpa.BidirectionalOneToManySubclassesJPA.Example1;
  21. import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example2;
  22. import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example3;
  23. import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example4;
  24. import java.util.Collections;
  25. /**
  26. * @author Max Ross <max.ross@gmail.com>
  27. */
  28. public class JPABidirectionalOneToManySubclassTest extends JPATestCase {
  29. public void testExample1Subclass() throws EntityNotFoundException {
  30. // insertion
  31. Example1.B parent = new Example1.B();
  32. parent.setAString("a string");
  33. parent.setBString("b string");
  34. Example1.X child = new Example1.X();
  35. child.setXString("x string");
  36. parent.getChildren().add(child);
  37. beginTxn();
  38. em.persist(parent);
  39. commitTxn();
  40. Entity parentEntity =
  41. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  42. Entity childEntity = ds.get(child.getId());
  43. assertEquals(3, parentEntity.getProperties().size());
  44. assertEquals("a string", parentEntity.getProperty("aString"));
  45. assertEquals("b string", parentEntity.getProperty("bString"));
  46. assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children"));
  47. assertEquals(1, childEntity.getProperties().size());
  48. assertEquals("x string", childEntity.getProperty("xString"));
  49. // lookup
  50. beginTxn();
  51. parent = em.find(parent.getClass(), parent.getId());
  52. assertEquals("a string", parent.getAString());
  53. assertEquals("b string", parent.getBString());
  54. assertEquals(1, parent.getChildren().size());
  55. assertEquals(child.getId(), parent.getChildren().get(0).getId());
  56. commitTxn();
  57. beginTxn();
  58. child = em.find(child.getClass(), child.getId());
  59. assertEquals("x string", child.getXString());
  60. commitTxn();
  61. // cascade delete
  62. beginTxn();
  63. em.remove(em.merge(parent));
  64. commitTxn();
  65. assertEquals(0, countForClass(parent.getClass()));
  66. assertEquals(0, countForClass(child.getClass()));
  67. }
  68. public void testExample1Superclass() throws EntityNotFoundException {
  69. // insertion
  70. Example1.A parent = new Example1.A();
  71. parent.setAString("a string");
  72. Example1.X child = new Example1.X();
  73. child.setXString("x string");
  74. parent.getChildren().add(child);
  75. beginTxn();
  76. em.persist(parent);
  77. commitTxn();
  78. Entity parentEntity =
  79. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  80. Entity childEntity = ds.get(child.getId());
  81. assertEquals(2, parentEntity.getProperties().size());
  82. assertEquals("a string", parentEntity.getProperty("aString"));
  83. assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children"));
  84. assertEquals(1, childEntity.getProperties().size());
  85. assertEquals("x string", childEntity.getProperty("xString"));
  86. // lookup
  87. beginTxn();
  88. parent = em.find(parent.getClass(), parent.getId());
  89. assertEquals("a string", parent.getAString());
  90. assertEquals(1, parent.getChildren().size());
  91. assertEquals(child.getId(), parent.getChildren().get(0).getId());
  92. commitTxn();
  93. beginTxn();
  94. child = em.find(child.getClass(), child.getId());
  95. assertEquals("x string", child.getXString());
  96. commitTxn();
  97. // cascade delete
  98. beginTxn();
  99. em.remove(em.merge(parent));
  100. commitTxn();
  101. assertEquals(0, countForClass(parent.getClass()));
  102. assertEquals(0, countForClass(child.getClass()));
  103. }
  104. public void testExample2Subclass() throws EntityNotFoundException {
  105. // insertion
  106. Example2.B parent = new Example2.B();
  107. parent.setAString("a string");
  108. parent.setBString("b string");
  109. Example2.Y child = new Example2.Y();
  110. child.setXString("x string");
  111. child.setYString("y string");
  112. parent.getChildren().add(child);
  113. beginTxn();
  114. em.persist(parent);
  115. commitTxn();
  116. Entity parentEntity =
  117. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  118. Entity childEntity = ds.get(child.getId());
  119. assertEquals(3, parentEntity.getProperties().size());
  120. assertEquals("a string", parentEntity.getProperty("aString"));
  121. assertEquals("b string", parentEntity.getProperty("bString"));
  122. assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children"));
  123. assertEquals(2, childEntity.getProperties().size());
  124. assertEquals("x string", childEntity.getProperty("xString"));
  125. assertEquals("y string", childEntity.getProperty("yString"));
  126. // lookup
  127. beginTxn();
  128. parent = em.find(parent.getClass(), parent.getId());
  129. assertEquals("a string", parent.getAString());
  130. assertEquals("b string", parent.getBString());
  131. assertEquals(1, parent.getChildren().size());
  132. assertEquals(child.getId(), parent.getChildren().get(0).getId());
  133. commitTxn();
  134. beginTxn();
  135. child = em.find(child.getClass(), child.getId());
  136. assertEquals("x string", child.getXString());
  137. assertEquals("y string", child.getYString());
  138. commitTxn();
  139. // cascade delete
  140. beginTxn();
  141. em.remove(em.merge(parent));
  142. commitTxn();
  143. assertEquals(0, countForClass(parent.getClass()));
  144. assertEquals(0, countForClass(child.getClass()));
  145. }
  146. public void testExample2Superclass() throws EntityNotFoundException {
  147. // insertion
  148. Example2.A parent = new Example2.A();
  149. parent.setAString("a string");
  150. beginTxn();
  151. em.persist(parent);
  152. commitTxn();
  153. Entity parentEntity =
  154. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  155. assertEquals(1, parentEntity.getProperties().size());
  156. assertEquals("a string", parentEntity.getProperty("aString"));
  157. // lookup
  158. beginTxn();
  159. parent = em.find(parent.getClass(), parent.getId());
  160. assertEquals("a string", parent.getAString());
  161. commitTxn();
  162. // delete
  163. beginTxn();
  164. em.remove(em.merge(parent));
  165. commitTxn();
  166. assertEquals(0, countForClass(parent.getClass()));
  167. }
  168. public void testExample3Subclass() throws EntityNotFoundException {
  169. // insertion
  170. Example3.B parent = new Example3.B();
  171. parent.setAString("a string");
  172. parent.setBString("b string");
  173. Example3.Y child = new Example3.Y();
  174. child.setXString("x string");
  175. child.setYString("y string");
  176. parent.getChildren().add(child);
  177. beginTxn();
  178. em.persist(parent);
  179. commitTxn();
  180. Entity parentEntity =
  181. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  182. Entity childEntity = ds.get(child.getId());
  183. assertEquals(3, parentEntity.getProperties().size());
  184. assertEquals("a string", parentEntity.getProperty("aString"));
  185. assertEquals("b string", parentEntity.getProperty("bString"));
  186. assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children"));
  187. assertEquals(2, childEntity.getProperties().size());
  188. assertEquals("x string", childEntity.getProperty("xString"));
  189. assertEquals("y string", childEntity.getProperty("yString"));
  190. // lookup
  191. beginTxn();
  192. parent = em.find(parent.getClass(), parent.getId());
  193. assertEquals("a string", parent.getAString());
  194. assertEquals("b string", parent.getBString());
  195. assertEquals(1, parent.getChildren().size());
  196. assertEquals(child.getId(), parent.getChildren().get(0).getId());
  197. commitTxn();
  198. beginTxn();
  199. child = em.find(child.getClass(), child.getId());
  200. assertEquals("x string", child.getXString());
  201. assertEquals("y string", child.getYString());
  202. commitTxn();
  203. // cascade delete
  204. beginTxn();
  205. em.remove(em.merge(parent));
  206. commitTxn();
  207. assertEquals(0, countForClass(parent.getClass()));
  208. assertEquals(0, countForClass(child.getClass()));
  209. }
  210. public void testExample3Superclass() throws EntityNotFoundException {
  211. // insertion
  212. Example3.A parent = new Example3.A();
  213. parent.setAString("a string");
  214. beginTxn();
  215. em.persist(parent);
  216. commitTxn();
  217. Entity parentEntity =
  218. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  219. assertEquals(1, parentEntity.getProperties().size());
  220. assertEquals("a string", parentEntity.getProperty("aString"));
  221. // lookup
  222. beginTxn();
  223. parent = em.find(parent.getClass(), parent.getId());
  224. assertEquals("a string", parent.getAString());
  225. commitTxn();
  226. // delete
  227. beginTxn();
  228. em.remove(em.merge(parent));
  229. commitTxn();
  230. assertEquals(0, countForClass(parent.getClass()));
  231. }
  232. public void testExample4Subclass() throws EntityNotFoundException {
  233. // insertion
  234. Example4.B parent = new Example4.B();
  235. parent.setAString("a string");
  236. parent.setBString("b string");
  237. Example4.Y child = new Example4.Y();
  238. child.setXString("x string");
  239. child.setYString("y string");
  240. parent.getChildren().add(child);
  241. beginTxn();
  242. em.persist(parent);
  243. commitTxn();
  244. Entity parentEntity =
  245. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  246. Entity childEntity = ds.get(child.getId());
  247. assertEquals(3, parentEntity.getProperties().size());
  248. assertEquals("a string", parentEntity.getProperty("aString"));
  249. assertEquals("b string", parentEntity.getProperty("bString"));
  250. assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children"));
  251. assertEquals(2, childEntity.getProperties().size());
  252. assertEquals("x string", childEntity.getProperty("xString"));
  253. assertEquals("y string", childEntity.getProperty("yString"));
  254. // lookup
  255. beginTxn();
  256. parent = em.find(parent.getClass(), parent.getId());
  257. assertEquals("a string", parent.getAString());
  258. assertEquals("b string", parent.getBString());
  259. assertEquals(1, parent.getChildren().size());
  260. assertEquals(child.getId(), parent.getChildren().get(0).getId());
  261. commitTxn();
  262. beginTxn();
  263. child = em.find(child.getClass(), child.getId());
  264. assertEquals("x string", child.getXString());
  265. assertEquals("y string", child.getYString());
  266. commitTxn();
  267. // cascade delete
  268. beginTxn();
  269. em.remove(em.merge(parent));
  270. commitTxn();
  271. assertEquals(0, countForClass(parent.getClass()));
  272. assertEquals(0, countForClass(child.getClass()));
  273. }
  274. public void testExample4Superclass() throws EntityNotFoundException {
  275. // insertion
  276. Example4.A parent = new Example4.A();
  277. parent.setAString("a string");
  278. beginTxn();
  279. em.persist(parent);
  280. commitTxn();
  281. Entity parentEntity =
  282. ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
  283. assertEquals(2, parentEntity.getProperties().size());
  284. assertEquals("a string", parentEntity.getProperty("aString"));
  285. assertTrue(parentEntity.hasProperty("children"));
  286. assertNull(parentEntity.getProperty("children"));
  287. // lookup
  288. beginTxn();
  289. parent = em.find(parent.getClass(), parent.getId());
  290. assertEquals("a string", parent.getAString());
  291. commitTxn();
  292. // delete
  293. beginTxn();
  294. em.remove(em.merge(parent));
  295. commitTxn();
  296. assertEquals(0, countForClass(parent.getClass()));
  297. }
  298. public void testWrongChildType() {
  299. Example1.A parent = new Example1.A();
  300. parent.setAString("a string");
  301. Example1.Y child = new Example1.Y();
  302. child.setXString("x string");
  303. parent.getChildren().add(child);
  304. beginTxn();
  305. em.persist(parent);
  306. try {
  307. commitTxn();
  308. fail("expected exception");
  309. } catch (UnsupportedOperationException uoe) {
  310. // good
  311. }
  312. }
  313. public void testWrongChildType_Update() {
  314. Example1.A parent = new Example1.A();
  315. parent.setAString("a string");
  316. beginTxn();
  317. em.persist(parent);
  318. commitTxn();
  319. beginTxn();
  320. parent = em.find(parent.getClass(), parent.getId());
  321. Example1.Y child = new Example1.Y();
  322. child.setXString("x string");
  323. parent.getChildren().add(child);
  324. try {
  325. commitTxn();
  326. fail("expected exception");
  327. } catch (UnsupportedOperationException uoe) {
  328. // good
  329. }
  330. }
  331. }