Search in sources :

Example 16 with Painting

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

the class NestedDataContextWriteIT method testCommitChangesToParentPropagatedKey.

@Test
public void testCommitChangesToParentPropagatedKey() throws Exception {
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    final Painting childMaster = childContext.newObject(Painting.class);
    childMaster.setPaintingTitle("Master");
    final PaintingInfo childDetail1 = childContext.newObject(PaintingInfo.class);
    childDetail1.setTextReview("Detail1");
    childDetail1.setPainting(childMaster);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childMaster.getPersistenceState());
            assertEquals(PersistenceState.COMMITTED, childDetail1.getPersistenceState());
            Painting parentMaster = (Painting) context.getGraphManager().getNode(childMaster.getObjectId());
            assertNotNull(parentMaster);
            assertEquals(PersistenceState.NEW, parentMaster.getPersistenceState());
            PaintingInfo parentDetail1 = (PaintingInfo) context.getGraphManager().getNode(childDetail1.getObjectId());
            assertNotNull(parentDetail1);
            assertEquals(PersistenceState.NEW, parentDetail1.getPersistenceState());
            assertSame(parentMaster, parentDetail1.getPainting());
            assertSame(parentDetail1, parentMaster.getToPaintingInfo());
        }
    });
}
Also used : UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) PaintingInfo(org.apache.cayenne.testdo.testmap.PaintingInfo) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 17 with Painting

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

the class NestedDataContextWriteIT method testAddRemove.

@Test
public void testAddRemove() {
    DataContext context = createDataContext();
    ObjectContext child = runtime.newContext(context);
    Artist a = child.newObject(Artist.class);
    a.setArtistName("X");
    child.commitChanges();
    Painting p1 = child.newObject(Painting.class);
    p1.setPaintingTitle("P1");
    a.addToPaintingArray(p1);
    Painting p2 = child.newObject(Painting.class);
    p2.setPaintingTitle("P2");
    a.addToPaintingArray(p2);
    a.removeFromPaintingArray(p2);
    // this causes an error on commit
    child.deleteObjects(p2);
    child.commitChangesToParent();
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 18 with Painting

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

the class NestedDataContextWriteIT method testNullifyToOne.

/**
 * A test case for CAY-698 bug.
 */
@Test
public void testNullifyToOne() throws Exception {
    createNullifyToOneDataSet();
    final DataContext context = createDataContext();
    final ObjectContext childContext = runtime.newContext(context);
    ObjectContext childContextPeer = runtime.newContext(context);
    final Painting childP1 = Cayenne.objectForPK(childContext, Painting.class, 33001);
    // trigger object creation in the peer nested DC
    Cayenne.objectForPK(childContextPeer, Painting.class, 33001);
    childP1.setToArtist(null);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            childContext.commitChangesToParent();
            assertEquals(PersistenceState.COMMITTED, childP1.getPersistenceState());
            Painting parentP1 = (Painting) context.getGraphManager().getNode(childP1.getObjectId());
            assertNotNull(parentP1);
            assertEquals(PersistenceState.MODIFIED, parentP1.getPersistenceState());
            assertNull(parentP1.getToArtist());
        }
    });
}
Also used : UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 19 with Painting

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

the class ObjectStoreIT method testUnregisterThenRegister.

@Test
public void testUnregisterThenRegister() throws Exception {
    // Create a gallery.
    Gallery g = context.newObject(Gallery.class);
    g.setGalleryName("Test Gallery");
    // Create an artist in the same context.
    Artist a = context.newObject(Artist.class);
    a.setArtistName("Test Artist");
    // Create a painting in the same context.
    Painting p = context.newObject(Painting.class);
    p.setPaintingTitle("Test Painting");
    // Set the painting's gallery.
    p.setToGallery(g);
    assertEquals(g, p.getToGallery());
    // Unregister the painting from the context.
    context.unregisterObjects(Collections.singletonList(p));
    // Make sure that even though the painting has been removed from the context's
    // object graph that the reference to the gallery is the same.
    assertEquals(g, p.getToGallery());
    // Now, set the relationship between "p" & "a." Since "p" is not registered with a
    // context, but "a" is, "p" should be auto-registered with the context of "a."
    p.setToArtist(a);
    // Now commit the gallery, artist, & painting.
    context.commitChanges();
    // Check one last time that the painting's gallery is set to what we expect.
    assertEquals(g, p.getToGallery());
    // Now, retrieve the same painting from the DB. Note that the gallery relationship
    // is null even though according to our painting, that should not be the case; a
    // NULL
    // value has been recorded to the DB for the painting's gallery_id field.
    // 
    // The full object graph is not being re-registered during auto-registration
    // with the context.
    Painting newP = (Painting) Cayenne.objectForPK(context, p.getObjectId());
    assertNotNull(newP.getToGallery());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 20 with Painting

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

the class ExpressionFactoryIT method testCollectionMatch.

// CAY-416
@Test
public void testCollectionMatch() {
    Artist artist = context.newObject(Artist.class);
    artist.setArtistName("artist");
    Painting p1 = context.newObject(Painting.class), p2 = context.newObject(Painting.class), p3 = context.newObject(Painting.class);
    p1.setPaintingTitle("p1");
    p2.setPaintingTitle("p2");
    p3.setPaintingTitle("p3");
    artist.addToPaintingArray(p1);
    artist.addToPaintingArray(p2);
    context.commitChanges();
    assertTrue(ExpressionFactory.matchExp("paintingArray", p1).match(artist));
    assertFalse(ExpressionFactory.matchExp("paintingArray", p3).match(artist));
    // changed to align with SQL
    assertTrue(ExpressionFactory.noMatchExp("paintingArray", p1).match(artist));
    assertTrue(ExpressionFactory.noMatchExp("paintingArray", p3).match(artist));
    assertTrue(ExpressionFactory.matchExp("paintingArray.paintingTitle", "p1").match(artist));
    assertFalse(ExpressionFactory.matchExp("paintingArray.paintingTitle", "p3").match(artist));
    // changed to align with SQL
    assertTrue(ExpressionFactory.noMatchExp("paintingArray.paintingTitle", "p1").match(artist));
    assertTrue(ExpressionFactory.noMatchExp("paintingArray.paintingTitle", "p3").match(artist));
    assertTrue(ExpressionFactory.inExp("paintingTitle", "p1").match(p1));
    assertFalse(ExpressionFactory.notInExp("paintingTitle", "p3").match(p3));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

Painting (org.apache.cayenne.testdo.testmap.Painting)222 Test (org.junit.Test)218 Artist (org.apache.cayenne.testdo.testmap.Artist)144 SelectQuery (org.apache.cayenne.query.SelectQuery)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)35 List (java.util.List)30 Expression (org.apache.cayenne.exp.Expression)29 ArrayList (java.util.ArrayList)21 BigDecimal (java.math.BigDecimal)19 PaintingInfo (org.apache.cayenne.testdo.testmap.PaintingInfo)14 ObjectContext (org.apache.cayenne.ObjectContext)13 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)13 Gallery (org.apache.cayenne.testdo.testmap.Gallery)13 ValueHolder (org.apache.cayenne.ValueHolder)12 ROPainting (org.apache.cayenne.testdo.testmap.ROPainting)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 DataRow (org.apache.cayenne.DataRow)7 ProcedureQuery (org.apache.cayenne.query.ProcedureQuery)6 LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)6 RefreshQuery (org.apache.cayenne.query.RefreshQuery)5