Search in sources :

Example 61 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ShallowMergeOperationIT method testMerge_PersistenceStates.

@Test
public void testMerge_PersistenceStates() throws Exception {
    createArtistsDataSet();
    final ObjectContext childContext = runtime.newContext(context);
    final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
    final Artist _new = context.newObject(Artist.class);
    final Artist hollow = Cayenne.objectForPK(context, Artist.class, 33001);
    context.invalidateObjects(hollow);
    final Artist committed = Cayenne.objectForPK(context, Artist.class, 33002);
    final Artist modified = Cayenne.objectForPK(context, Artist.class, 33003);
    modified.setArtistName("M1");
    final Artist 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());
    // now check how objects in different state behave
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Persistent newPeer = op.merge(_new);
            assertEquals(_new.getObjectId(), newPeer.getObjectId());
            assertEquals(PersistenceState.COMMITTED, newPeer.getPersistenceState());
            assertSame(childContext, newPeer.getObjectContext());
            assertSame(context, _new.getObjectContext());
            Persistent hollowPeer = op.merge(hollow);
            assertEquals(PersistenceState.HOLLOW, hollowPeer.getPersistenceState());
            assertEquals(hollow.getObjectId(), hollowPeer.getObjectId());
            assertSame(childContext, hollowPeer.getObjectContext());
            assertSame(context, hollow.getObjectContext());
            Persistent committedPeer = op.merge(committed);
            assertEquals(PersistenceState.COMMITTED, committedPeer.getPersistenceState());
            assertEquals(committed.getObjectId(), committedPeer.getObjectId());
            assertSame(childContext, committedPeer.getObjectContext());
            assertSame(context, committed.getObjectContext());
            Artist modifiedPeer = op.merge(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 = op.merge(deleted);
            assertEquals(PersistenceState.COMMITTED, deletedPeer.getPersistenceState());
            assertEquals(deleted.getObjectId(), deletedPeer.getObjectId());
            assertSame(childContext, deletedPeer.getObjectContext());
            assertSame(context, deleted.getObjectContext());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 62 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class RefreshQueryTest method testObjectConstructor.

@Test
public void testObjectConstructor() {
    Persistent p = new MockPersistentObject();
    RefreshQuery q = new RefreshQuery(p);
    assertNotNull(q.getObjects());
    assertEquals(1, q.getObjects().size());
    assertSame(p, q.getObjects().iterator().next());
    assertNull(q.getQuery());
    assertNull(q.getGroupKeys());
    assertFalse(q.isRefreshAll());
}
Also used : MockPersistentObject(org.apache.cayenne.MockPersistentObject) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 63 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ASTListTest method testHashCode.

@Test
public void testHashCode() throws Exception {
    ObjectId objectId = new ObjectId("Artist", "ARTIST_ID", 1);
    Persistent artist = mock(Persistent.class);
    when(artist.getObjectId()).thenReturn(objectId);
    ASTList exp = new ASTList(Collections.singletonList(artist));
    List<Persistent> collection = new ArrayList<>();
    collection.add(artist);
    ASTList exp2 = new ASTList(collection);
    ASTList exp3 = new ASTList(Collections.emptyList());
    assertEquals(exp.hashCode(), exp2.hashCode());
    assertNotEquals(exp.hashCode(), exp3.hashCode());
}
Also used : ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 64 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class EvaluatorTest method testEvaluator_Persistent_StringId.

@Test
public void testEvaluator_Persistent_StringId() {
    ObjectId lhsId = new ObjectId("X", "k", "A");
    Persistent lhs = mock(Persistent.class);
    when(lhs.getObjectId()).thenReturn(lhsId);
    Evaluator e = Evaluator.evaluator(lhs);
    assertNotNull(e);
    ObjectId rhsId1 = new ObjectId("X", "k", "A");
    Persistent rhs1 = mock(Persistent.class);
    when(rhs1.getObjectId()).thenReturn(rhsId1);
    assertTrue(e.eq(lhs, rhs1));
    assertTrue(e.eq(lhs, rhsId1));
    assertTrue(e.eq(lhs, "A"));
    ObjectId rhsId2 = new ObjectId("X", "k", "B");
    Persistent rhs2 = mock(Persistent.class);
    when(rhs2.getObjectId()).thenReturn(rhsId2);
    assertFalse(e.eq(lhs, rhs2));
    assertFalse(e.eq(lhs, rhsId2));
    assertFalse(e.eq(lhs, "B"));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 65 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ClientServerChannelQueryAction method convertSingleObject.

private Object convertSingleObject(PrefetchTreeNode prefetchTree, ObjectDetachOperation op, Object serverObject) {
    Persistent object = (Persistent) serverObject;
    ObjectId id = object.getObjectId();
    // sanity check
    if (id == null) {
        throw new CayenneRuntimeException("Server returned an object without an id: %s", object);
    }
    // have to resolve descriptor here for every object, as
    // often a query will not have any info indicating the
    // entity type
    ClassDescriptor serverDescriptor = serverResolver.getClassDescriptor(id.getEntityName());
    return op.detach(object, serverDescriptor, prefetchTree);
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent)

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