📄 testutils.java
字号:
package test.org.mandarax.testsupport;
/*
* Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.sql.ResultSetMetaData;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.VariableTerm;
import org.mandarax.util.logging.LogCategories;
import org.mandarax.util.regex.WildcardMatcher;
import java.io.*;
/**
* Class containing some useful test utilities.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.0
*/
public class TestUtils {
// folder test cases can use to store tmp files
public static String TMP_FOLDER = "tmptestdata";
private static boolean folderOK = false;
static {
try {
File folder = new File(TMP_FOLDER);
folderOK =folder.exists() || folder.mkdir();
if (!folderOK) LogCategories.LOG_TEST.error("Cannot create folder for test data!");
}
catch (Exception x) {
LogCategories.LOG_TEST.error("Cannot create folder for test data!",x);
}
}
/**
* Get a file for test data.
* @param fileName the file name (without folder)
* @return a file
*/
public static File getFile(String fileName) {
return new File(getFileName(fileName));
}
/**
* Get a folder for test data.
* @param fileName the file name (without folder)
* @return a folder
*/
public static File getFolder(String fileName) {
String name = folderOK?(TMP_FOLDER+"/"+fileName):fileName;
File f = new File(name);
if (!f.exists()) f.mkdir();
return f;
}
/**
* Get a file name for test data.
* @param fileName the file name (without folder)
*/
public static String getFileName(String fileName) {
return folderOK?(TMP_FOLDER+"/"+fileName):fileName;
}
/**
* Convert records to a string.
* This is useful when comparing arrays or collections of arrays in the debugger.
* @param record an array of objects
* @param df the data format used to turn dates into strings
* @return a string
*/
public static String record2string(Object[] record,DateFormat df) {
StringBuffer buf = new StringBuffer();
buf.append("{");
for (int i=0;i<record.length;i++) {
if (i>0) buf.append(';');
if (record[i] instanceof Date) buf.append(df.format(record[i]));
else buf.append(record[i]);
}
buf.append("}");
return buf.toString();
}
/**
* Convert the results (an entire result set) to a collection
* of strings.
* @param rs a result set
* @param container the collection used to store the strings
* @param return the container
*/
public static Collection asStrings(ResultSet rs,Collection container,DateFormat df) throws Exception {
List vars = rs.getQueryVariables();
while (rs.next()) {
Object[] values = new Object[vars.size()];
for (int j=0;j<vars.size();j++) values[j] = rs.getResult((VariableTerm)vars.get(j));
container.add(TestUtils.record2string(values,df));
}
return container;
}
/**
* Convert the results (an entire SQL result set) to a collection
* of strings. Exclude columns matching a certain pattern.
* @param rs a result set
* @param container the collection used to store the strings
* @param return the container
* @param excludePattern do not include values from columns matching this pattern
*/
public static Collection asStrings(java.sql.ResultSet rs,Collection container,DateFormat df,String excludePattern) throws Exception {
ResultSetMetaData metaData = rs.getMetaData();
// build column list
List cols = new ArrayList();
for (int j=0;j<metaData.getColumnCount();j++) {
String col = metaData.getColumnName(j+1);
if (excludePattern==null || !WildcardMatcher.FILE_INSTANCE.matches(excludePattern,col)) {
cols.add(col);
}
}
// build list of records
while (rs.next()) {
Object[] values = new Object[cols.size()];
for (int j=0;j<cols.size();j++) {
String col = cols.get(j).toString();
values[j] = rs.getObject(col);
}
container.add(TestUtils.record2string(values,df));
}
return container;
}
/**
* Convert the results (an entire SQL result set) to a collection
* of strings. Exclude columns matching a certain pattern.
* @param rs a result set
* @param container the collection used to store the strings
* @param return the container
*/
public static Collection asStrings(java.sql.ResultSet rs,Collection container,DateFormat df) throws Exception {
return asStrings(rs,container,df,null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -