|
| 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 java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.ibatis.domain.blog.Author; |
| 25 | +import org.apache.ibatis.domain.misc.RichType; |
| 26 | +import org.apache.ibatis.reflection.SystemMetaObject; |
| 27 | +import org.apache.ibatis.reflection.property.PropertyTokenizer; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.*; |
| 32 | +import static org.junit.jupiter.api.Assertions.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author <a href="[email protected]">mawen12</a> |
| 36 | + * @see BeanWrapper |
| 37 | + */ |
| 38 | +class BeanWrapperUnitTest extends ObjectWrapperBaseTest { |
| 39 | + |
| 40 | + private RichType richType; |
| 41 | + |
| 42 | + private ObjectWrapper wrapper; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setup() { |
| 46 | + this.richType = new RichType(); |
| 47 | + this.wrapper = new BeanWrapper(SystemMetaObject.forObject(richType), richType); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @Override |
| 52 | + void shouldGet() { |
| 53 | + richType.setRichProperty("mybatis"); |
| 54 | + |
| 55 | + Object value = wrapper.get(new PropertyTokenizer("richProperty")); |
| 56 | + |
| 57 | + assertEquals("mybatis", value); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void shouldGetWhichContainsDelim() { |
| 62 | + RichType nested = new RichType(); |
| 63 | + nested.setRichProperty("mybatis"); |
| 64 | + richType.setRichType(nested); |
| 65 | + |
| 66 | + Object value = wrapper.get(new PropertyTokenizer("richType.richProperty")); |
| 67 | + |
| 68 | + assertEquals("mybatis", value); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void shouldGetWhichContainsIndex() { |
| 73 | + richType.setRichList(Arrays.asList(1L, "abc")); |
| 74 | + richType.setRichMap(new HashMap<String, Object>(){{ |
| 75 | + put("key1", "value1"); |
| 76 | + put("key2", "value2"); |
| 77 | + }}); |
| 78 | + |
| 79 | + assertEquals("abc", wrapper.get(new PropertyTokenizer("richList[1]"))); |
| 80 | + assertEquals("value2", wrapper.get(new PropertyTokenizer("richMap[key2]"))); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @Override |
| 86 | + void shouldSet() { |
| 87 | + wrapper.set(new PropertyTokenizer("richProperty"), "mybatis"); |
| 88 | + |
| 89 | + assertEquals("mybatis", richType.getRichProperty()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void shouldSetWhichContainsDelim() { |
| 94 | + wrapper.set(new PropertyTokenizer("richType.richProperty"), "mybatis"); |
| 95 | + |
| 96 | + assertEquals("mybatis", richType.getRichType().getRichProperty()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void shouldSetWhichContainsIndex() { |
| 101 | + List<Object> list = Arrays.asList(1L, 2L); |
| 102 | + richType.setRichList(list); |
| 103 | + |
| 104 | + wrapper.set(new PropertyTokenizer("richList[0]"), "mybatis"); |
| 105 | + |
| 106 | + assertEquals("mybatis", list.get(0)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @Override |
| 111 | + void shouldFindProperty() { |
| 112 | + String property = wrapper.findProperty("richProperty", false); |
| 113 | + |
| 114 | + assertEquals("richProperty", property); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void shouldFindPropertyContainsDelim() { |
| 119 | + String property = wrapper.findProperty("richType.richProperty", false); |
| 120 | + |
| 121 | + assertEquals("richType.richProperty", property); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void shouldFindPropertyContainsIndex() { |
| 126 | + String property = wrapper.findProperty("richList[0]", false); |
| 127 | + |
| 128 | + assertNull(property); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + @Override |
| 133 | + void shouldGetGetterNames() { |
| 134 | + String[] getterNames = wrapper.getGetterNames(); |
| 135 | + |
| 136 | + assertThat(getterNames).containsExactlyInAnyOrder("richType", "richProperty", "richList", "richMap", "richField"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + @Override |
| 141 | + void shouldGetSetterNames() { |
| 142 | + String[] setterNames = wrapper.getSetterNames(); |
| 143 | + |
| 144 | + assertThat(setterNames).containsExactlyInAnyOrder("richType", "richProperty", "richList", "richMap", "richField"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @Override |
| 149 | + void shouldGetGetterType() { |
| 150 | + assertEquals(RichType.class, wrapper.getGetterType("richType")); |
| 151 | + assertEquals(String.class, wrapper.getGetterType("richField")); |
| 152 | + assertEquals(String.class, wrapper.getGetterType("richProperty")); |
| 153 | + assertEquals(Map.class, wrapper.getGetterType("richMap")); |
| 154 | + assertEquals(List.class, wrapper.getGetterType("richList")); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + @Override |
| 159 | + void shouldGetSetterType() { |
| 160 | + assertEquals(RichType.class, wrapper.getSetterType("richType")); |
| 161 | + assertEquals(String.class, wrapper.getSetterType("richField")); |
| 162 | + assertEquals(String.class, wrapper.getSetterType("richProperty")); |
| 163 | + assertEquals(Map.class, wrapper.getSetterType("richMap")); |
| 164 | + assertEquals(List.class, wrapper.getSetterType("richList")); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + @Override |
| 169 | + void shouldHasGetter() { |
| 170 | + assertTrue(wrapper.hasGetter("richType")); |
| 171 | + assertTrue(wrapper.hasGetter("richField")); |
| 172 | + assertTrue(wrapper.hasGetter("richProperty")); |
| 173 | + assertTrue(wrapper.hasGetter("richMap")); |
| 174 | + assertTrue(wrapper.hasGetter("richList")); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + @Override |
| 179 | + void shouldHasSetter() { |
| 180 | + assertTrue(wrapper.hasGetter("richType")); |
| 181 | + assertTrue(wrapper.hasGetter("richField")); |
| 182 | + assertTrue(wrapper.hasGetter("richProperty")); |
| 183 | + assertTrue(wrapper.hasGetter("richMap")); |
| 184 | + assertTrue(wrapper.hasGetter("richList")); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + @Override |
| 189 | + void shouldIsCollection() { |
| 190 | + assertFalse(wrapper.isCollection()); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + @Override |
| 195 | + void shouldInstantiatePropertyValue() { |
| 196 | + // Nothing |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + @Override |
| 201 | + void shouldAddElement() { |
| 202 | + assertThatExceptionOfType(UnsupportedOperationException.class) |
| 203 | + .isThrownBy(() -> wrapper.add("1")); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + @Override |
| 208 | + void shouldAddAll() { |
| 209 | + assertThatExceptionOfType(UnsupportedOperationException.class) |
| 210 | + .isThrownBy(() -> wrapper.addAll(new ArrayList<>())); |
| 211 | + } |
| 212 | + |
| 213 | +} |
0 commit comments