-
Notifications
You must be signed in to change notification settings - Fork 2
FileProcessing
#Processing files
FitGoodies allows testers to analyze the content of flat file databases and XML files.
FitGoodies support blurry file selection. A file can be selected by a given path and a pattern which is provided as a regular expression. You can also specify the encoding, which is used to read a file.
To select a text file in /tmp, use:
fitgoodies.file.FileFixture | |
directory | /tmp |
pattern | .*\.txt |
encoding | utf-8 |
Every fixture that inherits AbstractFileFixture (CSVFileRecordFixture
, DelimiterFileRecordFixture
, FixedLengthFileRecordFixture
, XMLFileFixture
) now uses the first text file in /tmp. You can override this behavior using the TableParameters directory, pattern and encoding. Instead of directory and pattern, file can be used to specify an exact path name. See the next sections for examples.
Let's assume we have the following file.
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<shelf>
<book id="0">
<author>Vincent Massol, Ted Husted</author>
<title>JUnit in Action</title>
<isbn>1930110995</isbn>
<price>26.37</price>
</book>
<book id="1">
<author>Steve Loughran, Erik Hatcher</author>
<title>Ant in Action</title>
<isbn>193239480X</isbn>
<price>31.49</price>
</book>
</shelf>
FitGoodies can process them using XPath-Expressions. Using this way, you can inspect the leaves of the resulting DOM tree. Here is an example:
fitgoodies.file.XMLFileFixture | file=demo-output/bookshelf.xml |
/shelf/book[@id=1]/author | Steve Loughran, Erik Hatcher |
/shelf/book[1]/isbn | 1930110995 |
At the moment, only the second column supports CrossReferences.
CSV files are "comma separated values" or "character separated values". Read more about them in Wikpedia.
An example CSV file looks like this:
"Hello World","This is cell 2","This is row 1"
This is row 2,"This -> "", is crazy",42
This fixture checks the file:
fitgoodies.file.CSVFileFixture | pattern = \d+\.csv | |
Hello World | This is cell 2 | This is row 1 |
This is row 2 | This -> "", is crazy | 42 |
The CSVFileFixture supports two additional TableParameters: delimiter and mask. If not set, delimiter, which separates records, defaults to comma (,) and mask, which is used to mask delimiters, defaults to the double quote character (").
If the masking of delimiters is not needed, FitGoodies also provides the DelimiterFileRecordFixture. It takes one parameter: delimiter.
The above example, transformed:
Hello World&This is cell 2&This is row 1
This is row 2&This -> "", is crazy&42
fitgoodies.file.DelimiterFileRecordFixture | delimiter=& | |
Hello World | This is cell 2 | This is row 1 |
This is row 2 | This -> "", is crazy | 42 |
Lets take the book example again. This time, we don't store them in XML but in a text file with a fixed record length.
0 1930110995 JUnit in Action Vincent Massol, Ted Husted 26.37
1 193239480X Ant in Action Steve Loughran, Erik Hatcher 31.49
The record with is defined as:
- the id: 5 chars
- the ISBN: 15 chars (yeah, this is nonsense)
- the title: 22 chars (we only have books with small titles ;-) )
- the author(s): 35 chars
- the price: 5 chars
In addition, the records are separated by newlines, which have no meaning, so we want to skip newlines at the end of records:
fitgoodies.file.FixedLengthFileRecordFixture | file=bookshelf.txt | skipEOL=true | ||
5 | 15 | 22 | 35 | 5 |
0 | 1930110995 | JUnit in Action | Vincent Massol, Ted Husted | 26.37 |
1 | 193239480X | Ant in Action | Steve Loughran, Erik Hatcher | 31.49 |
skipEOL is the only parameter of this fixture, it defaults to false.