1 21 22 package org.dbunit.dataset; 23 24 import junit.framework.TestCase; 25 import org.dbunit.dataset.datatype.DataType; 26 27 import java.sql.Time ; 28 import java.sql.Timestamp ; 29 30 35 public class DataSetUtilsTest extends TestCase 36 { 37 public DataSetUtilsTest(String s) 38 { 39 super(s); 40 } 41 42 43 public void testGetQualifiedName() throws Exception 44 { 45 assertEquals("prefix + name", "prefix.name", 46 DataSetUtils.getQualifiedName("prefix", "name")); 47 48 assertEquals("null prefix", "name", 49 DataSetUtils.getQualifiedName(null, "name")); 50 51 assertEquals("empty prefix", "name", 52 DataSetUtils.getQualifiedName("", "name")); 53 54 assertEquals("existing prefix", "prefix.name", 55 DataSetUtils.getQualifiedName("wrongPrefix", "prefix.name")); 56 57 assertEquals("escaped prefix + name", "prefix.name", 58 DataSetUtils.getQualifiedName("prefix", "name")); 59 60 assertEquals("escaped prefix + name", "[prefix].[name]", 61 DataSetUtils.getQualifiedName("prefix", "name", "[?]")); 62 63 } 64 65 public void testGetEscapedName() throws Exception 66 { 67 assertEquals("'name'", DataSetUtils.getEscapedName("name", "'?'")); 68 69 assertEquals("[name]", DataSetUtils.getEscapedName("name", "[?]")); 70 71 assertEquals(null, DataSetUtils.getEscapedName(null, "[?]")); 72 73 assertEquals("name", DataSetUtils.getEscapedName("name", null)); 74 75 assertEquals("name", DataSetUtils.getEscapedName("name", "invalid pattern!")); 76 } 77 78 public void testGetColumn() throws Exception 79 { 80 Column[] columns = new Column[]{ 81 new Column("c0", DataType.UNKNOWN), 82 new Column("c1", DataType.UNKNOWN), 83 new Column("c2", DataType.UNKNOWN), 84 new Column("c3", DataType.UNKNOWN), 85 new Column("c4", DataType.UNKNOWN), 86 }; 87 88 for (int i = 0; i < columns.length; i++) 89 { 90 assertEquals("find column same", columns[i], 91 DataSetUtils.getColumn("c" + i, columns)); 92 } 93 } 94 95 public void testGetColumnCaseInsensitive() throws Exception 96 { 97 Column[] columns = new Column[]{ 98 new Column("c0", DataType.UNKNOWN), 99 new Column("C1", DataType.UNKNOWN), 100 new Column("c2", DataType.UNKNOWN), 101 new Column("C3", DataType.UNKNOWN), 102 new Column("c4", DataType.UNKNOWN), 103 }; 104 105 for (int i = 0; i < columns.length; i++) 106 { 107 assertEquals("find column same", columns[i], 108 DataSetUtils.getColumn("c" + i, columns)); 109 } 110 } 111 112 public void testGetTables() throws Exception 113 { 114 String [] expected = {"t0", "t1", "t2", "t3"}; 115 ITable[] testTables = new ITable[]{ 116 new DefaultTable("t0"), 117 new DefaultTable("t1"), 118 new DefaultTable("t2"), 119 new DefaultTable("t3"), 120 }; 121 122 ITable[] tables = DataSetUtils.getTables(new DefaultDataSet(testTables)); 123 assertEquals("table count", expected.length, tables.length); 124 for (int i = 0; i < tables.length; i++) 125 { 126 String name = tables[i].getTableMetaData().getTableName(); 127 assertEquals("table name", expected[i], name); 128 } 129 } 130 131 public void testGetTablesByNames() throws Exception 132 { 133 String [] expected = {"t0", "t2"}; 134 ITable[] testTables = new ITable[]{ 135 new DefaultTable("t0"), 136 new DefaultTable("t1"), 137 new DefaultTable("t2"), 138 new DefaultTable("t3"), 139 }; 140 141 ITable[] tables = DataSetUtils.getTables(expected, 142 new DefaultDataSet(testTables)); 143 assertEquals("table count", expected.length, tables.length); 144 for (int i = 0; i < tables.length; i++) 145 { 146 String name = tables[i].getTableMetaData().getTableName(); 147 assertEquals("table name", expected[i], name); 148 } 149 } 150 151 public void testGetReserseNames() throws Exception 152 { 153 String [] expected = {"t3", "t2", "t1", "t0"}; 154 ITable[] testTables = new ITable[]{ 155 new DefaultTable("t0"), 156 new DefaultTable("t1"), 157 new DefaultTable("t2"), 158 new DefaultTable("t3"), 159 }; 160 161 String [] names = DataSetUtils.getReverseTableNames(new DefaultDataSet(testTables)); 162 assertEquals("table count", expected.length, names.length); 163 for (int i = 0; i < names.length; i++) 164 { 165 assertEquals("table name", expected[i], names[i]); 166 } 167 } 168 169 public void testGetSqlValueString() throws Exception 170 { 171 ValueStringData[] values = new ValueStringData[]{ 172 new ValueStringData(null, DataType.REAL, "NULL"), 173 new ValueStringData("1234", DataType.NUMERIC, "1234"), 174 new ValueStringData("1234", DataType.VARCHAR, "'1234'"), 175 new ValueStringData(new Float (1234.45), DataType.REAL, "1234.45"), 176 new ValueStringData(new java.sql.Date (0L), DataType.DATE, 177 "{d '" + new java.sql.Date (0L).toString() + "'}"), 178 new ValueStringData(new Time (0L), DataType.TIME, 179 "{t '" + new Time (0L).toString() + "'}"), 180 new ValueStringData(new Timestamp (0L), DataType.TIMESTAMP, 181 "{ts '" + new Timestamp (0L).toString() + "'}"), 182 new ValueStringData("12'34", DataType.VARCHAR, "'12''34'"), 183 new ValueStringData("'1234", DataType.VARCHAR, "'''1234'"), 184 new ValueStringData("1234'", DataType.VARCHAR, "'1234'''"), 185 new ValueStringData("'12'34'", DataType.VARCHAR, "'''12''34'''"), 186 }; 187 188 for (int i = 0; i < values.length; i++) 189 { 190 ValueStringData data = values[i]; 191 String valueString = DataSetUtils.getSqlValueString( 192 data.getValue(), data.getDataType()); 193 assertEquals("data " + i, data.getExpected(), valueString); 194 } 195 } 196 197 private class ValueStringData 198 { 199 private final Object _value; 200 private final DataType _dataType; 201 private final String _expected; 202 203 public ValueStringData(Object value, DataType dataType, String expected) 204 { 205 _value = value; 206 _dataType = dataType; 207 _expected = expected; 208 } 209 210 public Object getValue() 211 { 212 return _value; 213 } 214 215 public DataType getDataType() 216 { 217 return _dataType; 218 } 219 220 public String getExpected() 221 { 222 return _expected; 223 } 224 } 225 226 } 227 228 229 230 231 232 233 234 235 | Popular Tags |