Search in sources :

Example 51 with Artist

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

the class DataContextRefreshingIT method testInvalidateRootWithChangedToOneTarget.

@Test
public void testInvalidateRootWithChangedToOneTarget() throws Exception {
    createTwoArtistsAndPaintingDataSet();
    Painting painting = (Painting) context.performQuery(new SelectQuery(Painting.class)).get(0);
    Artist artistBefore = painting.getToArtist();
    assertNotNull(artistBefore);
    assertEquals("artist2", artistBefore.getArtistName());
    assertEquals(1, tPainting.update().set("ARTIST_ID", 6).execute());
    context.invalidateObjects(painting);
    assertNotSame(artistBefore, painting.getToArtist());
    assertEquals("artist3", painting.getToArtist().getArtistName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 52 with Artist

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

the class DataContextRefreshingIT method testRefetchRootWithUpdatedAttributes.

@Test
public void testRefetchRootWithUpdatedAttributes() throws Exception {
    createSingleArtistDataSet();
    String nameBefore = "artist2";
    String nameAfter = "not an artist";
    SelectQuery queryBefore = new SelectQuery(Artist.class, ExpressionFactory.matchExp("artistName", nameBefore));
    Artist artist = (Artist) context.performQuery(queryBefore).get(0);
    assertEquals(nameBefore, artist.getArtistName());
    assertEquals(1, tArtist.update().set("ARTIST_NAME", nameAfter).execute());
    // fetch into the same context
    List<Artist> artists = context.performQuery(queryBefore);
    assertEquals(0, artists.size());
    SelectQuery queryAfter = new SelectQuery(Artist.class, ExpressionFactory.matchExp("artistName", nameAfter));
    artist = (Artist) context.performQuery(queryAfter).get(0);
    assertNotNull(artist);
    assertEquals(nameAfter, artist.getArtistName());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Test(org.junit.Test)

Example 53 with Artist

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

the class DataContextRefreshingIT method testInvalidateRootWithUpdatedAttributes.

@Test
public void testInvalidateRootWithUpdatedAttributes() throws Exception {
    createSingleArtistDataSet();
    String nameBefore = "artist2";
    String nameAfter = "not an artist";
    Artist artist = (Artist) context.performQuery(new SelectQuery(Artist.class)).get(0);
    assertNotNull(artist);
    assertEquals(nameBefore, artist.getArtistName());
    // update via DataNode directly
    assertEquals(1, tArtist.update().set("ARTIST_NAME", nameAfter).execute());
    context.invalidateObjects(artist);
    assertEquals(nameAfter, artist.getArtistName());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Example 54 with Artist

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

the class DataContextRelationshipQueryIT method testUnrefreshingToOne.

@Test
public void testUnrefreshingToOne() throws Exception {
    createOneArtistOnePaintingDataSet();
    Painting p = Cayenne.objectForPK(context, Painting.class, 1);
    // resolve artist once before running non-refreshing query, to check that we do
    // not refresh the object
    Artist a = Cayenne.objectForPK(context, Artist.class, 1);
    long v = a.getSnapshotVersion();
    int writeCalls = a.getPropertyWrittenDirectly();
    assertEquals("a1", a.getArtistName());
    assertEquals(1, tArtist.update().set("ARTIST_NAME", "a2").where("ARTIST_ID", 1).execute());
    RelationshipQuery toOne = new RelationshipQuery(p.getObjectId(), Painting.TO_ARTIST.getName(), false);
    List<Artist> related = context.performQuery(toOne);
    assertEquals(1, related.size());
    assertTrue(related.contains(a));
    assertEquals("a1", a.getArtistName());
    assertEquals(v, a.getSnapshotVersion());
    assertEquals("Looks like relationship query caused snapshot refresh", writeCalls, a.getPropertyWrittenDirectly());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 55 with Artist

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

the class DataContextRelationshipQueryIT method testRefreshingToOne.

@Test
public void testRefreshingToOne() throws Exception {
    createOneArtistOnePaintingDataSet();
    Painting p = Cayenne.objectForPK(context, Painting.class, 1);
    // resolve artist once before running non-refreshing query, to check that we do
    // not refresh the object
    Artist a = Cayenne.objectForPK(context, Artist.class, 1);
    long v = a.getSnapshotVersion();
    int writeCalls = a.getPropertyWrittenDirectly();
    assertEquals("a1", a.getArtistName());
    assertEquals(1, tArtist.update().set("ARTIST_NAME", "a2").where("ARTIST_ID", 1).execute());
    RelationshipQuery toOne = new RelationshipQuery(p.getObjectId(), Painting.TO_ARTIST.getName(), true);
    List<Artist> related = context.performQuery(toOne);
    assertEquals(1, related.size());
    assertTrue(related.contains(a));
    assertEquals("a2", a.getArtistName());
    assertTrue("Looks like relationship query didn't cause a snapshot refresh", v < a.getSnapshotVersion());
    assertTrue("Looks like relationship query didn't cause a snapshot refresh", writeCalls < a.getPropertyWrittenDirectly());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) 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