use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class NestedDataContextReadIT method testReadToOneRelationship.
@Test
public void testReadToOneRelationship() throws Exception {
createRelationshipDataSet();
final ObjectContext child = runtime.newContext(context);
// test how different object states appear in the child on select
Painting hollowTargetSrc = Cayenne.objectForPK(context, Painting.class, 33001);
Artist hollowTarget = hollowTargetSrc.getToArtist();
Painting modifiedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33002);
Artist modifiedTarget = modifiedTargetSrc.getToArtist();
modifiedTarget.setArtistName("M1");
final Painting deletedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33003);
Artist deletedTarget = deletedTargetSrc.getToArtist();
deletedTargetSrc.setToArtist(null);
context.deleteObjects(deletedTarget);
Painting committedTargetSrc = Cayenne.objectForPK(context, Painting.class, 33004);
Artist committedTarget = committedTargetSrc.getToArtist();
committedTarget.getArtistName();
final Painting newTargetSrc = Cayenne.objectForPK(context, Painting.class, 33005);
Artist newTarget = context.newObject(Artist.class);
newTarget.setArtistName("N1");
newTargetSrc.setToArtist(newTarget);
assertEquals(PersistenceState.COMMITTED, hollowTargetSrc.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, modifiedTargetSrc.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, deletedTargetSrc.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, committedTargetSrc.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, newTargetSrc.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, hollowTarget.getPersistenceState());
assertEquals(PersistenceState.MODIFIED, modifiedTarget.getPersistenceState());
assertEquals(PersistenceState.DELETED, deletedTarget.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, committedTarget.getPersistenceState());
assertEquals(PersistenceState.NEW, newTarget.getPersistenceState());
// run an ordered query, so we can address specific objects directly by index
SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
q.addOrdering(Painting.PAINTING_TITLE.asc());
final List<Painting> childSources = q.select(child);
assertEquals(5, childSources.size());
queryInterceptor.runWithQueriesBlocked(() -> {
Painting childHollowTargetSrc = childSources.get(0);
assertSame(child, childHollowTargetSrc.getObjectContext());
Artist childHollowTarget = childHollowTargetSrc.getToArtist();
assertNotNull(childHollowTarget);
assertEquals(PersistenceState.HOLLOW, childHollowTarget.getPersistenceState());
assertSame(child, childHollowTarget.getObjectContext());
Artist childModifiedTarget = childSources.get(1).getToArtist();
assertEquals(PersistenceState.COMMITTED, childModifiedTarget.getPersistenceState());
assertSame(child, childModifiedTarget.getObjectContext());
assertEquals("M1", childModifiedTarget.getArtistName());
Painting childDeletedTargetSrc = childSources.get(2);
// make sure we got the right object...
assertEquals(deletedTargetSrc.getObjectId(), childDeletedTargetSrc.getObjectId());
Artist childDeletedTarget = childDeletedTargetSrc.getToArtist();
assertNull(childDeletedTarget);
Artist childCommittedTarget = childSources.get(3).getToArtist();
assertEquals(PersistenceState.COMMITTED, childCommittedTarget.getPersistenceState());
assertSame(child, childCommittedTarget.getObjectContext());
Painting childNewTargetSrc = childSources.get(4);
// make sure we got the right object...
assertEquals(newTargetSrc.getObjectId(), childNewTargetSrc.getObjectId());
Artist childNewTarget = childNewTargetSrc.getToArtist();
assertNotNull(childNewTarget);
assertEquals(PersistenceState.COMMITTED, childNewTarget.getPersistenceState());
assertSame(child, childNewTarget.getObjectContext());
assertEquals("N1", childNewTarget.getArtistName());
});
}
use of org.apache.cayenne.ObjectContext 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.ObjectContext in project cayenne by apache.
the class NestedDataContextValidationIT method testValidateOnCommitToParent.
@Test
public void testValidateOnCommitToParent() {
context.setValidatingObjectsOnCommit(true);
ObjectContext childContext = runtime.newContext(context);
assertTrue("Child context must have inherited the validation flag from parent", ((DataContext) childContext).isValidatingObjectsOnCommit());
Artist a1 = childContext.newObject(Artist.class);
try {
childContext.commitChangesToParent();
fail("No validation was performed");
} catch (ValidationException e) {
// expected
}
assertFalse(context.hasChanges());
a1.setArtistName("T");
childContext.commitChangesToParent();
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChangesToParentDeleted.
@Test
public void testCommitChangesToParentDeleted() throws Exception {
createArtistsDataSet();
DataContext context = createDataContext();
ObjectContext childContext = runtime.newContext(context);
// make sure we fetch in predictable order
SelectQuery query = new SelectQuery(Artist.class);
query.addOrdering(Artist.ARTIST_NAME.asc());
List<?> objects = childContext.performQuery(query);
assertEquals(4, objects.size());
// delete AND modify
Artist childDeleted = (Artist) objects.get(2);
childContext.deleteObjects(childDeleted);
childDeleted.setArtistName("DDD");
// don't block queries - on delete Cayenne may need to resolve delete rules via
// fetch
childContext.commitChangesToParent();
// * all modified child objects must be in committed state now
// * all modifications should be propagated to the parent
// * no actual commit should occur.
assertEquals(PersistenceState.TRANSIENT, childDeleted.getPersistenceState());
Artist parentDeleted = (Artist) context.getGraphManager().getNode(childDeleted.getObjectId());
assertNotNull(parentDeleted);
assertEquals(PersistenceState.DELETED, parentDeleted.getPersistenceState());
assertEquals("DDD", parentDeleted.getArtistName());
}
use of org.apache.cayenne.ObjectContext in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChangesToParent.
@Test
public void testCommitChangesToParent() throws Exception {
createArtistsDataSet();
final DataContext context = createDataContext();
final ObjectContext childContext = runtime.newContext(context);
// make sure we fetch in predictable order
SelectQuery query = new SelectQuery(Artist.class);
query.addOrdering(Artist.ARTIST_NAME.asc());
List<?> objects = childContext.performQuery(query);
assertEquals(4, objects.size());
final Artist childNew = childContext.newObject(Artist.class);
childNew.setArtistName("NNN");
final Artist childModified = (Artist) objects.get(0);
childModified.setArtistName("MMM");
final Artist childCommitted = (Artist) objects.get(1);
final Artist childHollow = (Artist) objects.get(3);
childContext.invalidateObjects(childHollow);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
childContext.commitChangesToParent();
// * all modified child objects must be in committed state now
// * all modifications should be propagated to the parent
// * no actual commit should occur.
assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
Artist parentNew = (Artist) context.getGraphManager().getNode(childNew.getObjectId());
Artist parentModified = (Artist) context.getGraphManager().getNode(childModified.getObjectId());
Artist parentCommitted = (Artist) context.getGraphManager().getNode(childCommitted.getObjectId());
Artist parentHollow = (Artist) context.getGraphManager().getNode(childHollow.getObjectId());
assertNotNull(parentNew);
assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
assertEquals("NNN", parentNew.getArtistName());
assertNotNull(parentModified);
assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
assertEquals("MMM", parentModified.getArtistName());
assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModified.getObjectId()));
assertNotNull(parentCommitted);
assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
assertNotNull(parentHollow);
// TODO: we can assert that when we figure out how nested "invalidate"
// should
// work
// assertEquals(PersistenceState.HOLLOW,
// parentHollow.getPersistenceState());
}
});
}
Aggregations