Skip to content

Commit 3bffa98

Browse files
committed
CAY-2902 Shared Query Cache overwrites newer object state (added failing unit test)
1 parent 8bfd2f7 commit 3bffa98

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

cayenne/src/test/java/org/apache/cayenne/cache/QueryCacheIT.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,46 @@ public void testSharedCacheStoresAnImmutableList() {
9494
List<Artist> result2 = context1.performQuery(q);
9595
assertEquals("the list stored in the shared query cache cannot be mutated after being returned", 1, result2.size());
9696
}
97+
98+
@Test
99+
public void testLocalCacheRerunDoesntClobberNewerInMemoryState() {
100+
101+
Artist a = context1.newObject(Artist.class);
102+
a.setArtistName("artist");
103+
context1.commitChanges();
104+
105+
ObjectSelect<Artist> query = ObjectSelect.query(Artist.class).localCache(); // LOCAL CACHE
106+
Artist result1 = query.selectFirst(context1);
107+
assertEquals("should populate shared cache", "artist", result1.getArtistName());
108+
109+
a.setArtistName("modified"); // change the name in memory, and on disk
110+
context1.commitChanges();
111+
112+
Artist result2 = ObjectSelect.query(Artist.class).selectFirst(context1);
113+
assertEquals("should be no cache used", "modified", result2.getArtistName());
114+
115+
Artist result3 = query.selectFirst(context1);
116+
assertEquals("should use shared cache, but shouldn't wipe up newer in-memory data", "modified", result3.getArtistName());
117+
}
118+
119+
@Test
120+
public void testSharedCacheRerunDoesntClobberNewerInMemoryState() {
121+
122+
Artist a = context1.newObject(Artist.class);
123+
a.setArtistName("artist");
124+
context1.commitChanges();
125+
126+
ObjectSelect<Artist> query = ObjectSelect.query(Artist.class).sharedCache(); // SHARED CACHE
127+
Artist result1 = query.selectFirst(context1);
128+
assertEquals("should populate shared cache", "artist", result1.getArtistName());
129+
130+
a.setArtistName("modified"); // change the name in memory, and on disk
131+
context1.commitChanges();
132+
133+
Artist result2 = ObjectSelect.query(Artist.class).selectFirst(context1);
134+
assertEquals("should be no cache used", "modified", result2.getArtistName());
135+
136+
Artist result3 = query.selectFirst(context1);
137+
assertEquals("should use shared cache, but shouldn't wipe out newer in-memory data", "modified", result3.getArtistName());
138+
}
97139
}

0 commit comments

Comments
 (0)