PageRenderTime 89ms CodeModel.GetById 19ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://datanucleus-appengine.googlecode.com/
Java | 821 lines | 690 code | 105 blank | 26 comment | 0 complexity | edd416b0d268427c63585ba0cab52e2a MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.jpa;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.api.datastore.EntityNotFoundException;
 20import com.google.appengine.api.datastore.Key;
 21import com.google.appengine.api.datastore.KeyFactory;
 22import com.google.appengine.datanucleus.TestUtils;
 23import com.google.appengine.datanucleus.Utils;
 24import com.google.appengine.datanucleus.test.jpa.BidirectionalChildListJPA;
 25import com.google.appengine.datanucleus.test.jpa.BidirectionalChildLongPkListJPA;
 26import com.google.appengine.datanucleus.test.jpa.BidirectionalChildStringPkListJPA;
 27import com.google.appengine.datanucleus.test.jpa.BidirectionalChildUnencodedStringPkListJPA;
 28import com.google.appengine.datanucleus.test.jpa.Book;
 29import com.google.appengine.datanucleus.test.jpa.HasChildWithSeparateNameFieldJPA;
 30import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkSeparateNameFieldJPA;
 31import com.google.appengine.datanucleus.test.jpa.HasKeyPkJPA;
 32import com.google.appengine.datanucleus.test.jpa.HasLongPkOneToManyBidirChildrenJPA;
 33import com.google.appengine.datanucleus.test.jpa.HasOneToManyChildAtMultipleLevelsJPA;
 34import com.google.appengine.datanucleus.test.jpa.HasOneToManyKeyPkListJPA;
 35import com.google.appengine.datanucleus.test.jpa.HasOneToManyListJPA;
 36import com.google.appengine.datanucleus.test.jpa.HasOneToManyLongPkListJPA;
 37import com.google.appengine.datanucleus.test.jpa.HasOneToManyStringPkListJPA;
 38import com.google.appengine.datanucleus.test.jpa.HasOneToManyUnencodedStringPkListJPA;
 39import com.google.appengine.datanucleus.test.jpa.HasOneToManyWithOrderByJPA;
 40import com.google.appengine.datanucleus.test.jpa.HasUnencodedStringPkOneToManyBidirChildrenJPA;
 41
 42
 43import java.util.Collections;
 44
 45import static com.google.appengine.datanucleus.TestUtils.assertKeyParentEquals;
 46
 47/**
 48 * @author Max Ross <maxr@google.com>
 49 */
 50public class JPAOneToManyListTest extends JPAOneToManyTestCase {
 51
 52  public void testInsert_NewParentAndChild() throws Exception {
 53    testInsert_NewParentAndChild(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 54                                 TXN_START_END);
 55  }
 56
 57  public void testInsert_NewParentAndChild_NoTxn() throws Exception {
 58    testInsert_NewParentAndChild(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 59                                 NEW_EM_START_END);
 60  }
 61
 62  public void testInsert_ExistingParentNewChild() throws Exception {
 63    testInsert_ExistingParentNewChild(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 64                                      TXN_START_END);
 65  }
 66
 67  public void testInsert_ExistingParentNewChild_NoTxn() throws Exception {
 68    testInsert_ExistingParentNewChild(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 69                                      NEW_EM_START_END);
 70  }
 71
 72  public void testUpdate_UpdateChildWithMerge() throws Exception {
 73    testUpdate_UpdateChildWithMerge(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 74                                      TXN_START_END);
 75  }
 76
 77  public void testUpdate_UpdateChildWithMerge_NoTxn() throws Exception {
 78    testUpdate_UpdateChildWithMerge(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 79                                      NEW_EM_START_END);
 80  }
 81
 82  public void testUpdate_UpdateChild() throws Exception {
 83    testUpdate_UpdateChild(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 84                                      TXN_START_END);
 85  }
 86
 87  public void testUpdate_UpdateChild_NoTxn() throws Exception {
 88    testUpdate_UpdateChild(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 89                                      NEW_EM_START_END);
 90  }
 91
 92  public void testUpdate_NullOutChildren() throws Exception {
 93    testUpdate_NullOutChildren(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 94                                      TXN_START_END);
 95  }
 96
 97  public void testUpdate_NullOutChildren_NoTxn() throws Exception {
 98    testUpdate_NullOutChildren(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
 99                                      NEW_EM_START_END);
100  }
101
102  public void testUpdate_ClearOutChildren() throws Exception {
103    testUpdate_ClearOutChildren(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
104                                      TXN_START_END);
105  }
106  public void testUpdate_ClearOutChildren_NoTxn() throws Exception {
107    testUpdate_ClearOutChildren(new BidirectionalChildListJPA(), new HasOneToManyListJPA(),
108                                      NEW_EM_START_END);
109  }
110  public void testFindWithOrderBy() throws Exception {
111    testFindWithOrderBy(HasOneToManyWithOrderByJPA.class,
112                                      TXN_START_END);
113  }
114  public void testFindWithOrderBy_NoTxn() throws Exception {
115    testFindWithOrderBy(HasOneToManyWithOrderByJPA.class,
116                                      NEW_EM_START_END);
117  }
118  public void testFind() throws Exception {
119    testFind(HasOneToManyListJPA.class, BidirectionalChildListJPA.class,
120                                      TXN_START_END);
121  }
122  public void testFind_NoTxn() throws Exception {
123    testFind(HasOneToManyListJPA.class, BidirectionalChildListJPA.class,
124                                      NEW_EM_START_END);
125  }
126  public void testQuery() throws Exception {
127    testQuery(HasOneToManyListJPA.class, BidirectionalChildListJPA.class,
128                                      TXN_START_END);
129  }
130  public void testQuery_NoTxn() throws Exception {
131    testQuery(HasOneToManyListJPA.class, BidirectionalChildListJPA.class,
132                                      NEW_EM_START_END);
133  }
134  public void testChildFetchedLazily() throws Exception {
135    testChildFetchedLazily(HasOneToManyListJPA.class, BidirectionalChildListJPA.class);
136  }
137  public void testDeleteParentDeletesChild() throws Exception {
138    testDeleteParentDeletesChild(HasOneToManyListJPA.class, BidirectionalChildListJPA.class,
139                                      TXN_START_END);
140  }
141  public void testDeleteParentDeletesChild_NoTxn() throws Exception {
142    testDeleteParentDeletesChild(HasOneToManyListJPA.class, BidirectionalChildListJPA.class,
143                                      NEW_EM_START_END);
144  }
145
146  public void testSwapAtPosition() throws Exception {
147    testSwapAtPosition(TXN_START_END);
148  }
149
150  public void testSwapAtPosition_NoTxn() throws Exception {
151    testSwapAtPosition(NEW_EM_START_END);
152  }
153
154  private void testSwapAtPosition(StartEnd startEnd) throws Exception {
155    HasOneToManyListJPA pojo = new HasOneToManyListJPA();
156    pojo.setVal("yar");
157    BidirectionalChildListJPA bidir1 = new BidirectionalChildListJPA();
158    BidirectionalChildListJPA bidir2 = new BidirectionalChildListJPA();
159    bidir2.setChildVal("yam");
160    Book b = newBook();
161    HasKeyPkJPA hasKeyPk = new HasKeyPkJPA();
162
163    pojo.getBooks().add(b);
164    pojo.getHasKeyPks().add(hasKeyPk);
165    pojo.getBidirChildren().add(bidir1);
166    bidir1.setParent(pojo);
167
168    startEnd.start();
169    em.persist(pojo);
170    startEnd.end();
171
172    assertCountsInDatastore(HasOneToManyListJPA.class, BidirectionalChildListJPA.class, 1, 1);
173
174    startEnd.start();
175    pojo = em.find(pojo.getClass(), pojo.getId());
176    String bidir1Id = pojo.getBidirChildren().get(0).getId();
177    String bookId = pojo.getBooks().get(0).getId();
178    Key hasKeyPk1Key = pojo.getHasKeyPks().get(0).getId();
179    pojo.getBidirChildren().set(0, bidir2);
180
181    Book b2 = newBook();
182    b2.setTitle("another title");
183    pojo.getBooks().set(0, b2);
184
185    HasKeyPkJPA hasKeyPk2 = new HasKeyPkJPA();
186    hasKeyPk2.setStr("another str");
187    pojo.getHasKeyPks().set(0, hasKeyPk2);
188    startEnd.end();
189
190    startEnd.start();
191    assertNotNull(pojo.getId());
192    assertEquals(1, pojo.getBooks().size());
193    assertEquals(1, pojo.getHasKeyPks().size());
194    assertEquals(1, pojo.getBidirChildren().size());
195    assertNotNull(bidir2.getId());
196    assertNotNull(bidir2.getParent());
197    assertNotNull(b2.getId());
198    assertNotNull(hasKeyPk2.getId());
199
200    Entity pojoEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
201    assertNotNull(pojoEntity);
202    assertEquals(4, pojoEntity.getProperties().size());
203    assertEquals(Collections.singletonList(KeyFactory.stringToKey(bidir2.getId())), pojoEntity.getProperty("bidirChildren"));
204    assertEquals(Collections.singletonList(KeyFactory.stringToKey(b2.getId())), pojoEntity.getProperty("books"));
205    assertEquals(Collections.singletonList(hasKeyPk2.getId()), pojoEntity.getProperty("hasKeyPks"));
206
207    startEnd.end();
208
209    try {
210      ds.get(KeyFactory.stringToKey(bidir1Id));
211      fail("expected EntityNotFoundException");
212    } catch (EntityNotFoundException enfe) {
213      // good
214    }
215    try {
216      ds.get(KeyFactory.stringToKey(bookId));
217      fail("expected EntityNotFoundException");
218    } catch (EntityNotFoundException enfe) {
219      // good
220    }
221    try {
222      ds.get(hasKeyPk1Key);
223      fail("expected EntityNotFoundException");
224    } catch (EntityNotFoundException enfe) {
225      // good
226    }
227    Entity bidirChildEntity = ds.get(KeyFactory.stringToKey(bidir2.getId()));
228    assertNotNull(bidirChildEntity);
229    assertEquals("yam", bidirChildEntity.getProperty("childVal"));
230    assertEquals(KeyFactory.stringToKey(bidir2.getId()), bidirChildEntity.getKey());
231    assertKeyParentEquals(pojo.getId(), bidirChildEntity, bidir2.getId());
232
233    Entity bookEntity = ds.get(KeyFactory.stringToKey(b2.getId()));
234    assertNotNull(bookEntity);
235    assertEquals("max", bookEntity.getProperty("author"));
236    assertEquals("22333", bookEntity.getProperty("isbn"));
237    assertEquals("another title", bookEntity.getProperty("title"));
238    assertEquals(KeyFactory.stringToKey(b2.getId()), bookEntity.getKey());
239    assertKeyParentEquals(pojo.getId(), bookEntity, b2.getId());
240
241    Entity hasKeyPkEntity = ds.get(hasKeyPk2.getId());
242    assertNotNull(hasKeyPkEntity);
243    assertEquals("another str", hasKeyPkEntity.getProperty("str"));
244    assertEquals(hasKeyPk2.getId(), hasKeyPkEntity.getKey());
245    assertKeyParentEquals(pojo.getId(), hasKeyPkEntity, hasKeyPk2.getId());
246
247    Entity parentEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
248    assertNotNull(parentEntity);
249    assertEquals(4, parentEntity.getProperties().size());
250    assertEquals("yar", parentEntity.getProperty("val"));
251    assertEquals(Collections.singletonList(bidirChildEntity.getKey()), pojoEntity.getProperty("bidirChildren"));
252    assertEquals(Collections.singletonList(bookEntity.getKey()), pojoEntity.getProperty("books"));
253    assertEquals(Collections.singletonList(hasKeyPkEntity.getKey()), pojoEntity.getProperty("hasKeyPks"));
254
255    assertCountsInDatastore(HasOneToManyListJPA.class, BidirectionalChildListJPA.class, 1, 1);
256  }
257
258  public void testRemoveObject() throws Exception {
259    testRemoveObject(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
260        new BidirectionalChildListJPA(), TXN_START_END);
261  }
262
263  public void testRemoveObject_NoTxn() throws Exception {
264    testRemoveObject(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
265        new BidirectionalChildListJPA(), NEW_EM_START_END);
266  }
267
268  public void testRemoveAtPosition() throws Exception {
269    testRemoveAtPosition(TXN_START_END);
270  }
271
272  public void testRemoveAtPosition_NoTxn() throws Exception {
273    testRemoveAtPosition(NEW_EM_START_END);
274  }
275
276  private void testRemoveAtPosition(StartEnd startEnd) throws Exception {
277    HasOneToManyListJPA pojo = new HasOneToManyListJPA();
278    pojo.setVal("yar");
279    BidirectionalChildListJPA bidir1 = new BidirectionalChildListJPA();
280    BidirectionalChildListJPA bidir2 = new BidirectionalChildListJPA();
281    bidir1.setChildVal("yam1");
282    bidir2.setChildVal("yam2");
283    Book b1 = newBook();
284    Book b2 = newBook();
285    b2.setTitle("another title");
286    HasKeyPkJPA hasKeyPk1 = new HasKeyPkJPA();
287    HasKeyPkJPA hasKeyPk2 = new HasKeyPkJPA();
288    hasKeyPk2.setStr("yar 2");
289    pojo.getBooks().add(b1);
290    pojo.getBooks().add(b2);
291    pojo.getHasKeyPks().add(hasKeyPk1);
292    pojo.getHasKeyPks().add(hasKeyPk2);
293    pojo.getBidirChildren().add(bidir1);
294    pojo.getBidirChildren().add(bidir2);
295
296    startEnd.start();
297    em.persist(pojo);
298    startEnd.end();
299
300    assertCountsInDatastore(HasOneToManyListJPA.class, BidirectionalChildListJPA.class, 1, 2);
301
302    String bidir1Id = pojo.getBidirChildren().get(0).getId();
303    String bookId = pojo.getBooks().get(0).getId();
304    Key hasKeyPk1Key = pojo.getHasKeyPks().get(0).getId();
305    pojo.getBidirChildren().remove(0);
306    pojo.getBooks().remove(0);
307    pojo.getHasKeyPks().remove(0);
308
309    startEnd.start();
310    em.merge(pojo);
311    startEnd.end();
312
313    startEnd.start();
314    pojo = em.find(pojo.getClass(), pojo.getId());
315    assertNotNull(pojo.getId());
316    assertEquals(1, pojo.getBooks().size());
317    assertEquals(1, pojo.getHasKeyPks().size());
318    assertEquals(1, pojo.getBidirChildren().size());
319    assertNotNull(bidir2.getId());
320    assertNotNull(bidir2.getParent());
321    assertNotNull(b2.getId());
322    assertNotNull(hasKeyPk2.getId());
323    assertEquals(bidir2.getId(), pojo.getBidirChildren().get(0).getId());
324    assertEquals(hasKeyPk2.getId(), pojo.getHasKeyPks().get(0).getId());
325    assertEquals(b2.getId(), pojo.getBooks().get(0).getId());
326
327    Entity pojoEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
328    assertNotNull(pojoEntity);
329    assertEquals(4, pojoEntity.getProperties().size());
330    assertEquals(Collections.singletonList(KeyFactory.stringToKey(bidir2.getId())), pojoEntity.getProperty("bidirChildren"));
331    assertEquals(Collections.singletonList(KeyFactory.stringToKey(b2.getId())), pojoEntity.getProperty("books"));
332    assertEquals(Collections.singletonList(hasKeyPk2.getId()), pojoEntity.getProperty("hasKeyPks"));
333
334    startEnd.end();
335
336    try {
337      ds.get(KeyFactory.stringToKey(bidir1Id));
338      fail("expected EntityNotFoundException");
339    } catch (EntityNotFoundException enfe) {
340      // good
341    }
342    try {
343      ds.get(KeyFactory.stringToKey(bookId));
344      fail("expected EntityNotFoundException");
345    } catch (EntityNotFoundException enfe) {
346      // good
347    }
348    try {
349      ds.get(hasKeyPk1Key);
350      fail("expected EntityNotFoundException");
351    } catch (EntityNotFoundException enfe) {
352      // good
353    }
354    Entity bidirChildEntity = ds.get(KeyFactory.stringToKey(bidir2.getId()));
355    assertNotNull(bidirChildEntity);
356    assertEquals("yam2", bidirChildEntity.getProperty("childVal"));
357    assertEquals(KeyFactory.stringToKey(bidir2.getId()), bidirChildEntity.getKey());
358    assertKeyParentEquals(pojo.getId(), bidirChildEntity, bidir2.getId());
359
360    Entity bookEntity = ds.get(KeyFactory.stringToKey(b2.getId()));
361    assertNotNull(bookEntity);
362    assertEquals("max", bookEntity.getProperty("author"));
363    assertEquals("22333", bookEntity.getProperty("isbn"));
364    assertEquals("another title", bookEntity.getProperty("title"));
365    assertEquals(KeyFactory.stringToKey(b2.getId()), bookEntity.getKey());
366    assertKeyParentEquals(pojo.getId(), bookEntity, b2.getId());
367
368    Entity hasKeyPkEntity = ds.get(hasKeyPk2.getId());
369    assertNotNull(hasKeyPkEntity);
370    assertEquals("yar 2", hasKeyPkEntity.getProperty("str"));
371    assertEquals(hasKeyPk2.getId(), hasKeyPkEntity.getKey());
372    assertKeyParentEquals(pojo.getId(), hasKeyPkEntity, hasKeyPk2.getId());
373
374    Entity parentEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
375    assertNotNull(parentEntity);
376    assertEquals(4, parentEntity.getProperties().size());
377    assertEquals("yar", parentEntity.getProperty("val"));
378    assertEquals(Collections.singletonList(bidirChildEntity.getKey()), pojoEntity.getProperty("bidirChildren"));
379    assertEquals(Collections.singletonList(bookEntity.getKey()), pojoEntity.getProperty("books"));
380    assertEquals(Collections.singletonList(hasKeyPkEntity.getKey()), pojoEntity.getProperty("hasKeyPks"));
381
382    assertCountsInDatastore(HasOneToManyListJPA.class, BidirectionalChildListJPA.class, 1, 1);
383  }
384
385  public void testAddAtPosition() throws Exception {
386    testAddAtPosition(TXN_START_END);
387  }
388  public void testAddAtPosition_NoTxn() throws Exception {
389    testAddAtPosition(NEW_EM_START_END);
390  }
391
392  private void testAddAtPosition(StartEnd startEnd) throws Exception {
393    HasOneToManyListJPA pojo = new HasOneToManyListJPA();
394    pojo.setVal("yar");
395    BidirectionalChildListJPA bidir1 = new BidirectionalChildListJPA();
396    BidirectionalChildListJPA bidir2 = new BidirectionalChildListJPA();
397    bidir1.setChildVal("yam1");
398    bidir2.setChildVal("yam2");
399    Book b1 = newBook();
400    Book b2 = newBook();
401    b2.setTitle("another title");
402    HasKeyPkJPA hasKeyPk1 = new HasKeyPkJPA();
403    HasKeyPkJPA hasKeyPk2 = new HasKeyPkJPA();
404    hasKeyPk2.setStr("yar 2");
405    pojo.getBooks().add(b1);
406    pojo.getHasKeyPks().add(hasKeyPk1);
407    pojo.getBidirChildren().add(bidir1);
408
409    startEnd.start();
410    em.persist(pojo);
411    startEnd.end();
412
413    assertCountsInDatastore(HasOneToManyListJPA.class, BidirectionalChildListJPA.class, 1, 1);
414
415    startEnd.start();
416    pojo = em.find(pojo.getClass(), pojo.getId());
417    String bidir1Id = pojo.getBidirChildren().get(0).getId();
418    String bookId = pojo.getBooks().get(0).getId();
419    Key hasKeyPk1Key = pojo.getHasKeyPks().get(0).getId();
420    pojo.getBidirChildren().add(0, bidir2);
421    pojo.getBooks().add(0, b2);
422    pojo.getHasKeyPks().add(0, hasKeyPk2);
423    startEnd.end();
424
425    startEnd.start();
426    assertNotNull(pojo.getId());
427    assertEquals(2, pojo.getBooks().size());
428    assertEquals(2, pojo.getHasKeyPks().size());
429    assertEquals(2, pojo.getBidirChildren().size());
430    assertNotNull(bidir2.getId());
431    assertNotNull(bidir2.getParent());
432    assertNotNull(b2.getId());
433    assertNotNull(hasKeyPk2.getId());
434
435    Entity pojoEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
436    assertNotNull(pojoEntity);
437    assertEquals(4, pojoEntity.getProperties().size());
438    assertEquals(Utils.newArrayList(KeyFactory.stringToKey(bidir2.getId()), KeyFactory.stringToKey(bidir1Id)), pojoEntity.getProperty("bidirChildren"));
439    assertEquals(Utils.newArrayList(KeyFactory.stringToKey(b2.getId()), KeyFactory.stringToKey(bookId)), pojoEntity.getProperty("books"));
440    assertEquals(Utils.newArrayList(hasKeyPk2.getId(), hasKeyPk1Key), pojoEntity.getProperty("hasKeyPks"));
441
442    startEnd.end();
443
444    ds.get(KeyFactory.stringToKey(bidir1Id));
445    ds.get(KeyFactory.stringToKey(bookId));
446    ds.get(hasKeyPk1Key);
447
448    Entity bidirChildEntity1 = ds.get(KeyFactory.stringToKey(bidir1Id));
449    Entity bidirChildEntity2 = ds.get(KeyFactory.stringToKey(bidir2.getId()));
450    assertNotNull(bidirChildEntity2);
451    assertEquals("yam2", bidirChildEntity2.getProperty("childVal"));
452    assertEquals(KeyFactory.stringToKey(bidir2.getId()), bidirChildEntity2.getKey());
453    assertKeyParentEquals(pojo.getId(), bidirChildEntity2, bidir2.getId());
454
455    Entity bookEntity1 = ds.get(KeyFactory.stringToKey(bookId));
456    Entity bookEntity2 = ds.get(KeyFactory.stringToKey(b2.getId()));
457    assertNotNull(bookEntity2);
458    assertEquals("max", bookEntity2.getProperty("author"));
459    assertEquals("22333", bookEntity2.getProperty("isbn"));
460    assertEquals("another title", bookEntity2.getProperty("title"));
461    assertEquals(KeyFactory.stringToKey(b2.getId()), bookEntity2.getKey());
462    assertKeyParentEquals(pojo.getId(), bookEntity2, b2.getId());
463
464    Entity hasKeyPkEntity1 = ds.get(hasKeyPk1Key);
465    Entity hasKeyPkEntity2 = ds.get(hasKeyPk2.getId());
466    assertNotNull(hasKeyPkEntity2);
467    assertEquals("yar 2", hasKeyPkEntity2.getProperty("str"));
468    assertEquals(hasKeyPk2.getId(), hasKeyPkEntity2.getKey());
469    assertKeyParentEquals(pojo.getId(), hasKeyPkEntity2, hasKeyPk2.getId());
470
471    Entity parentEntity = ds.get(KeyFactory.stringToKey(pojo.getId()));
472    assertNotNull(parentEntity);
473    assertEquals(4, parentEntity.getProperties().size());
474    assertEquals("yar", parentEntity.getProperty("val"));
475    assertEquals(Utils.newArrayList(bidirChildEntity2.getKey(), bidirChildEntity1.getKey()), pojoEntity.getProperty("bidirChildren"));
476    assertEquals(Utils.newArrayList(bookEntity2.getKey(), bookEntity1.getKey()), pojoEntity.getProperty("books"));
477    assertEquals(Utils.newArrayList(hasKeyPkEntity2.getKey(), hasKeyPkEntity1.getKey()), pojoEntity.getProperty("hasKeyPks"));
478
479    assertCountsInDatastore(HasOneToManyListJPA.class, BidirectionalChildListJPA.class, 1, 2);
480  }
481
482  public void testChangeParent() throws Exception {
483    testChangeParent(new HasOneToManyListJPA(), new HasOneToManyListJPA(), TXN_START_END);
484  }
485  public void testChangeParent_NoTxn() throws Exception {
486    testChangeParent(new HasOneToManyListJPA(), new HasOneToManyListJPA(), NEW_EM_START_END);
487  }
488
489  public void testNewParentNewChild_NamedKeyOnChild() throws Exception {
490    testNewParentNewChild_NamedKeyOnChild(new HasOneToManyListJPA(), TXN_START_END);
491  }
492  public void testNewParentNewChild_NamedKeyOnChild_NoTxn() throws Exception {
493    testNewParentNewChild_NamedKeyOnChild(new HasOneToManyListJPA(), NEW_EM_START_END);
494  }
495
496  public void testInsert_NewParentAndChild_LongPk() throws Exception {
497    testInsert_NewParentAndChild_LongPk(TXN_START_END);
498  }
499  public void testInsert_NewParentAndChild_LongPk_NoTxn() throws Exception {
500    testInsert_NewParentAndChild_LongPk(NEW_EM_START_END);
501  }
502  private void testInsert_NewParentAndChild_LongPk(StartEnd startEnd) throws Exception {
503    BidirectionalChildLongPkListJPA bidirChild = new BidirectionalChildLongPkListJPA();
504    bidirChild.setChildVal("yam");
505
506    Book b = newBook();
507
508    HasKeyPkJPA hasKeyPk = new HasKeyPkJPA();
509    hasKeyPk.setStr("yag");
510
511    HasOneToManyLongPkListJPA parent = new HasOneToManyLongPkListJPA();
512    parent.getBidirChildren().add(bidirChild);
513    bidirChild.setParent(parent);
514    parent.getBooks().add(b);
515    parent.getHasKeyPks().add(hasKeyPk);
516    parent.setVal("yar");
517
518    startEnd.start();
519    em.persist(parent);
520    startEnd.end();
521
522    assertNotNull(bidirChild.getId());
523    assertNotNull(b.getId());
524    assertNotNull(hasKeyPk.getId());
525
526    Entity bidirChildEntity = ds.get(KeyFactory.stringToKey(bidirChild.getId()));
527    assertNotNull(bidirChildEntity);
528    assertEquals("yam", bidirChildEntity.getProperty("childVal"));
529    assertEquals(KeyFactory.stringToKey(bidirChild.getId()), bidirChildEntity.getKey());
530    assertKeyParentEquals(parent.getClass(), parent.getId(), bidirChildEntity, bidirChild.getId());
531
532    Entity bookEntity = ds.get(KeyFactory.stringToKey(b.getId()));
533    assertNotNull(bookEntity);
534    assertEquals("max", bookEntity.getProperty("author"));
535    assertEquals("22333", bookEntity.getProperty("isbn"));
536    assertEquals("yam", bookEntity.getProperty("title"));
537    assertEquals(KeyFactory.stringToKey(b.getId()), bookEntity.getKey());
538    assertKeyParentEquals(parent.getClass(), parent.getId(), bookEntity, b.getId());
539
540    Entity hasKeyPkEntity = ds.get(hasKeyPk.getId());
541    assertNotNull(hasKeyPkEntity);
542    assertEquals("yag", hasKeyPkEntity.getProperty("str"));
543    assertEquals(hasKeyPk.getId(), hasKeyPkEntity.getKey());
544    assertKeyParentEquals(parent.getClass(), parent.getId(), hasKeyPkEntity, hasKeyPk.getId());
545
546    Entity parentEntity = ds.get(TestUtils.createKey(parent, parent.getId()));
547    assertNotNull(parentEntity);
548    assertEquals(4, parentEntity.getProperties().size());
549    assertEquals("yar", parentEntity.getProperty("val"));
550    assertEquals(Utils.newArrayList(bidirChildEntity.getKey()), parentEntity.getProperty("bidirChildren"));
551    assertEquals(Utils.newArrayList(bookEntity.getKey()), parentEntity.getProperty("books"));
552    assertEquals(Utils.newArrayList(hasKeyPkEntity.getKey()), parentEntity.getProperty("hasKeyPks"));
553
554    assertEquals(HasOneToManyLongPkListJPA.class.getName(), 1, countForClass(HasOneToManyLongPkListJPA.class));
555    assertEquals(BidirectionalChildLongPkListJPA.class.getName(), 1, countForClass(
556        BidirectionalChildLongPkListJPA.class));
557    assertEquals(Book.class.getName(), 1, countForClass(Book.class));
558    assertEquals(HasKeyPkJPA.class.getName(), 1, countForClass(HasKeyPkJPA.class));
559  }
560
561  public void testInsert_NewParentAndChild_StringPk() throws Exception {
562    testInsert_NewParentAndChild_StringPk(TXN_START_END);
563  }
564  public void testInsert_NewParentAndChild_StringPk_NoTxn() throws Exception {
565    testInsert_NewParentAndChild_StringPk(NEW_EM_START_END);
566  }
567  private void testInsert_NewParentAndChild_StringPk(StartEnd startEnd) throws Exception {
568    BidirectionalChildStringPkListJPA bidirChild = new BidirectionalChildStringPkListJPA();
569    bidirChild.setChildVal("yam");
570
571    Book b = newBook();
572
573    HasKeyPkJPA hasKeyPk = new HasKeyPkJPA();
574    hasKeyPk.setStr("yag");
575
576    HasOneToManyStringPkListJPA parent = new HasOneToManyStringPkListJPA();
577    parent.setId("yar");
578    parent.getBidirChildren().add(bidirChild);
579    bidirChild.setParent(parent);
580    parent.getBooks().add(b);
581    parent.getHasKeyPks().add(hasKeyPk);
582    parent.setVal("yar");
583
584    startEnd.start();
585    em.persist(parent);
586    startEnd.end();
587
588    assertNotNull(bidirChild.getId());
589    assertNotNull(b.getId());
590    assertNotNull(hasKeyPk.getId());
591
592    Entity bidirChildEntity = ds.get(KeyFactory.stringToKey(bidirChild.getId()));
593    assertNotNull(bidirChildEntity);
594    assertEquals("yam", bidirChildEntity.getProperty("childVal"));
595    assertEquals(KeyFactory.stringToKey(bidirChild.getId()), bidirChildEntity.getKey());
596    assertKeyParentEquals(parent.getClass(), parent.getId(), bidirChildEntity, bidirChild.getId());
597
598    Entity bookEntity = ds.get(KeyFactory.stringToKey(b.getId()));
599    assertNotNull(bookEntity);
600    assertEquals("max", bookEntity.getProperty("author"));
601    assertEquals("22333", bookEntity.getProperty("isbn"));
602    assertEquals("yam", bookEntity.getProperty("title"));
603    assertEquals(KeyFactory.stringToKey(b.getId()), bookEntity.getKey());
604    assertKeyParentEquals(parent.getClass(), parent.getId(), bookEntity, b.getId());
605
606    Entity hasKeyPkEntity = ds.get(hasKeyPk.getId());
607    assertNotNull(hasKeyPkEntity);
608    assertEquals("yag", hasKeyPkEntity.getProperty("str"));
609    assertEquals(hasKeyPk.getId(), hasKeyPkEntity.getKey());
610    assertKeyParentEquals(parent.getClass(), parent.getId(), hasKeyPkEntity, hasKeyPk.getId());
611
612    Entity parentEntity = ds.get(TestUtils.createKey(parent, parent.getId()));
613    assertNotNull(parentEntity);
614    assertEquals(4, parentEntity.getProperties().size());
615    assertEquals("yar", parentEntity.getProperty("val"));
616    assertEquals(Utils.newArrayList(bidirChildEntity.getKey()), parentEntity.getProperty("bidirChildren"));
617    assertEquals(Utils.newArrayList(bookEntity.getKey()), parentEntity.getProperty("books"));
618    assertEquals(Utils.newArrayList(hasKeyPkEntity.getKey()), parentEntity.getProperty("hasKeyPks"));
619
620    assertEquals(HasOneToManyStringPkListJPA.class.getName(), 1, countForClass(
621        HasOneToManyStringPkListJPA.class));
622    assertEquals(BidirectionalChildStringPkListJPA.class.getName(), 1, countForClass(
623        BidirectionalChildStringPkListJPA.class));
624    assertEquals(Book.class.getName(), 1, countForClass(Book.class));
625    assertEquals(HasKeyPkJPA.class.getName(), 1, countForClass(HasKeyPkJPA.class));
626  }
627
628  public void testAddAlreadyPersistedChildToParent_NoTxnSameEm() {
629    testAddAlreadyPersistedChildToParent_NoTxnSameEm(new HasOneToManyListJPA());
630  }
631
632  public void testAddAlreadyPersistedChildToParent_NoTxnDifferentEm() {
633    testAddAlreadyPersistedChildToParent_NoTxnDifferentEm(new HasOneToManyListJPA());
634  }
635
636  public void testLongPkOneToManyBidirChildren() throws Exception {
637    testLongPkOneToManyBidirChildren(TXN_START_END);
638  }
639  public void testLongPkOneToManyBidirChildren_NoTxn() throws Exception {
640    testLongPkOneToManyBidirChildren(NEW_EM_START_END);
641  }
642  private void testLongPkOneToManyBidirChildren(StartEnd startEnd)
643      throws Exception {
644    HasLongPkOneToManyBidirChildrenJPA pojo = new HasLongPkOneToManyBidirChildrenJPA();
645    HasLongPkOneToManyBidirChildrenJPA.ChildA
646        a = new HasLongPkOneToManyBidirChildrenJPA.ChildA();
647    pojo.setChildAList(Utils.newArrayList(a));
648    startEnd.start();
649    em.persist(pojo);
650    startEnd.end();
651    startEnd.start();
652    pojo = em.find(HasLongPkOneToManyBidirChildrenJPA.class, pojo.getId());
653    assertEquals(1, pojo.getChildAList().size());
654    assertEquals(pojo, pojo.getChildAList().get(0).getParent());
655    startEnd.end();
656  }
657
658  public void testUnencodedStringPkOneToManyBidirChildren() throws Exception {
659    testUnencodedStringPkOneToManyBidirChildren(TXN_START_END);
660  }
661  public void testUnencodedStringPkOneToManyBidirChildren_NoTxn() throws Exception {
662    testUnencodedStringPkOneToManyBidirChildren(NEW_EM_START_END);
663  }
664  private void testUnencodedStringPkOneToManyBidirChildren(StartEnd startEnd)
665      throws Exception {
666    HasUnencodedStringPkOneToManyBidirChildrenJPA
667        pojo = new HasUnencodedStringPkOneToManyBidirChildrenJPA();
668    pojo.setId("yar");
669    HasUnencodedStringPkOneToManyBidirChildrenJPA.ChildA
670        a = new HasUnencodedStringPkOneToManyBidirChildrenJPA.ChildA();
671    pojo.setChildAList(Utils.newArrayList(a));
672    startEnd.start();
673    em.persist(pojo);
674    startEnd.end();
675    startEnd.start();
676    pojo = em.find(HasUnencodedStringPkOneToManyBidirChildrenJPA.class, pojo.getId());
677    assertEquals(1, pojo.getChildAList().size());
678    assertEquals(pojo, pojo.getChildAList().get(0).getParent());
679    startEnd.end();
680  }
681
682  public void testFetchOfOneToManyParentWithKeyPk() throws Exception {
683    testFetchOfOneToManyParentWithKeyPk(new HasOneToManyKeyPkListJPA(), TXN_START_END);
684  }
685  public void testFetchOfOneToManyParentWithKeyPk_NoTxn() throws Exception {
686    testFetchOfOneToManyParentWithKeyPk(new HasOneToManyKeyPkListJPA(), NEW_EM_START_END);
687  }
688
689  public void testFetchOfOneToManyParentWithLongPk() throws Exception {
690    testFetchOfOneToManyParentWithLongPk(new HasOneToManyLongPkListJPA(), TXN_START_END);
691  }
692  public void testFetchOfOneToManyParentWithLongPk_NoTxn() throws Exception {
693    testFetchOfOneToManyParentWithLongPk(new HasOneToManyLongPkListJPA(), NEW_EM_START_END);
694  }
695
696  public void testFetchOfOneToManyParentWithUnencodedStringPk() throws Exception {
697    testFetchOfOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkListJPA(), TXN_START_END);
698  }
699  public void testFetchOfOneToManyParentWithUnencodedStringPk_NoTxn() throws Exception {
700    testFetchOfOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkListJPA(), NEW_EM_START_END);
701  }
702
703  public void testAddChildToOneToManyParentWithLongPk() throws Exception {
704    testAddChildToOneToManyParentWithLongPk(
705        new HasOneToManyLongPkListJPA(), new BidirectionalChildLongPkListJPA(), TXN_START_END);
706  }
707  public void testAddChildToOneToManyParentWithLongPk_NoTxn() throws Exception {
708    testAddChildToOneToManyParentWithLongPk(
709        new HasOneToManyLongPkListJPA(), new BidirectionalChildLongPkListJPA(), NEW_EM_START_END);
710  }
711
712  public void testAddChildToOneToManyParentWithUnencodedStringPk() throws Exception {
713    testAddChildToOneToManyParentWithUnencodedStringPk(
714        new HasOneToManyUnencodedStringPkListJPA(), new BidirectionalChildUnencodedStringPkListJPA(),
715        TXN_START_END);
716  }
717  public void testAddChildToOneToManyParentWithUnencodedStringPk_NoTxn() throws Exception {
718    testAddChildToOneToManyParentWithUnencodedStringPk(
719        new HasOneToManyUnencodedStringPkListJPA(), new BidirectionalChildUnencodedStringPkListJPA(),
720    NEW_EM_START_END);
721  }
722
723  public void testOneToManyChildAtMultipleLevels() throws Exception {
724    testOneToManyChildAtMultipleLevels(TXN_START_END);
725  }
726  public void testOneToManyChildAtMultipleLevels_NoTxn() throws Exception {
727    testOneToManyChildAtMultipleLevels(NEW_EM_START_END);
728  }
729  private void testOneToManyChildAtMultipleLevels(StartEnd startEnd)
730      throws Exception {
731    HasOneToManyChildAtMultipleLevelsJPA pojo = new HasOneToManyChildAtMultipleLevelsJPA();
732    Book b1 = new Book();
733    pojo.setBooks(Utils.newArrayList(b1));
734    HasOneToManyChildAtMultipleLevelsJPA child = new HasOneToManyChildAtMultipleLevelsJPA();
735    Book b2 = new Book();
736    child.setBooks(Utils.newArrayList(b2));
737    pojo.setChild(child);
738    startEnd.start();
739    em.persist(pojo);
740    startEnd.end();
741    startEnd.start();
742    assertEquals(2, countForClass(Book.class));
743    pojo = em.find(HasOneToManyChildAtMultipleLevelsJPA.class, pojo.getId());
744    assertEquals(child.getId(), pojo.getChild().getId());
745    assertEquals(1, pojo.getBooks().size());
746    assertEquals(pojo.getBooks().get(0), b1);
747    assertEquals(child.getBooks().get(0), b2);
748    assertEquals(1, child.getBooks().size());
749    startEnd.end();
750  }
751
752  public void testAddQueriedParentToBidirChild() throws Exception {
753    testAddQueriedParentToBidirChild(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
754                                     TXN_START_END);
755  }
756  public void testAddQueriedParentToBidirChild_NoTxn() throws Exception {
757    testAddQueriedParentToBidirChild(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
758                                     NEW_EM_START_END);
759  }
760
761  public void testAddFetchedParentToBidirChild() throws Exception {
762    testAddFetchedParentToBidirChild(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
763                                     TXN_START_END);
764  }
765  public void testAddFetchedParentToBidirChild_NoTxn() throws Exception {
766    testAddFetchedParentToBidirChild(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
767                                     NEW_EM_START_END);
768  }
769
770  public void testDeleteChildWithSeparateNameField() throws Exception {
771    testDeleteChildWithSeparateNameField(TXN_START_END);
772  }
773  public void testDeleteChildWithSeparateNameField_NoTxn() throws Exception {
774    testDeleteChildWithSeparateNameField(TXN_START_END);
775  }
776  private void testDeleteChildWithSeparateNameField(StartEnd startEnd) throws Exception {
777    HasChildWithSeparateNameFieldJPA parent = new HasChildWithSeparateNameFieldJPA();
778    HasEncodedStringPkSeparateNameFieldJPA child = new HasEncodedStringPkSeparateNameFieldJPA();
779    child.setName("the name");
780    parent.getChildren().add(child);
781    startEnd.start();
782    em.persist(parent);
783    startEnd.end();
784    startEnd.start();
785    parent = em.find(HasChildWithSeparateNameFieldJPA.class, parent.getId());
786    em.remove(parent);
787    startEnd.end();
788  }
789
790  public void testOnlyOneParentPutOnParentAndChildUpdate() throws Throwable {
791    testOnlyOneParentPutOnParentAndChildUpdate(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
792                                               TXN_START_END);
793  }
794  public void testOnlyOneParentPutOnParentAndChildUpdate_NoTxn() throws Throwable {
795    testOnlyOneParentPutOnParentAndChildUpdate(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
796                                               NEW_EM_START_END);
797  }
798
799  public void testOnlyOnePutOnChildUpdate() throws Throwable {
800    testOnlyOnePutOnChildUpdate(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
801                                TXN_START_END);
802  }
803  public void testOnlyOnePutOnChildUpdate_NoTxn() throws Throwable {
804    testOnlyOnePutOnChildUpdate(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
805                                NEW_EM_START_END);
806  }
807
808  public void testOnlyOneParentPutOnChildDelete() throws Throwable {
809    // 1 put to remove the keys
810    int expectedUpdatePuts = 1;
811    testOnlyOneParentPutOnChildDelete(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
812                                      TXN_START_END, expectedUpdatePuts);
813  }
814
815  public void testOnlyOneParentPutOnChildDelete_NoTxn() throws Throwable {
816    // updates are atomic when non-tx, so get 1 after each collection clear and 1 for the update.
817    int expectedUpdatePuts = 5;
818    testOnlyOneParentPutOnChildDelete(new HasOneToManyListJPA(), new BidirectionalChildListJPA(),
819                                      NEW_EM_START_END, expectedUpdatePuts);
820  }
821}