KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > DataSetUtils


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 /**
33  * This class contains various methods for manipulating datasets.
34  *
35  * @author Manuel Laflamme
36  * @version $Revision: 1.19 $
37  * @since Feb 19, 2002
38  */

39 public class DataSetUtils
40 {
41     private DataSetUtils()
42     {
43     }
44
45     /**
46      * Asserts that the two specified dataset are equals. This method ignore
47      * the tables order.
48      *
49      * @deprecated Use Assertion.assertEquals
50      */

51     public static void assertEquals(IDataSet expectedDataSet,
52             IDataSet actualDataSet) throws Exception JavaDoc
53     {
54         Assertion.assertEquals(expectedDataSet, actualDataSet);
55     }
56
57
58     /**
59      * Asserts that the two specified tables are equals. This method ignore the
60      * table names, the columns order, the columns data type and the primary
61      * keys.
62      *
63      * @deprecated Use Assertion.assertEquals
64      */

65     public static void assertEquals(ITable expectedTable, ITable actualTable)
66             throws Exception JavaDoc
67     {
68         Assertion.assertEquals(expectedTable, actualTable);
69     }
70
71
72     /**
73      * Returns the specified name qualified with the specified prefix. The name
74      * is not modified if the prefix is <code>null</code> or if the name is
75      * already qualified.
76      * <p>
77      * Example: <br>
78      * <code>getQualifiedName(null, "NAME")</code> returns
79      * <code>"NAME"</code>. <code>getQualifiedName("PREFIX", "NAME")</code>
80      * returns <code>"PREFIX.NAME"</code> and
81      * <code>getQualifiedName("PREFIX2", "PREFIX1.NAME")</code>
82      * returns <code>"PREFIX1.NAME"</code>.
83      *
84      * @param prefix the prefix
85      * @param name the name
86      * @return the qualified name
87      */

88     public static String JavaDoc getQualifiedName(String JavaDoc prefix, String JavaDoc name)
89     {
90         return getQualifiedName(prefix, name, null);
91     }
92
93     public static String JavaDoc getQualifiedName(String JavaDoc prefix, String JavaDoc name,
94             String JavaDoc 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 JavaDoc getEscapedName(String JavaDoc name, String JavaDoc 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 JavaDoc prefix = escapePattern.substring(0, index);
121             String JavaDoc suffix = escapePattern.substring(index + 1);
122
123             return prefix + name + suffix;
124         }
125         return name;
126     }
127
128     /**
129      * Returns the specified value as a string to be use in an SQL Statement.
130      * For example the string <code>myValue</code> is returned as
131      * <code>'myValue'</code>.
132      *
133      * @param value the value
134      * @param dataType the value data type
135      * @return the SQL string value
136      */

137     public static String JavaDoc getSqlValueString(Object JavaDoc value, DataType dataType)
138             throws TypeCastException
139     {
140         if (value == null || value == ITable.NO_VALUE)
141         {
142             return "NULL";
143         }
144
145         String JavaDoc 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             // no single quotes
164
if (stringValue.indexOf("'") < 0)
165             {
166                 return stringValue = "'" + stringValue + "'";
167             }
168
169             // escaping single quotes
170
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(stringValue.length() * 2);
171             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(stringValue, "'", true);
172
173             buffer.append("'");
174             while (tokenizer.hasMoreTokens())
175             {
176                 String JavaDoc 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     /**
192      * Search and returns the specified column from the specified column array.
193      *
194      * @param columnName the name of the column to search.
195      * @param columns the array of columns from which the column must be searched.
196      * @return the column or <code>null</code> if the column is not found
197      */

198     public static Column getColumn(String JavaDoc 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     /**
213      * Search and returns the specified tables from the specified dataSet.
214      *
215      * @param names the names of the tables to search.
216      * @param dataSet the dataset from which the tables must be searched.
217      * @return the tables or an empty array if no tables are found.
218      */

219     public static ITable[] getTables(String JavaDoc[] 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 JavaDoc name = names[i];
226             tables[i] = dataSet.getTable(name);
227         }
228
229         return tables;
230     }
231
232     /**
233      * Returns the tables from the specified dataset.
234      */

235     public static ITable[] getTables(IDataSet dataSet) throws DataSetException
236     {
237         return getTables(dataSet.iterator());
238     }
239
240     /**
241      * Returns the tables from the specified iterator.
242      */

243     public static ITable[] getTables(ITableIterator iterator) throws DataSetException
244     {
245         List JavaDoc tableList = new ArrayList JavaDoc();
246         while(iterator.next())
247         {
248             tableList.add(iterator.getTable());
249         }
250         return (ITable[])tableList.toArray(new ITable[0]);
251     }
252
253     /**
254      * Returns the table names from the specified dataset in reverse order.
255      */

256     public static String JavaDoc[] getReverseTableNames(IDataSet dataSet)
257             throws DataSetException
258     {
259         return reverseStringArray(dataSet.getTableNames());
260     }
261
262     public static String JavaDoc[] reverseStringArray(String JavaDoc[] array)
263     {
264         String JavaDoc[] newArray = new String JavaDoc[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