|
22 | 22 | import org.apache.cayenne.di.Inject; |
23 | 23 | import org.apache.cayenne.query.ObjectSelect; |
24 | 24 | import org.apache.cayenne.testdo.testmap.Artist; |
| 25 | +import org.apache.cayenne.testdo.testmap.Painting; |
25 | 26 | import org.apache.cayenne.unit.di.runtime.CayenneProjects; |
26 | 27 | import org.apache.cayenne.unit.di.runtime.RuntimeCase; |
27 | 28 | import org.apache.cayenne.unit.di.runtime.UseCayenneRuntime; |
@@ -94,4 +95,118 @@ public void testSharedCacheStoresAnImmutableList() { |
94 | 95 | List<Artist> result2 = context1.performQuery(q); |
95 | 96 | assertEquals("the list stored in the shared query cache cannot be mutated after being returned", 1, result2.size()); |
96 | 97 | } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testLocalCacheRerunDoesntClobberNewerInMemoryState() { |
| 101 | + |
| 102 | + Artist a = context1.newObject(Artist.class); |
| 103 | + a.setArtistName("artist"); |
| 104 | + context1.commitChanges(); |
| 105 | + |
| 106 | + ObjectSelect<Artist> query = ObjectSelect.query(Artist.class).localCache(); // LOCAL CACHE |
| 107 | + Artist result1 = query.selectFirst(context1); |
| 108 | + assertEquals("should populate shared cache", "artist", result1.getArtistName()); |
| 109 | + |
| 110 | + a.setArtistName("modified"); // change the name in memory, and on disk |
| 111 | + context1.commitChanges(); |
| 112 | + |
| 113 | + Artist result2 = ObjectSelect.query(Artist.class).selectFirst(context1); |
| 114 | + assertEquals("should be no cache used", "modified", result2.getArtistName()); |
| 115 | + |
| 116 | + Artist result3 = query.selectFirst(context1); |
| 117 | + assertEquals("should use shared cache, but shouldn't wipe up newer in-memory data", "modified", result3.getArtistName()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testSharedCacheRerunDoesntClobberNewerInMemoryState() { |
| 122 | + |
| 123 | + Artist a = context1.newObject(Artist.class); |
| 124 | + a.setArtistName("artist"); |
| 125 | + context1.commitChanges(); |
| 126 | + |
| 127 | + ObjectSelect<Artist> query = ObjectSelect.query(Artist.class).sharedCache(); // SHARED CACHE |
| 128 | + Artist result1 = query.selectFirst(context1); |
| 129 | + assertEquals("should populate shared cache", "artist", result1.getArtistName()); |
| 130 | + |
| 131 | + a.setArtistName("modified"); // change the name in memory, and on disk |
| 132 | + context1.commitChanges(); |
| 133 | + |
| 134 | + Artist result2 = ObjectSelect.query(Artist.class).selectFirst(context1); |
| 135 | + assertEquals("should be no cache used", "modified", result2.getArtistName()); |
| 136 | + |
| 137 | + Artist result3 = query.selectFirst(context1); |
| 138 | + assertEquals("should use shared cache, but shouldn't wipe out newer in-memory data", "modified", result3.getArtistName()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testSharedCacheWithPrefetchDoesntClobberNewerInMemoryState() { |
| 143 | + |
| 144 | + Artist a = context1.newObject(Artist.class); |
| 145 | + a.setArtistName("artist"); |
| 146 | + |
| 147 | + Painting p = context1.newObject(Painting.class); |
| 148 | + p.setPaintingTitle("painting"); |
| 149 | + p.setToArtist(a); |
| 150 | + |
| 151 | + context1.commitChanges(); |
| 152 | + |
| 153 | + // Query with shared cache AND prefetch |
| 154 | + ObjectSelect<Painting> query = ObjectSelect.query(Painting.class) |
| 155 | + .prefetch(Painting.TO_ARTIST.disjoint()) |
| 156 | + .sharedCache(); |
| 157 | + |
| 158 | + Painting result1 = query.selectFirst(context1); |
| 159 | + assertEquals("should populate shared cache", "artist", result1.getToArtist().getArtistName()); |
| 160 | + |
| 161 | + // Modify the artist name in memory and on disk |
| 162 | + a.setArtistName("modified"); |
| 163 | + context1.commitChanges(); |
| 164 | + |
| 165 | + // Non-cached query should see the change |
| 166 | + Painting result2 = ObjectSelect.query(Painting.class) |
| 167 | + .prefetch(Painting.TO_ARTIST.disjoint()) |
| 168 | + .selectFirst(context1); |
| 169 | + assertEquals("should be no cache used", "modified", result2.getToArtist().getArtistName()); |
| 170 | + |
| 171 | + // Re-run cached query with prefetch - should still see the newer data |
| 172 | + Painting result3 = query.selectFirst(context1); |
| 173 | + assertEquals("should use shared cache with prefetch, but shouldn't wipe out newer in-memory data", |
| 174 | + "modified", result3.getToArtist().getArtistName()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testSharedCacheWithPrefetchDoesntClobberNewerPrefetchedObjectState() { |
| 179 | + |
| 180 | + Artist a = context1.newObject(Artist.class); |
| 181 | + a.setArtistName("artist"); |
| 182 | + |
| 183 | + Painting p = context1.newObject(Painting.class); |
| 184 | + p.setPaintingTitle("painting"); |
| 185 | + p.setToArtist(a); |
| 186 | + |
| 187 | + context1.commitChanges(); |
| 188 | + |
| 189 | + // Query with shared cache AND prefetch to load the artist |
| 190 | + ObjectSelect<Painting> query = ObjectSelect.query(Painting.class) |
| 191 | + .prefetch(Painting.TO_ARTIST.disjoint()) |
| 192 | + .sharedCache(); |
| 193 | + |
| 194 | + Painting result1 = query.selectFirst(context1); |
| 195 | + Artist artist1 = result1.getToArtist(); |
| 196 | + assertEquals("should populate shared cache", "artist", artist1.getArtistName()); |
| 197 | + |
| 198 | + // Modify the prefetched artist object directly (simulate in-memory change) |
| 199 | + artist1.setArtistName("modified"); |
| 200 | + context1.commitChanges(); |
| 201 | + |
| 202 | + // Re-run cached query - should NOT clobber the in-memory modification |
| 203 | + Painting result2 = query.selectFirst(context1); |
| 204 | + Artist artist2 = result2.getToArtist(); |
| 205 | + |
| 206 | + // Should be the same object instances (from context) |
| 207 | + assertEquals("should be same painting object in context", result1, result2); |
| 208 | + assertEquals("should be same artist object in context", artist1, artist2); |
| 209 | + assertEquals("cached query should not clobber newer in-memory state of prefetched object", |
| 210 | + "modified", artist2.getArtistName()); |
| 211 | + } |
97 | 212 | } |
0 commit comments