use of org.apache.cayenne.unit.di.UnitTestClosure 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());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChangesToParentPropagatedKey.
@Test
public void testCommitChangesToParentPropagatedKey() throws Exception {
final DataContext context = createDataContext();
final ObjectContext childContext = runtime.newContext(context);
final Painting childMaster = childContext.newObject(Painting.class);
childMaster.setPaintingTitle("Master");
final PaintingInfo childDetail1 = childContext.newObject(PaintingInfo.class);
childDetail1.setTextReview("Detail1");
childDetail1.setPainting(childMaster);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
childContext.commitChangesToParent();
assertEquals(PersistenceState.COMMITTED, childMaster.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childDetail1.getPersistenceState());
Painting parentMaster = (Painting) context.getGraphManager().getNode(childMaster.getObjectId());
assertNotNull(parentMaster);
assertEquals(PersistenceState.NEW, parentMaster.getPersistenceState());
PaintingInfo parentDetail1 = (PaintingInfo) context.getGraphManager().getNode(childDetail1.getObjectId());
assertNotNull(parentDetail1);
assertEquals(PersistenceState.NEW, parentDetail1.getPersistenceState());
assertSame(parentMaster, parentDetail1.getPainting());
assertSame(parentDetail1, parentMaster.getToPaintingInfo());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class NestedDataContextWriteIT method testCommitChangesToParentFlattened.
@Test
public void testCommitChangesToParentFlattened() throws Exception {
final DataContext context = createDataContext();
final ObjectContext childContext = runtime.newContext(context);
final Artist childO1 = childContext.newObject(Artist.class);
childO1.setArtistName("Master");
final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
childO2.setName("Detail1");
childO2.addToArtistArray(childO1);
ObjEntity ent = childContext.getEntityResolver().getObjEntity("ArtGroup");
Collection<ObjRelationship> rels = ent.getDeclaredRelationships();
for (ObjRelationship rel : rels) {
System.out.println(rel.getName());
}
assertEquals(1, childO1.getGroupArray().size());
assertEquals(1, childO2.getArtistArray().size());
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
childContext.commitChangesToParent();
assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
Artist parentO1 = (Artist) context.getGraphManager().getNode(childO1.getObjectId());
assertNotNull(parentO1);
assertEquals(PersistenceState.NEW, parentO1.getPersistenceState());
ArtGroup parentO2 = (ArtGroup) context.getGraphManager().getNode(childO2.getObjectId());
assertNotNull(parentO2);
assertEquals(PersistenceState.NEW, parentO2.getPersistenceState());
assertEquals(1, parentO1.getGroupArray().size());
assertEquals(1, parentO2.getArtistArray().size());
assertTrue(parentO2.getArtistArray().contains(parentO1));
assertTrue(parentO1.getGroupArray().contains(parentO2));
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class NestedDataContextWriteIT method testNullifyToOne.
/**
* A test case for CAY-698 bug.
*/
@Test
public void testNullifyToOne() throws Exception {
createNullifyToOneDataSet();
final DataContext context = createDataContext();
final ObjectContext childContext = runtime.newContext(context);
ObjectContext childContextPeer = runtime.newContext(context);
final Painting childP1 = Cayenne.objectForPK(childContext, Painting.class, 33001);
// trigger object creation in the peer nested DC
Cayenne.objectForPK(childContextPeer, Painting.class, 33001);
childP1.setToArtist(null);
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
childContext.commitChangesToParent();
assertEquals(PersistenceState.COMMITTED, childP1.getPersistenceState());
Painting parentP1 = (Painting) context.getGraphManager().getNode(childP1.getObjectId());
assertNotNull(parentP1);
assertEquals(PersistenceState.MODIFIED, parentP1.getPersistenceState());
assertNull(parentP1.getToArtist());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class DataContextRelationshipQuery_PolymorphicIT method testPolymorphicSharedCache.
@Test
public void testPolymorphicSharedCache() throws SQLException {
// see CAY-2101... we are trying to get a snapshot from a new object in the shared cache, and then read this
// object via a relationship, so that shared cache is consulted
Employee e = context1.newObject(Employee.class);
e.setName("E1");
e.setSalary(1234.01f);
PersonNotes n = context1.newObject(PersonNotes.class);
n.setNotes("N1");
n.setPerson(e);
context1.commitChanges();
// use different context to ensure we hit shared cache for relationship resolving
final PersonNotes nPeer = Cayenne.objectForPK(context2, PersonNotes.class, Cayenne.intPKForObject(n));
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
@Override
public void execute() {
assertTrue(nPeer.getPerson() instanceof Employee);
}
});
}
Aggregations