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 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 name) { 38 super(name); 39 } 40 41 public void setUp() throws Exception { 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); aggrFields = new ArrayList(); 51 aggrFields.add(report.findField(new Long (23))); 52 aggrFields.add(report.findField(new Long (24))); 53 aggrFields.add(report.findField(new Long (25))); 54 aggrFields.add(report.findField(new Long (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 [] expected = {"A", "B", "D", "2", "24", "3", "29", "29", "C", "D", "12", "42", "54", "54", "83", "83"}; runTest("sum", expected); 71 } 72 73 public void testMin() throws IOException, FileNotFoundException { 74 String [] 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 [] 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 [] 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 [] 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 [] 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 funcName, String [] expected) 130 throws IOException, FileNotFoundException 131 { 132 setAggregateFieldFunction(funcName); 133 134 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 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 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 value, BufferedReader in) 154 throws IOException 155 { 156 String 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 [] args) { 166 junit.textui.TestRunner.run(suite()); 167 System.exit(0); 168 } 169 170 } 171 | Popular Tags |