Search in sources :

Example 16 with ParallelTestContainer

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

Example 17 with ParallelTestContainer

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

Example 18 with ParallelTestContainer

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);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Artist(org.apache.cayenne.testdo.testmap.Artist) HashMap(java.util.HashMap) DataRow(org.apache.cayenne.DataRow) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Test(org.junit.Test)

Example 19 with ParallelTestContainer

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

Example 20 with ParallelTestContainer

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);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DataRow(org.apache.cayenne.DataRow) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) Date(java.util.Date) 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