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());
}
});
}
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());
}
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());
}
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"));
}
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);
}
Aggregations