Search in sources :

Example 56 with UnitTestClosure

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

the class CayenneContextGraphDiffCompressorIT method testDelete.

@Test
public void testDelete() {
    ClientMtTable1 o1 = context.newObject(ClientMtTable1.class);
    o1.setGlobalAttribute1("v1");
    context.deleteObjects(o1);
    DataChannelSyncStats stats = clientServerInterceptor.runWithSyncStatsCollection(new UnitTestClosure() {

        public void execute() {
            context.commitChanges();
        }
    });
    assertEquals(0, stats.nodePropertiesChanged);
    assertEquals(0, stats.nodesCreated);
    assertEquals(0, stats.nodesRemoved);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) DataChannelSyncStats(org.apache.cayenne.unit.di.DataChannelSyncStats) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Test(org.junit.Test)

Example 57 with UnitTestClosure

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

the class FlattenedPrefetchIT method testManyToMany.

@Test
public void testManyToMany() throws Exception {
    createPrefetchDataSet1();
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.GROUP_ARRAY.disjoint());
    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 58 with UnitTestClosure

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

the class FlattenedPrefetchIT method testMultiPrefetch.

@Test
public void testMultiPrefetch() throws Exception {
    createPrefetchDataSet2();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    q.addPrefetch(Painting.TO_ARTIST.dot(Artist.GROUP_ARRAY).disjoint());
    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)

Example 59 with UnitTestClosure

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

the class NestedDataContextWriteIT method testCommitChangesToParent_MergeProperties.

@Test
public void testCommitChangesToParent_MergeProperties() throws Exception {
    createMixedDataSet();
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    // make sure we fetch in predictable order
    SelectQuery query = new SelectQuery(Painting.class);
    query.addOrdering(Painting.PAINTING_TITLE.asc());
    List<?> objects = childContext.performQuery(query);
    assertEquals(6, objects.size());
    final Painting childModifiedSimple = (Painting) objects.get(0);
    childModifiedSimple.setPaintingTitle("C_PT");
    final Painting childModifiedToOne = (Painting) objects.get(1);
    childModifiedToOne.setToArtist(childModifiedSimple.getToArtist());
    final Artist childModifiedToMany = ((Painting) objects.get(2)).getToArtist();
    // ensure painting array is fully resolved...
    childModifiedToMany.getPaintingArray().size();
    childModifiedToMany.addToPaintingArray((Painting) objects.get(3));
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Painting parentModifiedSimple = null;
            Artist parentModifiedToMany = null;
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childModifiedSimple.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childModifiedToOne.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childModifiedToMany.getPersistenceState());
            parentModifiedSimple = (Painting) context.getGraphManager().getNode(childModifiedSimple.getObjectId());
            Painting parentModifiedToOne = (Painting) context.getGraphManager().getNode(childModifiedToOne.getObjectId());
            parentModifiedToMany = (Artist) context.getGraphManager().getNode(childModifiedToMany.getObjectId());
            assertNotNull(parentModifiedSimple);
            assertEquals(PersistenceState.MODIFIED, parentModifiedSimple.getPersistenceState());
            assertEquals("C_PT", parentModifiedSimple.getPaintingTitle());
            assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModifiedSimple.getObjectId()));
            assertNotNull(parentModifiedToOne);
            assertEquals(PersistenceState.MODIFIED, parentModifiedToOne.getPersistenceState());
            assertNotNull(parentModifiedToOne.getToArtist());
            assertEquals(33001, Cayenne.intPKForObject(parentModifiedToOne.getToArtist()));
            assertNotNull(context.getObjectStore().getChangesByObjectId().get(parentModifiedToOne.getObjectId()));
            // indirectly modified....
            assertNotNull(parentModifiedToMany);
            assertEquals(PersistenceState.MODIFIED, parentModifiedToMany.getPersistenceState());
            // here query is expected, as the parent was hollow and its to-many
            // relationship
            // is unresolved
            List<?> paintings = parentModifiedToMany.getPaintingArray();
            assertEquals(2, paintings.size());
        }
    });
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 60 with UnitTestClosure

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

the class NestedDataContextWriteIT method testCommitChangesToParentFlattenedMultipleFlush.

@Test
public void testCommitChangesToParentFlattenedMultipleFlush() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Artist childO1 = childContext.newObject(Artist.class);
    childO1.setArtistName("o1");
    final ArtGroup childO2 = childContext.newObject(ArtGroup.class);
    childO2.setName("o2");
    childO2.addToArtistArray(childO1);
    childContext.commitChangesToParent();
    final ArtGroup childO3 = childContext.newObject(ArtGroup.class);
    childO3.setName("o3");
    childO1.addToGroupArray(childO3);
    assertEquals(2, childO1.getGroupArray().size());
    assertEquals(1, childO2.getArtistArray().size());
    assertEquals(1, childO3.getArtistArray().size());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO3.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());
            ArtGroup parentO3 = (ArtGroup) context.getGraphManager().getNode(childO3.getObjectId());
            assertNotNull(parentO3);
            assertEquals(PersistenceState.NEW, parentO3.getPersistenceState());
            assertEquals(2, parentO1.getGroupArray().size());
            assertEquals(1, parentO2.getArtistArray().size());
            assertEquals(1, parentO3.getArtistArray().size());
            assertTrue(parentO2.getArtistArray().contains(parentO1));
            assertTrue(parentO3.getArtistArray().contains(parentO1));
            assertTrue(parentO1.getGroupArray().contains(parentO2));
            assertTrue(parentO1.getGroupArray().contains(parentO3));
        }
    });
    childO1.removeFromGroupArray(childO2);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childO1.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO2.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childO3.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());
            ArtGroup parentO3 = (ArtGroup) context.getGraphManager().getNode(childO3.getObjectId());
            assertNotNull(parentO3);
            assertEquals(PersistenceState.NEW, parentO3.getPersistenceState());
            assertEquals(1, parentO1.getGroupArray().size());
            assertEquals(0, parentO2.getArtistArray().size());
            assertEquals(1, parentO3.getArtistArray().size());
            assertTrue(parentO3.getArtistArray().contains(parentO1));
            assertTrue(parentO1.getGroupArray().contains(parentO3));
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) 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