1 15 package org.apache.tapestry.coerce; 16 17 import java.util.Collections ; 18 import java.util.Date ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.test.HiveMindTestCase; 22 import org.easymock.MockControl; 23 24 30 public class TestValueConverter extends HiveMindTestCase 31 { 32 33 public void testAlreadyCorrectType() 34 { 35 ValueConverter v = new ValueConverterImpl(); 36 37 Object input = new Integer (7); 38 39 assertSame(input, v.coerceValue(input, Integer .class)); 40 assertSame(input, v.coerceValue(input, Number .class)); 41 } 42 43 47 48 public void testAlreadyCorrectPrimitiveType() 49 { 50 ValueConverter v = new ValueConverterImpl(); 51 52 Object input = new Integer (9); 53 54 assertSame(input, v.coerceValue(input, int.class)); 55 } 56 57 public void testNoConverter() 58 { 59 ValueConverter v = new ValueConverterImpl(); 60 61 try 62 { 63 v.coerceValue("FRED", Date .class); 64 unreachable(); 65 } 66 catch (ApplicationRuntimeException ex) 67 { 68 assertEquals(CoerceMessages.noConverter(Date .class), ex.getMessage()); 69 } 70 } 71 72 75 76 public void testNoConverterPrimitive() 77 { 78 ValueConverter v = new ValueConverterImpl(); 79 80 try 81 { 82 v.coerceValue(new Object (), int.class); 83 unreachable(); 84 } 85 catch (ApplicationRuntimeException ex) 86 { 87 assertEquals(CoerceMessages.noConverter(Integer .class), ex.getMessage()); 88 } 89 } 90 91 public void testConverterFound() 92 { 93 MockControl tcc = newControl(TypeConverter.class); 94 TypeConverter tc = (TypeConverter) tcc.getMock(); 95 96 Object input = new Integer (7); 97 Object output = "SEVEN"; 98 99 tc.convertValue(input); 100 tcc.setReturnValue(output); 101 102 replayControls(); 103 104 ValueConverterImpl v = new ValueConverterImpl(); 105 106 TypeConverterContribution contrib = new TypeConverterContribution(); 107 108 contrib.setSubjectClass(String .class); 109 contrib.setConverter(tc); 110 111 v.setContributions(Collections.singletonList(contrib)); 112 113 v.initializeService(); 114 115 assertSame(output, v.coerceValue(input, String .class)); 116 117 verifyControls(); 118 } 119 120 public void testConverterFoundForNullValue() 121 { 122 MockControl tcc = newControl(TypeConverter.class); 123 TypeConverter tc = (TypeConverter) tcc.getMock(); 124 125 Object output = "NULL"; 126 127 tc.convertValue(null); 128 tcc.setReturnValue(output); 129 130 replayControls(); 131 132 ValueConverterImpl v = new ValueConverterImpl(); 133 134 TypeConverterContribution contrib = new TypeConverterContribution(); 135 136 contrib.setSubjectClass(String .class); 137 contrib.setConverter(tc); 138 139 v.setContributions(Collections.singletonList(contrib)); 140 141 v.initializeService(); 142 143 assertSame(output, v.coerceValue(null, String .class)); 144 145 verifyControls(); 146 } 147 148 public void testNoConverterFoundForNullValue() 149 { 150 ValueConverterImpl v = new ValueConverterImpl(); 151 152 assertNull(v.coerceValue(null, Date .class)); 153 } 154 155 public void testStringToNumberPrimitive() 156 { 157 ValueConverterImpl v = new ValueConverterImpl(); 158 159 Object result = v.coerceValue("123", int.class); 160 161 assertEquals(new Integer (123), result); 162 } 163 164 public void testStringToNumberWrapper() 165 { 166 ValueConverterImpl v = new ValueConverterImpl(); 167 168 Object result = v.coerceValue("47347437", Long .class); 169 170 assertEquals(new Long (47347437), result); 171 } 172 173 public void testInvalidStringToNumber() 174 { 175 ValueConverterImpl v = new ValueConverterImpl(); 176 177 try 178 { 179 v.coerceValue("fred12345", double.class); 180 unreachable(); 181 } 182 catch (ApplicationRuntimeException ex) 183 { 184 assertExceptionSubstring( 185 ex, 186 "Unable to convert 'fred12345' to an instance of java.lang.Double"); 187 } 188 189 } 190 191 public void testStringToNonNumericPrimitive() 192 { 193 MockControl tcc = newControl(TypeConverter.class); 194 TypeConverter tc = (TypeConverter) tcc.getMock(); 195 196 Object input = "false"; 197 Object output = Boolean.FALSE; 198 199 tc.convertValue(input); 200 tcc.setReturnValue(output); 201 202 replayControls(); 203 204 ValueConverterImpl v = new ValueConverterImpl(); 205 206 TypeConverterContribution contrib = new TypeConverterContribution(); 207 208 contrib.setSubjectClass(Boolean .class); 209 contrib.setConverter(tc); 210 211 v.setContributions(Collections.singletonList(contrib)); 212 213 v.initializeService(); 214 215 assertSame(output, v.coerceValue(input, Boolean .class)); 216 217 verifyControls(); 218 } 219 220 public void testNumberToNumber() 221 { 222 ValueConverterImpl v = new ValueConverterImpl(); 223 224 Object result = v.coerceValue(new Integer (123), long.class); 225 226 assertEquals(new Long (123), result); 227 } 228 } | Popular Tags |