View Javadoc

1   /*
2    * $Id: DataTypesTestCase.java 19250 2010-08-30 16:53:14Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transformer;
11  
12  import org.mule.api.transformer.DataType;
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.transformer.types.CollectionDataType;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.transformer.types.MimeTypes;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.Set;
23  
24  public class DataTypesTestCase extends AbstractMuleTestCase
25  {
26      //Just used for testing
27      private List<Exception> listOfExceptions;
28  
29      public void testSimpleTypes() throws Exception
30      {
31          DataType dt = DataTypeFactory.create(Exception.class);
32          DataType dt2 = DataTypeFactory.create(Exception.class);
33  
34          assertTrue(dt.isCompatibleWith(dt2));
35          assertEquals(dt, dt2);
36  
37          dt2 = DataTypeFactory.create(IOException.class);
38  
39          assertTrue(dt.isCompatibleWith(dt2));
40          assertFalse(dt.equals(dt2));
41  
42          //Check mime type matching
43          dt2 = DataTypeFactory.create(IOException.class, "application/exception+java");
44  
45          //Will match because the default mime type is '*/*'
46          assertTrue(dt.isCompatibleWith(dt2));
47          assertFalse(dt.equals(dt2));
48  
49          dt.setMimeType(MimeTypes.BINARY);
50  
51          assertFalse(dt.isCompatibleWith(dt2));
52          assertFalse(dt.equals(dt2));
53  
54          dt = DataTypeFactory.create(Exception.class);
55          dt2 = DataTypeFactory.STRING;
56  
57          assertFalse(dt.isCompatibleWith(dt2));
58          assertFalse(dt.equals(dt2));
59      }
60  
61      public void testCollectionTypes() throws Exception
62      {
63          DataType dt = DataTypeFactory.create(List.class);
64          DataType dt2 = DataTypeFactory.create(List.class);
65  
66          assertTrue(dt.isCompatibleWith(dt2));
67          assertEquals(dt, dt2);
68  
69          dt2 = DataTypeFactory.create(ArrayList.class);
70  
71          assertTrue(dt.isCompatibleWith(dt2));
72          assertFalse(dt.equals(dt2));
73  
74          //Check mime type matching
75          dt2 = DataTypeFactory.create(ArrayList.class, "application/list+java");
76  
77          //Will match because the default mime type is '*/*'
78          assertTrue(dt.isCompatibleWith(dt2));
79          assertFalse(dt.equals(dt2));
80  
81          dt.setMimeType(MimeTypes.BINARY);
82  
83          assertFalse(dt.isCompatibleWith(dt2));
84          assertFalse(dt.equals(dt2));
85  
86          dt = DataTypeFactory.create(List.class);
87          dt2 = DataTypeFactory.create(Set.class);
88  
89          assertFalse(dt.isCompatibleWith(dt2));
90          assertFalse(dt.equals(dt2));
91  
92      }
93  
94      public void testGenericCollectionTypes() throws Exception
95      {
96          DataType dt = DataTypeFactory.create(List.class, Exception.class);
97          DataType dt2 = DataTypeFactory.create(List.class, Exception.class);
98  
99          assertTrue(dt.isCompatibleWith(dt2));
100         assertEquals(dt, dt2);
101 
102         dt2 = DataTypeFactory.create(ArrayList.class, IOException.class);
103 
104         assertTrue(dt.isCompatibleWith(dt2));
105         assertFalse(dt.equals(dt2));
106 
107         //Check mime type matching
108         dt2 = DataTypeFactory.create(ArrayList.class, IOException.class, "application/list+java");
109 
110         //Will match because the default mime type is '*/*'
111         assertTrue(dt.isCompatibleWith(dt2));
112         assertFalse(dt.equals(dt2));
113 
114         dt.setMimeType(MimeTypes.BINARY);
115 
116         assertFalse(dt.isCompatibleWith(dt2));
117         assertFalse(dt.equals(dt2));
118 
119         //Test Generic Item types don't match
120         dt = DataTypeFactory.create(List.class, Exception.class);
121         dt2 = DataTypeFactory.create(List.class, String.class);
122 
123         assertFalse(dt.isCompatibleWith(dt2));
124         assertFalse(dt.equals(dt2));
125     }
126 
127 
128     public void testGenericCollectionTypesFromMethodReturn() throws Exception
129     {
130         DataType dt = DataTypeFactory.createFromReturnType(getClass().getDeclaredMethod("listOfExceptionsMethod", String.class));
131         assertTrue(dt instanceof CollectionDataType);
132 
133         assertEquals(List.class, dt.getType());
134         assertEquals(Exception.class, ((CollectionDataType) dt).getItemType());
135 
136         DataType dt2 = DataTypeFactory.createFromReturnType(getClass().getDeclaredMethod("listOfExceptionsMethod", String.class));
137         assertTrue(dt.isCompatibleWith(dt2));
138         assertEquals(dt, dt2);
139 
140         dt2 = DataTypeFactory.createFromReturnType(getClass().getDeclaredMethod("listOfExceptionsMethod", Integer.class));
141         assertTrue(dt.isCompatibleWith(dt2));
142         assertFalse(dt.equals(dt2));
143 
144     }
145 
146     public void testGenericCollectionTypesFromMethodParam() throws Exception
147     {
148         DataType dt = DataTypeFactory.createFromParameterType(getClass().getDeclaredMethod("listOfExceptionsMethod", Collection.class), 0);
149         assertTrue(dt instanceof CollectionDataType);
150 
151         assertEquals(Collection.class, dt.getType());
152         assertEquals(Exception.class, ((CollectionDataType) dt).getItemType());
153 
154         DataType dt2 = DataTypeFactory.createFromParameterType(getClass().getDeclaredMethod("listOfExceptionsMethod", Collection.class), 0);
155         assertTrue(dt.isCompatibleWith(dt2));
156         assertEquals(dt, dt2);
157 
158         dt2 = DataTypeFactory.createFromParameterType(getClass().getDeclaredMethod("listOfExceptionsMethod", List.class), 0);
159         assertTrue(dt.isCompatibleWith(dt2));
160         assertFalse(dt.equals(dt2));
161     }
162 
163     public void testGenericCollectionTypesFromField() throws Exception
164     {
165         DataType dt = DataTypeFactory.createFromField(getClass().getDeclaredField("listOfExceptions"));
166         assertTrue(dt instanceof CollectionDataType);
167 
168         assertEquals(List.class, dt.getType());
169         assertEquals(Exception.class, ((CollectionDataType) dt).getItemType());
170     }
171 
172     private List<Exception> listOfExceptionsMethod(String s)
173     {
174         return null;
175     }
176 
177     private ArrayList<IOException> listOfExceptionsMethod(Integer i)
178     {
179         return null;
180     }
181 
182     private String listOfExceptionsMethod(Collection<Exception> exceptions)
183     {
184         return null;
185     }
186 
187     private Integer listOfExceptionsMethod(List<IOException> ioExceptions)
188     {
189         return null;
190     }
191 }