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

http://datanucleus-appengine.googlecode.com/ · Java · 335 lines · 280 code · 29 blank · 26 comment · 22 complexity · beeffa73b3789f36dd3bd0fcf3e0c4f1 MD5 · raw file

  1. /*
  2. * /**********************************************************************
  3. * Copyright (c) 2009 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * **********************************************************************/
  17. package com.google.appengine.datanucleus.jdo;
  18. import java.util.Collection;
  19. import org.datanucleus.util.NucleusLogger;
  20. import com.google.appengine.api.datastore.Entity;
  21. import com.google.appengine.api.datastore.EntityNotFoundException;
  22. import com.google.appengine.api.datastore.Key;
  23. import com.google.appengine.api.datastore.KeyFactory;
  24. import com.google.appengine.datanucleus.Utils;
  25. import com.google.appengine.datanucleus.test.jdo.EmbeddedArrayOwner;
  26. import com.google.appengine.datanucleus.test.jdo.EmbeddedChildPC;
  27. import com.google.appengine.datanucleus.test.jdo.EmbeddedCollectionOwner;
  28. import com.google.appengine.datanucleus.test.jdo.EmbeddedParentPC;
  29. import com.google.appengine.datanucleus.test.jdo.EmbeddedRelatedBase;
  30. import com.google.appengine.datanucleus.test.jdo.EmbeddedRelatedSub;
  31. import com.google.appengine.datanucleus.test.jdo.Flight;
  32. import com.google.appengine.datanucleus.test.jdo.HasEmbeddedJDO;
  33. import com.google.appengine.datanucleus.test.jdo.HasEmbeddedPc;
  34. import com.google.appengine.datanucleus.test.jdo.HasEmbeddedWithKeyPkJDO;
  35. import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO;
  36. /**
  37. * @author Max Ross <maxr@google.com>
  38. */
  39. public class JDOEmbeddedTest extends JDOTestCase {
  40. public void testEmbeddedWithGeneratedId() throws EntityNotFoundException {
  41. HasEmbeddedJDO pojo = new HasEmbeddedJDO();
  42. Flight f = new Flight();
  43. f.setId("yarg");
  44. f.setFlightNumber(23);
  45. f.setName("harold");
  46. f.setOrigin("bos");
  47. f.setDest("mia");
  48. f.setYou(24);
  49. f.setMe(25);
  50. pojo.setFlight(f);
  51. Flight f2 = new Flight();
  52. f2.setId("blarg");
  53. f2.setFlightNumber(26);
  54. f2.setName("jimmy");
  55. f2.setOrigin("jfk");
  56. f2.setDest("sea");
  57. f2.setYou(28);
  58. f2.setMe(29);
  59. pojo.setAnotherFlight(f2);
  60. HasEmbeddedJDO.Embedded1 embedded1 = new HasEmbeddedJDO.Embedded1();
  61. pojo.setEmbedded1(embedded1);
  62. embedded1.setVal1("v1");
  63. embedded1.setMultiVal1(Utils.newArrayList("yar1", "yar2"));
  64. HasEmbeddedJDO.Embedded2 embedded2 = new HasEmbeddedJDO.Embedded2();
  65. embedded2.setVal2("v2");
  66. embedded2.setMultiVal2(Utils.newArrayList("bar1", "bar2"));
  67. embedded1.setEmbedded2(embedded2);
  68. beginTxn();
  69. pm.makePersistent(pojo);
  70. commitTxn();
  71. Entity e = ds.get(KeyFactory.createKey(kindForClass(pojo.getClass()), pojo.getId()));
  72. assertTrue(e.hasProperty("flightId")); // Uses column names from embedded mapping
  73. assertTrue(e.hasProperty("origin")); // Uses column names from Flight class since not overridden
  74. assertTrue(e.hasProperty("dest")); // Uses column names from Flight class since not overridden
  75. assertTrue(e.hasProperty("name")); // Uses column names from Flight class since not overridden
  76. assertTrue(e.hasProperty("you")); // Uses column names from Flight class since not overridden
  77. assertTrue(e.hasProperty("me")); // Uses column names from Flight class since not overridden
  78. assertTrue(e.hasProperty("flight_number")); // Uses column names from Flight class since not overridden
  79. assertTrue(e.hasProperty("ID")); // Uses column names from embedded mapping
  80. assertTrue(e.hasProperty("ORIGIN")); // Uses column names from embedded mapping
  81. assertTrue(e.hasProperty("DEST")); // Uses column names from embedded mapping
  82. assertTrue(e.hasProperty("NAME")); // Uses column names from embedded mapping
  83. assertTrue(e.hasProperty("YOU")); // Uses column names from embedded mapping
  84. assertTrue(e.hasProperty("ME")); // Uses column names from embedded mapping
  85. assertTrue(e.hasProperty("FLIGHTNUMBER")); // Uses column names from embedded mapping
  86. assertTrue(e.hasProperty("val1"));
  87. assertTrue(e.hasProperty("multiVal1"));
  88. assertTrue(e.hasProperty("val2"));
  89. assertTrue(e.hasProperty("multiVal2"));
  90. assertEquals(18, e.getProperties().size());
  91. assertEquals(1, countForClass(HasEmbeddedJDO.class));
  92. assertEquals(0, countForClass(Flight.class));
  93. switchDatasource(PersistenceManagerFactoryName.transactional);
  94. beginTxn();
  95. pojo = pm.getObjectById(HasEmbeddedJDO.class, pojo.getId());
  96. assertNotNull(pojo.getFlight());
  97. // it's weird but flight doesn't have an equals() method
  98. assertTrue(f.customEquals(pojo.getFlight()));
  99. assertNotNull(pojo.getAnotherFlight());
  100. assertTrue(f2.customEquals(pojo.getAnotherFlight()));
  101. assertNotNull(pojo.getEmbedded1());
  102. assertEquals("v1", pojo.getEmbedded1().getVal1());
  103. assertEquals(Utils.newArrayList("yar1", "yar2"), pojo.getEmbedded1().getMultiVal1());
  104. assertNotNull(pojo.getEmbedded1().getEmbedded2());
  105. assertEquals("v2", pojo.getEmbedded1().getEmbedded2().getVal2());
  106. assertEquals(Utils.newArrayList("bar1", "bar2"), pojo.getEmbedded1().getEmbedded2().getMultiVal2());
  107. commitTxn();
  108. }
  109. public void testEmbeddedWithKeyPk_NullEmbedded() {
  110. HasEmbeddedWithKeyPkJDO pojo = new HasEmbeddedWithKeyPkJDO();
  111. beginTxn();
  112. pm.makePersistent(pojo);
  113. commitTxn();
  114. pm.evictAll();
  115. pmf.getDataStoreCache().evictAll();
  116. // Failed on GAE v1.x
  117. beginTxn();
  118. pojo = pm.getObjectById(HasEmbeddedWithKeyPkJDO.class, pojo.getId());
  119. assertNull(pojo.getEmbedded());
  120. commitTxn();
  121. }
  122. public void testEmbeddedWithKeyPk_NotNullEmbedded() {
  123. HasEmbeddedWithKeyPkJDO pojo = new HasEmbeddedWithKeyPkJDO();
  124. HasKeyPkJDO embedded = new HasKeyPkJDO();
  125. embedded.setStr("yar");
  126. pojo.setEmbedded(embedded);
  127. beginTxn();
  128. pm.makePersistent(pojo);
  129. commitTxn();
  130. pm.evictAll();
  131. pmf.getDataStoreCache().evictAll();
  132. beginTxn();
  133. pojo = pm.getObjectById(HasEmbeddedWithKeyPkJDO.class, pojo.getId());
  134. assertNotNull(pojo.getEmbedded());
  135. assertEquals("yar", pojo.getEmbedded().getStr());
  136. commitTxn();
  137. }
  138. public void testEmbeddedWithKeyPk_AddEmbeddedToExistingParent() {
  139. HasEmbeddedWithKeyPkJDO pojo = new HasEmbeddedWithKeyPkJDO();
  140. beginTxn();
  141. pm.makePersistent(pojo);
  142. commitTxn();
  143. HasKeyPkJDO embedded = new HasKeyPkJDO();
  144. embedded.setStr("yar");
  145. beginTxn();
  146. pojo.setEmbedded(embedded);
  147. pojo = pm.getObjectById(HasEmbeddedWithKeyPkJDO.class, pojo.getId());
  148. pojo.setEmbedded(embedded);
  149. commitTxn();
  150. }
  151. public void testEmbeddingPC() throws EntityNotFoundException {
  152. HasEmbeddedPc parent = new HasEmbeddedPc();
  153. HasKeyPkJDO embedded = new HasKeyPkJDO();
  154. embedded.setKey(KeyFactory.createKey("blar", 43L));
  155. parent.setEmbedded(embedded);
  156. beginTxn();
  157. pm.makePersistent(parent);
  158. commitTxn();
  159. Entity e = ds.get(parent.getKey());
  160. assertTrue(e.hasProperty("key"));
  161. }
  162. public void testEmbeddingPCWithIdField() {
  163. EmbeddedParentPC pi = new EmbeddedParentPC();
  164. pi.setChild(new EmbeddedChildPC(1, "Hi"));
  165. // Failed on GAE v1.x
  166. pm.currentTransaction().begin();
  167. pm.makePersistent(pi);
  168. pm.currentTransaction().commit();
  169. }
  170. public void testEmbeddedCollection() {
  171. Object id = null;
  172. Key ownerKey = null;
  173. try {
  174. EmbeddedCollectionOwner owner = new EmbeddedCollectionOwner();
  175. EmbeddedRelatedBase baseRel1 = new EmbeddedRelatedBase("First Base", 100);
  176. owner.addChild(baseRel1);
  177. EmbeddedRelatedSub subRel2 = new EmbeddedRelatedSub("Second Base", 200, "Other Type");
  178. owner.addChild(subRel2);
  179. pm.currentTransaction().begin();
  180. pm.makePersistent(owner);
  181. pm.currentTransaction().commit();
  182. id = pm.getObjectId(owner);
  183. ownerKey = owner.getKey();
  184. } catch (Exception e) {
  185. NucleusLogger.PERSISTENCE.error("Exception on persist of embedded collection", e);
  186. fail("Exception occurred on persist of embedded collection : " + e.getMessage());
  187. } finally {
  188. if (pm.currentTransaction().isActive()) {
  189. pm.currentTransaction().rollback();
  190. }
  191. pm.close();
  192. }
  193. pmf.getDataStoreCache().evictAll();
  194. // Check datastore values direct
  195. try {
  196. Entity entity = ds.get(ownerKey);
  197. assertTrue(entity.hasProperty("children.size"));
  198. Object propVal = entity.getProperty("children.size");
  199. assertNotNull(propVal);
  200. long numChildren = (Long)entity.getProperty("children.size");
  201. assertEquals(2, numChildren);
  202. assertTrue(entity.hasProperty("name.0"));
  203. assertTrue(entity.hasProperty("value.0"));
  204. assertTrue(entity.hasProperty("name.1"));
  205. assertTrue(entity.hasProperty("value.1"));
  206. } catch (EntityNotFoundException enfe) {
  207. fail("Failure to retrieve Entity for persisted owner with embedded collection");
  208. }
  209. // Check retrieval
  210. pm = pmf.getPersistenceManager();
  211. try {
  212. pm.currentTransaction().begin();
  213. EmbeddedCollectionOwner owner = (EmbeddedCollectionOwner)pm.getObjectById(id);
  214. Collection<EmbeddedRelatedBase> children = owner.getChildren();
  215. assertEquals(2, children.size());
  216. boolean firstPresent = false;
  217. boolean secondPresent = false;
  218. for (EmbeddedRelatedBase elem : children) {
  219. if (elem.getName().equals("First Base") && elem.getValue() == 100 &&
  220. elem.getClass().getName().equals(EmbeddedRelatedBase.class.getName())) {
  221. firstPresent = true;
  222. } else if (elem.getName().equals("Second Base") && elem.getValue() == 200 &&
  223. elem.getClass().getName().equals(EmbeddedRelatedSub.class.getName())) {
  224. secondPresent = true;
  225. }
  226. }
  227. assertTrue(firstPresent);
  228. assertTrue(secondPresent);
  229. pm.currentTransaction().commit();
  230. } catch (Exception e) {
  231. NucleusLogger.PERSISTENCE.error("Exception on retrieve of embedded collection", e);
  232. fail("Exception occurred on retrieve of embedded collection : " + e.getMessage());
  233. } finally {
  234. if (pm.currentTransaction().isActive()) {
  235. pm.currentTransaction().rollback();
  236. }
  237. }
  238. }
  239. public void testEmbeddedArray() {
  240. Object id = null;
  241. Key ownerKey = null;
  242. try {
  243. EmbeddedArrayOwner owner = new EmbeddedArrayOwner();
  244. EmbeddedRelatedBase baseRel1 = new EmbeddedRelatedBase("First Base", 100);
  245. EmbeddedRelatedSub subRel2 = new EmbeddedRelatedSub("Second Base", 200, "Other Type");
  246. EmbeddedRelatedBase[] array = new EmbeddedRelatedBase[]{baseRel1, subRel2};
  247. owner.setArray(array);
  248. pm.currentTransaction().begin();
  249. pm.makePersistent(owner);
  250. pm.currentTransaction().commit();
  251. id = pm.getObjectId(owner);
  252. ownerKey = owner.getKey();
  253. } catch (Exception e) {
  254. NucleusLogger.PERSISTENCE.error("Exception on persist of embedded array", e);
  255. fail("Exception occurred on persist of embedded array : " + e.getMessage());
  256. } finally {
  257. if (pm.currentTransaction().isActive()) {
  258. pm.currentTransaction().rollback();
  259. }
  260. pm.close();
  261. }
  262. pmf.getDataStoreCache().evictAll();
  263. // Check datastore values direct
  264. try {
  265. Entity entity = ds.get(ownerKey);
  266. assertTrue(entity.hasProperty("array.size"));
  267. Object propVal = entity.getProperty("array.size");
  268. assertNotNull(propVal);
  269. long numChildren = (Long)entity.getProperty("array.size");
  270. assertEquals(2, numChildren);
  271. assertTrue(entity.hasProperty("name.0"));
  272. assertTrue(entity.hasProperty("value.0"));
  273. assertTrue(entity.hasProperty("name.1"));
  274. assertTrue(entity.hasProperty("value.1"));
  275. } catch (EntityNotFoundException enfe) {
  276. fail("Failure to retrieve Entity for persisted owner with embedded array");
  277. }
  278. // Check retrieval
  279. pm = pmf.getPersistenceManager();
  280. try {
  281. pm.currentTransaction().begin();
  282. EmbeddedArrayOwner owner = (EmbeddedArrayOwner)pm.getObjectById(id);
  283. EmbeddedRelatedBase[] array = owner.getArray();
  284. assertEquals(2, array.length);
  285. for (int i=0;i<array.length;i++) {
  286. if (i == 0) {
  287. assertTrue("First element incorrect",
  288. array[i].getName().equals("First Base") && array[i].getValue() == 100 &&
  289. array[i].getClass().getName().equals(EmbeddedRelatedBase.class.getName()));
  290. } else if (i == 1) {
  291. assertTrue("Second element incorrect",
  292. array[i].getName().equals("Second Base") && array[i].getValue() == 200 &&
  293. array[i].getClass().getName().equals(EmbeddedRelatedSub.class.getName()));
  294. }
  295. }
  296. pm.currentTransaction().commit();
  297. } catch (Exception e) {
  298. NucleusLogger.PERSISTENCE.error("Exception on retrieve of embedded array", e);
  299. fail("Exception occurred on retrieve of embedded array : " + e.getMessage());
  300. } finally {
  301. if (pm.currentTransaction().isActive()) {
  302. pm.currentTransaction().rollback();
  303. }
  304. }
  305. }
  306. }