Source Code Cross Referenced for DataSetUtils.java in  » Testing » DbUnit » org » dbunit » dataset » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » DbUnit » org.dbunit.dataset 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.