use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ShallowMergeOperationIT method testMerge_NoOverride.
@Test
public void testMerge_NoOverride() throws Exception {
createArtistsDataSet();
ObjectContext childContext = runtime.newContext(context);
final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
int modifiedId = 33003;
final Artist modified = (Artist) Cayenne.objectForQuery(context, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
final Artist peerModified = (Artist) Cayenne.objectForQuery(childContext, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
modified.setArtistName("M1");
peerModified.setArtistName("M2");
assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, peerModified.getPersistenceState());
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
Persistent peerModified2 = op.merge(modified);
assertSame(peerModified, peerModified2);
assertEquals(PersistenceState.MODIFIED, peerModified2.getPersistenceState());
assertEquals("M2", peerModified.getArtistName());
assertEquals("M1", modified.getArtistName());
}
});
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class NestedDataContextReadIT method testSelect.
@Test
public void testSelect() throws Exception {
createArtistsDataSet();
ObjectContext child = runtime.newContext(context);
// test how different object states appear in the child on select
Persistent _new = context.newObject(Artist.class);
Persistent hollow = Cayenne.objectForPK(context, Artist.class, 33001);
context.invalidateObjects(hollow);
DataObject committed = Cayenne.objectForPK(context, Artist.class, 33002);
Artist modified = Cayenne.objectForPK(context, Artist.class, 33003);
modified.setArtistName("MODDED");
DataObject deleted = Cayenne.objectForPK(context, Artist.class, 33004);
context.deleteObjects(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());
List<Artist> objects = new SelectQuery<>(Artist.class).select(child);
assertEquals("All but NEW object must have been included", 4, objects.size());
for (Artist next : objects) {
assertEquals(PersistenceState.COMMITTED, next.getPersistenceState());
int id = Cayenne.intPKForObject(next);
if (id == 33003) {
assertEquals("MODDED", next.getArtistName());
}
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DataContextSerializationIT method testSerializeWithLocalCache.
@Test
public void testSerializeWithLocalCache() throws Exception {
createSingleArtistDataSet();
// manually assemble a DataContext with local cache....
DataDomain domain = context.getParentDataDomain();
DataRowStore snapshotCache = new DataRowStore(domain.getName(), new DefaultRuntimeProperties(domain.getProperties()), domain.getEventManager());
Map<Object, Persistent> map = new HashMap<>();
DataContext localCacheContext = new DataContext(domain, new ObjectStore(snapshotCache, map));
localCacheContext.setValidatingObjectsOnCommit(domain.isValidatingObjectsOnCommit());
localCacheContext.setUsingSharedSnapshotCache(false);
assertNotSame(domain.getSharedSnapshotCache(), localCacheContext.getObjectStore().getDataRowCache());
DataContext deserializedContext = Util.cloneViaSerialization(localCacheContext);
assertNotSame(localCacheContext, deserializedContext);
assertNotSame(localCacheContext.getObjectStore(), deserializedContext.getObjectStore());
assertSame(localCacheContext.getParentDataDomain(), deserializedContext.getParentDataDomain());
assertNotSame(localCacheContext.getObjectStore().getDataRowCache(), deserializedContext.getObjectStore().getDataRowCache());
assertNotSame(deserializedContext.getParentDataDomain().getSharedSnapshotCache(), deserializedContext.getObjectStore().getDataRowCache());
Artist a = Cayenne.objectForPK(deserializedContext, Artist.class, 33001);
assertNotNull(a);
a.setArtistName(a.getArtistName() + "___");
// this blows per CAY-796
deserializedContext.commitChanges();
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DataDomainCallbacksIT method testPostPersist.
@Test
public void testPostPersist() {
LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
Artist a1 = context.newObject(Artist.class);
a1.setArtistName("XX");
context.commitChanges();
assertFalse(a1.isPostPersisted());
registry.addCallback(LifecycleEvent.POST_PERSIST, Artist.class, "postPersistCallback");
MockCallingBackListener listener2 = new MockCallingBackListener() {
@Override
public void publicCallback(Object entity) {
super.publicCallback(entity);
assertFalse(((Persistent) entity).getObjectId().isTemporary());
}
};
registry.addListener(LifecycleEvent.POST_PERSIST, Artist.class, listener2, "publicCallback");
Artist a2 = context.newObject(Artist.class);
a2.setArtistName("XX");
context.commitChanges();
assertFalse(a1.isPostPersisted());
assertTrue(a2.isPostPersisted());
assertSame(a2, listener2.getPublicCalledbackEntity());
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class SelectQueryMetadataCacheKeyTest method cacheKeyWithPersistentObject.
/**
* Persistent objects should be converted to their ObjectIds.
*/
@Test
public void cacheKeyWithPersistentObject() {
Persistent persistent1 = mock(Persistent.class);
ObjectId objectId1 = mock(ObjectId.class);
when(objectId1.toString()).thenReturn("objId1");
when(persistent1.getObjectId()).thenReturn(objectId1);
Persistent persistent2 = mock(Persistent.class);
ObjectId objectId2 = mock(ObjectId.class);
when(objectId2.toString()).thenReturn("objId2");
when(persistent2.getObjectId()).thenReturn(objectId2);
ExpressionFactory.greaterOrEqualExp("testPath", persistent1).traverse(newHandler());
String s1 = cacheKey.toString();
ExpressionFactory.greaterOrEqualExp("testPath", persistent1).traverse(newHandler());
String s2 = cacheKey.toString();
ExpressionFactory.greaterOrEqualExp("testPath", persistent2).traverse(newHandler());
String s3 = cacheKey.toString();
assertTrue(s1.contains("objId1"));
assertTrue(s3.contains("objId2"));
assertEquals(s1, s2);
assertNotEquals(s2, s3);
}
Aggregations