Search in sources :

Example 76 with Artist

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

the class DataDomainCallbacksIT method testPostLoad_MixedResult.

@Test
public void testPostLoad_MixedResult() 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");
    context.commitChanges();
    assertEquals(0, a1.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    EJBQLQuery q = new EJBQLQuery("select a, a.artistName from Artist a");
    context.performQuery(q);
    assertEquals(1, a1.getPostLoaded());
    assertSame(a1, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 77 with Artist

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

the class DataDomainCallbacksIT method testPostLoad_LocalObject.

@Test
public void testPostLoad_LocalObject() throws Exception {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    registry.addCallback(LifecycleEvent.POST_LOAD, Artist.class, "postLoadCallback");
    MockCallingBackListener listener = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_LOAD, Artist.class, listener, "publicCallback");
    Artist a2 = context1.localObject(a1);
    assertEquals(PersistenceState.HOLLOW, a2.getPersistenceState());
    assertEquals(0, a2.getPostLoaded());
    assertNull(listener.getPublicCalledbackEntity());
    a2.getArtistName();
    assertEquals(1, a2.getPostLoaded());
    assertSame(a2, listener.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 78 with Artist

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

the class DataDomainCallbacksIT method testPostPersist.

@Test
public void testPostPersist() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    assertFalse(a1.isPostPersisted());
    registry.addCallback(LifecycleEvent.POST_PERSIST, Artist.class, "postPersistCallback");
    MockCallingBackListener listener2 = new MockCallingBackListener() {

        @Override
        public void publicCallback(Object entity) {
            super.publicCallback(entity);
            assertFalse(((Persistent) entity).getObjectId().isTemporary());
        }
    };
    registry.addListener(LifecycleEvent.POST_PERSIST, Artist.class, listener2, "publicCallback");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("XX");
    context.commitChanges();
    assertFalse(a1.isPostPersisted());
    assertTrue(a2.isPostPersisted());
    assertSame(a2, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Persistent(org.apache.cayenne.Persistent) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 79 with Artist

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

the class DataRowUtilsIT method testMerge.

@Test
public void testMerge() throws Exception {
    createOneArtist();
    String n1 = "changed";
    String n2 = "changed again";
    SelectQuery artistQ = new SelectQuery(Artist.class);
    Artist a1 = (Artist) context.performQuery(artistQ).get(0);
    a1.setArtistName(n1);
    DataRow s2 = new DataRow(2);
    s2.put("ARTIST_NAME", n2);
    s2.put("DATE_OF_BIRTH", new java.util.Date());
    ClassDescriptor d = context.getEntityResolver().getClassDescriptor("Artist");
    DataRowUtils.mergeObjectWithSnapshot(context, d, a1, s2);
    // name was modified, so it should not change during merge
    assertEquals(n1, a1.getArtistName());
    // date of birth came from database, it should be updated during merge
    assertEquals(s2.get("DATE_OF_BIRTH"), a1.getDateOfBirth());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 80 with Artist

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

the class DeleteObjectIT method testDeleteObjectsRelationshipCollection.

@Test
public void testDeleteObjectsRelationshipCollection() throws Exception {
    createObjectsRelationshipCollectionDataSet();
    Artist artist = Cayenne.objectForPK(context, Artist.class, 1);
    List<Painting> paintings = artist.getPaintingArray();
    assertEquals(3, paintings.size());
    // create a clone to be able to inspect paintings after deletion
    List<Painting> paintingsClone = new ArrayList<Painting>(paintings);
    for (Painting object : paintingsClone) {
        assertEquals(PersistenceState.COMMITTED, object.getPersistenceState());
    }
    context.deleteObjects(paintings);
    // as Painting -> Artist has Nullify rule, relationship list has to be
    // cleaned up,
    // and no exceptions thrown on concurrent modification...
    ObjRelationship r = context.getEntityResolver().getObjEntity(Painting.class).getRelationship("toArtist");
    assertEquals(DeleteRule.NULLIFY, r.getDeleteRule());
    assertEquals(0, paintings.size());
    for (Painting object : paintingsClone) {
        assertEquals(PersistenceState.DELETED, object.getPersistenceState());
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ArrayList(java.util.ArrayList) Painting(org.apache.cayenne.testdo.testmap.Painting) 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