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