View Javadoc
1   /*
2    *    Copyright 2009-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.reflection.wrapper;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import org.apache.ibatis.domain.blog.Author;
34  import org.apache.ibatis.reflection.MetaObject;
35  import org.apache.ibatis.reflection.SystemMetaObject;
36  import org.apache.ibatis.reflection.property.PropertyTokenizer;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  import org.mockito.Mock;
40  
41  /**
42   * @author <a href="1181963012mw@gmail.com">mawen12</a>
43   *
44   * @see MapWrapper
45   */
46  class MapWrapperUnitTest extends ObjectWrapperBase {
47  
48    @Mock
49    private Map<String, Object> map;
50  
51    @Mock
52    private List<Integer> list;
53  
54    private MetaObject metaObject;
55  
56    private ObjectWrapper wrapper;
57  
58    @BeforeEach
59    void setup() {
60      this.metaObject = SystemMetaObject.forObject(map);
61      this.wrapper = new MapWrapper(metaObject, map);
62    }
63  
64    @Test
65    @Override
66    void shouldGet() {
67      when(map.get("key")).thenReturn("value");
68  
69      Object value = wrapper.get(new PropertyTokenizer("key"));
70  
71      assertEquals("value", value);
72      verify(map).get("key");
73    }
74  
75    @Test
76    void shouldNotGetWhichContainsDelim() {
77      Author author = new Author(1);
78      when(map.get("author")).thenReturn(author);
79  
80      Object value = wrapper.get(new PropertyTokenizer("author.id"));
81  
82      assertEquals(1, value);
83    }
84  
85    @Test
86    void shouldGetWhichContainsIndex() {
87      when(list.get(0)).thenReturn(1);
88      when(map.get("key")).thenReturn(list);
89  
90      Object value = wrapper.get(new PropertyTokenizer("key[0]"));
91  
92      assertEquals(1, value);
93    }
94  
95    @Test
96    @Override
97    void shouldSet() {
98      wrapper.set(new PropertyTokenizer("key"), "value");
99  
100     verify(map).put("key", "value");
101   }
102 
103   @Test
104   void shouldSetWhichContainsDelim() {
105     wrapper.set(new PropertyTokenizer("author.id"), 1);
106 
107     verify(map).put("author", new HashMap<>() {
108       private static final long serialVersionUID = 1L;
109 
110       {
111         put("id", 1);
112       }
113     });
114   }
115 
116   @Test
117   void shouldSetWhichContainsIndex() {
118     when(map.get("key")).thenReturn(list);
119 
120     wrapper.set(new PropertyTokenizer("key[0]"), 1);
121 
122     verify(list).set(0, 1);
123   }
124 
125   @Test
126   @Override
127   void shouldFindProperty() {
128     assertEquals("abc", wrapper.findProperty("abc", true));
129     assertEquals("abc", wrapper.findProperty("abc", false));
130   }
131 
132   @Test
133   @Override
134   void shouldGetGetterNames() {
135     Set<String> sets = new HashSet<>() {
136       private static final long serialVersionUID = 1L;
137 
138       {
139         add("key1");
140         add("key2");
141       }
142     };
143     when(map.keySet()).thenReturn(sets);
144 
145     String[] getterNames = wrapper.getGetterNames();
146 
147     assertEquals(2, getterNames.length);
148     assertThat(getterNames).containsExactlyInAnyOrder("key1", "key2");
149   }
150 
151   @Test
152   @Override
153   void shouldGetSetterNames() {
154     Set<String> sets = new HashSet<>() {
155       private static final long serialVersionUID = 1L;
156 
157       {
158         add("key1");
159         add("key2");
160       }
161     };
162     when(map.keySet()).thenReturn(sets);
163 
164     String[] setterNames = wrapper.getSetterNames();
165 
166     assertEquals(2, setterNames.length);
167     assertThat(setterNames).containsExactlyInAnyOrder("key1", "key2");
168   }
169 
170   @Test
171   @Override
172   void shouldGetGetterType() {
173     when(map.get("key")).thenReturn("abc");
174 
175     Class<?> type = wrapper.getGetterType("key");
176 
177     assertEquals(String.class, type);
178   }
179 
180   @Test
181   void shouldGetGetterTypeWhichContainsIndex() {
182     when(map.get("key")).thenReturn(list);
183 
184     Class<?> collectionType = wrapper.getGetterType("key");
185     Class<?> type = wrapper.getGetterType("key[0]");
186 
187     assertEquals(list.getClass(), collectionType);
188     assertEquals(Object.class, type);
189   }
190 
191   @Test
192   @Override
193   void shouldGetSetterType() {
194     when(map.get("key")).thenReturn("abc");
195 
196     Class<?> type = wrapper.getSetterType("key");
197 
198     assertEquals(String.class, type);
199   }
200 
201   @Test
202   void shouldGetSetterTypeWhichContainsIndex() {
203     when(map.get("key")).thenReturn(list);
204 
205     Class<?> collectionType = wrapper.getSetterType("key");
206     Class<?> type = wrapper.getSetterType("key[0]");
207 
208     assertEquals(list.getClass(), collectionType);
209     assertEquals(Object.class, type);
210   }
211 
212   @Test
213   @Override
214   void shouldHasGetter() {
215     when(map.containsKey("key")).thenReturn(false);
216 
217     assertFalse(wrapper.hasGetter("key"));
218 
219     when(map.containsKey("key")).thenReturn(true);
220 
221     assertTrue(wrapper.hasGetter("key"));
222   }
223 
224   @Test
225   @Override
226   void shouldHasSetter() {
227     assertTrue(wrapper.hasSetter("abc"));
228   }
229 
230   @Test
231   @Override
232   void shouldIsCollection() {
233     assertFalse(wrapper.isCollection());
234   }
235 
236   @Test
237   @Override
238   void shouldInstantiatePropertyValue() {
239     MetaObject result = wrapper.instantiatePropertyValue("abc", new PropertyTokenizer("key"),
240         SystemMetaObject.DEFAULT_OBJECT_FACTORY);
241 
242     assertFalse(result.hasGetter("key"));
243   }
244 
245   @Test
246   @Override
247   void shouldAddElement() {
248     assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> wrapper.add("1"));
249   }
250 
251   @Test
252   @Override
253   void shouldAddAll() {
254     assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> wrapper.addAll(new ArrayList<>()));
255   }
256 }