PageRenderTime 44ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 301 lines | 250 code | 24 blank | 27 comment | 0 complexity | 3aab4e25910c6fe626aac8edb4bb8d9b MD5 | raw file
Possible License(s): Apache-2.0
  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.Key;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.api.datastore.Query;
  19. import com.google.appengine.datanucleus.test.jdo.Flight;
  20. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
  21. import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorStringPkJDO;
  22. import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO;
  23. import com.google.appengine.datanucleus.test.jdo.HasStringAncestorKeyPkJDO;
  24. import com.google.appengine.datanucleus.test.jdo.HasStringAncestorStringPkJDO;
  25. import javax.jdo.JDOFatalUserException;
  26. /**
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. public class JDOAncestorTest extends JDOTestCase {
  30. public void testInsert_IdGen() {
  31. Entity flightEntity = Flight.newFlightEntity("max", "bos", "mia", 3, 4);
  32. ds.put(flightEntity);
  33. Key flightKey = flightEntity.getKey();
  34. HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(KeyFactory.keyToString(flightKey));
  35. makePersistentInTxn(ha, TXN_START_END);
  36. Key keyWithParent = KeyFactory.stringToKey(ha.getId());
  37. assertEquals(flightKey, keyWithParent.getParent());
  38. // now we'll issue an ancestor query directly against the datastore and see
  39. // if our object comes back.
  40. Query q = new Query(ha.getClass().getSimpleName());
  41. q.setAncestor(flightKey);
  42. Entity result = ds.prepare(q).asSingleEntity();
  43. assertEquals(flightKey, result.getKey().getParent());
  44. }
  45. public void testInsert_NamedKey() {
  46. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  47. ds.put(flightEntity);
  48. Key flightKey = flightEntity.getKey();
  49. Key key = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), "named key", flightKey).getKey();
  50. HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(null, KeyFactory.keyToString(key));
  51. makePersistentInTxn(ha, TXN_START_END);
  52. Key keyWithParent = KeyFactory.stringToKey(ha.getId());
  53. assertEquals(flightKey, keyWithParent.getParent());
  54. // now we'll issue an ancestor query directly against the datastore and see
  55. // if our object comes back.
  56. Query q = new Query(ha.getClass().getSimpleName());
  57. q.setAncestor(flightKey);
  58. Entity result = ds.prepare(q).asSingleEntity();
  59. assertEquals(flightKey, result.getKey().getParent());
  60. assertEquals("named key", result.getKey().getName());
  61. assertEquals("parent named key", result.getKey().getParent().getName());
  62. }
  63. public void testInsert_SetAncestorAndPk() {
  64. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  65. ds.put(flightEntity);
  66. Key flightKey = flightEntity.getKey();
  67. HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(
  68. KeyFactory.keyToString(flightKey),
  69. KeyFactory.keyToString(KeyFactory.createKey(HasStringAncestorStringPkJDO.class.getSimpleName(), "named key")));
  70. beginTxn();
  71. try {
  72. pm.makePersistent(ha);
  73. fail("expected exception");
  74. } catch (JDOFatalUserException e) {
  75. // good
  76. rollbackTxn();
  77. }
  78. }
  79. public void testFetch() {
  80. Entity flightEntity = Flight.newFlightEntity("max", "bos", "mia", 3, 4);
  81. ds.put(flightEntity);
  82. Entity hasAncestorEntity = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), flightEntity.getKey());
  83. ds.put(hasAncestorEntity);
  84. beginTxn();
  85. HasStringAncestorStringPkJDO ha = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(hasAncestorEntity.getKey()));
  86. assertEquals(KeyFactory.keyToString(flightEntity.getKey()), ha.getAncestorId());
  87. commitTxn();
  88. }
  89. public void testFetchWithNamedKey() {
  90. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  91. ds.put(flightEntity);
  92. Entity hasAncestorEntity =
  93. new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), "named key", flightEntity.getKey());
  94. ds.put(hasAncestorEntity);
  95. beginTxn();
  96. HasStringAncestorStringPkJDO ha = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(hasAncestorEntity.getKey()));
  97. assertEquals(KeyFactory.keyToString(flightEntity.getKey()), ha.getAncestorId());
  98. assertEquals("named key", KeyFactory.stringToKey(ha.getId()).getName());
  99. assertEquals("parent named key", KeyFactory.stringToKey(ha.getId()).getParent().getName());
  100. commitTxn();
  101. }
  102. public void testInsertWithNullAncestor() {
  103. HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(null);
  104. makePersistentInTxn(ha, TXN_START_END);
  105. Key keyWithParent = KeyFactory.stringToKey(ha.getId());
  106. assertNull(keyWithParent.getParent());
  107. }
  108. public void testKeyPKKeyAncestor_NamedKey() throws EntityNotFoundException {
  109. HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
  110. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  111. ds.put(flightEntity);
  112. Key flightKey = flightEntity.getKey();
  113. Key pojoKey = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), "child named key", flightKey).getKey();
  114. pojo.setKey(pojoKey);
  115. beginTxn();
  116. pm.makePersistent(pojo);
  117. commitTxn();
  118. ds.get(pojoKey);
  119. beginTxn();
  120. pojo = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, pojoKey);
  121. assertEquals(pojo.getAncestorKey(), pojoKey.getParent());
  122. commitTxn();
  123. }
  124. public void testKeyPKKeyAncestor_NamedKeyWrongKind() throws EntityNotFoundException {
  125. HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
  126. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  127. ds.put(flightEntity);
  128. Key flightKey = flightEntity.getKey();
  129. Key pojoKey = new Entity("blarg", "child named key", flightKey).getKey();
  130. pojo.setKey(pojoKey);
  131. beginTxn();
  132. try {
  133. pm.makePersistent(pojo);
  134. fail("expected exception");
  135. } catch (JDOFatalUserException e) {
  136. // good
  137. rollbackTxn();
  138. }
  139. }
  140. public void testKeyPKKeyAncestor_IdGen() throws EntityNotFoundException {
  141. HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
  142. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  143. ds.put(flightEntity);
  144. Key flightKey = flightEntity.getKey();
  145. pojo.setAncestorKey(flightKey);
  146. beginTxn();
  147. pm.makePersistent(pojo);
  148. commitTxn();
  149. ds.get(pojo.getKey());
  150. beginTxn();
  151. pojo = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, pojo.getKey());
  152. assertEquals(pojo.getAncestorKey(), pojo.getKey().getParent());
  153. commitTxn();
  154. }
  155. public void testKeyPKKeyAncestor_SetAncestorAndKey() throws EntityNotFoundException {
  156. HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
  157. Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
  158. ds.put(flightEntity);
  159. Key flightKey = flightEntity.getKey();
  160. pojo.setAncestorKey(flightKey);
  161. Key pojoKey = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), "child named key", flightKey).getKey();
  162. pojo.setKey(pojoKey);
  163. beginTxn();
  164. try {
  165. pm.makePersistent(pojo);
  166. fail("expected exception");
  167. } catch (JDOFatalUserException e) {
  168. // good
  169. rollbackTxn();
  170. }
  171. }
  172. public void testInsertWithKeyPkAndAncestor() throws EntityNotFoundException {
  173. Entity e = new Entity("yam");
  174. ds.put(e);
  175. HasKeyPkJDO hk1 = new HasKeyPkJDO();
  176. hk1.setAncestorKey(e.getKey());
  177. beginTxn();
  178. pm.makePersistent(hk1);
  179. Key key = hk1.getKey();
  180. Key ancestorKey = hk1.getAncestorKey();
  181. assertNotNull(key);
  182. commitTxn();
  183. Entity reloaded = ds.get(hk1.getKey());
  184. assertEquals(ancestorKey, reloaded.getKey().getParent());
  185. }
  186. public void testInsertWithKeyPkAndStringAncestor_IdGen() throws EntityNotFoundException {
  187. Entity e = new Entity("yam");
  188. ds.put(e);
  189. HasStringAncestorKeyPkJDO hk1 = new HasStringAncestorKeyPkJDO();
  190. hk1.setAncestorKey(KeyFactory.keyToString(e.getKey()));
  191. beginTxn();
  192. pm.makePersistent(hk1);
  193. Key key = hk1.getKey();
  194. String ancestorKey = hk1.getAncestorKey();
  195. commitTxn();
  196. Entity reloaded = ds.get(key);
  197. assertEquals(ancestorKey, KeyFactory.keyToString(reloaded.getKey().getParent()));
  198. }
  199. public void testInsertWithKeyPkAndStringAncestor_NamedKey() throws EntityNotFoundException {
  200. Entity e = new Entity("yam");
  201. ds.put(e);
  202. HasStringAncestorKeyPkJDO hk1 = new HasStringAncestorKeyPkJDO();
  203. Key key = new Entity(HasStringAncestorKeyPkJDO.class.getSimpleName(), "named key", e.getKey()).getKey();
  204. hk1.setKey(key);
  205. beginTxn();
  206. pm.makePersistent(hk1);
  207. assertEquals(e.getKey(), KeyFactory.stringToKey(hk1.getAncestorKey()));
  208. String ancestorKey = hk1.getAncestorKey();
  209. commitTxn();
  210. Entity reloaded = ds.get(key);
  211. assertEquals(ancestorKey, KeyFactory.keyToString(reloaded.getKey().getParent()));
  212. }
  213. public void testInsertWithKeyPkAndStringAncestor_SetKeyAndAncestor() throws EntityNotFoundException {
  214. Entity e = new Entity("yam");
  215. ds.put(e);
  216. HasStringAncestorKeyPkJDO hk1 = new HasStringAncestorKeyPkJDO();
  217. Key key = KeyFactory.createKey(HasStringAncestorKeyPkJDO.class.getSimpleName(), "named key");
  218. hk1.setKey(key);
  219. hk1.setAncestorKey(KeyFactory.keyToString(e.getKey()));
  220. beginTxn();
  221. try {
  222. pm.makePersistent(hk1);
  223. fail("expected exception");
  224. } catch (JDOFatalUserException ex) {
  225. // good
  226. rollbackTxn();
  227. }
  228. }
  229. public void testInsertWithStringPkAndKeyAncestor_IdGen() throws EntityNotFoundException {
  230. Entity e = new Entity("yam");
  231. ds.put(e);
  232. HasKeyAncestorStringPkJDO hk1 = new HasKeyAncestorStringPkJDO();
  233. hk1.setAncestorKey(e.getKey());
  234. beginTxn();
  235. pm.makePersistent(hk1);
  236. String key = hk1.getKey();
  237. Key ancestorKey = hk1.getAncestorKey();
  238. commitTxn();
  239. Entity reloaded = ds.get(KeyFactory.stringToKey(key));
  240. assertEquals(ancestorKey, reloaded.getKey().getParent());
  241. }
  242. public void testInsertWithStringPkAndKeyAncestor_NamedKey() throws EntityNotFoundException {
  243. Entity e = new Entity("yam");
  244. ds.put(e);
  245. HasKeyAncestorStringPkJDO hk1 = new HasKeyAncestorStringPkJDO();
  246. Key keyToSet =
  247. new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), "yar", e.getKey()).getKey();
  248. hk1.setKey(KeyFactory.keyToString(keyToSet));
  249. beginTxn();
  250. pm.makePersistent(hk1);
  251. String key = hk1.getKey();
  252. assertEquals(e.getKey(), hk1.getAncestorKey());
  253. commitTxn();
  254. Entity reloaded = ds.get(KeyFactory.stringToKey(key));
  255. assertEquals(e.getKey(), reloaded.getKey().getParent());
  256. }
  257. public void testInsertWithStringPkAndKeyAncestor_SetAncestorAndPk() throws EntityNotFoundException {
  258. Entity parentEntity = new Entity("yam");
  259. ds.put(parentEntity);
  260. HasKeyAncestorStringPkJDO hk1 = new HasKeyAncestorStringPkJDO();
  261. Key keyToSet =
  262. new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), "yar", parentEntity.getKey()).getKey();
  263. hk1.setKey(KeyFactory.keyToString(keyToSet));
  264. hk1.setAncestorKey(keyToSet);
  265. beginTxn();
  266. try {
  267. pm.makePersistent(hk1);
  268. fail("expected exception");
  269. } catch (JDOFatalUserException e) {
  270. // good
  271. rollbackTxn();
  272. }
  273. }
  274. }