Search in sources :

Example 51 with Painting

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

the class SelectQueryIT method testMatchByRelatedObjectIdValue.

@Test
public void testMatchByRelatedObjectIdValue() {
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("a1");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("a2");
    Painting p1 = context.newObject(Painting.class);
    p1.setPaintingTitle("p1");
    p1.setToArtist(a1);
    Painting p2 = context.newObject(Painting.class);
    p2.setPaintingTitle("p2");
    p2.setToArtist(a2);
    context.commitChanges();
    SelectQuery<Painting> query = new SelectQuery<>(Painting.class);
    query.setQualifier(ExpressionFactory.matchExp("toArtist", Cayenne.longPKForObject(a1)));
    assertSame(p1, query.selectOne(context));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 52 with Painting

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

the class ExpressionIT method testMatch.

@Test
public void testMatch() {
    assertTrue(context instanceof DataContext);
    DataContext context2 = (DataContext) runtime.newContext();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("Equals");
    Painting p1 = context.newObject(Painting.class);
    p1.setToArtist(a1);
    p1.setPaintingTitle("painting1");
    context.commitChanges();
    SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class);
    Expression e = Painting.TO_ARTIST.eq(a1);
    query.setQualifier(e);
    assertNotSame(context2, context);
    List<Painting> objects = context2.select(query);
    assertEquals(1, objects.size());
    // 2 same objects in different contexts
    assertTrue(e.match(objects.get(0)));
    // we change one object - so the objects are different now
    // (PersistenceState different)
    a1.setArtistName("newName");
    SelectQuery<Painting> q2 = new SelectQuery<Painting>(Painting.class);
    Expression ex2 = Painting.TO_ARTIST.eq(a1);
    q2.setQualifier(ex2);
    assertTrue(ex2.match(objects.get(0)));
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("Equals");
    context.commitChanges();
    SelectQuery<Painting> q = new SelectQuery<Painting>(Painting.class);
    Expression ex = Painting.TO_ARTIST.eq(a2);
    q.setQualifier(ex);
    // 2 different objects in different contexts
    assertFalse(ex.match(objects.get(0)));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataContext(org.apache.cayenne.access.DataContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 53 with Painting

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

the class ExpressionIT method testFirst.

@Test
public void testFirst() {
    List<Painting> paintingList = new ArrayList<Painting>();
    Painting p1 = context.newObject(Painting.class);
    p1.setPaintingTitle("x1");
    paintingList.add(p1);
    Painting p2 = context.newObject(Painting.class);
    p2.setPaintingTitle("x2");
    paintingList.add(p2);
    Painting p3 = context.newObject(Painting.class);
    p3.setPaintingTitle("x3");
    paintingList.add(p3);
    Expression e1 = ExpressionFactory.likeExp("paintingTitle", "x%");
    assertSame(p1, e1.first(paintingList));
    Expression e3 = ExpressionFactory.matchExp("paintingTitle", "x3");
    assertSame(p3, e3.first(paintingList));
    Expression e4 = ExpressionFactory.matchExp("paintingTitle", "x4");
    assertNull(e4.first(paintingList));
}
Also used : ArrayList(java.util.ArrayList) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 54 with Painting

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

the class ASTBetweenTest method testEvaluate.

@Test
public void testEvaluate() {
    // evaluate both BETWEEN and NOT_BETWEEN
    Expression between = new ASTBetween(new ASTObjPath("estimatedPrice"), new BigDecimal(10d), new BigDecimal(20d));
    Expression notBetween = new ASTNotBetween(new ASTObjPath("estimatedPrice"), new BigDecimal(10d), new BigDecimal(20d));
    Painting noMatch = new Painting();
    noMatch.setEstimatedPrice(new BigDecimal(21));
    assertFalse(between.match(noMatch));
    assertTrue(notBetween.match(noMatch));
    Painting match1 = new Painting();
    match1.setEstimatedPrice(new BigDecimal(20));
    assertTrue(between.match(match1));
    assertFalse(notBetween.match(match1));
    Painting match2 = new Painting();
    match2.setEstimatedPrice(new BigDecimal(10));
    assertTrue("Failed: " + between, between.match(match2));
    assertFalse("Failed: " + notBetween, notBetween.match(match2));
    Painting match3 = new Painting();
    match3.setEstimatedPrice(new BigDecimal(11));
    assertTrue("Failed: " + between, between.match(match3));
    assertFalse("Failed: " + notBetween, notBetween.match(match3));
}
Also used : Expression(org.apache.cayenne.exp.Expression) BigDecimal(java.math.BigDecimal) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 55 with Painting

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

the class ASTEqualIT method testEvaluate_TempId.

@Test
public void testEvaluate_TempId() {
    Artist a1 = context.newObject(Artist.class);
    Artist a2 = context.newObject(Artist.class);
    Painting p1 = context.newObject(Painting.class);
    Painting p2 = context.newObject(Painting.class);
    Painting p3 = context.newObject(Painting.class);
    p1.setToArtist(a1);
    p2.setToArtist(a2);
    Expression e = new ASTEqual(new ASTObjPath("toArtist"), a1.getObjectId());
    assertTrue(e.match(p1));
    assertFalse(e.match(p2));
    assertFalse(e.match(p3));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Expression(org.apache.cayenne.exp.Expression) 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