use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class SimpleIdIncrementalFaultListPrefetchIT method testPrefetch4.
/**
* Test that a to-one relationship is initialized.
*/
@Test
public void testPrefetch4() throws Exception {
createArtistsAndPaintingsDataSet();
SelectQuery q = new SelectQuery(Painting.class);
q.setPageSize(3);
q.addPrefetch("toArtist");
IncrementalFaultList<?> result = (IncrementalFaultList) context.performQuery(q);
// get an objects from the second page
final DataObject p1 = (DataObject) result.get(q.getPageSize());
queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
Object toOnePrefetch = p1.readNestedProperty("toArtist");
assertNotNull(toOnePrefetch);
assertTrue("Expected DataObject, got: " + toOnePrefetch.getClass().getName(), toOnePrefetch instanceof DataObject);
DataObject a1 = (DataObject) toOnePrefetch;
assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class SingleTableInheritanceIT method testRelationshipAbstractFromSuperPrefetchingJoint.
@Test
public void testRelationshipAbstractFromSuperPrefetchingJoint() {
context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (3, 'AA', 'EE')"));
context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'AA', 3)"));
context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (4, 'BB', 3)"));
SelectQuery query = new SelectQuery(AbstractPerson.class);
query.addPrefetch(AbstractPerson.NOTES.joint());
final AbstractPerson person = (AbstractPerson) Cayenne.objectForQuery(context, query);
assertTrue(person instanceof Employee);
queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(2, person.getNotes().size());
String[] names = new String[2];
names[0] = person.getNotes().get(0).getNotes();
names[1] = person.getNotes().get(1).getNotes();
List<String> nameSet = Arrays.asList(names);
assertTrue(nameSet.contains("AA"));
assertTrue(nameSet.contains("BB"));
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class SingleTableInheritanceIT method testRelationshipAbstractToSuperPrefetchingJoint.
@Test
public void testRelationshipAbstractToSuperPrefetchingJoint() {
context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (3, 'AA', 'EE')"));
context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'AA', 3)"));
SelectQuery query = new SelectQuery(PersonNotes.class);
query.addPrefetch(PersonNotes.PERSON.joint());
final PersonNotes note = (PersonNotes) Cayenne.objectForQuery(context, query);
queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals("AA", note.getPerson().getName());
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.
the class SingleTableInheritanceIT method testRelationshipAbstractFromSuperPrefetchingDisjoint.
@Test
public void testRelationshipAbstractFromSuperPrefetchingDisjoint() {
context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (3, 'AA', 'EE')"));
context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'AA', 3)"));
context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (4, 'BB', 3)"));
SelectQuery query = new SelectQuery(AbstractPerson.class);
query.addPrefetch(AbstractPerson.NOTES.disjoint());
final AbstractPerson person = (AbstractPerson) Cayenne.objectForQuery(context, query);
assertTrue(person instanceof Employee);
queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {
public void execute() {
assertEquals(2, person.getNotes().size());
String[] names = new String[2];
names[0] = person.getNotes().get(0).getNotes();
names[1] = person.getNotes().get(1).getNotes();
List<String> nameSet = Arrays.asList(names);
assertTrue(nameSet.contains("AA"));
assertTrue(nameSet.contains("BB"));
}
});
}
use of org.apache.cayenne.unit.di.UnitTestClosure 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());
}
});
}
Aggregations