com.mockrunner.jdbc.FileResultSetFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Mockrunner is a lightweight framework for unit testing applications
in the J2EE environment. It supports servlets, filters, tag classes
and Struts actions. It includes a JDBC a JMS and a JCA test
framework and can be used to test EJB based applications.
The newest version!
package com.mockrunner.jdbc;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mockrunner.mock.jdbc.MockResultSet;
import com.mockrunner.util.common.FileUtil;
import com.mockrunner.util.common.StringUtil;
/**
* Can be used to create a ResultSet
based on
* a table specified in a CSV file. You can specify the delimiter
* of the columns (default is ;
). Furthermore you can specify if the first line
* contains the column names (default is false
) and if
* the column entries should be trimmed (default is true
).
* With {@link #setUseTemplates} you can enable template replacement in the
* files (default is false
, i.e. templates are disabled).
* The file can be specified directly or by its name. The class
* tries to find the file in the absolut or relative path and
* (if not found) by calling getResource
. Note that the
* file must exist in the local file system and cannot be loaded from
* inside a jar archive.
*/
public class FileResultSetFactory implements ResultSetFactory
{
private File file = null;
private String delimiter = ";";
private boolean firstLineContainsColumnNames = false;
private boolean trim = true;
private boolean useTemplates = false;
private String templateMarker = null;
private Map templates = null;
public FileResultSetFactory(String fileName)
{
this(new File(fileName));
}
public FileResultSetFactory(File file)
{
this.file = file;
setDefaultTemplateConfiguration();
}
/**
* Get the File
being used to read in the
* ResultSet
. Throws a RuntimeException
* if the file does not exist.
* @return the file
*/
public File getFile()
{
if (file.exists() && file.isFile())
{
return file;
}
else
{
try
{
file = FileUtil.findFile(file.getPath());
return file;
}
catch (FileNotFoundException exc)
{
throw new RuntimeException("Could not find: " + file.getPath());
}
}
}
/**
* Set the delimiter. Default is ";".
* @param delimiter the delimiter
*/
public void setDelimiter(String delimiter)
{
this.delimiter = delimiter;
}
/**
* Set if the first line contains the column names.
* Default is false
.
*/
public void setFirstLineContainsColumnNames(boolean firstLineContainsColumnNames)
{
this.firstLineContainsColumnNames = firstLineContainsColumnNames;
}
/**
* Set if the column entries should be trimmed.
* Default is true
.
*/
public void setTrim(boolean trim)
{
this.trim = trim;
}
/**
* Set this to true
to allow the use of templates
* in data files. A template is identified by a marker followed
* by a label. The template is replaced by a predefined string in
* the corresponding data file. E.g. with the default configuration,
* $defaultString
is replaced by an empty string
* in the file.
* The default configuration which is automatically set uses
* $
as a marker. See {@link #setDefaultTemplateConfiguration}
* for details. You can also set a custom template configuration using
* {@link #setTemplateConfiguration(String, Map)}.
* Default is false
, i.e. templates are disabled.
* @param useTemplates set true
to enable templates.
*/
public void setUseTemplates(boolean useTemplates)
{
this.useTemplates = useTemplates;
}
/**
* This method sets a custom template configuration. See
* {@link #setUseTemplates} for an explanation how templates work.
* marker + map key
is replaced by the corresponding map
* value
in the data files.
* Please use {@link #setDefaultTemplateConfiguration} to set a
* default configuration.
* @param marker the custom marker replacing the default $
* @param templates the custom template map
*/
public void setTemplateConfiguration(String marker, Map templates)
{
this.templates = templates;
this.templateMarker = marker;
}
/**
* This method sets the default template configuration. See
* {@link #setUseTemplates} for an explanation how templates work.
* The default marker is $
and the default templates are:
* $defaultString
is replaced by an empty string
* $defaultDate
is replaced by 1970-01-01
* $defaultInteger
is replaced by 0
* Please use {@link #setTemplateConfiguration(String, Map)} to set a
* custom marker and custom templates.
*/
public void setDefaultTemplateConfiguration()
{
Map templates = new HashMap();
templates.put("defaultString", "");
templates.put("defaultDate", "1970-01-01");
templates.put("defaultInteger", "0");
setTemplateConfiguration("$", templates);
}
public MockResultSet create(String id)
{
MockResultSet resultSet = new MockResultSet(id);
File fileToRead = getFile();
List lines = FileUtil.getLinesFromFile(fileToRead);
int firstLineNumber = 0;
if(firstLineContainsColumnNames)
{
String firstLine = (String)lines.get(firstLineNumber);
firstLineNumber++;
String[] names = StringUtil.split(firstLine, delimiter, trim);
for(int ii = 0; ii < names.length; ii++)
{
resultSet.addColumn(names[ii]);
}
}
for(int ii = firstLineNumber; ii < lines.size(); ii++)
{
String line = (String)lines.get(ii);
String[] values = StringUtil.split(line, delimiter, trim);
if(useTemplates)
{
for(int yy = 0; yy < values.length; yy++)
{
if(null != values[yy])
{
if(values[yy].startsWith(templateMarker) && templates.containsKey(values[yy].substring(1)))
{
values[yy] = (String)templates.get(values[yy].substring(1));
}
}
}
}
resultSet.addRow(values);
}
return resultSet;
}
}