Search in sources :

Example 21 with Persistent

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());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 22 with Persistent

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());
        }
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 23 with Persistent

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();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) HashMap(java.util.HashMap) Persistent(org.apache.cayenne.Persistent) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Test(org.junit.Test)

Example 24 with Persistent

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Persistent(org.apache.cayenne.Persistent) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 25 with Persistent

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);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Aggregations

Persistent (org.apache.cayenne.Persistent)88 ObjectId (org.apache.cayenne.ObjectId)38 Test (org.junit.Test)32 ArrayList (java.util.ArrayList)17 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 DataObject (org.apache.cayenne.DataObject)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)10 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)10 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)10 DataRow (org.apache.cayenne.DataRow)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 ObjectContext (org.apache.cayenne.ObjectContext)6 DbEntity (org.apache.cayenne.map.DbEntity)6 List (java.util.List)5