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