package jimm.datavision.test;
import jimm.datavision.*;
import jimm.datavision.field.AggregateField;
import jimm.datavision.layout.CharSepLE;
import jimm.datavision.test.mock.source.MockAggregateDataSource;
import java.io.*;
import java.util.*;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
/**
* Tests the aggregate functions by reading a report from an XML file, running
* it, and verifying the output.
* <p>
* These tests are tightly coupled with the contents of the file
* <code>aggregate_test.xml</code> and the contents of the data generated by
* a {@link MockAggregateDataSource}.
*
* @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
*/
public class AggregateTest extends TestCase {
protected static final File EXAMPLE_REPORT =
new File(AllTests.testDataFile("aggregate_test.xml"));
protected static final File OUT_FILE =
new File(System.getProperty("java.io.tmpdir"),
"datavision_aggregate_test_out.txt");
protected Report report;
protected Collection aggrFields;
public static Test suite() {
return new TestSuite(AggregateTest.class);
}
public AggregateTest(String name) {
super(name);
}
public void setUp() throws Exception {
report = new Report();
report.setDataSource(new MockAggregateDataSource(report));
OUT_FILE.deleteOnExit();
PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
report.setLayoutEngine(new CharSepLE(out, '\t'));
report.read(EXAMPLE_REPORT); // Must come after setting password
aggrFields = new ArrayList();
aggrFields.add(report.findField(new Long(23)));
aggrFields.add(report.findField(new Long(24)));
aggrFields.add(report.findField(new Long(25)));
aggrFields.add(report.findField(new Long(27)));
}
public void tearDown() {
if (OUT_FILE.exists())
OUT_FILE.delete();
}
public void testSum() throws IOException, FileNotFoundException {
String[] expected = {"A", "B", "D", // group headers
"2", "24", "3", // row values
"29", "29", // D, B group footers
"C", "D", // group headers
"12", "42", // row values
"54", "54", "83", // D, C, A group footers
"83"}; // report footer
runTest("sum", expected);
}
public void testMin() throws IOException, FileNotFoundException {
String[] expected = {"A", "B", "D",
"2", "24", "3",
"2", "2",
"C", "D",
"12", "42",
"12", "12", "2",
"2"};
runTest("min", expected);
}
public void testMax() throws IOException, FileNotFoundException {
String[] expected = {"A", "B", "D",
"2", "24", "3",
"24", "24",
"C", "D",
"12", "42",
"42", "42", "42",
"42"};
runTest("max", expected);
}
public void testCount() throws IOException, FileNotFoundException {
String[] expected = {"A", "B", "D",
"2", "24", "3",
"3", "3",
"C", "D",
"12", "42",
"2", "2", "5",
"5"};
runTest("count", expected);
}
public void testAverage() throws IOException, FileNotFoundException {
String[] expected = {"A", "B", "D",
"2", "24", "3",
"9.67", "9.67",
"C", "D",
"12", "42",
"27", "27", "16.6",
"16.6"};
runTest("average", expected);
}
public void testStddev() throws IOException, FileNotFoundException {
String[] expected = {"A", "B", "D",
"2", "24", "3",
"12.42", "12.42",
"C", "D",
"12", "42",
"21.21", "21.21", "16.73",
"16.73"};
runTest("stddev", expected);
}
public void runTest(String funcName, String[] expected)
throws IOException, FileNotFoundException
{
setAggregateFieldFunction(funcName);
// Running the report closes the output stream.
report.runReport();
BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
for (int i = 0; i < expected.length; ++i)
expect(i, expected[i], in);
String noMoreData = in.readLine();
assertNull("extra data in output, starting at line " + expected.length +
": " + noMoreData, noMoreData);
in.close();
}
protected void setAggregateFieldFunction(String function) {
for (Iterator iter = aggrFields.iterator(); iter.hasNext(); ) {
AggregateField aggr = (AggregateField)iter.next();
aggr.setFunction(function);
}
}
protected void expect(int lineNum, String value, BufferedReader in)
throws IOException
{
String line = in.readLine();
assertNotNull("Output is too short; stops after line " + lineNum, line);
assertEquals("Line " + (lineNum + 1), value, line);
}
public void testHasParameterFields() {
assertEquals(false, report.hasParameterFields());
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
System.exit(0);
}
}