|
| 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 | + |
| 17 | +package org.apache.ibatis.reflection.wrapper; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.apache.ibatis.reflection.DefaultReflectorFactory; |
| 26 | +import org.apache.ibatis.reflection.MetaObject; |
| 27 | +import org.apache.ibatis.reflection.ReflectionException; |
| 28 | +import org.apache.ibatis.reflection.factory.DefaultObjectFactory; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +class BeanWrapperTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + void assertBasicOperations() { |
| 35 | + Bean1 bean = new Bean1(); |
| 36 | + MetaObject metaObj = MetaObject.forObject(bean, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), |
| 37 | + new DefaultReflectorFactory()); |
| 38 | + assertFalse(metaObj.isCollection()); |
| 39 | + assertTrue(metaObj.hasGetter("id")); |
| 40 | + assertTrue(metaObj.hasSetter("id")); |
| 41 | + assertTrue(metaObj.hasGetter("bean2.id")); |
| 42 | + assertTrue(metaObj.hasSetter("bean2.id")); |
| 43 | + assertEquals("id", metaObj.findProperty("id", false)); |
| 44 | + assertNull(metaObj.findProperty("attr_val", false)); |
| 45 | + assertEquals("attrVal", metaObj.findProperty("attr_val", true)); |
| 46 | + String[] getterNames = metaObj.getGetterNames(); |
| 47 | + Arrays.sort(getterNames); |
| 48 | + assertArrayEquals(new String[] { "attrVal", "bean2", "bean2List", "id", "nums" }, getterNames); |
| 49 | + String[] setterNames = metaObj.getSetterNames(); |
| 50 | + Arrays.sort(setterNames); |
| 51 | + assertArrayEquals(new String[] { "attrVal", "bean2", "bean2List", "id", "nums" }, setterNames); |
| 52 | + assertEquals(String.class, metaObj.getGetterType("attrVal")); |
| 53 | + assertEquals(String.class, metaObj.getSetterType("attrVal")); |
| 54 | + assertEquals(String.class, metaObj.getGetterType("bean2.name")); |
| 55 | + assertEquals(String.class, metaObj.getSetterType("bean2.name")); |
| 56 | + |
| 57 | + assertTrue(metaObj.hasGetter("bean2List[0]")); |
| 58 | + try { |
| 59 | + metaObj.getValue("bean2List[0]"); |
| 60 | + fail(); |
| 61 | + } catch (ReflectionException e) { |
| 62 | + // TODO: more accurate message |
| 63 | + assertEquals("The 'bean2List' property of null is not a List or Array.", e.getMessage()); |
| 64 | + } |
| 65 | + assertTrue(metaObj.hasSetter("bean2List[0]")); |
| 66 | + |
| 67 | + List<Bean2> bean2List = new ArrayList<>(); |
| 68 | + for (int i = 0; i < 3; i++) { |
| 69 | + Bean2 bean2 = new Bean2(); |
| 70 | + bean2.setId(i); |
| 71 | + bean2.setName("name_" + i); |
| 72 | + bean2List.add(bean2); |
| 73 | + } |
| 74 | + bean.setBean2List(bean2List); |
| 75 | + |
| 76 | + assertEquals(0, metaObj.getValue("bean2List[0].id")); |
| 77 | + |
| 78 | + metaObj.setValue("attrVal", "value"); |
| 79 | + assertEquals("value", bean.getAttrVal()); |
| 80 | + |
| 81 | + metaObj.setValue("bean2List[1].name", "new name 1"); |
| 82 | + assertEquals("new name 1", bean.getBean2List().get(1).getName()); |
| 83 | + |
| 84 | + try { |
| 85 | + metaObj.getValue("nums[0]"); |
| 86 | + fail(); |
| 87 | + } catch (ReflectionException e) { |
| 88 | + // pass |
| 89 | + } |
| 90 | + metaObj.setValue("nums", new Integer[] { 5, 6, 7 }); |
| 91 | + assertTrue(metaObj.hasGetter("bean2List[0].child.id")); |
| 92 | + assertTrue(metaObj.hasSetter("bean2List[0].child.id")); |
| 93 | + |
| 94 | + { |
| 95 | + Bean2 bean2 = new Bean2(); |
| 96 | + bean2.setId(100); |
| 97 | + bean2.setName("name_100"); |
| 98 | + metaObj.setValue("bean2", bean2); |
| 99 | + assertEquals(String.class, metaObj.getGetterType("bean2.name")); |
| 100 | + assertEquals(String.class, metaObj.getSetterType("bean2.name")); |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + metaObj.setValue("bean2.child.bean2", "bogus"); |
| 105 | + fail(); |
| 106 | + } catch (ReflectionException e) { |
| 107 | + // pass |
| 108 | + } |
| 109 | + |
| 110 | + { |
| 111 | + Bean2 bean2 = new Bean2(); |
| 112 | + bean2.setId(101); |
| 113 | + bean2.setName("name_101"); |
| 114 | + metaObj.setValue("bean2.child.bean2", bean2); |
| 115 | + assertEquals(101, bean.getBean2().getChild().getBean2().getId()); |
| 116 | + } |
| 117 | + |
| 118 | + metaObj.setValue("bean2.child.nums", new Integer[] { 8, 9 }); |
| 119 | + metaObj.setValue("bean2.child.nums[0]", 88); |
| 120 | + assertEquals(88, bean.getBean2().getChild().getNums()[0]); |
| 121 | + |
| 122 | + assertFalse(metaObj.hasSetter("x[0].y")); |
| 123 | + assertFalse(metaObj.hasGetter("x[0].y")); |
| 124 | + |
| 125 | + try { |
| 126 | + metaObj.getValue("x"); |
| 127 | + fail(); |
| 128 | + } catch (ReflectionException e) { |
| 129 | + // pass |
| 130 | + } |
| 131 | + // assertEquals(Integer.class, metaObj.getSetterType("my_name")); |
| 132 | + // assertEquals("100", metaObj.getValue("a")); |
| 133 | + // assertNull(metaObj.getValue("b")); |
| 134 | + // assertEquals(Integer.valueOf(200), metaObj.getValue("my_name")); |
| 135 | + try { |
| 136 | + metaObj.add("x"); |
| 137 | + fail(); |
| 138 | + } catch (UnsupportedOperationException e) { |
| 139 | + // pass |
| 140 | + } |
| 141 | + try { |
| 142 | + metaObj.addAll(Arrays.asList("x", "y")); |
| 143 | + fail(); |
| 144 | + } catch (UnsupportedOperationException e) { |
| 145 | + // pass |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + static class Bean1 { |
| 151 | + private Integer id; |
| 152 | + private String attrVal; |
| 153 | + private Integer[] nums; |
| 154 | + private Bean2 bean2; |
| 155 | + private List<Bean2> bean2List; |
| 156 | + |
| 157 | + public Integer getId() { |
| 158 | + return id; |
| 159 | + } |
| 160 | + |
| 161 | + public void setId(Integer id) { |
| 162 | + this.id = id; |
| 163 | + } |
| 164 | + |
| 165 | + public String getAttrVal() { |
| 166 | + return attrVal; |
| 167 | + } |
| 168 | + |
| 169 | + public void setAttrVal(String attrVal) { |
| 170 | + this.attrVal = attrVal; |
| 171 | + } |
| 172 | + |
| 173 | + public Integer[] getNums() { |
| 174 | + return nums; |
| 175 | + } |
| 176 | + |
| 177 | + public void setNums(Integer[] nums) { |
| 178 | + this.nums = nums; |
| 179 | + } |
| 180 | + |
| 181 | + public Bean2 getBean2() { |
| 182 | + return bean2; |
| 183 | + } |
| 184 | + |
| 185 | + public void setBean2(Bean2 bean2) { |
| 186 | + this.bean2 = bean2; |
| 187 | + } |
| 188 | + |
| 189 | + public List<Bean2> getBean2List() { |
| 190 | + return bean2List; |
| 191 | + } |
| 192 | + |
| 193 | + public void setBean2List(List<Bean2> bean2List) { |
| 194 | + this.bean2List = bean2List; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + static class Bean2 { |
| 199 | + private Integer id; |
| 200 | + private String name; |
| 201 | + private Bean1 child; |
| 202 | + |
| 203 | + public Integer getId() { |
| 204 | + return id; |
| 205 | + } |
| 206 | + |
| 207 | + public void setId(Integer id) { |
| 208 | + this.id = id; |
| 209 | + } |
| 210 | + |
| 211 | + public String getName() { |
| 212 | + return name; |
| 213 | + } |
| 214 | + |
| 215 | + public void setName(String name) { |
| 216 | + this.name = name; |
| 217 | + } |
| 218 | + |
| 219 | + public Bean1 getChild() { |
| 220 | + return child; |
| 221 | + } |
| 222 | + |
| 223 | + public void setChild(Bean1 child) { |
| 224 | + this.child = child; |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments