Repository: cayenne Updated Branches: refs/heads/master b2c611036 -> 0bfc0f5e8
cleanup Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/0bfc0f5e Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/0bfc0f5e Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/0bfc0f5e Branch: refs/heads/master Commit: 0bfc0f5e8ec1c0d83d17e1b76ba12b58c20abcc7 Parents: b2c6110 Author: Nikita Timofeev <stari...@gmail.com> Authored: Sat Jul 7 10:09:20 2018 +0300 Committer: Nikita Timofeev <stari...@gmail.com> Committed: Sat Jul 7 10:09:20 2018 +0300 ---------------------------------------------------------------------- .../cayenne/access/DataContextPrefetchIT.java | 670 ++++++++----------- 1 file changed, 277 insertions(+), 393 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/0bfc0f5e/cayenne-server/src/test/java/org/apache/cayenne/access/DataContextPrefetchIT.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/DataContextPrefetchIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/DataContextPrefetchIT.java index dce94a4..c52f322 100644 --- a/cayenne-server/src/test/java/org/apache/cayenne/access/DataContextPrefetchIT.java +++ b/cayenne-server/src/test/java/org/apache/cayenne/access/DataContextPrefetchIT.java @@ -41,13 +41,13 @@ import org.apache.cayenne.testdo.testmap.Gallery; import org.apache.cayenne.testdo.testmap.Painting; import org.apache.cayenne.testdo.testmap.PaintingInfo; import org.apache.cayenne.unit.di.DataChannelInterceptor; -import org.apache.cayenne.unit.di.UnitTestClosure; import org.apache.cayenne.unit.di.server.CayenneProjects; import org.apache.cayenne.unit.di.server.ServerCase; import org.apache.cayenne.unit.di.server.UseServerRuntime; import org.junit.Before; import org.junit.Test; +import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; import java.util.Date; @@ -144,31 +144,34 @@ public class DataContextPrefetchIT extends ServerCase { tArtistExhibit.insert(101, 4); } + private void createArtistWithPaintingAndGallery() throws SQLException { + tArtist.insert(1, "artist1"); + tGallery.insert(1, "gallery1"); + tPainting.insert(1, "painting1", 1, 100, 1); + } + @Test - public void testPrefetchToMany_ViaProperty() throws Exception { + public void testPrefetchToMany_ViaPath() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class); - q.addPrefetch(Artist.PAINTING_ARRAY.disjoint()); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class); + q.addPrefetch("paintingArray"); final List<Artist> artists = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { - public void execute() { + assertEquals(2, artists.size()); - assertEquals(2, artists.size()); - - for (int i = 0; i < 2; i++) { - Artist a = artists.get(i); - List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray"); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - assertEquals(1, toMany.size()); + for (int i = 0; i < 2; i++) { + Artist a = artists.get(i); + List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray"); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); + assertEquals(1, toMany.size()); - Painting p = (Painting) toMany.get(0); - assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle()); - } + Painting p = (Painting) toMany.get(0); + assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle()); } }); } @@ -186,30 +189,27 @@ public class DataContextPrefetchIT extends ServerCase { final List<Artist> artists = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { - public void execute() { + assertEquals(2, artists.size()); - assertEquals(2, artists.size()); + Artist a1 = artists.get(0); + List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.PAINTING_ARRAY.getName()); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); + assertEquals(1, toMany.size()); - Artist a1 = artists.get(0); - List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.PAINTING_ARRAY.getName()); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - assertEquals(1, toMany.size()); + Painting p1 = (Painting) toMany.get(0); + assertEquals("p_" + a1.getArtistName(), p1.getPaintingTitle()); - Painting p1 = (Painting) toMany.get(0); - assertEquals("p_" + a1.getArtistName(), p1.getPaintingTitle()); + Artist a2 = artists.get(1); + List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.PAINTING_ARRAY.getName()); + assertNotNull(toMany2); + assertFalse(((ValueHolder) toMany2).isFault()); + assertEquals(1, toMany2.size()); - Artist a2 = artists.get(1); - List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.PAINTING_ARRAY.getName()); - assertNotNull(toMany2); - assertFalse(((ValueHolder) toMany2).isFault()); - assertEquals(1, toMany2.size()); - - Painting p2 = (Painting) toMany2.get(0); - assertEquals("p_" + a2.getArtistName(), p2.getPaintingTitle()); - } + Painting p2 = (Painting) toMany2.get(0); + assertEquals("p_" + a2.getArtistName(), p2.getPaintingTitle()); }); } @@ -217,27 +217,24 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetchToManyNoQualifier() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - SelectQuery q = new SelectQuery(Artist.class); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class); q.addPrefetch(Artist.PAINTING_ARRAY.disjoint()); - final List<Artist> artists = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + final List<Artist> artists = context.select(q); - public void execute() { + queryInterceptor.runWithQueriesBlocked(() -> { - assertEquals(2, artists.size()); + assertEquals(2, artists.size()); - for (int i = 0; i < 2; i++) { - Artist a = artists.get(i); - List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray"); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - assertEquals(1, toMany.size()); + for (int i = 0; i < 2; i++) { + Artist a = artists.get(i); + List<?> toMany = (List<?>) a.readPropertyDirectly("paintingArray"); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); + assertEquals(1, toMany.size()); - Painting p = (Painting) toMany.get(0); - assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle()); - } + Painting p = (Painting) toMany.get(0); + assertEquals("Invalid prefetched painting:" + p, "p_" + a.getArtistName(), p.getPaintingTitle()); } }); } @@ -251,125 +248,74 @@ public class DataContextPrefetchIT extends ServerCase { createTwoArtistsWithExhibitsDataSet(); - SelectQuery q = new SelectQuery(Artist.class); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class); q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.disjoint()); q.addOrdering(Artist.ARTIST_NAME.asc()); - final List<Artist> artists = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { - - public void execute() { - assertEquals(2, artists.size()); + final List<Artist> artists = context.select(q); - Artist a1 = artists.get(0); - assertEquals("artist2", a1.getArtistName()); - List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - assertEquals(2, toMany.size()); - - ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0); - assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState()); - assertSame(a1, artistExhibit.getToArtist()); - - Artist a2 = artists.get(1); - assertEquals("artist3", a2.getArtistName()); - List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); - assertNotNull(toMany2); - assertFalse(((ValueHolder) toMany2).isFault()); - assertEquals(3, toMany2.size()); - - ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0); - assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState()); - assertSame(a2, artistExhibit2.getToArtist()); - } + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(2, artists.size()); + + Artist a1 = artists.get(0); + assertEquals("artist2", a1.getArtistName()); + List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); + assertEquals(2, toMany.size()); + + ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0); + assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState()); + assertSame(a1, artistExhibit.getToArtist()); + + Artist a2 = artists.get(1); + assertEquals("artist3", a2.getArtistName()); + List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); + assertNotNull(toMany2); + assertFalse(((ValueHolder) toMany2).isFault()); + assertEquals(3, toMany2.size()); + + ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0); + assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState()); + assertSame(a2, artistExhibit2.getToArtist()); }); } @Test - public void testPrefetchToManyOnJoinTableJoinedPrefetch_ViaProperty() throws Exception { + public void testPrefetchToMany_OnJoinTableJoinedPrefetch() throws Exception { createTwoArtistsWithExhibitsDataSet(); - SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class); q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.joint()); q.addOrdering(Artist.ARTIST_NAME.asc()); final List<Artist> artists = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { - public void execute() { + assertEquals(2, artists.size()); - assertEquals(2, artists.size()); + Artist a1 = artists.get(0); + assertEquals("artist2", a1.getArtistName()); + List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); + assertEquals(2, toMany.size()); - Artist a1 = artists.get(0); - assertEquals("artist2", a1.getArtistName()); - List<?> toMany = (List<?>) a1.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - assertEquals(2, toMany.size()); - - ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0); - assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState()); - assertSame(a1, artistExhibit.getToArtist()); - - Artist a2 = artists.get(1); - assertEquals("artist3", a2.getArtistName()); - List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); - assertNotNull(toMany2); - assertFalse(((ValueHolder) toMany2).isFault()); - assertEquals(3, toMany2.size()); - - ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0); - assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState()); - assertSame(a2, artistExhibit2.getToArtist()); - } - }); - } - - /** - * Test that a to-many relationship is initialized when a target entity has - * a compound PK only partially involved in relationship. - */ - @Test - public void testPrefetchToManyOnJoinTableJoinedPrefetch() throws Exception { - createTwoArtistsWithExhibitsDataSet(); - - SelectQuery q = new SelectQuery(Artist.class); - q.addPrefetch(Artist.ARTIST_EXHIBIT_ARRAY.joint()); - q.addOrdering(Artist.ARTIST_NAME.asc()); - - final List<Artist> artists = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0); + assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState()); + assertSame(a1, artistExhibit.getToArtist()); - public void execute() { + Artist a2 = artists.get(1); + assertEquals("artist3", a2.getArtistName()); + List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); + assertNotNull(toMany2); + assertFalse(((ValueHolder) toMany2).isFault()); + assertEquals(3, toMany2.size()); - assertEquals(2, artists.size()); - - Artist a1 = artists.get(0); - assertEquals("artist2", a1.getArtistName()); - List<?> toMany = (List<?>) a1.readPropertyDirectly("artistExhibitArray"); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - assertEquals(2, toMany.size()); - - ArtistExhibit artistExhibit = (ArtistExhibit) toMany.get(0); - assertEquals(PersistenceState.COMMITTED, artistExhibit.getPersistenceState()); - assertSame(a1, artistExhibit.getToArtist()); - - Artist a2 = artists.get(1); - assertEquals("artist3", a2.getArtistName()); - List<?> toMany2 = (List<?>) a2.readPropertyDirectly(Artist.ARTIST_EXHIBIT_ARRAY.getName()); - assertNotNull(toMany2); - assertFalse(((ValueHolder) toMany2).isFault()); - assertEquals(3, toMany2.size()); - - ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0); - assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState()); - assertSame(a2, artistExhibit2.getToArtist()); - } + ArtistExhibit artistExhibit2 = (ArtistExhibit) toMany2.get(0); + assertEquals(PersistenceState.COMMITTED, artistExhibit2.getPersistenceState()); + assertSame(a2, artistExhibit2.getToArtist()); }); } @@ -388,17 +334,14 @@ public class DataContextPrefetchIT extends ServerCase { try { SelectQuery<Artist> q = new SelectQuery<>(Artist.class); q.addPrefetch(Artist.PAINTING_ARRAY.disjoint()); - final List<Artist> result = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + final List<Artist> result = context.select(q); - public void execute() { - assertFalse(result.isEmpty()); - Artist a1 = result.get(0); - List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray"); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - } + queryInterceptor.runWithQueriesBlocked(() -> { + assertFalse(result.isEmpty()); + Artist a1 = result.get(0); + List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray"); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); }); } finally { paintingEntity.addRelationship(relationship); @@ -415,21 +358,18 @@ public class DataContextPrefetchIT extends ServerCase { try { - SelectQuery q = new SelectQuery(Artist.class); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class); q.setQualifier(ExpressionFactory.matchExp("artistName", "artist2")); q.addPrefetch(Artist.PAINTING_ARRAY.disjoint()); - final List<Artist> result = context.performQuery(q); + final List<Artist> result = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { - - public void execute() { - assertFalse(result.isEmpty()); - Artist a1 = result.get(0); - List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray"); - assertNotNull(toMany); - assertFalse(((ValueHolder) toMany).isFault()); - } + queryInterceptor.runWithQueriesBlocked(() -> { + assertFalse(result.isEmpty()); + Artist a1 = result.get(0); + List<?> toMany = (List<?>) a1.readPropertyDirectly("paintingArray"); + assertNotNull(toMany); + assertFalse(((ValueHolder) toMany).isFault()); }); } finally { @@ -441,25 +381,22 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetch_ToOne() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - SelectQuery q = new SelectQuery(Painting.class); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class); q.addPrefetch(Painting.TO_ARTIST.disjoint()); - final List<Painting> result = context.performQuery(q); + final List<Painting> result = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { + assertFalse(result.isEmpty()); + Painting p1 = result.get(0); - public void execute() { - assertFalse(result.isEmpty()); - Painting p1 = result.get(0); - - Object toOnePrefetch = p1.readNestedProperty("toArtist"); - assertNotNull(toOnePrefetch); - assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), - toOnePrefetch instanceof Artist); + Object toOnePrefetch = p1.readNestedProperty("toArtist"); + assertNotNull(toOnePrefetch); + assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), + toOnePrefetch instanceof Artist); - Artist a1 = (Artist) toOnePrefetch; - assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState()); - } + Artist a1 = (Artist) toOnePrefetch; + assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState()); }); } @@ -467,11 +404,11 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetch_ToOne_DbPath() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - SelectQuery q = new SelectQuery(Painting.class); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class); q.addPrefetch(Painting.TO_ARTIST.disjoint()); q.andQualifier(ExpressionFactory.matchDbExp("toArtist.ARTIST_NAME", "artist2")); - List<Painting> results = context.performQuery(q); + List<Painting> results = context.select(q); assertEquals(1, results.size()); } @@ -480,16 +417,16 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetch_ToOne_ObjPath() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - SelectQuery q = new SelectQuery(Painting.class); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class); q.addPrefetch(Painting.TO_ARTIST.disjoint()); q.andQualifier(ExpressionFactory.matchExp("toArtist.artistName", "artist2")); - List<Painting> results = context.performQuery(q); + List<Painting> results = context.select(q); assertEquals(1, results.size()); } @Test - public void testPrefetch_ReflexiveRelationship() throws Exception { + public void testPrefetch_ReflexiveRelationship() { ArtGroup parent = (ArtGroup) context.newObject("ArtGroup"); parent.setName("parent"); ArtGroup child = (ArtGroup) context.newObject("ArtGroup"); @@ -497,21 +434,18 @@ public class DataContextPrefetchIT extends ServerCase { child.setToParentGroup(parent); context.commitChanges(); - SelectQuery q = new SelectQuery("ArtGroup"); + SelectQuery<ArtGroup> q = new SelectQuery<>("ArtGroup"); q.setQualifier(ExpressionFactory.matchExp("name", "child")); q.addPrefetch("toParentGroup"); - final List<ArtGroup> results = context.performQuery(q); + final List<ArtGroup> results = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - public void execute() { - assertEquals(1, results.size()); - - ArtGroup fetchedChild = results.get(0); - // The parent must be fully fetched, not just HOLLOW (a fault) - assertEquals(PersistenceState.COMMITTED, fetchedChild.getToParentGroup().getPersistenceState()); - } + ArtGroup fetchedChild = results.get(0); + // The parent must be fully fetched, not just HOLLOW (a fault) + assertEquals(PersistenceState.COMMITTED, fetchedChild.getToParentGroup().getPersistenceState()); }); child.setToParentGroup(null); @@ -524,21 +458,18 @@ public class DataContextPrefetchIT extends ServerCase { Expression exp = ExpressionFactory.matchExp("toArtist.artistName", "artist3"); - SelectQuery q = new SelectQuery(Painting.class, exp); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class, exp); q.addPrefetch(Painting.TO_ARTIST.disjoint()); - final List<Painting> results = context.performQuery(q); + final List<Painting> results = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - public void execute() { - assertEquals(1, results.size()); + Painting painting = results.get(0); - Painting painting = results.get(0); - - // The parent must be fully fetched, not just HOLLOW (a fault) - assertEquals(PersistenceState.COMMITTED, painting.getToArtist().getPersistenceState()); - } + // The parent must be fully fetched, not just HOLLOW (a fault) + assertEquals(PersistenceState.COMMITTED, painting.getToArtist().getPersistenceState()); }); } @@ -559,20 +490,17 @@ public class DataContextPrefetchIT extends ServerCase { // OUTER join part intentionally doesn't match anything Expression exp = Property.create("groupArray+.name", String.class).eq("XX").orExp(Artist.ARTIST_NAME.eq("artist2")); - SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class, exp); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class, exp); q.addPrefetch(Artist.PAINTING_ARRAY.disjoint()); final List<Artist> results = context.select(q); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - public void execute() { - assertEquals(1, results.size()); - - Artist a = results.get(0); - assertEquals("artist2", a.getArtistName()); - assertEquals(2, a.getPaintingArray().size()); - } + Artist a = results.get(0); + assertEquals("artist2", a.getArtistName()); + assertEquals(2, a.getPaintingArray().size()); }); } @@ -581,7 +509,7 @@ public class DataContextPrefetchIT extends ServerCase { createTwoArtistsAndTwoPaintingsDataSet(); Expression artistExp = ExpressionFactory.matchExp("artistName", "artist3"); - SelectQuery artistQuery = new SelectQuery(Artist.class, artistExp); + SelectQuery<Artist> artistQuery = new SelectQuery<>(Artist.class, artistExp); Artist artist1 = (Artist) context.performQuery(artistQuery).get(0); // find the painting not matching the artist (this is the case where @@ -589,22 +517,19 @@ public class DataContextPrefetchIT extends ServerCase { // at least makes sense) Expression exp = ExpressionFactory.noMatchExp("toArtist", artist1); - SelectQuery q = new SelectQuery(Painting.class, exp); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class, exp); q.addPrefetch("toArtist"); - final List<Painting> results = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + final List<Painting> results = context.select(q); - public void execute() { - assertEquals(1, results.size()); + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - // see that artists are resolved... + // see that artists are resolved... - Painting px = results.get(0); - Artist ax = (Artist) px.readProperty(Painting.TO_ARTIST.getName()); - assertEquals(PersistenceState.COMMITTED, ax.getPersistenceState()); - } + Painting px = results.get(0); + Artist ax = (Artist) px.readProperty(Painting.TO_ARTIST.getName()); + assertEquals(PersistenceState.COMMITTED, ax.getPersistenceState()); }); } @@ -613,33 +538,30 @@ public class DataContextPrefetchIT extends ServerCase { createArtistWithTwoPaintingsAndTwoInfosDataSet(); Expression e = ExpressionFactory.likeExp("toArtist.artistName", "a%"); - SelectQuery q = new SelectQuery(Painting.class, e); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class, e); q.addPrefetch(Painting.TO_PAINTING_INFO.disjoint()); q.addOrdering(Painting.PAINTING_TITLE.asc()); - final List<Painting> results = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + final List<Painting> results = context.select(q); - public void execute() { - assertEquals(2, results.size()); + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(2, results.size()); - // testing non-null to-one target - Painting p0 = results.get(0); - Object o2 = p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); - assertTrue(o2 instanceof PaintingInfo); - PaintingInfo pi2 = (PaintingInfo) o2; - assertEquals(PersistenceState.COMMITTED, pi2.getPersistenceState()); - assertEquals(Cayenne.intPKForObject(p0), Cayenne.intPKForObject(pi2)); + // testing non-null to-one target + Painting p0 = results.get(0); + Object o2 = p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); + assertTrue(o2 instanceof PaintingInfo); + PaintingInfo pi2 = (PaintingInfo) o2; + assertEquals(PersistenceState.COMMITTED, pi2.getPersistenceState()); + assertEquals(Cayenne.intPKForObject(p0), Cayenne.intPKForObject(pi2)); - // testing null to-one target - Painting p1 = results.get(1); - assertNull(p1.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName())); + // testing null to-one target + Painting p1 = results.get(1); + assertNull(p1.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName())); - // there was a bug marking an object as dirty when clearing the - // relationships - assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState()); - } + // there was a bug marking an object as dirty when clearing the + // relationships + assertEquals(PersistenceState.COMMITTED, p1.getPersistenceState()); }); } @@ -648,11 +570,10 @@ public class DataContextPrefetchIT extends ServerCase { createTwoArtistsAndTwoPaintingsDataSet(); Expression e = ExpressionFactory.matchExp("dateOfBirth", new Date()); - SelectQuery q = new SelectQuery(Artist.class, e); + SelectQuery<Artist> q = new SelectQuery<>(Artist.class, e); q.addPrefetch("paintingArray"); - // prefetch with query using date in qualifier used to fail on SQL - // Server + // prefetch with query using date in qualifier used to fail on SQL Server // see CAY-119 for details context.performQuery(q); } @@ -662,19 +583,16 @@ public class DataContextPrefetchIT extends ServerCase { tPainting.insert(6, "p_Xty", null, 1000, null); - SelectQuery q = new SelectQuery(Painting.class); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class); q.addPrefetch(Painting.TO_ARTIST.disjoint()); - final List<Painting> paintings = context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + final List<Painting> paintings = context.select(q); - public void execute() { - assertEquals(1, paintings.size()); + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, paintings.size()); - Painting p2 = paintings.get(0); - assertNull(p2.readProperty(Painting.TO_ARTIST.getName())); - } + Painting p2 = paintings.get(0); + assertNull(p2.readProperty(Painting.TO_ARTIST.getName())); }); } @@ -682,35 +600,32 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetchToOneSharedCache() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - final SelectQuery q = new SelectQuery(Painting.class); + SelectQuery<Painting> q = new SelectQuery<>(Painting.class); q.addPrefetch(Painting.TO_ARTIST.disjoint()); q.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE); - context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + context.select(q); - public void execute() { - // per CAY-499 second run of a cached query with prefetches - // (i.e. when the - // result is served from cache) used to throw an exception... + queryInterceptor.runWithQueriesBlocked(() -> { + // per CAY-499 second run of a cached query with prefetches + // (i.e. when the + // result is served from cache) used to throw an exception... - List<Painting> cachedResult = context.performQuery(q); + List<Painting> cachedResult = context.select(q); - assertFalse(cachedResult.isEmpty()); - Painting p1 = cachedResult.get(0); + assertFalse(cachedResult.isEmpty()); + Painting p1 = cachedResult.get(0); - Object toOnePrefetch = p1.readNestedProperty("toArtist"); - assertNotNull(toOnePrefetch); - assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), - toOnePrefetch instanceof Artist); + Object toOnePrefetch = p1.readNestedProperty("toArtist"); + assertNotNull(toOnePrefetch); + assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), + toOnePrefetch instanceof Artist); - Artist a1 = (Artist) toOnePrefetch; - assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState()); + Artist a1 = (Artist) toOnePrefetch; + assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState()); - // and just in case - run one more time... - context.performQuery(q); - } + // and just in case - run one more time... + context.performQuery(q); }); } @@ -718,35 +633,32 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetchToOneLocalCache() throws Exception { createTwoArtistsAndTwoPaintingsDataSet(); - final SelectQuery q = new SelectQuery(Painting.class); + final SelectQuery<Painting> q = new SelectQuery<>(Painting.class); q.addPrefetch(Painting.TO_ARTIST.disjoint()); q.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE); - context.performQuery(q); - - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + context.select(q); - public void execute() { - // per CAY-499 second run of a cached query with prefetches - // (i.e. when the - // result is served from cache) used to throw an exception... + queryInterceptor.runWithQueriesBlocked(() -> { + // per CAY-499 second run of a cached query with prefetches + // (i.e. when the + // result is served from cache) used to throw an exception... - List<Painting> cachedResult = context.performQuery(q); + List<Painting> cachedResult = context.select(q); - assertFalse(cachedResult.isEmpty()); - Painting p1 = cachedResult.get(0); + assertFalse(cachedResult.isEmpty()); + Painting p1 = cachedResult.get(0); - Object toOnePrefetch = p1.readNestedProperty("toArtist"); - assertNotNull(toOnePrefetch); - assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), - toOnePrefetch instanceof Artist); + Object toOnePrefetch = p1.readNestedProperty("toArtist"); + assertNotNull(toOnePrefetch); + assertTrue("Expected Artist, got: " + toOnePrefetch.getClass().getName(), + toOnePrefetch instanceof Artist); - Artist a1 = (Artist) toOnePrefetch; - assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState()); + Artist a1 = (Artist) toOnePrefetch; + assertEquals(PersistenceState.COMMITTED, a1.getPersistenceState()); - // and just in case - run one more time... - context.performQuery(q); - } + // and just in case - run one more time... + context.performQuery(q); }); } @@ -754,22 +666,19 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetchToOneWithBackRelationship() throws Exception { createArtistWithTwoPaintingsAndTwoInfosDataSet(); - SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class); + SelectQuery<Painting> query = new SelectQuery<>(Painting.class); query.andQualifier(Painting.PAINTING_TITLE.eq("p_artist2")); query.addPrefetch(Painting.TO_PAINTING_INFO.disjoint()); query.addPrefetch(Painting.TO_PAINTING_INFO.dot(PaintingInfo.PAINTING).disjoint()); final List<Painting> results = context.select(query); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { - - public void execute() { - assertEquals(1, results.size()); + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - Painting p0 = results.get(0); - PaintingInfo pi0 = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); - assertNotNull(pi0); - assertNotNull(pi0.readPropertyDirectly(PaintingInfo.PAINTING.getName())); - } + Painting p0 = results.get(0); + PaintingInfo pi0 = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); + assertNotNull(pi0); + assertNotNull(pi0.readPropertyDirectly(PaintingInfo.PAINTING.getName())); }); } @@ -783,17 +692,14 @@ public class DataContextPrefetchIT extends ServerCase { query.addPrefetch(Painting.TO_ARTIST.dot(Artist.PAINTING_ARRAY).disjoint()); final List<Painting> results = context.select(query); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { - - public void execute() { - assertEquals(1, results.size()); + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - Painting p0 = results.get(0); - Artist a0 = (Artist) p0.readPropertyDirectly(Painting.TO_ARTIST.getName()); - assertNotNull(a0); - List<?> paintings = (List<?>) a0.readPropertyDirectly(Artist.PAINTING_ARRAY.getName()); - assertEquals(2, paintings.size()); - } + Painting p0 = results.get(0); + Artist a0 = (Artist) p0.readPropertyDirectly(Painting.TO_ARTIST.getName()); + assertNotNull(a0); + List<?> paintings = (List<?>) a0.readPropertyDirectly(Artist.PAINTING_ARRAY.getName()); + assertEquals(2, paintings.size()); }); } @@ -801,22 +707,19 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetchToOneWithBackRelationship_Joint() throws Exception { createArtistWithTwoPaintingsAndTwoInfosDataSet(); - SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class); + SelectQuery<Painting> query = new SelectQuery<>(Painting.class); query.andQualifier(Painting.PAINTING_TITLE.eq("p_artist2")); query.addPrefetch(Painting.TO_PAINTING_INFO.joint()); query.addPrefetch(Painting.TO_PAINTING_INFO.dot(PaintingInfo.PAINTING).joint()); final List<Painting> results = context.select(query); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - public void execute() { - assertEquals(1, results.size()); - - Painting p0 = results.get(0); - PaintingInfo pi0 = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); - assertNotNull(pi0); - assertNotNull(pi0.readPropertyDirectly(PaintingInfo.PAINTING.getName())); - } + Painting p0 = results.get(0); + PaintingInfo pi0 = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); + assertNotNull(pi0); + assertNotNull(pi0.readPropertyDirectly(PaintingInfo.PAINTING.getName())); }); } @@ -824,24 +727,21 @@ public class DataContextPrefetchIT extends ServerCase { public void testPrefetchJointAndDisjointByIdTogether() throws Exception { createArtistWithTwoPaintingsAndTwoInfosDataSet(); - SelectQuery<Painting> query = new SelectQuery<Painting>(Painting.class); + SelectQuery<Painting> query = new SelectQuery<>(Painting.class); query.andQualifier(Painting.PAINTING_TITLE.eq("p_artist2")); query.addPrefetch(Painting.TO_ARTIST.joint()); query.addPrefetch(Painting.TO_PAINTING_INFO.disjointById()); final List<Painting> results = context.select(query); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { + queryInterceptor.runWithQueriesBlocked(() -> { + assertEquals(1, results.size()); - public void execute() { - assertEquals(1, results.size()); + Painting p0 = results.get(0); + Artist a0 = (Artist) p0.readPropertyDirectly(Painting.TO_ARTIST.getName()); + assertNotNull(a0); - Painting p0 = results.get(0); - Artist a0 = (Artist) p0.readPropertyDirectly(Painting.TO_ARTIST.getName()); - assertNotNull(a0); - - PaintingInfo info = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); - assertNotNull(info); - } + PaintingInfo info = (PaintingInfo) p0.readPropertyDirectly(Painting.TO_PAINTING_INFO.getName()); + assertNotNull(info); }); } @@ -850,12 +750,7 @@ public class DataContextPrefetchIT extends ServerCase { */ @Test public void testPrefetchWithLocalCache() throws Exception { - tArtist.deleteAll(); - tGallery.deleteAll(); - tPainting.deleteAll(); - tArtist.insert(1, "artist1"); - tGallery.insert(1, "gallery1"); - tPainting.insert(1, "painting1", 1, 100, 1); + createArtistWithPaintingAndGallery(); List<Painting> paintings = ObjectSelect.query(Painting.class) .localCache("g1").select(context); @@ -868,39 +763,31 @@ public class DataContextPrefetchIT extends ServerCase { assertEquals(1, paintings.size()); assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { - - public void execute() { - List<Painting> paintings = ObjectSelect.query(Painting.class) - .prefetch(Painting.TO_ARTIST.joint()) - .localCache("g1").select(context); - assertEquals(1, paintings.size()); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); - } + queryInterceptor.runWithQueriesBlocked(() -> { + List<Painting> paintings1 = ObjectSelect.query(Painting.class) + .prefetch(Painting.TO_ARTIST.joint()) + .localCache("g1").select(context); + assertEquals(1, paintings1.size()); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); }); } @Test public void testPrefetchWithSharedCache() throws Exception { - tArtist.deleteAll(); - tGallery.deleteAll(); - tPainting.deleteAll(); - tArtist.insert(1, "artist1"); - tGallery.insert(1, "gallery1"); - tPainting.insert(1, "painting1", 1, 100, 1); + createArtistWithPaintingAndGallery(); - final ObjectSelect<Painting> s1 = ObjectSelect.query(Painting.class) - .sharedCache("g1"); + ObjectSelect<Painting> s1 = ObjectSelect.query(Painting.class) + .sharedCache("g1"); - final ObjectSelect<Painting> s2 = ObjectSelect.query(Painting.class) - .prefetch(Painting.TO_ARTIST.disjoint()) - .sharedCache("g1"); + ObjectSelect<Painting> s2 = ObjectSelect.query(Painting.class) + .prefetch(Painting.TO_ARTIST.disjoint()) + .sharedCache("g1"); - final ObjectSelect<Painting> s3 = ObjectSelect.query(Painting.class) - .prefetch(Painting.TO_GALLERY.joint()) - .sharedCache("g1"); + ObjectSelect<Painting> s3 = ObjectSelect.query(Painting.class) + .prefetch(Painting.TO_GALLERY.joint()) + .sharedCache("g1"); - final ObjectSelect<Painting> s4 = ObjectSelect.query(Painting.class) + ObjectSelect<Painting> s4 = ObjectSelect.query(Painting.class) .prefetch(Painting.TO_ARTIST.disjoint()) .prefetch(Painting.TO_GALLERY.joint()) .sharedCache("g1"); @@ -926,25 +813,22 @@ public class DataContextPrefetchIT extends ServerCase { assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery); - queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() { - - public void execute() { - // select from cache - List<Painting> paintings = s2.select(context); - assertEquals(1, paintings.size()); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Fault); - - paintings = s3.select(context); - assertEquals(1, paintings.size()); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery); - - paintings = s4.select(context); - assertEquals(1, paintings.size()); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); - assertTrue(paintings.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery); - } + queryInterceptor.runWithQueriesBlocked(() -> { + // select from cache + List<Painting> paintings1 = s2.select(context); + assertEquals(1, paintings1.size()); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Fault); + + paintings1 = s3.select(context); + assertEquals(1, paintings1.size()); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Fault); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery); + + paintings1 = s4.select(context); + assertEquals(1, paintings1.size()); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_ARTIST.getName()) instanceof Artist); + assertTrue(paintings1.get(0).readPropertyDirectly(Painting.TO_GALLERY.getName()) instanceof Gallery); }); }