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