1 package jimm.datavision.test; 2 import jimm.datavision.*; 3 import jimm.datavision.field.*; 4 import jimm.datavision.layout.CharSepLE; 5 import jimm.datavision.test.mock.source.MockDataSource; 6 import java.io.*; 7 import java.util.Date ; 8 import java.text.DecimalFormat ; 9 import java.text.SimpleDateFormat ; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 import junit.framework.Test; 13 14 25 public class ReportRunTest extends TestCase { 26 27 protected static final File EXAMPLE_REPORT = 28 new File(AllTests.testDataFile("test.xml")); 29 protected static final File PARAM_INPUT_FILE = 30 new File(AllTests.testDataFile("test_parameters.xml")); 31 protected static final File OUT_FILE = 32 new File(System.getProperty("java.io.tmpdir"), 33 "datavision_report_run_test_out.txt"); 34 protected static final String REPORT_DATE_FORMAT = "yyyy-MM-dd"; 37 static final String OFFICES[] = { 40 "Chicago", "New Jersey", "New York" 41 }; 42 protected static final String STRING_PARAM_VALUE = "Chicago"; 45 protected static final String REPORT_TITLE = "Example Report"; 48 49 protected Report report; 50 protected DecimalFormat dollarFormatter; 51 protected DecimalFormat lastColFormatter; 52 protected SimpleDateFormat titleDateFormatter; 53 protected int reportRowNumber; 54 protected int officeRowNumber; 55 protected int postDateRowNumber; 56 57 public static Test suite() { 58 return new TestSuite(ReportRunTest.class); 59 } 60 61 public ReportRunTest(String name) { 62 super(name); 63 } 64 65 public void setUp() throws Exception { 66 dollarFormatter = new DecimalFormat ("$#,###.00"); 67 lastColFormatter = new DecimalFormat ("#,###.##"); 68 titleDateFormatter = new SimpleDateFormat (REPORT_DATE_FORMAT); 69 70 report = new Report(); 71 report.setDataSource(new MockDataSource(report)); 72 reportRowNumber = officeRowNumber = postDateRowNumber = 1; 73 74 report.read(EXAMPLE_REPORT); 75 76 if (report.hasParameterFields()) 77 report.setParameterXMLInput(PARAM_INPUT_FILE); 78 79 OUT_FILE.deleteOnExit(); 80 PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE)); 81 report.setLayoutEngine(new CharSepLE(out, '\t')); 82 } 83 84 public void tearDown() { 85 if (OUT_FILE.exists()) 86 OUT_FILE.delete(); 87 } 88 89 public void testReportRun() throws IOException, FileNotFoundException { 90 report.runReport(); 93 94 BufferedReader in = new BufferedReader(new FileReader(OUT_FILE)); 96 97 expectHeaders(in); 98 99 int total = 0; 103 for (int i = 0; i < OFFICES.length; ++i) 104 total += checkOfficeGroup(in, OFFICES[i]); 105 106 String line = in.readLine(); 108 assertNotNull(line); 109 if (line.startsWith("Page ")) 110 assertNotNull(line = in.readLine()); 111 112 assertEquals("Grand Total:\t" + dollarFormatter.format(total) 113 + "\t" + (reportRowNumber - 1) 114 + "\t" + (reportRowNumber - 1), 115 line); 116 117 assertNotNull(line = in.readLine()); 119 assertEquals("Page ", line.substring(0, 5)); 120 121 assertNull(in.readLine()); 123 124 in.close(); 125 } 126 127 protected void expectHeaders(BufferedReader in) throws IOException { 128 String line; 129 130 assertNotNull(line = in.readLine()); 133 assertEquals(0, line.indexOf(STRING_PARAM_VALUE + '\t' 134 + REPORT_TITLE + '\t' 135 + titleDateFormatter.format(new Date ()))); 136 137 assertNotNull(line = in.readLine()); 139 assertEquals(0, line.indexOf("Job #\tTitle\tHourly Rate")); 140 } 141 142 150 protected int checkOfficeGroup(BufferedReader in, String officeName) 151 throws IOException 152 { 153 String line; 154 int aggregate = 0; 155 156 officeRowNumber = 1; 157 158 assertNotNull(line = in.readLine()); 161 while (line.startsWith("Page ") || line.startsWith("Job #\t")) 162 assertNotNull(line = in.readLine()); 163 assertEquals(officeName, line); 164 165 Object postDateGroupEnd; 166 while ((postDateGroupEnd = checkPostDateGroup(in)) instanceof Integer ) 167 aggregate += ((Integer )postDateGroupEnd).intValue(); 168 line = (String )postDateGroupEnd; 169 170 assertEquals("Total:\t" + dollarFormatter.format(aggregate) 171 + "\t" + (reportRowNumber - 1) 172 + "\t" + (officeRowNumber - 1), 173 line); 174 175 return aggregate; 176 } 177 178 187 protected Object checkPostDateGroup(BufferedReader in) throws IOException { 188 int aggregate = 0; 189 String line; 190 191 postDateRowNumber = 1; 192 193 while (true) { 197 assertNotNull(line = in.readLine()); 198 if (line.startsWith("Page ") || line.startsWith("Job #\t")) 199 continue; 200 if (line.startsWith("Total:")) 201 return line; 202 203 assertTrue(Character.isDigit(line.charAt(0)) && line.length() == 10 205 && line.charAt(4) == '-' && line.charAt(7) == '-'); 206 break; 207 } 208 209 while (true) { 213 assertNotNull(line = in.readLine()); 214 if (line.startsWith("Post Date Total:")) 215 break; 216 if (line.startsWith("Page ") || line.startsWith("Job #\t")) 217 continue; 218 219 aggregate += checkDetailLine(line); 220 } 221 222 assertEquals("Post Date Total:\t" + dollarFormatter.format(aggregate) 223 + "\t" + (reportRowNumber - 1) 224 + "\t" + (postDateRowNumber - 1), 225 line); 226 227 return new Integer (aggregate); 228 } 229 230 237 protected int checkDetailLine(String line) { 238 int id = Integer.parseInt(line.substring(0, line.indexOf("\t"))); 240 241 String str = "" + id + "\tThis is the short description of job " + id; 244 assertTrue("line does not start with \"" + str + "\"", 245 line.startsWith(str)); 246 247 if (id == 0) { 250 assertEquals("0\tThis is the short description of job 0\t\t\t\t" 251 + reportRowNumber + "\t" + postDateRowNumber, 252 line); 253 } 254 else { 255 str = "\t" + (id * 100) 256 + "\t" + dollarFormatter.format(id) 257 + "\t" + lastColFormatter.format(id) 258 + "\t" + reportRowNumber 259 + "\t" + postDateRowNumber; 260 assertTrue("line \"" + line + "\" does not end with \"" + str, 261 line.endsWith(str)); 262 } 263 264 ++reportRowNumber; 265 ++officeRowNumber; 266 ++postDateRowNumber; 267 268 return id; 269 } 270 271 public void testParamInWhereClause() throws IOException, FileNotFoundException 273 { 274 report.getDataSource().getQuery() 276 .setEditableWhereClause("office.name = {?String Param}"); 277 report.setParameterXMLInput(PARAM_INPUT_FILE); 278 279 Date now = new Date (); report.runReport(); 283 284 BufferedReader in = new BufferedReader(new FileReader(OUT_FILE)); 286 String line; 287 288 assertNotNull(line = in.readLine()); 291 292 assertEquals(0, line.indexOf(STRING_PARAM_VALUE + '\t' 298 + REPORT_TITLE + '\t' 299 + titleDateFormatter.format(now))); 300 301 assertNotNull(line = in.readLine()); 303 assertEquals(0, line.indexOf("Job #\tTitle\tHourly Rate")); 304 305 int total = checkOfficeGroup(in, STRING_PARAM_VALUE); 309 310 assertNotNull(line = in.readLine()); 312 if (line.startsWith("Page ")) 313 assertNotNull(line = in.readLine()); 314 315 assertEquals("Grand Total:\t" + dollarFormatter.format(total) 316 + "\t" + (reportRowNumber - 1) 317 + "\t" + (reportRowNumber - 1), 318 line); 319 320 in.close(); 321 } 322 323 public void testNoRecords() { 327 try { 328 Section pageHeader = 330 report.getFirstSectionByArea(SectionArea.PAGE_HEADER); 331 assertNotNull(pageHeader); 332 ColumnField f = new ColumnField(null, report, pageHeader, 333 "office.name", true); 334 pageHeader.addField(f); 335 336 report.getDataSource().getQuery().setEditableWhereClause("1 = 2"); 338 339 report.runReport(); 341 } 342 catch (Exception e) { 343 fail("should not throw an exception just 'cause there are no records"); 344 } 345 } 346 347 public void testUseReportValue() throws Exception { 348 Formula f = report.findFormula("1"); 349 f.setEditableExpression("val = $report.value('jobs.hourly rate'); val.nil? ? nil : val / 100.0"); 350 351 report.runReport(); 354 355 BufferedReader in = new BufferedReader(new FileReader(OUT_FILE)); 357 358 expectHeaders(in); 359 360 int total = 0; 364 for (int i = 0; i < OFFICES.length; ++i) 365 total += checkOfficeGroup(in, OFFICES[i]); 366 367 String line = in.readLine(); 369 assertNotNull(line); 370 if (line.startsWith("Page ")) 371 assertNotNull(line = in.readLine()); 372 373 assertEquals("Grand Total:\t" + dollarFormatter.format(total) 374 + "\t" + (reportRowNumber - 1) 375 + "\t" + (reportRowNumber - 1), 376 line); 377 378 assertNotNull(line = in.readLine()); 380 assertEquals("Page ", line.substring(0, 5)); 381 382 assertNull(in.readLine()); 384 385 in.close(); 386 } 387 388 public static void main(String [] args) { 389 junit.textui.TestRunner.run(suite()); 390 System.exit(0); 391 } 392 393 } 394 | Popular Tags |