1 21 22 package org.dbunit.dataset; 23 24 import org.dbunit.Assertion; 25 import org.dbunit.dataset.datatype.DataType; 26 import org.dbunit.dataset.datatype.TypeCastException; 27 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 32 39 public class DataSetUtils 40 { 41 private DataSetUtils() 42 { 43 } 44 45 51 public static void assertEquals(IDataSet expectedDataSet, 52 IDataSet actualDataSet) throws Exception 53 { 54 Assertion.assertEquals(expectedDataSet, actualDataSet); 55 } 56 57 58 65 public static void assertEquals(ITable expectedTable, ITable actualTable) 66 throws Exception 67 { 68 Assertion.assertEquals(expectedTable, actualTable); 69 } 70 71 72 88 public static String getQualifiedName(String prefix, String name) 89 { 90 return getQualifiedName(prefix, name, null); 91 } 92 93 public static String getQualifiedName(String prefix, String name, 94 String escapePattern) 95 { 96 if (escapePattern != null) 97 { 98 prefix = getEscapedName(prefix, escapePattern); 99 name = getEscapedName(name, escapePattern); 100 } 101 102 if (prefix == null || prefix.equals("") || name.indexOf(".") >= 0) 103 { 104 return name; 105 } 106 107 return prefix + "." + name; 108 } 109 110 public static String getEscapedName(String name, String escapePattern) 111 { 112 if (name == null || escapePattern == null) 113 { 114 return name; 115 } 116 117 int index = escapePattern.indexOf("?"); 118 if (index >=0 ) 119 { 120 String prefix = escapePattern.substring(0, index); 121 String suffix = escapePattern.substring(index + 1); 122 123 return prefix + name + suffix; 124 } 125 return name; 126 } 127 128 137 public static String getSqlValueString(Object value, DataType dataType) 138 throws TypeCastException 139 { 140 if (value == null || value == ITable.NO_VALUE) 141 { 142 return "NULL"; 143 } 144 145 String stringValue = DataType.asString(value); 146 if (dataType == DataType.DATE) 147 { 148 return "{d '" + stringValue + "'}"; 149 } 150 151 if (dataType == DataType.TIME) 152 { 153 return "{t '" + stringValue + "'}"; 154 } 155 156 if (dataType == DataType.TIMESTAMP) 157 { 158 return "{ts '" + stringValue + "'}"; 159 } 160 161 if (!dataType.isNumber()) 162 { 163 if (stringValue.indexOf("'") < 0) 165 { 166 return stringValue = "'" + stringValue + "'"; 167 } 168 169 StringBuffer buffer = new StringBuffer (stringValue.length() * 2); 171 StringTokenizer tokenizer = new StringTokenizer (stringValue, "'", true); 172 173 buffer.append("'"); 174 while (tokenizer.hasMoreTokens()) 175 { 176 String token = tokenizer.nextToken(); 177 buffer.append(token); 178 if (token.equals("'")) 179 { 180 buffer.append("'"); 181 } 182 } 183 buffer.append("'"); 184 return buffer.toString(); 185 186 } 187 188 return stringValue; 189 } 190 191 198 public static Column getColumn(String columnName, Column[] columns) 199 { 200 for (int i = 0; i < columns.length; i++) 201 { 202 Column column = columns[i]; 203 if (columnName.equalsIgnoreCase(columns[i].getColumnName())) 204 { 205 return column; 206 } 207 } 208 209 return null; 210 } 211 212 219 public static ITable[] getTables(String [] names, IDataSet dataSet) 220 throws DataSetException 221 { 222 ITable[] tables = new ITable[names.length]; 223 for (int i = 0; i < names.length; i++) 224 { 225 String name = names[i]; 226 tables[i] = dataSet.getTable(name); 227 } 228 229 return tables; 230 } 231 232 235 public static ITable[] getTables(IDataSet dataSet) throws DataSetException 236 { 237 return getTables(dataSet.iterator()); 238 } 239 240 243 public static ITable[] getTables(ITableIterator iterator) throws DataSetException 244 { 245 List tableList = new ArrayList (); 246 while(iterator.next()) 247 { 248 tableList.add(iterator.getTable()); 249 } 250 return (ITable[])tableList.toArray(new ITable[0]); 251 } 252 253 256 public static String [] getReverseTableNames(IDataSet dataSet) 257 throws DataSetException 258 { 259 return reverseStringArray(dataSet.getTableNames()); 260 } 261 262 public static String [] reverseStringArray(String [] array) 263 { 264 String [] newArray = new String [array.length]; 265 for (int i = 0; i < array.length; i++) 266 { 267 newArray[array.length - 1 - i] = array[i]; 268 } 269 return newArray; 270 } 271 272 } 273 274 275 276 277 278 279 280 281 | Popular Tags |