PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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