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.Types ; 28 29 33 34 public class BooleanDataTypeTest extends AbstractDataTypeTest 35 { 36 private final static DataType THIS_TYPE = DataType.BOOLEAN; 37 38 public BooleanDataTypeTest(String name) 39 { 40 super(name); 41 } 42 43 46 public void testToString() throws Exception 47 { 48 assertEquals("name", "BIT", THIS_TYPE.toString()); 49 } 50 51 54 public void testGetTypeClass() throws Exception 55 { 56 assertEquals("class", Boolean .class, THIS_TYPE.getTypeClass()); 57 } 58 59 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", false, THIS_TYPE.isDateTime()); 70 } 71 72 public void testTypeCast() throws Exception 73 { 74 Object [] values = { 75 null, 76 "1", 77 "0", 78 "true", 79 "false", 80 Boolean.TRUE, 81 Boolean.FALSE, 82 new Integer (1), 83 new Integer (0), 84 new Integer (123), 85 }; 86 Boolean [] expected = { 87 null, 88 Boolean.TRUE, 89 Boolean.FALSE, 90 Boolean.TRUE, 91 Boolean.FALSE, 92 Boolean.TRUE, 93 Boolean.FALSE, 94 Boolean.TRUE, 95 Boolean.FALSE, 96 Boolean.TRUE, 97 }; 98 99 assertEquals("actual vs expected count", values.length, expected.length); 100 101 for (int i = 0; i < values.length; i++) 102 { 103 assertEquals("typecast " + i, expected[i], 104 THIS_TYPE.typeCast(values[i])); 105 } 106 } 107 108 public void testTypeCastNone() throws Exception 109 { 110 assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE)); 111 } 112 113 public void testTypeCastInvalid() throws Exception 114 { 115 Object [] values = {"bla"}; 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"); 123 } 124 catch (TypeCastException e) 125 { 126 } 127 } 128 } 129 130 public void testCompareEquals() throws Exception 131 { 132 Object [] values1 = { 133 null, 134 "1", 135 "0", 136 Boolean.TRUE, 137 Boolean.FALSE, 138 }; 139 Object [] values2 = { 140 null, 141 Boolean.TRUE, 142 Boolean.FALSE, 143 "true", 144 "false", 145 }; 146 147 assertEquals("values count", values1.length, values2.length); 148 149 for (int i = 0; i < values1.length; i++) 150 { 151 assertEquals("compare1 " + i, 152 0, THIS_TYPE.compare(values1[i], values2[i])); 153 assertEquals("compare2 " + i, 154 0, THIS_TYPE.compare(values2[i], values1[i])); 155 } 156 } 157 158 public void testCompareInvalid() throws Exception 159 { 160 Object [] values1 = { 161 "bla", 162 Boolean.FALSE, 163 }; 164 Object [] values2 = { 165 Boolean.TRUE, 166 "bla", 167 }; 168 169 assertEquals("values count", values1.length, values2.length); 170 171 for (int i = 0; i < values1.length; i++) 172 { 173 try 174 { 175 THIS_TYPE.compare(values1[i], values2[i]); 176 fail("Should have throw TypeCastException"); 177 } 178 catch (TypeCastException e) 179 { 180 181 } 182 183 try 184 { 185 THIS_TYPE.compare(values2[i], values1[i]); 186 fail("Should have throw TypeCastException"); 187 } 188 catch (TypeCastException e) 189 { 190 191 } 192 } 193 } 194 195 public void testCompareDifferent() throws Exception 196 { 197 Object [] less = { 198 null, 199 null, 200 Boolean.FALSE, 201 }; 202 Object [] greater = { 203 Boolean.TRUE, 204 Boolean.FALSE, 205 Boolean.TRUE, 206 }; 207 208 assertEquals("values count", less.length, greater.length); 209 210 for (int i = 0; i < less.length; i++) 211 { 212 assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0); 213 assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0); 214 } 215 } 216 217 public void testSqlType() throws Exception 218 { 219 assertEquals("forSqlType", THIS_TYPE, DataType.forSqlType(Types.BIT)); 220 assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString())); 221 assertEquals("getSqlType", Types.BIT, THIS_TYPE.getSqlType()); 222 } 223 224 public void testForObject() throws Exception 225 { 226 assertEquals(THIS_TYPE, DataType.forObject(Boolean.TRUE)); 227 } 228 229 232 public void testAsString() throws Exception 233 { 234 Boolean [] values = { 235 Boolean.TRUE, 236 Boolean.FALSE, 237 }; 238 239 String [] expected = { 240 "true", 241 "false", 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 Object [] expected = new Object [] { 255 null, 256 Boolean.TRUE, 257 Boolean.FALSE, 258 }; 259 260 ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet(); 261 resultSet.addExpectedIndexedValues(expected); 262 263 for (int i = 0; i < expected.length; i++) 264 { 265 Object expectedValue = expected[i]; 266 Object actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet); 267 assertEquals("value", expectedValue, actualValue); 268 } 269 } 270 } 271 | Popular Tags |