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