Ensure MapAccessor#canWrite only returns true for a Map target

Closes gh-33265
This commit is contained in:
Sam Brannen
2024-07-23 18:26:53 +03:00
parent 4684a17f00
commit 4fa9781549
2 changed files with 17 additions and 1 deletions

View File

@@ -81,6 +81,22 @@ class MapAccessorTests {
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2");
}
@Test
void canWrite() throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext();
Map<String, Object> testMap = getSimpleTestMap();
MapAccessor mapAccessor = new MapAccessor();
assertThat(mapAccessor.canWrite(context, new Object(), "foo")).isFalse();
assertThat(mapAccessor.canWrite(context, testMap, "foo")).isTrue();
// Cannot actually write to an immutable Map, but MapAccessor cannot easily check for that.
assertThat(mapAccessor.canWrite(context, Map.of(), "x")).isTrue();
mapAccessor = new MapAccessor(false);
assertThat(mapAccessor.canWrite(context, new Object(), "foo")).isFalse();
assertThat(mapAccessor.canWrite(context, testMap, "foo")).isFalse();
}
@Test
void isWritable() {
Map<String, Object> testMap = getSimpleTestMap();