1 21 22 package org.dbunit.dataset.datatype; 23 24 import org.dbunit.database.ExtendedMockSingleRowResultSet; 25 import org.dbunit.dataset.ITable; 26 27 import java.math.BigDecimal ; 28 import java.sql.Types ; 29 30 34 35 public class FloatDataTypeTest extends AbstractDataTypeTest 36 { 37 private final static DataType THIS_TYPE = DataType.REAL; 38 39 public FloatDataTypeTest(String name) 40 { 41 super(name); 42 } 43 44 public void testToString() throws Exception 45 { 46 assertEquals("name", "REAL", THIS_TYPE.toString()); 47 } 48 49 public void testGetTypeClass() throws Exception 50 { 51 assertEquals("class", Float .class, THIS_TYPE.getTypeClass()); 52 } 53 54 public void testIsNumber() throws Exception 55 { 56 assertEquals("is number", true, THIS_TYPE.isNumber()); 57 } 58 59 public void testIsDateTime() throws Exception 60 { 61 assertEquals("is date/time", false, THIS_TYPE.isDateTime()); 62 } 63 64 public void testTypeCast() throws Exception 65 { 66 Object [] values = { 67 null, 68 "5.555", 69 new Double (Float.MAX_VALUE), 70 new Double (Float.MIN_VALUE), 71 "-7500", 72 "2.34E3", 73 new Double (0.666), 74 new Double (5.49879), 75 "-99.9", 76 new BigDecimal (1234), 77 }; 78 79 Float [] expected = { 80 null, 81 new Float (5.555), 82 new Float (Float.MAX_VALUE), 83 new Float (Float.MIN_VALUE), 84 new Float (-7500), 85 Float.valueOf("2.34E3"), 86 new Float (0.666), 87 new Float (5.49879), 88 new Float (-99.9), 89 new Float (1234), 90 }; 91 92 assertEquals("actual vs expected count", values.length, expected.length); 93 94 for (int i = 0; i < values.length; i++) 95 { 96 assertEquals("typecast " + i, expected[i], 97 THIS_TYPE.typeCast(values[i])); 98 } 99 } 100 101 public void testTypeCastNone() throws Exception 102 { 103 assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE)); 104 } 105 106 public void testTypeCastInvalid() throws Exception 107 { 108 Object [] values = {new Object (), "bla", new java.util.Date ()}; 109 110 for (int i = 0; i < values.length; i++) 111 { 112 try 113 { 114 THIS_TYPE.typeCast(values[i]); 115 fail("Should throw TypeCastException"); 116 } 117 catch (TypeCastException e) 118 { 119 } 120 } 121 } 122 123 public void testCompareEquals() throws Exception 124 { 125 Object [] values1 = { 126 null, 127 "5.555", 128 new Double (Float.MAX_VALUE), 129 new Double (Float.MIN_VALUE), 130 "-7500", 131 "2.34E3", 132 new Double (0.666), 133 new Double (5.49879), 134 "-99.9", 135 new BigDecimal (1234), 136 }; 137 138 Float [] values2 = { 139 null, 140 new Float (5.555), 141 new Float (Float.MAX_VALUE), 142 new Float (Float.MIN_VALUE), 143 new Float (-7500), 144 Float.valueOf("2.34E3"), 145 new Float (0.666), 146 new Float (5.49879), 147 new Float (-99.9), 148 new Float (1234), 149 }; 150 151 assertEquals("values count", values1.length, values2.length); 152 153 for (int i = 0; i < values1.length; i++) 154 { 155 assertEquals("compare1 " + i, 0, THIS_TYPE.compare(values1[i], values2[i])); 156 assertEquals("compare2 " + i, 0, THIS_TYPE.compare(values2[i], values1[i])); 157 } 158 } 159 160 public void testCompareInvalid() throws Exception 161 { 162 Object [] values1 = { 163 new Object (), 164 "bla", 165 new java.util.Date () 166 }; 167 Object [] values2 = { 168 null, 169 null, 170 null 171 }; 172 173 assertEquals("values count", values1.length, values2.length); 174 175 for (int i = 0; i < values1.length; i++) 176 { 177 try 178 { 179 THIS_TYPE.compare(values1[i], values2[i]); 180 fail("Should throw TypeCastException"); 181 } 182 catch (TypeCastException e) 183 { 184 } 185 186 try 187 { 188 THIS_TYPE.compare(values2[i], values1[i]); 189 fail("Should throw TypeCastException"); 190 } 191 catch (TypeCastException e) 192 { 193 } 194 } 195 } 196 197 public void testCompareDifferent() throws Exception 198 { 199 Object [] less = { 200 null, 201 "-7500", 202 new Double (Float.MIN_VALUE), 203 }; 204 205 Object [] greater = { 206 "0", 207 "5.555", 208 new Float (Float.MAX_VALUE), 209 }; 210 211 assertEquals("values count", less.length, greater.length); 212 213 for (int i = 0; i < less.length; i++) 214 { 215 assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0); 216 assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0); 217 } 218 } 219 220 public void testSqlType() throws Exception 221 { 222 assertEquals(THIS_TYPE, DataType.forSqlType(Types.REAL)); 223 assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString())); 224 assertEquals(Types.REAL, THIS_TYPE.getSqlType()); 225 } 226 227 public void testForObject() throws Exception 228 { 229 assertEquals(THIS_TYPE, DataType.forObject(new Float (1234))); 230 } 231 232 public void testAsString() throws Exception 233 { 234 Object [] values = { 235 new Float ("1234"), 236 new Float ("12.34"), 237 }; 238 239 String [] expected = { 240 "1234.0", 241 "12.34", 242 }; 243 244 assertEquals("actual vs expected count", values.length, expected.length); 245 246 for (int i = 0; i < values.length; i++) 247 { 248 assertEquals("asString " + i, expected[i], DataType.asString(values[i])); 249 } 250 } 251 252 public void testGetSqlValue() throws Exception 253 { 254 Float [] expected = { 255 null, 256 new Float (5.555), 257 new Float (Float.MAX_VALUE), 258 new Float (Float.MIN_VALUE), 259 new Float (-7500), 260 Float.valueOf("2.34E3"), 261 new Float (0.666), 262 new Float (5.49879), 263 new Float (-99.9), 264 new Float (1234), 265 }; 266 267 ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet(); 268 resultSet.addExpectedIndexedValues(expected); 269 270 for (int i = 0; i < expected.length; i++) 271 { 272 Object expectedValue = expected[i]; 273 Object actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet); 274 assertEquals("value", expectedValue, actualValue); 275 } 276 } 277 } 278 | Popular Tags |