Search in sources :

Example 16 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class ShallowMergeOperationIT method testMerge_NoOverride.

@Test
public void testMerge_NoOverride() throws Exception {
    createArtistsDataSet();
    ObjectContext childContext = runtime.newContext(context);
    final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
    int modifiedId = 33003;
    final Artist modified = (Artist) Cayenne.objectForQuery(context, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    final Artist peerModified = (Artist) Cayenne.objectForQuery(childContext, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    modified.setArtistName("M1");
    peerModified.setArtistName("M2");
    assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, peerModified.getPersistenceState());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Persistent peerModified2 = op.merge(modified);
            assertSame(peerModified, peerModified2);
            assertEquals(PersistenceState.MODIFIED, peerModified2.getPersistenceState());
            assertEquals("M2", peerModified.getArtistName());
            assertEquals("M1", modified.getArtistName());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 17 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class SimpleIdIncrementalFaultListPrefetchIT method testPrefetch1.

/**
 * Test that all queries specified in prefetch are executed with a single prefetch
 * path.
 */
@Test
public void testPrefetch1() throws Exception {
    createArtistsAndPaintingsDataSet();
    Expression e = ExpressionFactory.likeExp("artistName", "artist1%");
    SelectQuery q = new SelectQuery("Artist", e);
    q.addPrefetch("paintingArray");
    q.setPageSize(3);
    final IncrementalFaultList<?> result = (IncrementalFaultList) context.performQuery(q);
    assertEquals(6, result.size());
    // currently queries with prefetch do not resolve their first page
    assertEquals(result.size(), result.getUnfetchedObjects());
    int count = queryInterceptor.runWithQueryCounter(new UnitTestClosure() {

        public void execute() {
            // go through the second page objects and count queries
            for (int i = 3; i < 6; i++) {
                result.get(i);
            }
        }
    });
    // within the same page only one query should've been executed
    assertEquals(1, count);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 18 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class SingleTableInheritanceIT method testRelationshipAbstractToSuperPrefetchingDisjoint.

@Test
public void testRelationshipAbstractToSuperPrefetchingDisjoint() {
    context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (2, 'AA', 'EE')"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (2, 'AA', 2)"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'BB', 2)"));
    SelectQuery query = new SelectQuery(PersonNotes.class);
    query.addPrefetch(PersonNotes.PERSON.disjoint());
    query.addOrdering(PersonNotes.NOTES.asc());
    List<PersonNotes> notes = context.performQuery(query);
    assertEquals(2, notes.size());
    final PersonNotes note = notes.get(0);
    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 19 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class FlattenedPrefetchIT method testJointManyToMany.

@Test
public void testJointManyToMany() throws Exception {
    createPrefetchDataSet1();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.GROUP_ARRAY.joint());
    final List<Artist> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Artist a : objects) {
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 20 with UnitTestClosure

use of org.apache.cayenne.unit.di.UnitTestClosure in project cayenne by apache.

the class FlattenedPrefetchIT method testJointMultiPrefetch.

@Test
public void testJointMultiPrefetch() throws Exception {
    createPrefetchDataSet2();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    q.addPrefetch(Painting.TO_ARTIST.dot(Artist.GROUP_ARRAY).joint());
    final List<Painting> objects = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(3, objects.size());
            for (Painting p : objects) {
                Artist a = p.getToArtist();
                assertEquals(PersistenceState.COMMITTED, a.getPersistenceState());
                List<ArtGroup> list = a.getGroupArray();
                assertNotNull(list);
                assertFalse("artist's groups not resolved: " + a, ((ValueHolder) list).isFault());
                assertTrue(list.size() > 0);
                for (ArtGroup g : list) {
                    assertEquals(PersistenceState.COMMITTED, g.getPersistenceState());
                }
                // assert no duplicates
                Set<ArtGroup> s = new HashSet<ArtGroup>(list);
                assertEquals(s.size(), list.size());
            }
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Set(java.util.Set) HashSet(java.util.HashSet) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ValueHolder(org.apache.cayenne.ValueHolder) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Painting(org.apache.cayenne.testdo.testmap.Painting) 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