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: }
|