use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextDelegateSharedCacheIT method testShouldProcessDeleteOnExternalChange.
/**
* Test case to prove that delegate method is invoked on external change of object in
* the store.
*
* @throws Exception
*/
@Test
public void testShouldProcessDeleteOnExternalChange() throws Exception {
final boolean[] methodInvoked = new boolean[1];
DataContextDelegate delegate = new MockDataContextDelegate() {
@Override
public boolean shouldProcessDelete(DataObject object) {
methodInvoked[0] = true;
return true;
}
};
context1.setDelegate(delegate);
// make sure we have a fully resolved copy of an artist object
// in the second context
Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertFalse(altArtist == artist);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
// Update and save artist in peer context
context.deleteObjects(artist);
context.commitChanges();
// assert that delegate was consulted when an object store
// was refreshed
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertTrue("Delegate was not consulted", methodInvoked[0]);
}
};
helper.runTest(3000);
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextDelegateSharedCacheIT method testBlockShouldProcessDeleteOnExternalChange.
/**
* Test case to prove that delegate method is invoked on external change of object in
* the store, and is able to block further object processing.
*
* @throws Exception
*/
@Test
public void testBlockShouldProcessDeleteOnExternalChange() throws Exception {
final boolean[] methodInvoked = new boolean[1];
DataContextDelegate delegate = new MockDataContextDelegate() {
@Override
public boolean shouldProcessDelete(DataObject object) {
methodInvoked[0] = true;
return false;
}
};
context1.setDelegate(delegate);
// make sure we have a fully resolved copy of an artist object
// in the second context
Artist altArtist = context1.localObject(artist);
assertNotNull(altArtist);
assertFalse(altArtist == artist);
assertEquals(artist.getArtistName(), altArtist.getArtistName());
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
// Update and save artist in peer context
context.deleteObjects(artist);
context.commitChanges();
// assert that delegate was consulted when an object store
// was refreshed, and actually blocked object expulsion
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertTrue("Delegate was not consulted", methodInvoked[0]);
}
};
helper.runTest(3000);
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
assertNotNull(altArtist.getObjectContext());
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class SoftDeleteBatchTranslatorIT method testUpdate.
@Test
public void testUpdate() throws Exception {
final DbEntity entity = context.getEntityResolver().getObjEntity(SoftDelete.class).getDbEntity();
BatchTranslatorFactory oldFactory = dataNode.getBatchTranslatorFactory();
try {
dataNode.setBatchTranslatorFactory(new SoftDeleteTranslatorFactory());
final SoftDelete test = context.newObject(SoftDelete.class);
test.setName("SoftDeleteBatchQueryBuilderTest");
context.commitChanges();
new ParallelTestContainer() {
@Override
protected void assertResult() {
Expression exp = ExpressionFactory.matchExp("name", test.getName());
assertEquals(1, ObjectSelect.query(SoftDelete.class, exp).selectCount(context));
exp = ExpressionFactory.matchDbExp("DELETED", true);
assertEquals(0, ObjectSelect.query(SoftDelete.class, exp).selectCount(context));
}
}.runTest(200);
context.deleteObjects(test);
assertEquals(test.getPersistenceState(), PersistenceState.DELETED);
context.commitChanges();
new ParallelTestContainer() {
@Override
protected void assertResult() {
Expression exp = ExpressionFactory.matchExp("name", test.getName());
assertEquals(0, ObjectSelect.query(SoftDelete.class, exp).selectCount(context));
SQLTemplate template = new SQLTemplate(entity, "SELECT * FROM SOFT_DELETE");
template.setFetchingDataRows(true);
assertEquals(1, context.performQuery(template).size());
}
}.runTest(200);
} finally {
context.performQuery(new SQLTemplate(entity, "DELETE FROM SOFT_DELETE"));
dataNode.setBatchTranslatorFactory(oldFactory);
}
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class CayenneContextWithDataContextIT method testPostAddOnObjectCallback.
@Test
public void testPostAddOnObjectCallback() throws Exception {
final DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();
LifecycleCallbackRegistry callbackRegistry = serverContext.getEntityResolver().getCallbackRegistry();
try {
callbackRegistry.addCallback(LifecycleEvent.POST_ADD, MtTable1.class, "prePersistMethod");
final Persistent clientObject = clientContext.newObject(ClientMtTable1.class);
clientContext.commitChanges();
new ParallelTestContainer() {
@Override
protected void assertResult() {
// find peer
MtTable1 peer = (MtTable1) serverContext.getGraphManager().getNode(clientObject.getObjectId());
assertNotNull(peer);
assertTrue(peer.isPrePersisted());
}
}.runTest(1000);
} finally {
callbackRegistry.clear();
}
}
use of org.apache.cayenne.test.parallel.ParallelTestContainer in project cayenne by apache.
the class DataContextSharedCacheIT method testSnapshotDeletePropagationToModified.
/**
* Test case to prove that deleting an object in one ObjectStore and committed to the
* database will be reflected in the peer ObjectStore using the same DataRowCache. By
* default MODIFIED objects will be changed to NEW.
*/
@Test
public void testSnapshotDeletePropagationToModified() throws Exception {
// make sure we have a fully resolved copy of an artist object
// in the second context
final Artist altArtist = context1.localObject(artist);
altArtist.getArtistName();
assertNotNull(altArtist);
assertFalse(altArtist == artist);
// modify peer
altArtist.setArtistName("version2");
assertEquals(PersistenceState.MODIFIED, altArtist.getPersistenceState());
// Update Artist
context.deleteObjects(artist);
context.commitChanges();
// check underlying cache
assertNull(context.getObjectStore().getDataRowCache().getCachedSnapshot(altArtist.getObjectId()));
// check peer artist
ParallelTestContainer helper = new ParallelTestContainer() {
@Override
protected void assertResult() throws Exception {
assertEquals(PersistenceState.NEW, altArtist.getPersistenceState());
}
};
helper.runTest(3000);
// check if now we can save this object again, and with the original
// ObjectId
ObjectId id = altArtist.getObjectId();
assertNotNull(id);
assertNotNull(id.getIdSnapshot().get(Artist.ARTIST_ID_PK_COLUMN));
assertFalse(id.isTemporary());
context1.commitChanges();
assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
}
Aggregations