Search in sources :

Example 71 with Artist

use of org.apache.cayenne.testdo.testmap.Artist 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)

Example 72 with Artist

use of org.apache.cayenne.testdo.testmap.Artist in project cayenne by apache.

the class DataContextSharedCacheIT method testSnapshotInsertPropagationToManyRefresh.

/**
 * Test case to prove that inserting an object in one ObjectStore and committing to
 * the database will be reflected in the peer ObjectStore using the same DataRowCache.
 * This would mean refreshing to-many collections.
 */
@Test
public void testSnapshotInsertPropagationToManyRefresh() throws Exception {
    Painting painting1 = (Painting) context.newObject("Painting");
    painting1.setPaintingTitle("p1");
    painting1.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);
    assertEquals(artist.getArtistName(), altArtist.getArtistName());
    assertEquals(painting1.getPaintingTitle(), altPainting1.getPaintingTitle());
    assertEquals(1, altArtist.getPaintingArray().size());
    assertEquals(PersistenceState.COMMITTED, altArtist.getPersistenceState());
    assertEquals(PersistenceState.COMMITTED, altPainting1.getPersistenceState());
    // insert new painting and add to artist
    Painting painting2 = (Painting) context.newObject("Painting");
    painting2.setPaintingTitle("p2");
    painting2.setToArtist(artist);
    context.commitChanges();
    // check peer artist
    // use threaded helper as a barrier, to avoid triggering faults earlier than
    // needed
    ParallelTestContainer helper = new ParallelTestContainer() {

        @Override
        protected void assertResult() throws Exception {
            Object value = altArtist.readPropertyDirectly("paintingArray");
            assertTrue("Unexpected: " + value, value instanceof ToManyList);
            assertTrue(((ToManyList) value).isFault());
        }
    };
    helper.runTest(2000);
    List<Painting> list = altArtist.getPaintingArray();
    assertEquals(2, list.size());
}
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 73 with Artist

use of org.apache.cayenne.testdo.testmap.Artist in project cayenne by apache.

the class DataContextValidationIT method testValidatingObjectsOnCommit.

@Test
public void testValidatingObjectsOnCommit() throws Exception {
    // test that validation is called properly
    context.setValidatingObjectsOnCommit(true);
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("a1");
    context.commitChanges();
    assertTrue(a1.isValidateForSaveCalled());
    context.setValidatingObjectsOnCommit(false);
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("a2");
    context.commitChanges();
    assertFalse(a2.isValidateForSaveCalled());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Example 74 with Artist

use of org.apache.cayenne.testdo.testmap.Artist in project cayenne by apache.

the class DataDomainCallbacksIT method testPostLoad_Prefetch.

@Test
public void testPostLoad_Prefetch() throws Exception {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
    MockCallingBackListener listener = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    Painting p1 = context.newObject(Painting.class);
    p1.setToArtist(a1);
    p1.setPaintingTitle("XXX");
    context.commitChanges();
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.disjoint());
    p1 = (Painting) context1.performQuery(q).get(0);
    // artist is prefetched here, and a callback must have been invoked
    a1 = p1.getToArtist();
    assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState());
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 75 with Artist

use of org.apache.cayenne.testdo.testmap.Artist in project cayenne by apache.

the class DataDomainCallbacksIT method testPostRemove.

@Test
public void testPostRemove() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    registry.addCallback(LifecycleEvent.POST_REMOVE, Artist.class, "postRemoveCallback");
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_REMOVE, Artist.class, listener2, "publicCallback");
    context.deleteObjects(a1);
    context.commitChanges();
    assertTrue(a1.isPostRemoved());
    assertSame(a1, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Aggregations

Artist (org.apache.cayenne.testdo.testmap.Artist)490 Test (org.junit.Test)481 Painting (org.apache.cayenne.testdo.testmap.Painting)145 SelectQuery (org.apache.cayenne.query.SelectQuery)126 Expression (org.apache.cayenne.exp.Expression)67 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)47 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)39 List (java.util.List)36 ObjectContext (org.apache.cayenne.ObjectContext)30 SQLTemplate (org.apache.cayenne.query.SQLTemplate)26 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)26 DataRow (org.apache.cayenne.DataRow)21 ArrayList (java.util.ArrayList)20 ValueHolder (org.apache.cayenne.ValueHolder)18 ArtGroup (org.apache.cayenne.testdo.testmap.ArtGroup)16 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)15 ObjectId (org.apache.cayenne.ObjectId)14 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)12 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)12