1
2
3
4
5
6
7
8
9
10 package org.mule.util.generics;
11
12 import org.mule.tck.junit4.AbstractMuleTestCase;
13
14 import java.lang.reflect.Method;
15 import java.lang.reflect.Type;
16 import java.util.AbstractMap;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26
27 public class GenericsUtilsTestCase extends AbstractMuleTestCase
28 {
29
30 protected Class<?> targetClass;
31
32 protected String methods[];
33
34 protected Type expectedResults[];
35
36 @Before
37 public void createTestData()
38 {
39 this.targetClass = Foo.class;
40 this.methods = new String[]{"a", "b", "b2", "b3", "c", "d", "d2", "d3", "e", "e2", "e3"};
41 this.expectedResults = new Class[]{
42 Integer.class, null, Set.class, Set.class, null, Integer.class,
43 Integer.class, Integer.class, Integer.class, Integer.class, Integer.class};
44 }
45
46 protected Type getType(Method method)
47 {
48 return GenericsUtils.getMapValueReturnType(method);
49 }
50
51 @Test
52 public void testA() throws Exception
53 {
54 executeTest();
55 }
56
57 @Test
58 public void testB() throws Exception
59 {
60 executeTest();
61 }
62
63 @Test
64 public void testB2() throws Exception
65 {
66 executeTest();
67 }
68
69 @Test
70 public void testB3() throws Exception
71 {
72 executeTest();
73 }
74
75 @Test
76 public void testC() throws Exception
77 {
78 executeTest();
79 }
80
81 @Test
82 public void testD() throws Exception
83 {
84 executeTest();
85 }
86
87 @Test
88 public void testD2() throws Exception
89 {
90 executeTest();
91 }
92
93 @Test
94 public void testD3() throws Exception
95 {
96 executeTest();
97 }
98
99 @Test
100 public void testE() throws Exception
101 {
102 executeTest();
103 }
104
105 @Test
106 public void testE2() throws Exception
107 {
108 executeTest();
109 }
110
111 @Test
112 public void testE3() throws Exception
113 {
114 executeTest();
115 }
116
117 @Test
118 public void testProgrammaticListIntrospection() throws Exception
119 {
120 Method setter = GenericBean.class.getMethod("setResourceList", List.class);
121 assertEquals(String.class,
122 GenericsUtils.getCollectionParameterType(new MethodParameter(setter, 0)));
123
124 Method getter = GenericBean.class.getMethod("getResourceList");
125 assertEquals(String.class,
126 GenericsUtils.getCollectionReturnType(getter));
127 }
128
129
130 private abstract class CustomMap<T> extends AbstractMap<String, Integer>
131 {
132 }
133
134
135 private abstract class OtherCustomMap<T> implements Map<String, Integer>
136 {
137 }
138
139
140 private interface Foo
141 {
142
143 Map<String, Integer> a();
144
145 Map<?, ?> b();
146
147 Map<?, ? extends Set> b2();
148
149 Map<?, ? super Set> b3();
150
151 Map c();
152
153 CustomMap<Date> d();
154
155 CustomMap<?> d2();
156
157 CustomMap d3();
158
159 OtherCustomMap<Date> e();
160
161 OtherCustomMap<?> e2();
162
163 OtherCustomMap e3();
164 }
165
166
167 protected void executeTest() throws NoSuchMethodException
168 {
169 String methodName = name.getMethodName().trim().replaceFirst("test", "").toLowerCase();
170 for (int i = 0; i < this.methods.length; i++)
171 {
172 if (methodName.equals(this.methods[i]))
173 {
174 Method method = this.targetClass.getMethod(methodName);
175 Type type = getType(method);
176 assertEquals(this.expectedResults[i], type);
177 return;
178 }
179 }
180 throw new IllegalStateException("Bad test data");
181 }
182
183
184 }