use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class NestedDataContextPeerEventsIT method testPeerObjectUpdatedToManyRelationship.
@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
Artist a = context.newObject(Artist.class);
a.setArtistName("X");
Painting px = context.newObject(Painting.class);
px.setToArtist(a);
px.setPaintingTitle("PX");
Painting py = context.newObject(Painting.class);
py.setPaintingTitle("PY");
context.commitChanges();
// pause to let the context events propagate
Thread.sleep(500);
ObjectContext peer1 = runtime.newContext(context);
Painting py1 = peer1.localObject(py);
Artist a1 = peer1.localObject(a);
final ObjectContext peer2 = runtime.newContext(context);
final Painting py2 = peer2.localObject(py);
final Artist a2 = peer2.localObject(a);
a1.addToPaintingArray(py1);
assertEquals(1, a2.getPaintingArray().size());
assertFalse(a2.getPaintingArray().contains(py2));
peer1.commitChangesToParent();
// pause to let the context events propagate
new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals(2, a2.getPaintingArray().size());
assertTrue(a2.getPaintingArray().contains(py2));
assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
}
}.runTest(2000);
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextDataChannelEventsIT method testChangeEventOnPeerChangeSecondNestingLevel.
@Test
public void testChangeEventOnPeerChangeSecondNestingLevel() throws Exception {
ObjectContext childPeer1 = runtime.newContext(context);
Artist a = childPeer1.newObject(Artist.class);
a.setArtistName("X");
childPeer1.commitChanges();
final MockChannelListener listener = new MockChannelListener();
EventUtil.listenForChannelEvents((DataChannel) childPeer1, listener);
ObjectContext childPeer2 = runtime.newContext(context);
Artist a1 = childPeer2.localObject(a);
a1.setArtistName("Y");
childPeer2.commitChangesToParent();
new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertFalse(listener.graphCommitted);
assertTrue(listener.graphChanged);
assertFalse(listener.graphRolledBack);
}
}.runTest(10000);
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotChangePropagationOnSelect.
/**
* Test case to prove that refreshing snapshots as a result of the database fetch will
* be propagated across DataContexts.
*/
@Test
public void testSnapshotChangePropagationOnSelect() throws Exception {
String originalName = artist.getArtistName();
final String newName = "version2";
// update artist using raw SQL
SQLTemplate query = sqlTemplateCustomizer.createSQLTemplate(Artist.class, "UPDATE ARTIST SET ARTIST_NAME = #bind($newName) " + "WHERE ARTIST_NAME = #bind($oldName)");
Map<String, Object> map = new HashMap<>(3);
map.put("newName", newName);
map.put("oldName", originalName);
query.setParams(map);
context.performNonSelectingQuery(query);
// fetch updated artist into the new context, and see if the original
// one gets updated
List<Artist> artists = ObjectSelect.query(Artist.class).where(Artist.ARTIST_NAME.eq(newName)).select(context);
assertEquals(1, artists.size());
Artist altArtist = artists.get(0);
// check underlying cache
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(altArtist.getObjectId());
assertNotNull(freshSnapshot);
assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
// check both artists
assertEquals(newName, altArtist.getArtistName());
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals("Peer object state wasn't refreshed on fetch", newName, artist.getArtistName());
}
};
helper.runTest(3000);
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotDeletePropagationToManyRefresh.
/**
* Test case to prove that deleting an object in one ObjectStore and committing to the
* database will be reflected in the peer ObjectStore using the same DataRowCache,
* including proper processing of deleted object being held in to-many collections.
*/
@Test
public void testSnapshotDeletePropagationToManyRefresh() throws Exception {
Painting painting1 = (Painting) context.newObject("Painting");
painting1.setPaintingTitle("p1");
painting1.setToArtist(artist);
Painting painting2 = (Painting) context.newObject("Painting");
painting2.setPaintingTitle("p2");
painting2.setToArtist(artist);
context.commitChanges();
// make sure we have a fully resolved copy of an artist and painting
// objects
// in the second context
final Artist altArtist = context1.localObject(artist);
final Painting altPainting1 = context1.localObject(painting1);
final Painting altPainting2 = context1.localObject(painting2);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(painting1.getPaintingTitle(), altPainting1.getPaintingTitle());
assertEquals(painting2.getPaintingTitle(), altPainting2.getPaintingTitle());
assertEquals(2, altArtist.getPaintingArray().size());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, altPainting1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, altPainting2.getPersistenceState());
// make sure toOne relationships from Paintings
// are resolved...
altPainting1.getToArtist();
altPainting2.getToArtist();
assertSame(altArtist, altPainting1.readPropertyDirectly("toArtist"));
assertSame(altArtist, altPainting2.readPropertyDirectly("toArtist"));
// delete painting
context.deleteObjects(painting1);
context.commitChanges();
// check underlying cache
assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(painting1.getObjectId()));
// check peer artist
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals(PersistenceState.TRANSIENT, altPainting1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
Collection<Painting> list = altArtist.getPaintingArray();
assertEquals(1, list.size());
assertFalse(list.contains(altPainting1));
}
};
helper.runTest(3000);
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotChangePropagationToModifiedObjects.
/**
* Test case to prove that changes made to an object in one ObjectStore and committed
* to the database will be correctly merged in the peer ObjectStore using the same
* DataRowCache. E.g. modified objects will be merged so that no new changes are lost.
*/
@Test
public void testSnapshotChangePropagationToModifiedObjects() throws Exception {
String originalName = artist.getArtistName();
Date originalDate = artist.getDateOfBirth();
String newName = "version2";
final Date newDate = new Date(originalDate.getTime() - 10000);
final String newAltName = "version3";
// make sure we have a fully resolved copy of an artist object
// in the second context
final Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertFalse(altArtist == artist);
assertEquals(originalName, altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
// Update Artist peers independently
artist.setArtistName(newName);
artist.setDateOfBirth(newDate);
altArtist.setArtistName(newAltName);
context.commitChanges();
// check underlying cache
DataRow freshSnapshot = context.getObjectStore().getDataRowCache().getCachedSnapshot(altArtist.getObjectId());
assertEquals(newName, freshSnapshot.get("ARTIST_NAME"));
assertEquals(newDate, freshSnapshot.get("DATE_OF_BIRTH"));
// check peer artist
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals(newAltName, altArtist.getArtistName());
assertEquals(newDate, altArtist.getDateOfBirth());
assertEquals(PersistenceState.MODIFIED, altArtist.getPersistenceState());
}
};
helper.runTest(3000);
}
Aggregations