001: package jimm.datavision.test;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.field.AggregateField;
005: import jimm.datavision.layout.CharSepLE;
006: import jimm.datavision.test.mock.source.MockAggregateDataSource;
007: import java.io.*;
008: import java.util.*;
009: import junit.framework.TestCase;
010: import junit.framework.TestSuite;
011: import junit.framework.Test;
012:
013: /**
014: * Tests the aggregate functions by reading a report from an XML file, running
015: * it, and verifying the output.
016: * <p>
017: * These tests are tightly coupled with the contents of the file
018: * <code>aggregate_test.xml</code> and the contents of the data generated by
019: * a {@link MockAggregateDataSource}.
020: *
021: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
022: */
023: public class AggregateTest extends TestCase {
024:
025: protected static final File EXAMPLE_REPORT = new File(AllTests
026: .testDataFile("aggregate_test.xml"));
027: protected static final File OUT_FILE = new File(System
028: .getProperty("java.io.tmpdir"),
029: "datavision_aggregate_test_out.txt");
030:
031: protected Report report;
032: protected Collection aggrFields;
033:
034: public static Test suite() {
035: return new TestSuite(AggregateTest.class);
036: }
037:
038: public AggregateTest(String name) {
039: super (name);
040: }
041:
042: public void setUp() throws Exception {
043: report = new Report();
044: report.setDataSource(new MockAggregateDataSource(report));
045:
046: OUT_FILE.deleteOnExit();
047: PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
048: report.setLayoutEngine(new CharSepLE(out, '\t'));
049:
050: report.read(EXAMPLE_REPORT); // Must come after setting password
051: aggrFields = new ArrayList();
052: aggrFields.add(report.findField(new Long(23)));
053: aggrFields.add(report.findField(new Long(24)));
054: aggrFields.add(report.findField(new Long(25)));
055: aggrFields.add(report.findField(new Long(27)));
056: }
057:
058: public void tearDown() {
059: if (OUT_FILE.exists())
060: OUT_FILE.delete();
061: }
062:
063: public void testSum() throws IOException, FileNotFoundException {
064: String[] expected = { "A", "B", "D", // group headers
065: "2", "24", "3", // row values
066: "29", "29", // D, B group footers
067: "C", "D", // group headers
068: "12", "42", // row values
069: "54", "54", "83", // D, C, A group footers
070: "83" }; // report footer
071: runTest("sum", expected);
072: }
073:
074: public void testMin() throws IOException, FileNotFoundException {
075: String[] expected = { "A", "B", "D", "2", "24", "3", "2", "2",
076: "C", "D", "12", "42", "12", "12", "2", "2" };
077: runTest("min", expected);
078: }
079:
080: public void testMax() throws IOException, FileNotFoundException {
081: String[] expected = { "A", "B", "D", "2", "24", "3", "24",
082: "24", "C", "D", "12", "42", "42", "42", "42", "42" };
083:
084: runTest("max", expected);
085: }
086:
087: public void testCount() throws IOException, FileNotFoundException {
088: String[] expected = { "A", "B", "D", "2", "24", "3", "3", "3",
089: "C", "D", "12", "42", "2", "2", "5", "5" };
090: runTest("count", expected);
091: }
092:
093: public void testAverage() throws IOException, FileNotFoundException {
094: String[] expected = { "A", "B", "D", "2", "24", "3", "9.67",
095: "9.67", "C", "D", "12", "42", "27", "27", "16.6",
096: "16.6" };
097: runTest("average", expected);
098: }
099:
100: public void testStddev() throws IOException, FileNotFoundException {
101: String[] expected = { "A", "B", "D", "2", "24", "3", "12.42",
102: "12.42", "C", "D", "12", "42", "21.21", "21.21",
103: "16.73", "16.73" };
104: runTest("stddev", expected);
105: }
106:
107: public void runTest(String funcName, String[] expected)
108: throws IOException, FileNotFoundException {
109: setAggregateFieldFunction(funcName);
110:
111: // Running the report closes the output stream.
112: report.runReport();
113: BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
114:
115: for (int i = 0; i < expected.length; ++i)
116: expect(i, expected[i], in);
117:
118: String noMoreData = in.readLine();
119: assertNull("extra data in output, starting at line "
120: + expected.length + ": " + noMoreData, noMoreData);
121: in.close();
122: }
123:
124: protected void setAggregateFieldFunction(String function) {
125: for (Iterator iter = aggrFields.iterator(); iter.hasNext();) {
126: AggregateField aggr = (AggregateField) iter.next();
127: aggr.setFunction(function);
128: }
129: }
130:
131: protected void expect(int lineNum, String value, BufferedReader in)
132: throws IOException {
133: String line = in.readLine();
134: assertNotNull("Output is too short; stops after line "
135: + lineNum, line);
136: assertEquals("Line " + (lineNum + 1), value, line);
137: }
138:
139: public void testHasParameterFields() {
140: assertEquals(false, report.hasParameterFields());
141: }
142:
143: public static void main(String[] args) {
144: junit.textui.TestRunner.run(suite());
145: System.exit(0);
146: }
147:
148: }
|