public void testLocalObjectSynchronize() throws Exception {
deleteTestData();
createTestData("testArtists");
DataContext context = createDataContext();
ObjectContext childContext = context.createChildContext();
Persistent _new = context.newObject(Artist.class);
Persistent hollow = context.localObject(new ObjectId(
"Artist",
Artist.ARTIST_ID_PK_COLUMN,
33001), null);
DataObject committed = (DataObject) Cayenne.objectForQuery(
context,
new ObjectIdQuery(new ObjectId(
"Artist",
Artist.ARTIST_ID_PK_COLUMN,
33002)));
int modifiedId = 33003;
Artist modified = (Artist) Cayenne.objectForQuery(
context,
new ObjectIdQuery(new ObjectId(
"Artist",
Artist.ARTIST_ID_PK_COLUMN,
modifiedId)));
modified.setArtistName("M1");
DataObject deleted = (DataObject) Cayenne.objectForQuery(
context,
new ObjectIdQuery(new ObjectId(
"Artist",
Artist.ARTIST_ID_PK_COLUMN,
33004)));
context.deleteObject(deleted);
assertEquals(PersistenceState.HOLLOW, hollow.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, committed.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
assertEquals(PersistenceState.DELETED, deleted.getPersistenceState());
assertEquals(PersistenceState.NEW, _new.getPersistenceState());
// now check how objects in different state behave
blockQueries();
try {
Persistent newPeer = childContext.localObject(_new.getObjectId(), _new);
assertEquals(_new.getObjectId(), newPeer.getObjectId());
assertEquals(PersistenceState.COMMITTED, newPeer.getPersistenceState());
assertSame(childContext, newPeer.getObjectContext());
assertSame(context, _new.getObjectContext());
Persistent hollowPeer = childContext
.localObject(hollow.getObjectId(), hollow);
assertEquals(PersistenceState.HOLLOW, hollowPeer.getPersistenceState());
assertEquals(hollow.getObjectId(), hollowPeer.getObjectId());
assertSame(childContext, hollowPeer.getObjectContext());
assertSame(context, hollow.getObjectContext());
Persistent committedPeer = childContext.localObject(
committed.getObjectId(),
committed);
assertEquals(PersistenceState.COMMITTED, committedPeer.getPersistenceState());
assertEquals(committed.getObjectId(), committedPeer.getObjectId());
assertSame(childContext, committedPeer.getObjectContext());
assertSame(context, committed.getObjectContext());
Artist modifiedPeer = (Artist) childContext.localObject(modified
.getObjectId(), modified);
assertEquals(PersistenceState.COMMITTED, modifiedPeer.getPersistenceState());
assertEquals(modified.getObjectId(), modifiedPeer.getObjectId());
assertEquals("M1", modifiedPeer.getArtistName());
assertSame(childContext, modifiedPeer.getObjectContext());
assertSame(context, modified.getObjectContext());
Persistent deletedPeer = childContext.localObject(
deleted.getObjectId(),
deleted);
assertEquals(PersistenceState.COMMITTED, deletedPeer.getPersistenceState());
assertEquals(deleted.getObjectId(), deletedPeer.getObjectId());
assertSame(childContext, deletedPeer.getObjectContext());