KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > test > AggregateTest


1 package jimm.datavision.test;
2 import jimm.datavision.*;
3 import jimm.datavision.field.AggregateField;
4 import jimm.datavision.layout.CharSepLE;
5 import jimm.datavision.test.mock.source.MockAggregateDataSource;
6 import java.io.*;
7 import java.util.*;
8 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10 import junit.framework.Test;
11
12 /**
13  * Tests the aggregate functions by reading a report from an XML file, running
14  * it, and verifying the output.
15  * <p>
16  * These tests are tightly coupled with the contents of the file
17  * <code>aggregate_test.xml</code> and the contents of the data generated by
18  * a {@link MockAggregateDataSource}.
19  *
20  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
21  */

22 public class AggregateTest extends TestCase {
23
24 protected static final File EXAMPLE_REPORT =
25     new File(AllTests.testDataFile("aggregate_test.xml"));
26 protected static final File OUT_FILE =
27     new File(System.getProperty("java.io.tmpdir"),
28          "datavision_aggregate_test_out.txt");
29
30 protected Report report;
31 protected Collection aggrFields;
32
33 public static Test suite() {
34     return new TestSuite(AggregateTest.class);
35 }
36
37 public AggregateTest(String JavaDoc name) {
38     super(name);
39 }
40
41 public void setUp() throws Exception JavaDoc {
42     report = new Report();
43     report.setDataSource(new MockAggregateDataSource(report));
44
45     OUT_FILE.deleteOnExit();
46     PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
47     report.setLayoutEngine(new CharSepLE(out, '\t'));
48
49     report.read(EXAMPLE_REPORT); // Must come after setting password
50
aggrFields = new ArrayList();
51     aggrFields.add(report.findField(new Long JavaDoc(23)));
52     aggrFields.add(report.findField(new Long JavaDoc(24)));
53     aggrFields.add(report.findField(new Long JavaDoc(25)));
54     aggrFields.add(report.findField(new Long JavaDoc(27)));
55 }
56
57 public void tearDown() {
58     if (OUT_FILE.exists())
59     OUT_FILE.delete();
60 }
61
62 public void testSum() throws IOException, FileNotFoundException {
63     String JavaDoc[] expected = {"A", "B", "D", // group headers
64
"2", "24", "3", // row values
65
"29", "29", // D, B group footers
66
"C", "D", // group headers
67
"12", "42", // row values
68
"54", "54", "83", // D, C, A group footers
69
"83"}; // report footer
70
runTest("sum", expected);
71 }
72
73 public void testMin() throws IOException, FileNotFoundException {
74     String JavaDoc[] expected = {"A", "B", "D",
75              "2", "24", "3",
76              "2", "2",
77              "C", "D",
78              "12", "42",
79              "12", "12", "2",
80              "2"};
81     runTest("min", expected);
82 }
83
84 public void testMax() throws IOException, FileNotFoundException {
85     String JavaDoc[] expected = {"A", "B", "D",
86              "2", "24", "3",
87              "24", "24",
88              "C", "D",
89              "12", "42",
90              "42", "42", "42",
91              "42"};
92
93     runTest("max", expected);
94 }
95
96 public void testCount() throws IOException, FileNotFoundException {
97     String JavaDoc[] expected = {"A", "B", "D",
98              "2", "24", "3",
99              "3", "3",
100              "C", "D",
101              "12", "42",
102              "2", "2", "5",
103              "5"};
104     runTest("count", expected);
105 }
106
107 public void testAverage() throws IOException, FileNotFoundException {
108     String JavaDoc[] expected = {"A", "B", "D",
109              "2", "24", "3",
110              "9.67", "9.67",
111              "C", "D",
112              "12", "42",
113              "27", "27", "16.6",
114              "16.6"};
115     runTest("average", expected);
116 }
117
118 public void testStddev() throws IOException, FileNotFoundException {
119     String JavaDoc[] expected = {"A", "B", "D",
120              "2", "24", "3",
121              "12.42", "12.42",
122              "C", "D",
123              "12", "42",
124              "21.21", "21.21", "16.73",
125              "16.73"};
126     runTest("stddev", expected);
127 }
128
129 public void runTest(String JavaDoc funcName, String JavaDoc[] expected)
130     throws IOException, FileNotFoundException
131 {
132     setAggregateFieldFunction(funcName);
133
134     // Running the report closes the output stream.
135
report.runReport();
136     BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
137
138     for (int i = 0; i < expected.length; ++i)
139     expect(i, expected[i], in);
140
141     String JavaDoc noMoreData = in.readLine();
142     assertNull("extra data in output, starting at line " + expected.length +
143            ": " + noMoreData, noMoreData);
144     in.close();
145 }
146
147 protected void setAggregateFieldFunction(String JavaDoc function) {
148     for (Iterator iter = aggrFields.iterator(); iter.hasNext(); ) {
149     AggregateField aggr = (AggregateField)iter.next();
150     aggr.setFunction(function);
151     }
152 }
153 protected void expect(int lineNum, String JavaDoc value, BufferedReader in)
154     throws IOException
155 {
156     String JavaDoc line = in.readLine();
157     assertNotNull("Output is too short; stops after line " + lineNum, line);
158     assertEquals("Line " + (lineNum + 1), value, line);
159 }
160
161 public void testHasParameterFields() {
162     assertEquals(false, report.hasParameterFields());
163 }
164
165 public static void main(String JavaDoc[] args) {
166     junit.textui.TestRunner.run(suite());
167     System.exit(0);
168 }
169
170 }
171
Popular Tags