001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.Assertion;
028: import org.dbunit.dataset.datatype.DataType;
029: import org.dbunit.dataset.datatype.TypeCastException;
030:
031: import java.util.ArrayList;
032: import java.util.List;
033: import java.util.StringTokenizer;
034:
035: /**
036: * This class contains various methods for manipulating datasets.
037: *
038: * @author Manuel Laflamme
039: * @version $Revision: 554 $
040: * @since Feb 19, 2002
041: */
042: public class DataSetUtils {
043:
044: /**
045: * Logger for this class
046: */
047: private static final Logger logger = LoggerFactory
048: .getLogger(DataSetUtils.class);
049:
050: private DataSetUtils() {
051: }
052:
053: /**
054: * Asserts that the two specified dataset are equals. This method ignore
055: * the tables order.
056: *
057: * @deprecated Use Assertion.assertEquals
058: */
059: public static void assertEquals(IDataSet expectedDataSet,
060: IDataSet actualDataSet) throws Exception {
061: logger.debug("assertEquals(expectedDataSet=" + expectedDataSet
062: + ", actualDataSet=" + actualDataSet + ") - start");
063:
064: Assertion.assertEquals(expectedDataSet, actualDataSet);
065: }
066:
067: /**
068: * Asserts that the two specified tables are equals. This method ignore the
069: * table names, the columns order, the columns data type and the primary
070: * keys.
071: *
072: * @deprecated Use Assertion.assertEquals
073: */
074: public static void assertEquals(ITable expectedTable,
075: ITable actualTable) throws Exception {
076: logger.debug("assertEquals(expectedTable=" + expectedTable
077: + ", actualTable=" + actualTable + ") - start");
078:
079: Assertion.assertEquals(expectedTable, actualTable);
080: }
081:
082: /**
083: * Returns the specified name qualified with the specified prefix. The name
084: * is not modified if the prefix is <code>null</code> or if the name is
085: * already qualified.
086: * <p>
087: * Example: <br>
088: * <code>getQualifiedName(null, "NAME")</code> returns
089: * <code>"NAME"</code>. <code>getQualifiedName("PREFIX", "NAME")</code>
090: * returns <code>"PREFIX.NAME"</code> and
091: * <code>getQualifiedName("PREFIX2", "PREFIX1.NAME")</code>
092: * returns <code>"PREFIX1.NAME"</code>.
093: *
094: * @param prefix the prefix
095: * @param name the name
096: * @return the qualified name
097: */
098: public static String getQualifiedName(String prefix, String name) {
099: logger.debug("getQualifiedName(prefix=" + prefix + ", name="
100: + name + ") - start");
101:
102: return getQualifiedName(prefix, name, null);
103: }
104:
105: public static String getQualifiedName(String prefix, String name,
106: String escapePattern) {
107: logger.debug("getQualifiedName(prefix=" + prefix + ", name="
108: + name + ", escapePattern=" + escapePattern
109: + ") - start");
110:
111: if (escapePattern != null) {
112: prefix = getEscapedName(prefix, escapePattern);
113: name = getEscapedName(name, escapePattern);
114: }
115:
116: if (prefix == null || prefix.equals("")
117: || name.indexOf(".") >= 0) {
118: return name;
119: }
120:
121: return prefix + "." + name;
122: }
123:
124: public static String getEscapedName(String name,
125: String escapePattern) {
126: logger.debug("getEscapedName(name=" + name + ", escapePattern="
127: + escapePattern + ") - start");
128:
129: if (name == null || escapePattern == null) {
130: return name;
131: }
132:
133: int split = name.indexOf(".");
134: if (split > 1) {
135: return getEscapedName(name.substring(0, split),
136: escapePattern)
137: + "."
138: + getEscapedName(name.substring(split + 1),
139: escapePattern);
140: }
141:
142: int index = escapePattern.indexOf("?");
143: if (index >= 0) {
144: String prefix = escapePattern.substring(0, index);
145: String suffix = escapePattern.substring(index + 1);
146:
147: return prefix + name + suffix;
148: }
149: return name;
150: }
151:
152: /**
153: * Returns the specified value as a string to be use in an SQL Statement.
154: * For example the string <code>myValue</code> is returned as
155: * <code>'myValue'</code>.
156: *
157: * @param value the value
158: * @param dataType the value data type
159: * @return the SQL string value
160: */
161: public static String getSqlValueString(Object value,
162: DataType dataType) throws TypeCastException {
163: logger.debug("getSqlValueString(value=" + value + ", dataType="
164: + dataType + ") - start");
165:
166: if (value == null || value == ITable.NO_VALUE) {
167: return "NULL";
168: }
169:
170: String stringValue = DataType.asString(value);
171: if (dataType == DataType.DATE) {
172: return "{d '" + stringValue + "'}";
173: }
174:
175: if (dataType == DataType.TIME) {
176: return "{t '" + stringValue + "'}";
177: }
178:
179: if (dataType == DataType.TIMESTAMP) {
180: return "{ts '" + stringValue + "'}";
181: }
182:
183: if (!dataType.isNumber()) {
184: // no single quotes
185: if (stringValue.indexOf("'") < 0) {
186: return stringValue = "'" + stringValue + "'";
187: }
188:
189: // escaping single quotes
190: StringBuffer buffer = new StringBuffer(
191: stringValue.length() * 2);
192: StringTokenizer tokenizer = new StringTokenizer(
193: stringValue, "'", true);
194:
195: buffer.append("'");
196: while (tokenizer.hasMoreTokens()) {
197: String token = tokenizer.nextToken();
198: buffer.append(token);
199: if (token.equals("'")) {
200: buffer.append("'");
201: }
202: }
203: buffer.append("'");
204: return buffer.toString();
205:
206: }
207:
208: return stringValue;
209: }
210:
211: /**
212: * Search and returns the specified column from the specified column array.
213: *
214: * @param columnName the name of the column to search.
215: * @param columns the array of columns from which the column must be searched.
216: * @return the column or <code>null</code> if the column is not found
217: */
218: public static Column getColumn(String columnName, Column[] columns) {
219: logger.debug("getColumn(columnName=" + columnName
220: + ", columns=" + columns + ") - start");
221:
222: for (int i = 0; i < columns.length; i++) {
223: Column column = columns[i];
224: if (columnName.equalsIgnoreCase(columns[i].getColumnName())) {
225: return column;
226: }
227: }
228:
229: return null;
230: }
231:
232: /**
233: * Search and returns the specified tables from the specified dataSet.
234: *
235: * @param names the names of the tables to search.
236: * @param dataSet the dataset from which the tables must be searched.
237: * @return the tables or an empty array if no tables are found.
238: */
239: public static ITable[] getTables(String[] names, IDataSet dataSet)
240: throws DataSetException {
241: logger.debug("getTables(names=" + names + ", dataSet="
242: + dataSet + ") - start");
243:
244: ITable[] tables = new ITable[names.length];
245: for (int i = 0; i < names.length; i++) {
246: String name = names[i];
247: tables[i] = dataSet.getTable(name);
248: }
249:
250: return tables;
251: }
252:
253: /**
254: * Returns the tables from the specified dataset.
255: */
256: public static ITable[] getTables(IDataSet dataSet)
257: throws DataSetException {
258: logger.debug("getTables(dataSet=" + dataSet + ") - start");
259:
260: return getTables(dataSet.iterator());
261: }
262:
263: /**
264: * Returns the tables from the specified iterator.
265: */
266: public static ITable[] getTables(ITableIterator iterator)
267: throws DataSetException {
268: logger.debug("getTables(iterator=" + iterator + ") - start");
269:
270: List tableList = new ArrayList();
271: while (iterator.next()) {
272: tableList.add(iterator.getTable());
273: }
274: return (ITable[]) tableList.toArray(new ITable[0]);
275: }
276:
277: /**
278: * Returns the table names from the specified dataset in reverse order.
279: */
280: public static String[] getReverseTableNames(IDataSet dataSet)
281: throws DataSetException {
282: logger.debug("getReverseTableNames(dataSet=" + dataSet
283: + ") - start");
284:
285: return reverseStringArray(dataSet.getTableNames());
286: }
287:
288: public static String[] reverseStringArray(String[] array) {
289: logger.debug("reverseStringArray(array=" + array + ") - start");
290:
291: String[] newArray = new String[array.length];
292: for (int i = 0; i < array.length; i++) {
293: newArray[array.length - 1 - i] = array[i];
294: }
295: return newArray;
296: }
297:
298: }
|