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

http://datanucleus-appengine.googlecode.com/ · Java · 130 lines · 100 code · 11 blank · 19 comment · 0 complexity · de569a9a6b5baea458748e16fb3f9637 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.jpa;
  18. import java.util.Set;
  19. import com.google.appengine.api.datastore.Entity;
  20. import com.google.appengine.api.datastore.EntityNotFoundException;
  21. import com.google.appengine.api.datastore.KeyFactory;
  22. import com.google.appengine.datanucleus.Utils;
  23. import com.google.appengine.datanucleus.test.jpa.EmbeddableJPA;
  24. import com.google.appengine.datanucleus.test.jpa.HasEmbeddableCollection;
  25. import com.google.appengine.datanucleus.test.jpa.HasEmbeddedJPA;
  26. /**
  27. * @author Max Ross <maxr@google.com>
  28. */
  29. public class JPAEmbeddedTest extends JPATestCase {
  30. public void testEmbedded() throws EntityNotFoundException {
  31. HasEmbeddedJPA pojo = new HasEmbeddedJPA();
  32. EmbeddableJPA embedded = new EmbeddableJPA();
  33. embedded.setEmbeddedString("yar");
  34. embedded.setMultiVal(Utils.newArrayList("m1", "m2"));
  35. pojo.setEmbeddable(embedded);
  36. embedded = new EmbeddableJPA();
  37. embedded.setEmbeddedString("yar2");
  38. embedded.setMultiVal(Utils.newArrayList("m3", "m4"));
  39. pojo.setEmbeddable2(embedded);
  40. beginTxn();
  41. em.persist(pojo);
  42. commitTxn();
  43. assertEquals(1, countForClass(HasEmbeddedJPA.class));
  44. Entity e = ds.get(KeyFactory.createKey(pojo.getClass().getSimpleName(), pojo.getId()));
  45. assertEquals("yar", e.getProperty("embeddedString"));
  46. assertEquals(Utils.newArrayList("m1", "m2"), e.getProperty("multiVal"));
  47. assertEquals("yar2", e.getProperty("EMBEDDEDSTRING"));
  48. assertEquals(Utils.newArrayList("m3", "m4"), e.getProperty("MULTIVAL"));
  49. beginTxn();
  50. pojo = em.find(HasEmbeddedJPA.class, pojo.getId());
  51. assertNotNull(pojo.getEmbeddable());
  52. assertEquals("yar", pojo.getEmbeddable().getEmbeddedString());
  53. assertEquals(Utils.newArrayList("m1", "m2"), pojo.getEmbeddable().getMultiVal());
  54. assertEquals("yar2", pojo.getEmbeddable2().getEmbeddedString());
  55. assertEquals(Utils.newArrayList("m3", "m4"), pojo.getEmbeddable2().getMultiVal());
  56. commitTxn();
  57. }
  58. public void testEmbeddedWithKeyPk_NullEmbedded() {
  59. HasEmbeddedJPA pojo = new HasEmbeddedJPA();
  60. beginTxn();
  61. em.persist(pojo);
  62. commitTxn();
  63. em.clear();
  64. beginTxn();
  65. pojo = em.find(HasEmbeddedJPA.class, pojo.getId());
  66. assertNull(pojo.getEmbeddable());
  67. assertNull(pojo.getEmbeddable2());
  68. commitTxn();
  69. }
  70. public void testEmbeddedWithKeyPk_AddEmbeddedToExistingParent() {
  71. HasEmbeddedJPA pojo = new HasEmbeddedJPA();
  72. beginTxn();
  73. em.persist(pojo);
  74. commitTxn();
  75. EmbeddableJPA embeddable = new EmbeddableJPA();
  76. embeddable.setEmbeddedString("yar");
  77. EmbeddableJPA embeddable2 = new EmbeddableJPA();
  78. embeddable2.setEmbeddedString("yar2");
  79. beginTxn();
  80. pojo.setEmbeddable(embeddable);
  81. pojo.setEmbeddable2(embeddable2);
  82. pojo = em.find(HasEmbeddedJPA.class, pojo.getId()); // Finds it in L1 cache from first txn (but with fields set now)
  83. assertNotNull(pojo.getEmbeddable());
  84. assertNotNull(pojo.getEmbeddable().getEmbeddedString());
  85. assertNotNull(pojo.getEmbeddable2());
  86. assertNotNull(pojo.getEmbeddable2().getEmbeddedString());
  87. commitTxn(); // Flushes the changes to embeddable fields
  88. beginTxn();
  89. pojo = em.find(HasEmbeddedJPA.class, pojo.getId());
  90. assertNotNull(pojo.getEmbeddable());
  91. assertEquals("yar", pojo.getEmbeddable().getEmbeddedString());
  92. assertNotNull(pojo.getEmbeddable2());
  93. assertEquals("yar2", pojo.getEmbeddable2().getEmbeddedString());
  94. commitTxn();
  95. }
  96. public void testEmbeddedCollectionOfEmbeddables() {
  97. HasEmbeddableCollection pojo = new HasEmbeddableCollection();
  98. EmbeddableJPA emb1 = new EmbeddableJPA();
  99. emb1.setEmbeddedString("The String1");
  100. pojo.getTheSet().add(emb1);
  101. EmbeddableJPA emb2 = new EmbeddableJPA();
  102. emb2.setEmbeddedString("The String2");
  103. pojo.getTheSet().add(emb2);
  104. beginTxn();
  105. em.persist(pojo);
  106. commitTxn();
  107. Long pk = pojo.getId();
  108. em.close();
  109. em = emf.createEntityManager();
  110. beginTxn();
  111. HasEmbeddableCollection thePojo = em.find(HasEmbeddableCollection.class, pk);
  112. assertNotNull(thePojo);
  113. Set<EmbeddableJPA> theEmbColl = thePojo.getTheSet();
  114. assertNotNull(thePojo);
  115. assertEquals("size of embedded collection is wrong", 2, theEmbColl.size());
  116. commitTxn();
  117. em.close();
  118. }
  119. }