Source Code Cross Referenced for TestUtils.java in  » Rule-Engine » Mandarax » test » org » mandarax » testsupport » 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 » Rule Engine » Mandarax » test.org.mandarax.testsupport 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package test.org.mandarax.testsupport;
002:
003:        /*
004:         * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
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 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:        import java.sql.ResultSetMetaData;
022:        import java.text.DateFormat;
023:        import java.util.ArrayList;
024:        import java.util.Collection;
025:        import java.util.Date;
026:        import java.util.List;
027:        import org.mandarax.kernel.ResultSet;
028:        import org.mandarax.kernel.VariableTerm;
029:        import org.mandarax.util.logging.LogCategories;
030:        import org.mandarax.util.regex.WildcardMatcher;
031:
032:        import java.io.*;
033:
034:        /**
035:         * Class containing some useful test utilities.
036:         * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037:         * @version 3.4 <7 March 05>
038:         * @since 3.0
039:         */
040:        public class TestUtils {
041:            // folder test cases can  use to store tmp files
042:            public static String TMP_FOLDER = "tmptestdata";
043:            private static boolean folderOK = false;
044:
045:            static {
046:                try {
047:                    File folder = new File(TMP_FOLDER);
048:                    folderOK = folder.exists() || folder.mkdir();
049:                    if (!folderOK)
050:                        LogCategories.LOG_TEST
051:                                .error("Cannot create folder for test data!");
052:                } catch (Exception x) {
053:                    LogCategories.LOG_TEST.error(
054:                            "Cannot create folder for test data!", x);
055:                }
056:            }
057:
058:            /**
059:             * Get a file for test data.
060:             * @param fileName the file name (without folder)
061:             * @return a file
062:             */
063:            public static File getFile(String fileName) {
064:                return new File(getFileName(fileName));
065:            }
066:
067:            /**
068:             * Get a folder for test data.
069:             * @param fileName the file name (without folder)
070:             * @return a folder
071:             */
072:            public static File getFolder(String fileName) {
073:                String name = folderOK ? (TMP_FOLDER + "/" + fileName)
074:                        : fileName;
075:                File f = new File(name);
076:                if (!f.exists())
077:                    f.mkdir();
078:                return f;
079:            }
080:
081:            /**
082:             * Get a file name for test data.
083:             * @param fileName the file name (without folder)
084:             */
085:            public static String getFileName(String fileName) {
086:                return folderOK ? (TMP_FOLDER + "/" + fileName) : fileName;
087:            }
088:
089:            /**
090:             * Convert records to a string.
091:             * This is useful when comparing arrays or collections of arrays in the debugger.
092:             * @param record an array of objects
093:             * @param df the data format used to turn dates into strings
094:             * @return a string
095:             */
096:            public static String record2string(Object[] record, DateFormat df) {
097:                StringBuffer buf = new StringBuffer();
098:                buf.append("{");
099:                for (int i = 0; i < record.length; i++) {
100:                    if (i > 0)
101:                        buf.append(';');
102:                    if (record[i] instanceof  Date)
103:                        buf.append(df.format(record[i]));
104:                    else
105:                        buf.append(record[i]);
106:                }
107:                buf.append("}");
108:                return buf.toString();
109:            }
110:
111:            /**
112:             * Convert the results (an entire result set) to a collection
113:             * of strings.
114:             * @param rs a result set
115:             * @param container the collection used to store the strings
116:             * @param return the container
117:             */
118:            public static Collection asStrings(ResultSet rs,
119:                    Collection container, DateFormat df) throws Exception {
120:                List vars = rs.getQueryVariables();
121:                while (rs.next()) {
122:                    Object[] values = new Object[vars.size()];
123:                    for (int j = 0; j < vars.size(); j++)
124:                        values[j] = rs.getResult((VariableTerm) vars.get(j));
125:                    container.add(TestUtils.record2string(values, df));
126:                }
127:                return container;
128:            }
129:
130:            /**
131:             * Convert the results (an entire SQL result set) to a collection
132:             * of strings. Exclude columns matching a certain pattern.
133:             * @param rs a result set
134:             * @param container the collection used to store the strings
135:             * @param return the container
136:             * @param excludePattern do not include values from columns matching this pattern
137:             */
138:            public static Collection asStrings(java.sql.ResultSet rs,
139:                    Collection container, DateFormat df, String excludePattern)
140:                    throws Exception {
141:                ResultSetMetaData metaData = rs.getMetaData();
142:                // build column list
143:                List cols = new ArrayList();
144:                for (int j = 0; j < metaData.getColumnCount(); j++) {
145:                    String col = metaData.getColumnName(j + 1);
146:                    if (excludePattern == null
147:                            || !WildcardMatcher.FILE_INSTANCE.matches(
148:                                    excludePattern, col)) {
149:                        cols.add(col);
150:                    }
151:                }
152:                // build list of records
153:                while (rs.next()) {
154:                    Object[] values = new Object[cols.size()];
155:                    for (int j = 0; j < cols.size(); j++) {
156:                        String col = cols.get(j).toString();
157:                        values[j] = rs.getObject(col);
158:                    }
159:                    container.add(TestUtils.record2string(values, df));
160:                }
161:                return container;
162:            }
163:
164:            /**
165:             * Convert the results (an entire SQL result set) to a collection
166:             * of strings. Exclude columns matching a certain pattern.
167:             * @param rs a result set
168:             * @param container the collection used to store the strings
169:             * @param return the container
170:             */
171:            public static Collection asStrings(java.sql.ResultSet rs,
172:                    Collection container, DateFormat df) throws Exception {
173:                return asStrings(rs, container, df, null);
174:            }
175:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.