Search in sources :

Example 91 with UnitTestClosure

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());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) DataObject(org.apache.cayenne.DataObject) Test(org.junit.Test)

Example 92 with UnitTestClosure

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"));
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 93 with UnitTestClosure

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());
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 94 with UnitTestClosure

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"));
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 95 with UnitTestClosure

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

Aggregations

UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)107 Test (org.junit.Test)107 SelectQuery (org.apache.cayenne.query.SelectQuery)64 List (java.util.List)48 Artist (org.apache.cayenne.testdo.testmap.Artist)47 Painting (org.apache.cayenne.testdo.testmap.Painting)35 ValueHolder (org.apache.cayenne.ValueHolder)23 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)19 ArrayList (java.util.ArrayList)15 ObjectContext (org.apache.cayenne.ObjectContext)14 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)9 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)8 Iterator (java.util.Iterator)7 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)7 Expression (org.apache.cayenne.exp.Expression)6 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)6 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)6 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)5 RemoteIncrementalFaultList (org.apache.cayenne.remote.RemoteIncrementalFaultList)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5