Search in sources :

Example 1 with Painting

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

the class ToManyListIT method testSavedUnresolvedMerge.

@Test
public void testSavedUnresolvedMerge() throws Exception {
    ToManyList list = createForExistingArtist();
    Painting p1 = context.newObject(Painting.class);
    p1.setPaintingTitle("p1");
    // list being tested is a separate copy from
    // the relationship list that Artist has, so adding a painting
    // here will not add the painting to the array being tested
    ((Artist) list.getRelationshipOwner()).addToPaintingArray(p1);
    context.commitChanges();
    // immediately tag Artist as MODIFIED, since we are messing up with relationship
    // bypassing normal CayenneDataObject methods
    list.getRelationshipOwner().setPersistenceState(PersistenceState.MODIFIED);
    assertTrue("List must be unresolved...", list.isFault());
    list.add(p1);
    assertTrue("List must be unresolved when adding an object...", list.isFault());
    assertTrue(addedToUnresolved(list).contains(p1));
    Painting p2 = context.newObject(Painting.class);
    list.add(p2);
    assertTrue("List must be unresolved when adding an object...", list.isFault());
    assertTrue(addedToUnresolved(list).contains(p2));
    // now resolve the list and see how merge worked
    int size = list.size();
    assertFalse("List must be resolved after checking a size...", list.isFault());
    assertEquals(2, size);
    assertTrue(getValue(list).contains(p2));
    assertTrue(getValue(list).contains(p1));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 2 with Painting

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

the class ToManyListIT method testNewAddRemove.

@Test
public void testNewAddRemove() throws Exception {
    ToManyList list = createForNewArtist();
    assertFalse("Expected resolved list when created with a new object", list.isFault());
    assertEquals(0, list.size());
    Painting p1 = context.newObject(Painting.class);
    list.add(p1);
    assertEquals(1, list.size());
    Painting p2 = context.newObject(Painting.class);
    list.add(p2);
    assertEquals(2, list.size());
    list.remove(p1);
    assertEquals(1, list.size());
}
Also used : Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 3 with Painting

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

the class ToManyListIT method testSavedUnresolvedAddRemove.

@Test
public void testSavedUnresolvedAddRemove() throws Exception {
    ToManyList list = createForExistingArtist();
    // immediately tag Artist as MODIFIED, since we are messing up with relationship
    // bypassing normal CayenneDataObject methods
    list.getRelationshipOwner().setPersistenceState(PersistenceState.MODIFIED);
    assertTrue("List must be unresolved for an existing object", list.isFault());
    Painting p1 = context.newObject(Painting.class);
    list.add(p1);
    assertTrue("List must be unresolved when adding an object...", list.isFault());
    assertTrue(addedToUnresolved(list).contains(p1));
    Painting p2 = context.newObject(Painting.class);
    list.add(p2);
    assertTrue("List must be unresolved when adding an object...", list.isFault());
    assertTrue(addedToUnresolved(list).contains(p2));
    list.remove(p1);
    assertTrue("List must be unresolved when removing an object...", list.isFault());
    assertFalse(addedToUnresolved(list).contains(p1));
    // now resolve
    int size = list.size();
    assertFalse("List must be resolved after checking a size...", list.isFault());
    assertEquals(1, size);
    assertTrue(getValue(list).contains(p2));
}
Also used : Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 4 with Painting

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

the class ToManyListIT method testRealRelationship.

@Test
public void testRealRelationship() throws Exception {
    Artist artist = context.newObject(Artist.class);
    artist.setArtistName("aaa");
    Painting p1 = context.newObject(Painting.class);
    p1.setPaintingTitle("p1");
    context.commitChanges();
    context.invalidateObjects(artist);
    ToManyList list = (ToManyList) artist.getPaintingArray();
    assertTrue("List must be unresolved...", list.isFault());
    Painting p2 = context.newObject(Painting.class);
    p2.setPaintingTitle("p2");
    artist.addToPaintingArray(p1);
    artist.addToPaintingArray(p2);
    assertTrue("List must be unresolved...", list.isFault());
    context.commitChanges();
    assertTrue("List must be unresolved...", list.isFault());
    int size = list.size();
    assertFalse("List must be resolved...", list.isFault());
    assertTrue(list.contains(p1));
    assertTrue(list.contains(p2));
    assertEquals(2, size);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 5 with Painting

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

the class DefaultSelectTranslatorIT method testDbEntityQualifier_OuterJoin.

@Test
public void testDbEntityQualifier_OuterJoin() throws Exception {
    SelectQuery<Painting> q = SelectQuery.query(Painting.class);
    q.addOrdering("toArtist+.artistName", SortOrder.ASCENDING);
    final DbEntity entity = context.getEntityResolver().getDbEntity("ARTIST");
    final DbEntity middleEntity = context.getEntityResolver().getDbEntity("ARTIST_GROUP");
    DefaultSelectTranslator transl = new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver());
    entity.setQualifier(ExpressionFactory.exp("ARTIST_NAME = \"123\""));
    middleEntity.setQualifier(ExpressionFactory.exp("GROUP_ID = 1987"));
    try {
        String generatedSql = transl.getSql();
        // do some simple assertions to make sure all parts are in
        assertNotNull(generatedSql);
        assertTrue(generatedSql.startsWith("SELECT "));
        assertTrue(generatedSql.indexOf(" FROM ") > 0);
        if (generatedSql.contains("RTRIM")) {
            assertTrue(generatedSql.indexOf("ARTIST_NAME) = ") > generatedSql.indexOf("RTRIM("));
        } else if (generatedSql.contains("TRIM")) {
            assertTrue(generatedSql.indexOf("ARTIST_NAME) = ") > generatedSql.indexOf("TRIM("));
        } else {
            assertTrue(generatedSql.indexOf("ARTIST_NAME = ") > 0);
        }
    } finally {
        entity.setQualifier(null);
        middleEntity.setQualifier(null);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) CompoundPainting(org.apache.cayenne.testdo.testmap.CompoundPainting) 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