Search in sources :

Example 6 with ParallelTestContainer

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 7 with ParallelTestContainer

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataObject(org.apache.cayenne.DataObject) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 8 with ParallelTestContainer

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);
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) DbEntity(org.apache.cayenne.map.DbEntity) SoftDelete(org.apache.cayenne.testdo.soft_delete.SoftDelete) Expression(org.apache.cayenne.exp.Expression) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 9 with ParallelTestContainer

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();
    }
}
Also used : DataContext(org.apache.cayenne.access.DataContext) MtTable1(org.apache.cayenne.testdo.mt.MtTable1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 10 with ParallelTestContainer

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());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Aggregations

ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)37 Test (org.junit.Test)34 Artist (org.apache.cayenne.testdo.testmap.Artist)26 ObjectContext (org.apache.cayenne.ObjectContext)10 DataRow (org.apache.cayenne.DataRow)6 SQLTemplate (org.apache.cayenne.query.SQLTemplate)5 Painting (org.apache.cayenne.testdo.testmap.Painting)4 DataObject (org.apache.cayenne.DataObject)3 Master (org.apache.cayenne.testdo.relationships_child_master.Master)3 ObjectId (org.apache.cayenne.ObjectId)2 Expression (org.apache.cayenne.exp.Expression)2 DbEntity (org.apache.cayenne.map.DbEntity)2 Child (org.apache.cayenne.testdo.relationships_child_master.Child)2 SoftDelete (org.apache.cayenne.testdo.soft_delete.SoftDelete)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 DataContext (org.apache.cayenne.access.DataContext)1 SnapshotEvent (org.apache.cayenne.access.event.SnapshotEvent)1 BatchTranslatorFactory (org.apache.cayenne.access.translator.batch.BatchTranslatorFactory)1