use of org.apache.cayenne.testdo.testmap.ArtGroup in project cayenne by apache.
the class CayenneDataObjectFlattenedRelIT method testAddToFlattenedRelationship.
@Test
public void testAddToFlattenedRelationship() throws Exception {
create1Artist1ArtGroupDataSet();
Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);
assertEquals(0, a1.getGroupArray().size());
SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp("name", "g1"));
List<?> results = context.performQuery(q);
assertEquals(1, results.size());
assertFalse(context.hasChanges());
ArtGroup group = (ArtGroup) results.get(0);
a1.addToGroupArray(group);
assertTrue(context.hasChanges());
List<?> groupList = a1.getGroupArray();
assertEquals(1, groupList.size());
assertEquals("g1", ((ArtGroup) groupList.get(0)).getName());
// Ensure that the commit doesn't fail
a1.getObjectContext().commitChanges();
// and check again
assertFalse(context.hasChanges());
// refetch artist with a different context
ObjectContext context2 = runtime.newContext();
a1 = Cayenne.objectForPK(context2, Artist.class, 33001);
groupList = a1.getGroupArray();
assertEquals(1, groupList.size());
assertEquals("g1", ((ArtGroup) groupList.get(0)).getName());
}
use of org.apache.cayenne.testdo.testmap.ArtGroup in project cayenne by apache.
the class CayenneDataObjectRelationshipsIT method testReflexiveRelationshipInsertOrder1.
@Test
public void testReflexiveRelationshipInsertOrder1() {
ArtGroup parentGroup = context.newObject(ArtGroup.class);
parentGroup.setName("parent");
ArtGroup childGroup1 = context.newObject(ArtGroup.class);
childGroup1.setName("child1");
childGroup1.setToParentGroup(parentGroup);
context.commitChanges();
childGroup1.setToParentGroup(null);
context.commitChanges();
}
use of org.apache.cayenne.testdo.testmap.ArtGroup 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());
}
}
});
}
use of org.apache.cayenne.testdo.testmap.ArtGroup 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());
}
}
});
}
use of org.apache.cayenne.testdo.testmap.ArtGroup 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));
}
});
}
Aggregations