1
2
3
4
5
6
7 package org.mule.transformer;
8
9 import org.mule.api.transformer.DataType;
10 import org.mule.tck.junit4.AbstractMuleTestCase;
11 import org.mule.transformer.types.CollectionDataType;
12 import org.mule.transformer.types.DataTypeFactory;
13 import org.mule.transformer.types.MimeTypes;
14
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.junit.Test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 public class DataTypesTestCase extends AbstractMuleTestCase
28 {
29
30 private List<Exception> listOfExceptions;
31
32 @Test
33 public void testSimpleTypes() throws Exception
34 {
35 DataType dt = DataTypeFactory.create(Exception.class);
36 DataType dt2 = DataTypeFactory.create(Exception.class);
37
38 assertTrue(dt.isCompatibleWith(dt2));
39 assertEquals(dt, dt2);
40
41 dt2 = DataTypeFactory.create(IOException.class);
42
43 assertTrue(dt.isCompatibleWith(dt2));
44 assertFalse(dt.equals(dt2));
45
46
47 dt2 = DataTypeFactory.create(IOException.class, "application/exception+java");
48
49
50 assertTrue(dt.isCompatibleWith(dt2));
51 assertFalse(dt.equals(dt2));
52
53 dt.setMimeType(MimeTypes.BINARY);
54
55 assertFalse(dt.isCompatibleWith(dt2));
56 assertFalse(dt.equals(dt2));
57
58 dt = DataTypeFactory.create(Exception.class);
59 dt2 = DataTypeFactory.STRING;
60
61 assertFalse(dt.isCompatibleWith(dt2));
62 assertFalse(dt.equals(dt2));
63 }
64
65 @Test
66 public void testCollectionTypes() throws Exception
67 {
68 DataType dt = DataTypeFactory.create(List.class);
69 DataType dt2 = DataTypeFactory.create(List.class);
70
71 assertTrue(dt.isCompatibleWith(dt2));
72 assertEquals(dt, dt2);
73
74 dt2 = DataTypeFactory.create(ArrayList.class);
75
76 assertTrue(dt.isCompatibleWith(dt2));
77 assertFalse(dt.equals(dt2));
78
79
80 dt2 = DataTypeFactory.create(ArrayList.class, "application/list+java");
81
82
83 assertTrue(dt.isCompatibleWith(dt2));
84 assertFalse(dt.equals(dt2));
85
86 dt.setMimeType(MimeTypes.BINARY);
87
88 assertFalse(dt.isCompatibleWith(dt2));
89 assertFalse(dt.equals(dt2));
90
91 dt = DataTypeFactory.create(List.class);
92 dt2 = DataTypeFactory.create(Set.class);
93
94 assertFalse(dt.isCompatibleWith(dt2));
95 assertFalse(dt.equals(dt2));
96
97 }
98
99 @Test
100 public void testGenericCollectionTypes() throws Exception
101 {
102 DataType dt = DataTypeFactory.create(List.class, Exception.class);
103 DataType dt2 = DataTypeFactory.create(List.class, Exception.class);
104
105 assertTrue(dt.isCompatibleWith(dt2));
106 assertEquals(dt, dt2);
107
108 dt2 = DataTypeFactory.create(ArrayList.class, IOException.class);
109
110 assertTrue(dt.isCompatibleWith(dt2));
111 assertFalse(dt.equals(dt2));
112
113
114 dt2 = DataTypeFactory.create(ArrayList.class, IOException.class, "application/list+java");
115
116
117 assertTrue(dt.isCompatibleWith(dt2));
118 assertFalse(dt.equals(dt2));
119
120 dt.setMimeType(MimeTypes.BINARY);
121
122 assertFalse(dt.isCompatibleWith(dt2));
123 assertFalse(dt.equals(dt2));
124
125
126 dt = DataTypeFactory.create(List.class, Exception.class);
127 dt2 = DataTypeFactory.create(List.class, String.class);
128
129 assertFalse(dt.isCompatibleWith(dt2));
130 assertFalse(dt.equals(dt2));
131 }
132
133
134 @Test
135 public void testGenericCollectionTypesFromMethodReturn() throws Exception
136 {
137 DataType dt = DataTypeFactory.createFromReturnType(getClass().getDeclaredMethod("listOfExceptionsMethod", String.class));
138 assertTrue(dt instanceof CollectionDataType);
139
140 assertEquals(List.class, dt.getType());
141 assertEquals(Exception.class, ((CollectionDataType) dt).getItemType());
142
143 DataType dt2 = DataTypeFactory.createFromReturnType(getClass().getDeclaredMethod("listOfExceptionsMethod", String.class));
144 assertTrue(dt.isCompatibleWith(dt2));
145 assertEquals(dt, dt2);
146
147 dt2 = DataTypeFactory.createFromReturnType(getClass().getDeclaredMethod("listOfExceptionsMethod", Integer.class));
148 assertTrue(dt.isCompatibleWith(dt2));
149 assertFalse(dt.equals(dt2));
150
151 }
152
153 @Test
154 public void testGenericCollectionTypesFromMethodParam() throws Exception
155 {
156 DataType dt = DataTypeFactory.createFromParameterType(getClass().getDeclaredMethod("listOfExceptionsMethod", Collection.class), 0);
157 assertTrue(dt instanceof CollectionDataType);
158
159 assertEquals(Collection.class, dt.getType());
160 assertEquals(Exception.class, ((CollectionDataType) dt).getItemType());
161
162 DataType dt2 = DataTypeFactory.createFromParameterType(getClass().getDeclaredMethod("listOfExceptionsMethod", Collection.class), 0);
163 assertTrue(dt.isCompatibleWith(dt2));
164 assertEquals(dt, dt2);
165
166 dt2 = DataTypeFactory.createFromParameterType(getClass().getDeclaredMethod("listOfExceptionsMethod", List.class), 0);
167 assertTrue(dt.isCompatibleWith(dt2));
168 assertFalse(dt.equals(dt2));
169 }
170
171 @Test
172 public void testGenericCollectionTypesFromField() throws Exception
173 {
174 DataType dt = DataTypeFactory.createFromField(getClass().getDeclaredField("listOfExceptions"));
175 assertTrue(dt instanceof CollectionDataType);
176
177 assertEquals(List.class, dt.getType());
178 assertEquals(Exception.class, ((CollectionDataType) dt).getItemType());
179 }
180
181 private List<Exception> listOfExceptionsMethod(String s)
182 {
183 return null;
184 }
185
186 private ArrayList<IOException> listOfExceptionsMethod(Integer i)
187 {
188 return null;
189 }
190
191 private String listOfExceptionsMethod(Collection<Exception> exceptions)
192 {
193 return null;
194 }
195
196 private Integer listOfExceptionsMethod(List<IOException> ioExceptions)
197 {
198 return null;
199 }
200 }