Search in sources :

Example 1 with ArtGroup

use of org.apache.cayenne.testdo.testmap.ArtGroup 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 2 with ArtGroup

use of org.apache.cayenne.testdo.testmap.ArtGroup 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)

Example 3 with ArtGroup

use of org.apache.cayenne.testdo.testmap.ArtGroup 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));
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 4 with ArtGroup

use of org.apache.cayenne.testdo.testmap.ArtGroup in project cayenne by apache.

the class DataContextPrefetchIT method testPrefetch_ReflexiveRelationship.

@Test
public void testPrefetch_ReflexiveRelationship() throws Exception {
    ArtGroup parent = (ArtGroup) context.newObject("ArtGroup");
    parent.setName("parent");
    ArtGroup child = (ArtGroup) context.newObject("ArtGroup");
    child.setName("child");
    child.setToParentGroup(parent);
    context.commitChanges();
    SelectQuery q = new SelectQuery("ArtGroup");
    q.setQualifier(ExpressionFactory.matchExp("name", "child"));
    q.addPrefetch("toParentGroup");
    final List<ArtGroup> results = context.performQuery(q);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(1, results.size());
            ArtGroup fetchedChild = results.get(0);
            // The parent must be fully fetched, not just HOLLOW (a fault)
            assertEquals(PersistenceState.COMMITTED, fetchedChild.getToParentGroup().getPersistenceState());
        }
    });
    child.setToParentGroup(null);
    context.commitChanges();
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Example 5 with ArtGroup

use of org.apache.cayenne.testdo.testmap.ArtGroup in project cayenne by apache.

the class DataContextDeleteRulesIT method testNullifyToOne.

@Test
public void testNullifyToOne() {
    // ArtGroup toParentGroup
    ArtGroup parentGroup = (ArtGroup) context.newObject("ArtGroup");
    parentGroup.setName("Parent");
    ArtGroup childGroup = (ArtGroup) context.newObject("ArtGroup");
    childGroup.setName("Child");
    parentGroup.addToChildGroupsArray(childGroup);
    // Check to make sure that the relationships are both exactly correct
    // before starting. We're not really testing this, but it is imperative
    // that it is correct before testing the real details.
    assertEquals(parentGroup, childGroup.getToParentGroup());
    assertTrue(parentGroup.getChildGroupsArray().contains(childGroup));
    // Always good to commit before deleting... bad things happen otherwise
    context.commitChanges();
    context.deleteObjects(childGroup);
    // The things we are testing.
    assertFalse(parentGroup.getChildGroupsArray().contains(childGroup));
    // Although deleted, the property should be null (good cleanup policy)
    // assertNull(childGroup.getToParentGroup());
    // And be sure that the commit works afterwards, just for sanity
    context.commitChanges();
}
Also used : ArtGroup(org.apache.cayenne.testdo.testmap.ArtGroup) Test(org.junit.Test)

Aggregations

ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)29 Test (org.junit.Test)29 Artist (org.apache.cayenne.testdo.testmap.Artist)16 SelectQuery (org.apache.cayenne.query.SelectQuery)9 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)8 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 ValueHolder (org.apache.cayenne.ValueHolder)4 ObjectContext (org.apache.cayenne.ObjectContext)2 SQLTemplate (org.apache.cayenne.query.SQLTemplate)2 Painting (org.apache.cayenne.testdo.testmap.Painting)2 MockDataNode (org.apache.cayenne.access.MockDataNode)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 ObjRelationship (org.apache.cayenne.map.ObjRelationship)1