|
17 | 17 | package org.springframework.context.expression;
|
18 | 18 |
|
19 | 19 | import java.util.HashMap;
|
| 20 | +import java.util.List; |
20 | 21 | import java.util.Map;
|
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.Test;
|
|
36 | 37 | class MapAccessorTests {
|
37 | 38 |
|
38 | 39 | @Test
|
39 |
| - void mapAccessorCompilable() { |
| 40 | + void compilationSupport() { |
40 | 41 | Map<String, Object> testMap = getSimpleTestMap();
|
41 | 42 | StandardEvaluationContext sec = new StandardEvaluationContext();
|
42 | 43 | sec.addPropertyAccessor(new MapAccessor());
|
43 | 44 | SpelExpressionParser sep = new SpelExpressionParser();
|
44 | 45 |
|
45 | 46 | // basic
|
46 | 47 | Expression ex = sep.parseExpression("foo");
|
47 |
| - assertThat(ex.getValue(sec,testMap)).isEqualTo("bar"); |
| 48 | + assertThat(ex.getValue(sec, testMap)).isEqualTo("bar"); |
48 | 49 | assertThat(SpelCompiler.compile(ex)).isTrue();
|
49 |
| - assertThat(ex.getValue(sec,testMap)).isEqualTo("bar"); |
| 50 | + assertThat(ex.getValue(sec, testMap)).isEqualTo("bar"); |
50 | 51 |
|
51 | 52 | // compound expression
|
52 | 53 | ex = sep.parseExpression("foo.toUpperCase()");
|
53 |
| - assertThat(ex.getValue(sec,testMap)).isEqualTo("BAR"); |
| 54 | + assertThat(ex.getValue(sec, testMap)).isEqualTo("BAR"); |
54 | 55 | assertThat(SpelCompiler.compile(ex)).isTrue();
|
55 |
| - assertThat(ex.getValue(sec,testMap)).isEqualTo("BAR"); |
| 56 | + assertThat(ex.getValue(sec, testMap)).isEqualTo("BAR"); |
56 | 57 |
|
57 | 58 | // nested map
|
58 |
| - Map<String,Map<String,Object>> nestedMap = getNestedTestMap(); |
| 59 | + Map<String, Map<String, Object>> nestedMap = getNestedTestMap(); |
59 | 60 | ex = sep.parseExpression("aaa.foo.toUpperCase()");
|
60 |
| - assertThat(ex.getValue(sec,nestedMap)).isEqualTo("BAR"); |
| 61 | + assertThat(ex.getValue(sec, nestedMap)).isEqualTo("BAR"); |
61 | 62 | assertThat(SpelCompiler.compile(ex)).isTrue();
|
62 |
| - assertThat(ex.getValue(sec,nestedMap)).isEqualTo("BAR"); |
| 63 | + assertThat(ex.getValue(sec, nestedMap)).isEqualTo("BAR"); |
63 | 64 |
|
64 | 65 | // avoiding inserting checkcast because first part of expression returns a Map
|
65 | 66 | ex = sep.parseExpression("getMap().foo");
|
66 | 67 | MapGetter mapGetter = new MapGetter();
|
67 |
| - assertThat(ex.getValue(sec,mapGetter)).isEqualTo("bar"); |
| 68 | + assertThat(ex.getValue(sec, mapGetter)).isEqualTo("bar"); |
68 | 69 | assertThat(SpelCompiler.compile(ex)).isTrue();
|
69 |
| - assertThat(ex.getValue(sec,mapGetter)).isEqualTo("bar"); |
| 70 | + assertThat(ex.getValue(sec, mapGetter)).isEqualTo("bar"); |
70 | 71 |
|
71 | 72 | // basic isWritable
|
72 | 73 | ex = sep.parseExpression("foo");
|
73 |
| - assertThat(ex.isWritable(sec,testMap)).isTrue(); |
| 74 | + assertThat(ex.isWritable(sec, testMap)).isTrue(); |
74 | 75 |
|
75 | 76 | // basic write
|
76 | 77 | ex = sep.parseExpression("foo2");
|
77 | 78 | ex.setValue(sec, testMap, "bar2");
|
78 |
| - assertThat(ex.getValue(sec,testMap)).isEqualTo("bar2"); |
| 79 | + assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2"); |
79 | 80 | assertThat(SpelCompiler.compile(ex)).isTrue();
|
80 |
| - assertThat(ex.getValue(sec,testMap)).isEqualTo("bar2"); |
| 81 | + assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2"); |
81 | 82 | }
|
82 | 83 |
|
83 | 84 | @Test
|
84 |
| - void mapAccessorNotWritable() { |
| 85 | + void isWritable() { |
85 | 86 | Map<String, Object> testMap = getSimpleTestMap();
|
86 | 87 | StandardEvaluationContext sec = new StandardEvaluationContext();
|
87 |
| - sec.addPropertyAccessor(new MapAccessor(false)); |
88 | 88 | SpelExpressionParser sep = new SpelExpressionParser();
|
89 | 89 | Expression ex = sep.parseExpression("foo");
|
| 90 | + |
| 91 | + assertThat(ex.isWritable(sec, testMap)).isFalse(); |
| 92 | + |
| 93 | + sec.setPropertyAccessors(List.of(new MapAccessor(true))); |
| 94 | + assertThat(ex.isWritable(sec, testMap)).isTrue(); |
| 95 | + |
| 96 | + sec.setPropertyAccessors(List.of(new MapAccessor(false))); |
90 | 97 | assertThat(ex.isWritable(sec, testMap)).isFalse();
|
91 | 98 | }
|
92 | 99 |
|
| 100 | + |
| 101 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
93 | 102 | public static class MapGetter {
|
94 |
| - Map<String,Object> map = new HashMap<>(); |
| 103 | + Map map = new HashMap<>(); |
95 | 104 |
|
96 | 105 | public MapGetter() {
|
97 |
| - map.put("foo", "bar"); |
| 106 | + this.map.put("foo", "bar"); |
98 | 107 | }
|
99 | 108 |
|
100 |
| - @SuppressWarnings("rawtypes") |
101 | 109 | public Map getMap() {
|
102 |
| - return map; |
| 110 | + return this.map; |
103 | 111 | }
|
104 | 112 | }
|
105 | 113 |
|
106 |
| - public Map<String,Object> getSimpleTestMap() { |
| 114 | + private static Map<String,Object> getSimpleTestMap() { |
107 | 115 | Map<String,Object> map = new HashMap<>();
|
108 | 116 | map.put("foo","bar");
|
109 | 117 | return map;
|
110 | 118 | }
|
111 | 119 |
|
112 |
| - public Map<String,Map<String,Object>> getNestedTestMap() { |
| 120 | + private static Map<String,Map<String,Object>> getNestedTestMap() { |
113 | 121 | Map<String,Object> map = new HashMap<>();
|
114 | 122 | map.put("foo","bar");
|
115 | 123 | Map<String,Map<String,Object>> map2 = new HashMap<>();
|
|
0 commit comments