PageRenderTime 79ms CodeModel.GetById 13ms app.highlight 59ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 859 lines | 723 code | 86 blank | 50 comment | 0 complexity | fcf1f74bae4b317d0ab9c511e815bae1 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.jdo;
 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.jdo.Flight;
 25import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorStringPkJDO;
 26import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO;
 27import com.google.appengine.datanucleus.test.jdo.HasMultiValuePropsJDO;
 28import com.google.appengine.datanucleus.test.jdo.HasStringAncestorStringPkJDO;
 29import com.google.appengine.datanucleus.test.jdo.HasVersionWithFieldJDO;
 30import com.google.appengine.datanucleus.test.jdo.Name;
 31import com.google.appengine.datanucleus.test.jdo.NullDataJDO;
 32import com.google.appengine.datanucleus.test.jdo.Person;
 33
 34
 35import java.util.ArrayList;
 36import java.util.HashSet;
 37import java.util.LinkedHashSet;
 38import java.util.LinkedList;
 39import java.util.List;
 40import java.util.Set;
 41import java.util.SortedSet;
 42import java.util.TreeSet;
 43
 44import javax.jdo.JDOFatalUserException;
 45import javax.jdo.JDOHelper;
 46import javax.jdo.JDOOptimisticVerificationException;
 47
 48/**
 49 * @author Erick Armbrust <earmbrust@google.com>
 50 * @author Max Ross <maxr@google.com>
 51 */
 52public class JDOUpdateTest extends JDOTestCase {
 53
 54  private static final String DEFAULT_VERSION_PROPERTY_NAME = "VERSION";
 55
 56  public void testSimpleUpdate() throws EntityNotFoundException {
 57    Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
 58
 59    String keyStr = KeyFactory.keyToString(key);
 60    beginTxn();
 61    Flight flight = pm.getObjectById(Flight.class, keyStr);
 62
 63    assertEquals(keyStr, flight.getId());
 64    assertEquals("yam", flight.getOrigin());
 65    assertEquals("bam", flight.getDest());
 66    assertEquals("1", flight.getName());
 67    assertEquals(1, flight.getYou());
 68    assertEquals(2, flight.getMe());
 69
 70    flight.setName("2");
 71    commitTxn();
 72
 73    Entity flightCheck = ds.get(key);
 74    assertEquals("yam", flightCheck.getProperty("origin"));
 75    assertEquals("bam", flightCheck.getProperty("dest"));
 76    assertEquals("2", flightCheck.getProperty("name"));
 77    assertEquals(1L, flightCheck.getProperty("you"));
 78    assertEquals(2L, flightCheck.getProperty("me"));
 79    // verify that the version got bumped
 80    assertEquals(2L,
 81        flightCheck.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
 82  }
 83
 84  public void testSimpleUpdateWithNamedKey() throws EntityNotFoundException {
 85    Key key = ds.put(Flight.newFlightEntity("named key", "1", "yam", "bam", 1, 2));
 86
 87    String keyStr = KeyFactory.keyToString(key);
 88    beginTxn();
 89    Flight flight = pm.getObjectById(Flight.class, keyStr);
 90
 91    assertEquals(keyStr, flight.getId());
 92    assertEquals("yam", flight.getOrigin());
 93    assertEquals("bam", flight.getDest());
 94    assertEquals("1", flight.getName());
 95    assertEquals(1, flight.getYou());
 96    assertEquals(2, flight.getMe());
 97
 98    flight.setName("2");
 99    commitTxn();
100
101    Entity flightCheck = ds.get(key);
102    assertEquals("yam", flightCheck.getProperty("origin"));
103    assertEquals("bam", flightCheck.getProperty("dest"));
104    assertEquals("2", flightCheck.getProperty("name"));
105    assertEquals(1L, flightCheck.getProperty("you"));
106    assertEquals(2L, flightCheck.getProperty("me"));
107    // verify that the version got bumped
108    assertEquals(2L,
109        flightCheck.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
110    assertEquals("named key", flightCheck.getKey().getName());
111  }
112
113  public void testOptimisticLocking_Update_NoField() {
114    switchDatasource(PersistenceManagerFactoryName.nontransactional);
115    Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
116    Key key = ds.put(flightEntity);
117
118    String keyStr = KeyFactory.keyToString(key);
119    beginTxn();
120    Flight flight = pm.getObjectById(Flight.class, keyStr);
121
122    flight.setName("2");
123    flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
124    // we update the flight directly in the datastore right before commit
125    ds.put(flightEntity);
126    try {
127      commitTxn();
128      fail("expected optimistic exception");
129    } catch (JDOOptimisticVerificationException jove) {
130      // good
131    }
132  }
133
134  public void testOptimisticLocking_Attach_NoField() {
135    switchDatasource(PersistenceManagerFactoryName.nontransactional);
136    Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
137    Key key = ds.put(flightEntity);
138
139    String keyStr = KeyFactory.keyToString(key);
140    beginTxn();
141    Flight flight = pm.detachCopy(pm.getObjectById(Flight.class, keyStr));
142    commitTxn();
143    beginTxn();
144    flight.setName("2");
145    pm.makePersistent(flight);
146    flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
147    // we update the flight directly in the datastore right before commit
148    ds.put(flightEntity);
149    try {
150      commitTxn();
151      fail("expected optimistic exception");
152    } catch (JDOOptimisticVerificationException jove) {
153      // good
154    }
155  }
156
157  public void testOptimisticLocking_Delete_NoField() {
158    switchDatasource(PersistenceManagerFactoryName.nontransactional);
159    Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
160    Key key = ds.put(flightEntity);
161
162    String keyStr = KeyFactory.keyToString(key);
163    beginTxn();
164    Flight flight = pm.getObjectById(Flight.class, keyStr);
165
166    flight.setName("2");
167    flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
168    // we remove the flight from the datastore right before commit
169    ds.delete(key);
170    try {
171      commitTxn();
172      fail("expected optimistic exception");
173    } catch (JDOOptimisticVerificationException jove) {
174      // good
175    }
176  }
177
178  public void testOptimisticLocking_Update_HasVersionField() {
179    switchDatasource(PersistenceManagerFactoryName.nontransactional);
180    Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
181    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
182    Key key = ds.put(entity);
183
184    String keyStr = KeyFactory.keyToString(key);
185    beginTxn();
186    HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
187
188    hvwf.setValue("value");
189    commitTxn();
190    beginTxn();
191    hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
192    assertEquals(2L, hvwf.getVersion());
193    // make sure the version gets bumped
194    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 3L);
195
196    hvwf.setValue("another value");
197    // we update the entity directly in the datastore right before commit
198    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 7L);
199    ds.put(entity);
200    try {
201      commitTxn();
202      fail("expected optimistic exception");
203    } catch (JDOOptimisticVerificationException jove) {
204      // good
205    }
206    // make sure the version didn't change on the model object
207    assertEquals(2L, JDOHelper.getVersion(hvwf));
208  }
209
210  public void testOptimisticLocking_Delete_HasVersionField() {
211    switchDatasource(PersistenceManagerFactoryName.nontransactional);
212    Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
213    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
214    Key key = ds.put(entity);
215
216    String keyStr = KeyFactory.keyToString(key);
217    beginTxn();
218    HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
219
220    // delete the entity in the datastore right before we commit
221    ds.delete(key);
222    hvwf.setValue("value");
223    try {
224      commitTxn();
225      fail("expected optimistic exception");
226    } catch (JDOOptimisticVerificationException jove) {
227      // good
228    }
229    // make sure the version didn't change on the model object
230    assertEquals(1L, JDOHelper.getVersion(hvwf));
231  }
232
233  public void testNonTransactionalUpdate() throws EntityNotFoundException {
234    switchDatasource(PersistenceManagerFactoryName.nontransactional);
235
236    Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
237    Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
238    f.setYou(77);
239    pm.close();
240    Entity flightEntity = ds.get(key);
241    assertEquals(77L, flightEntity.getProperty("you"));
242    pm = pmf.getPersistenceManager();
243  }
244
245  public void testChangeStringPK_SetNonKeyString() throws EntityNotFoundException {
246    Key key = ds.put(Flight.newFlightEntity("named key", "1", "yam", "bam", 1, 2));
247
248    String keyStr = KeyFactory.keyToString(key);
249    beginTxn();
250    Flight flight = pm.getObjectById(Flight.class, keyStr);
251
252    assertEquals(keyStr, flight.getId());
253    assertEquals("yam", flight.getOrigin());
254    assertEquals("bam", flight.getDest());
255    assertEquals("1", flight.getName());
256    assertEquals(1, flight.getYou());
257    assertEquals(2, flight.getMe());
258
259    flight.setName("2");
260    flight.setId("foo");
261    try {
262      commitTxn();
263      fail("expected exception");
264    } catch (JDOFatalUserException e) {
265      // good
266      rollbackTxn();
267    }
268
269    Entity flightCheck = ds.get(key);
270    assertEquals("yam", flightCheck.getProperty("origin"));
271    assertEquals("bam", flightCheck.getProperty("dest"));
272    assertEquals("1", flightCheck.getProperty("name"));
273    assertEquals(1L, flightCheck.getProperty("you"));
274    assertEquals(2L, flightCheck.getProperty("me"));
275    // verify that the version got bumped
276    assertEquals(1L,
277        flightCheck.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
278    assertEquals("named key", flightCheck.getKey().getName());
279  }
280
281  public void testChangeStringPK_SetNull() throws EntityNotFoundException {
282    Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
283    beginTxn();
284    Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
285    f.setId(null);
286    pm.makePersistent(f);
287    try {
288      commitTxn();
289      fail("expected exception");
290    } catch (JDOFatalUserException e) {
291      // good
292      rollbackTxn();
293    }
294    assertEquals(1, countForClass(Flight.class));
295  }
296
297  public void testChangePK_SetKeyString() throws EntityNotFoundException {
298    Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
299    beginTxn();
300    Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
301    f.setId(KeyFactory.keyToString(KeyFactory.createKey(Flight.class.getSimpleName(), "yar")));
302    f.setYou(77);
303    pm.makePersistent(f);
304    try {
305      commitTxn();
306      fail("expected exception");
307    } catch (JDOFatalUserException e) {
308      // good
309      rollbackTxn();
310    }
311    assertEquals(1, countForClass(Flight.class));
312    Entity e = ds.get(key);
313    assertEquals(1L, e.getProperty("you"));
314    assertEquals(1, countForClass(Flight.class));
315  }
316
317  public void testChangeKeyPK_SetDifferentKey() throws EntityNotFoundException {
318    Key key = ds.put(new Entity(HasKeyPkJDO.class.getSimpleName()));
319
320    beginTxn();
321    HasKeyPkJDO pojo = pm.getObjectById(HasKeyPkJDO.class, key);
322
323    pojo.setKey(KeyFactory.createKey(HasKeyPkJDO.class.getSimpleName(), 33));
324    try {
325      commitTxn();
326      fail("expected exception");
327    } catch (JDOFatalUserException e) {
328      // good
329      rollbackTxn();
330    }
331    assertEquals(1, countForClass(HasKeyPkJDO.class));
332  }
333
334  public void testChangeKeyPK_SetNull() throws EntityNotFoundException {
335    Key key = ds.put(new Entity(HasKeyPkJDO.class.getSimpleName()));
336    beginTxn();
337    HasKeyPkJDO pojo = pm.getObjectById(HasKeyPkJDO.class, key);
338    pojo.setKey(null);
339    try {
340      commitTxn();
341      fail("expected exception");
342    } catch (JDOFatalUserException e) {
343      // good
344      rollbackTxn();
345    }
346    assertEquals(1, countForClass(HasKeyPkJDO.class));
347  }
348
349  public void testUpdateList_Add() throws EntityNotFoundException {
350    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
351    List<String> list = Utils.newArrayList("a", "b");
352    pojo.setStrList(list);
353    beginTxn();
354    pm.makePersistent(pojo);
355    commitTxn();
356    pm.close();
357    pm = pmf.getPersistenceManager();
358    beginTxn();
359    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
360    pojo.getStrList().add("zoom");
361    commitTxn();
362    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
363    assertEquals(3, ((List<?>)e.getProperty("strList")).size());
364  }
365
366  public void testUpdateList_Reset() throws EntityNotFoundException {
367    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
368    List<String> list = Utils.newArrayList("a", "b");
369    pojo.setStrList(list);
370    beginTxn();
371    pm.makePersistent(pojo);
372    commitTxn();
373    pm.close();
374    pm = pmf.getPersistenceManager();
375    beginTxn();
376    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
377    list = Utils.newArrayList("a", "b", "zoom");
378    pojo.setStrList(list);
379    commitTxn();
380    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
381    assertEquals(3, ((List<?>)e.getProperty("strList")).size());
382  }
383
384  public void testUpdateCollection_Add() throws EntityNotFoundException {
385    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
386    List<Integer> list = Utils.newArrayList(2, 3);
387    pojo.setIntColl(list);
388    beginTxn();
389    pm.makePersistent(pojo);
390    commitTxn();
391    pm.close();
392    pm = pmf.getPersistenceManager();
393    beginTxn();
394    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
395    pojo.getIntColl().add(4);
396    commitTxn();
397    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
398    assertEquals(3, ((List<?>)e.getProperty("intColl")).size());
399  }
400
401  public void testUpdateCollection_Reset() throws EntityNotFoundException {
402    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
403    List<Integer> list = Utils.newArrayList(2, 3);
404    pojo.setIntColl(list);
405    beginTxn();
406    pm.makePersistent(pojo);
407    commitTxn();
408    pm.close();
409    pm = pmf.getPersistenceManager();
410    beginTxn();
411    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
412    list = Utils.newArrayList(2, 3, 4);
413    pojo.setIntColl(list);
414    commitTxn();
415    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
416    assertEquals(3, ((List<?>)e.getProperty("intColl")).size());
417  }
418
419  public void testUpdateArrayList_Add() throws EntityNotFoundException {
420    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
421    ArrayList<String> list = Utils.newArrayList("a", "b");
422    pojo.setStrArrayList(list);
423    beginTxn();
424    pm.makePersistent(pojo);
425    commitTxn();
426    pm.close();
427    pm = pmf.getPersistenceManager();
428    beginTxn();
429    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
430    pojo.getStrArrayList().add("zoom");
431    commitTxn();
432    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
433    assertEquals(3, ((List<?>)e.getProperty("strArrayList")).size());
434  }
435
436  public void testUpdateArrayList_Reset() throws EntityNotFoundException {
437    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
438    ArrayList<String> list = Utils.newArrayList("a", "b");
439    pojo.setStrArrayList(list);
440    beginTxn();
441    pm.makePersistent(pojo);
442    commitTxn();
443    pm.close();
444    pm = pmf.getPersistenceManager();
445    beginTxn();
446    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
447    list = Utils.newArrayList("a", "b", "zoom");
448    pojo.setStrArrayList(list);
449    commitTxn();
450    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
451    assertEquals(3, ((List<?>)e.getProperty("strArrayList")).size());
452  }
453
454  public void testUpdateLinkedList_Add() throws EntityNotFoundException {
455    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
456    LinkedList<String> list = Utils.newLinkedList("a", "b");
457    pojo.setStrLinkedList(list);
458    beginTxn();
459    pm.makePersistent(pojo);
460    commitTxn();
461    pm.close();
462    pm = pmf.getPersistenceManager();
463    beginTxn();
464    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
465    pojo.getStrLinkedList().add("zoom");
466    commitTxn();
467    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
468    assertEquals(3, ((List<?>)e.getProperty("strLinkedList")).size());
469  }
470
471  public void testUpdateLinkedList_Reset() throws EntityNotFoundException {
472    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
473    LinkedList<String> list = Utils.newLinkedList("a", "b");
474    pojo.setStrLinkedList(list);
475    beginTxn();
476    pm.makePersistent(pojo);
477    commitTxn();
478    pm.close();
479    pm = pmf.getPersistenceManager();
480    beginTxn();
481    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
482    list = Utils.newLinkedList("a", "b", "zoom");
483    pojo.setStrLinkedList(list);
484    commitTxn();
485    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
486    assertEquals(3, ((List<?>)e.getProperty("strLinkedList")).size());
487  }
488
489  public void testUpdateSet_Add() throws EntityNotFoundException {
490    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
491    Set<String> set = Utils.newHashSet("a", "b");
492    pojo.setStrSet(set);
493    beginTxn();
494    pm.makePersistent(pojo);
495    commitTxn();
496    pm.close();
497    pm = pmf.getPersistenceManager();
498    beginTxn();
499    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
500    pojo.getStrSet().add("zoom");
501    commitTxn();
502    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
503    assertEquals(3, ((List<?>)e.getProperty("strSet")).size());
504  }
505
506  public void testUpdateSet_Reset() throws EntityNotFoundException {
507    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
508    Set<String> set = Utils.newHashSet("a", "b");
509    pojo.setStrSet(set);
510    beginTxn();
511    pm.makePersistent(pojo);
512    commitTxn();
513    pm.close();
514    pm = pmf.getPersistenceManager();
515    beginTxn();
516    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
517    set = Utils.newHashSet("a", "b", "zoom");
518    pojo.setStrSet(set);
519    commitTxn();
520    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
521    assertEquals(3, ((List<?>)e.getProperty("strSet")).size());
522  }
523
524  public void testUpdateHashSet_Add() throws EntityNotFoundException {
525    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
526    HashSet<String> set = Utils.newHashSet("a", "b");
527    pojo.setStrHashSet(set);
528    beginTxn();
529    pm.makePersistent(pojo);
530    commitTxn();
531    pm.close();
532    pm = pmf.getPersistenceManager();
533    beginTxn();
534    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
535    pojo.getStrHashSet().add("zoom");
536    commitTxn();
537    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
538    assertEquals(3, ((List<?>)e.getProperty("strHashSet")).size());
539  }
540
541  public void testUpdateHashSet_Reset() throws EntityNotFoundException {
542    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
543    HashSet<String> set = Utils.newHashSet("a", "b");
544    pojo.setStrHashSet(set);
545    beginTxn();
546    pm.makePersistent(pojo);
547    commitTxn();
548    pm.close();
549    pm = pmf.getPersistenceManager();
550    beginTxn();
551    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
552    set = Utils.newHashSet("a", "b", "zoom");
553    pojo.setStrHashSet(set);
554    commitTxn();
555    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
556    assertEquals(3, ((List<?>)e.getProperty("strHashSet")).size());
557  }
558
559  public void testUpdateLinkedHashSet_Add() throws EntityNotFoundException {
560    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
561    LinkedHashSet<String> set = Utils.newLinkedHashSet("a", "b");
562    pojo.setStrLinkedHashSet(set);
563    beginTxn();
564    pm.makePersistent(pojo);
565    commitTxn();
566    pm.close();
567    pm = pmf.getPersistenceManager();
568    beginTxn();
569    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
570    pojo.getStrLinkedHashSet().add("zoom");
571    commitTxn();
572    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
573    assertEquals(3, ((List<?>)e.getProperty("strLinkedHashSet")).size());
574  }
575
576  public void testUpdateLinkedHashSet_Reset() throws EntityNotFoundException {
577    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
578    LinkedHashSet<String> set = Utils.newLinkedHashSet("a", "b");
579    pojo.setStrLinkedHashSet(set);
580    beginTxn();
581    pm.makePersistent(pojo);
582    commitTxn();
583    pm.close();
584    pm = pmf.getPersistenceManager();
585    beginTxn();
586    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
587    set = Utils.newLinkedHashSet("a", "b", "zoom");
588    pojo.setStrLinkedHashSet(set);
589    commitTxn();
590    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
591    assertEquals(3, ((List<?>)e.getProperty("strLinkedHashSet")).size());
592  }
593
594  public void testUpdateTreeSet_Add() throws EntityNotFoundException {
595    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
596    TreeSet<String> set = Utils.newTreeSet("a", "b");
597    pojo.setStrTreeSet(set);
598    beginTxn();
599    pm.makePersistent(pojo);
600    commitTxn();
601    pm.close();
602    pm = pmf.getPersistenceManager();
603    beginTxn();
604    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
605    pojo.getStrTreeSet().add("zoom");
606    commitTxn();
607    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
608    assertEquals(3, ((List<?>)e.getProperty("strTreeSet")).size());
609  }
610
611  public void testUpdateTreeSet_Reset() throws EntityNotFoundException {
612    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
613    TreeSet<String> set = Utils.newTreeSet("a", "b");
614    pojo.setStrTreeSet(set);
615    beginTxn();
616    pm.makePersistent(pojo);
617    commitTxn();
618    pm.close();
619    pm = pmf.getPersistenceManager();
620    beginTxn();
621    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
622    set = Utils.newTreeSet("a", "b", "zoom");
623    pojo.setStrTreeSet(set);
624    commitTxn();
625    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
626    assertEquals(3, ((List<?>)e.getProperty("strTreeSet")).size());
627  }
628
629  public void testUpdateSortedSet_Add() throws EntityNotFoundException {
630    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
631    SortedSet<String> set = Utils.newTreeSet("a", "b");
632    pojo.setStrSortedSet(set);
633    beginTxn();
634    pm.makePersistent(pojo);
635    commitTxn();
636    pm.close();
637    pm = pmf.getPersistenceManager();
638    beginTxn();
639    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
640    pojo.getStrSortedSet().add("zoom");
641    commitTxn();
642    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
643    assertEquals(3, ((List<?>)e.getProperty("strSortedSet")).size());
644  }
645
646  public void testUpdateSortedSet_Reset() throws EntityNotFoundException {
647    HasMultiValuePropsJDO pojo = new HasMultiValuePropsJDO();
648    SortedSet<String> set = Utils.newTreeSet("a", "b");
649    pojo.setStrSortedSet(set);
650    beginTxn();
651    pm.makePersistent(pojo);
652    commitTxn();
653    pm.close();
654    pm = pmf.getPersistenceManager();
655    beginTxn();
656    pojo = pm.getObjectById(HasMultiValuePropsJDO.class, pojo.getId());
657    set = Utils.newTreeSet("a", "b", "zoom");
658    pojo.setStrSortedSet(set);
659    commitTxn();
660    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
661    assertEquals(3, ((List<?>)e.getProperty("strSortedSet")).size());
662  }
663
664  public void testUpdateArray_Reset() throws EntityNotFoundException {
665    NullDataJDO pojo = new NullDataJDO();
666    String[] array = new String[] {"a", "b"};
667    pojo.setArray(array);
668    beginTxn();
669    pm.makePersistent(pojo);
670    commitTxn();
671    pm.close();
672    pm = pmf.getPersistenceManager();
673    beginTxn();
674    pojo = pm.getObjectById(NullDataJDO.class, pojo.getId());
675    array = new String[] {"a", "b", "c"};
676    pojo.setArray(array);
677    commitTxn();
678    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
679    assertEquals(3, ((List<?>)e.getProperty("array")).size());
680  }
681
682  // There's really no way to make this pass since we can't intercept
683  // calls that modify array elements.
684  public void xxxtestUpdateArray_ModifyExistingElement() throws EntityNotFoundException {
685    NullDataJDO pojo = new NullDataJDO();
686    String[] array = new String[] {"a", "b"};
687    pojo.setArray(array);
688    beginTxn();
689    pm.makePersistent(pojo);
690    commitTxn();
691    pm.close();
692    pm = pmf.getPersistenceManager();
693    beginTxn();
694    pojo = pm.getObjectById(NullDataJDO.class, pojo.getId());
695    pojo.getArray()[0] = "c";
696    pojo.setArray(array);
697    commitTxn();
698    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
699    assertEquals("c", ((List<?>)e.getProperty("array")).get(0));
700  }
701
702  public void testEmbeddable() throws EntityNotFoundException {
703    Person p = new Person();
704    p.setName(new Name());
705    p.getName().setFirst("jimmy");
706    p.getName().setLast("jam");
707    p.setAnotherName(new Name());
708    p.getAnotherName().setFirst("anotherjimmy");
709    p.getAnotherName().setLast("anotherjam");
710    makePersistentInTxn(p, TXN_START_END);
711
712    assertNotNull(p.getId());
713
714    beginTxn();
715    p = pm.getObjectById(Person.class, p.getId());
716    p.getName().setLast("not jam");
717    p.getName().setFirst("not jimmy");
718    commitTxn();
719
720    Entity entity = ds.get(TestUtils.createKey(p, p.getId()));
721    assertNotNull(entity);
722    assertEquals("not jimmy", entity.getProperty("first"));
723    assertEquals("not jam", entity.getProperty("last"));
724    assertEquals("anotherjimmy", entity.getProperty("anotherFirst"));
725    assertEquals("anotherjam", entity.getProperty("anotherLast"));
726  }
727
728  public void testUpdateStrPrimaryKey_SetNewName() {
729    Key key = ds.put(Flight.newFlightEntity("name", "bos", "mia", 3, 4, 44));
730
731    beginTxn();
732    Flight f = pm.getObjectById(Flight.class, key.getId());
733    f.setId("other");
734    pm.makePersistent(f);
735    try {
736      commitTxn();
737      fail("expected exception");
738    } catch (JDOFatalUserException e) {
739      // good
740      rollbackTxn();
741    }
742  }
743
744  public void testUpdateStrPrimaryKey_SetNewKey() {
745    Key key = ds.put(Flight.newFlightEntity("name", "bos", "mia", 3, 4, 44));
746
747    beginTxn();
748    Flight f = pm.getObjectById(Flight.class, key.getId());
749    f.setId(KeyFactory.keyToString(KeyFactory.createKey(key.getKind(), "jimmy")));
750    pm.makePersistent(f);
751    try {
752      commitTxn();
753      fail("expected exception");
754    } catch (JDOFatalUserException e) {
755      // good
756      rollbackTxn();
757    }
758  }
759
760  public void testUpdateStrPrimaryKey_NullKey() {
761    Key key = ds.put(Flight.newFlightEntity("name", "bos", "mia", 3, 4, 44));
762
763    beginTxn();
764    Flight f = pm.getObjectById(Flight.class, key.getId());
765    f.setId(null);
766    pm.makePersistent(f);
767    try {
768      commitTxn();
769      fail("expected exception");
770    } catch (JDOFatalUserException e) {
771      // good
772      rollbackTxn();
773    }
774  }
775
776  public void testUpdateStrAncestor_SetNewName() {
777    Key parentKey = ds.put(new Entity(String.class.getSimpleName()));
778    Key key = ds.put(new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentKey));
779
780    beginTxn();
781    HasStringAncestorStringPkJDO pojo = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(key));
782    pojo.setAncestorId("other");
783    try {
784      commitTxn();
785      fail("expected exception");
786    } catch (JDOFatalUserException e) {
787      // good
788      rollbackTxn();
789    }
790  }
791
792  public void testUpdateStrAncestor_SetNewKey() {
793    Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
794    Key key = ds.put(new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentKey));
795
796    beginTxn();
797    HasStringAncestorStringPkJDO pojo = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(key));
798    pojo.setAncestorId(KeyFactory.keyToString(KeyFactory.createKey(key.getKind(), 33)));
799
800    try {
801      commitTxn();
802      fail("expected exception");
803    } catch (JDOFatalUserException e) {
804      // good
805      rollbackTxn();
806    }
807  }
808
809  public void testUpdateStrAncestor_NullKey() {
810    Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
811    Key key = ds.put(new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentKey));
812
813    beginTxn();
814    HasStringAncestorStringPkJDO pojo = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(key));
815    pojo.setAncestorId(null);
816    try {
817      commitTxn();
818      fail("expected exception");
819    } catch (JDOFatalUserException e) {
820      // good
821      rollbackTxn();
822    }
823  }
824
825  public void testUpdateKeyAncestor_SetNewKey() {
826    Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
827    Key key = ds.put(new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), parentKey));
828
829    beginTxn();
830    HasKeyAncestorStringPkJDO pojo = pm.getObjectById(
831        HasKeyAncestorStringPkJDO.class, KeyFactory.keyToString(key));
832    pojo.setAncestorKey(KeyFactory.createKey(key.getKind(), 33));
833
834    try {
835      commitTxn();
836      fail("expected exception");
837    } catch (JDOFatalUserException e) {
838      // good
839      rollbackTxn();
840    }
841  }
842
843  public void testUpdateKeyAncestor_NullKey() {
844    Key parentKey = ds.put(new Entity(Flight.class.getSimpleName()));
845    Key key = ds.put(new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), parentKey));
846
847    beginTxn();
848    HasKeyAncestorStringPkJDO pojo = pm.getObjectById(
849        HasKeyAncestorStringPkJDO.class, KeyFactory.keyToString(key));
850    pojo.setAncestorKey(null);
851    try {
852      commitTxn();
853      fail("expected exception");
854    } catch (JDOFatalUserException e) {
855      // good
856      rollbackTxn();
857    }
858  }
859}