View Javadoc
1   /*
2    *    Copyright 2009-2023 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;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ibatis.domain.misc.RichType;
26  import org.apache.ibatis.domain.misc.generics.GenericConcrete;
27  import org.junit.jupiter.api.Test;
28  
29  class MetaClassTest {
30  
31    @Test
32    void shouldTestDataTypeOfGenericMethod() {
33      ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
34      MetaClass meta = MetaClass.forClass(GenericConcrete.class, reflectorFactory);
35      assertEquals(Long.class, meta.getGetterType("id"));
36      assertEquals(Long.class, meta.getSetterType("id"));
37    }
38  
39    @Test
40    void shouldThrowReflectionExceptionGetGetterType() {
41      try {
42        ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
43        MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
44        meta.getGetterType("aString");
45        org.junit.jupiter.api.Assertions.fail("should have thrown ReflectionException");
46      } catch (ReflectionException expected) {
47        assertEquals(
48            "There is no getter for property named \'aString\' in \'class org.apache.ibatis.domain.misc.RichType\'",
49            expected.getMessage());
50      }
51    }
52  
53    @Test
54    void shouldCheckGetterExistance() {
55      ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
56      MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
57      assertTrue(meta.hasGetter("richField"));
58      assertTrue(meta.hasGetter("richProperty"));
59      assertTrue(meta.hasGetter("richList"));
60      assertTrue(meta.hasGetter("richMap"));
61      assertTrue(meta.hasGetter("richList[0]"));
62  
63      assertTrue(meta.hasGetter("richType"));
64      assertTrue(meta.hasGetter("richType.richField"));
65      assertTrue(meta.hasGetter("richType.richProperty"));
66      assertTrue(meta.hasGetter("richType.richList"));
67      assertTrue(meta.hasGetter("richType.richMap"));
68      assertTrue(meta.hasGetter("richType.richList[0]"));
69  
70      assertEquals("richType.richProperty", meta.findProperty("richType.richProperty", false));
71  
72      assertFalse(meta.hasGetter("[0]"));
73    }
74  
75    @Test
76    void shouldCheckSetterExistance() {
77      ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
78      MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
79      assertTrue(meta.hasSetter("richField"));
80      assertTrue(meta.hasSetter("richProperty"));
81      assertTrue(meta.hasSetter("richList"));
82      assertTrue(meta.hasSetter("richMap"));
83      assertTrue(meta.hasSetter("richList[0]"));
84  
85      assertTrue(meta.hasSetter("richType"));
86      assertTrue(meta.hasSetter("richType.richField"));
87      assertTrue(meta.hasSetter("richType.richProperty"));
88      assertTrue(meta.hasSetter("richType.richList"));
89      assertTrue(meta.hasSetter("richType.richMap"));
90      assertTrue(meta.hasSetter("richType.richList[0]"));
91  
92      assertFalse(meta.hasSetter("[0]"));
93    }
94  
95    @Test
96    void shouldCheckTypeForEachGetter() {
97      ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
98      MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
99      assertEquals(String.class, meta.getGetterType("richField"));
100     assertEquals(String.class, meta.getGetterType("richProperty"));
101     assertEquals(List.class, meta.getGetterType("richList"));
102     assertEquals(Map.class, meta.getGetterType("richMap"));
103     assertEquals(List.class, meta.getGetterType("richList[0]"));
104 
105     assertEquals(RichType.class, meta.getGetterType("richType"));
106     assertEquals(String.class, meta.getGetterType("richType.richField"));
107     assertEquals(String.class, meta.getGetterType("richType.richProperty"));
108     assertEquals(List.class, meta.getGetterType("richType.richList"));
109     assertEquals(Map.class, meta.getGetterType("richType.richMap"));
110     assertEquals(List.class, meta.getGetterType("richType.richList[0]"));
111   }
112 
113   @Test
114   void shouldCheckTypeForEachSetter() {
115     ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
116     MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
117     assertEquals(String.class, meta.getSetterType("richField"));
118     assertEquals(String.class, meta.getSetterType("richProperty"));
119     assertEquals(List.class, meta.getSetterType("richList"));
120     assertEquals(Map.class, meta.getSetterType("richMap"));
121     assertEquals(List.class, meta.getSetterType("richList[0]"));
122 
123     assertEquals(RichType.class, meta.getSetterType("richType"));
124     assertEquals(String.class, meta.getSetterType("richType.richField"));
125     assertEquals(String.class, meta.getSetterType("richType.richProperty"));
126     assertEquals(List.class, meta.getSetterType("richType.richList"));
127     assertEquals(Map.class, meta.getSetterType("richType.richMap"));
128     assertEquals(List.class, meta.getSetterType("richType.richList[0]"));
129   }
130 
131   @Test
132   void shouldCheckGetterAndSetterNames() {
133     ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
134     MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
135     assertEquals(5, meta.getGetterNames().length);
136     assertEquals(5, meta.getSetterNames().length);
137   }
138 
139   @Test
140   void shouldFindPropertyName() {
141     ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
142     MetaClass meta = MetaClass.forClass(RichType.class, reflectorFactory);
143     assertEquals("richField", meta.findProperty("RICHfield"));
144   }
145 
146 }