1 package jimm.datavision.test; 2 import jimm.datavision.*; 3 import jimm.datavision.field.*; 4 import jimm.datavision.test.mock.source.MockDataSource; 5 import java.util.List ; 6 import java.io.File ; 7 import java.text.DecimalFormat ; 8 import junit.framework.TestCase; 9 import junit.framework.TestSuite; 10 import junit.framework.Test; 11 12 18 public class ReportTest extends TestCase { 19 20 protected static final File EXAMPLE_REPORT = 21 new File (AllTests.testDataFile("test.xml")); 22 protected static final File PARAM_INPUT_FILE = 23 new File (AllTests.testDataFile("test_parameters.xml")); 24 25 protected Report report; 26 protected DecimalFormat dollarFormatter; 27 protected DecimalFormat lastColFormatter; 28 29 public static Test suite() { 30 return new TestSuite(ReportTest.class); 31 } 32 33 public ReportTest(String name) { 34 super(name); 35 } 36 37 public void setUp() throws Exception { 38 report = new Report(); 39 report.setDataSource(new MockDataSource(report)); 40 report.read(EXAMPLE_REPORT); 41 report.setParameterXMLInput(PARAM_INPUT_FILE); 42 } 43 44 public void tearDown() { 45 report = null; 46 } 47 48 public void testAttributes() { 49 assertEquals("example1", report.getName()); 50 assertEquals("Example Report", report.getTitle()); 51 assertEquals("Jim Menard", report.getAuthor()); 52 assertEquals(new Long (7), report.generateNewParameterId()); 53 } 54 55 public void testPaperFormat() { 56 PaperFormat pf = report.getPaperFormat(); 57 assertNotNull(pf); 58 assertEquals(PaperFormat.PORTRAIT, pf.getOrientation()); 59 assertEquals("US-Letter", pf.getName()); 60 } 61 62 public void testImages() { 63 Section detail = report.getFirstSectionByArea(SectionArea.DETAIL); 64 65 ImageField image = new ImageField(new Long (99), report, detail, 68 "/tmp/foo.gif", true); 69 assertEquals("file:/tmp/foo.gif", image.getImageURL().toString()); 72 73 String url = "http://localhost/foo.gif"; 75 image.setValue(url); 76 assertEquals(url, image.getImageURL().toString()); 77 } 78 79 public void testSections() { 80 assertEquals(1, report.headers().size()); 82 assertEquals(1, report.footers().size()); 83 assertEquals(1, report.pageHeaders().size()); 84 assertEquals(1, report.pageFooters().size()); 85 assertEquals(1, report.details().size()); 86 assertEquals(2, report.countGroups()); 87 88 Section detail = report.getFirstSectionByArea(SectionArea.DETAIL); 90 assertNotNull(detail); 91 report.insertSectionBelow(detail); 92 assertEquals(2, report.details().size()); 93 } 94 95 public void testFormulas() { 96 Formula formula = report.findFormula("1"); 97 Long id = new Long (1); 98 assertNotNull(formula); 99 assertEquals("hourly rate / 100", formula.getName()); 100 assertEquals("{jobs.hourly rate}.nil? ? nil : {jobs.hourly rate} / 100.0", 101 formula.getExpression()); 102 assertEquals("formula:1", formula.dragString()); 103 assertEquals("{@hourly rate / 100}", formula.designLabel()); 104 assertEquals("{@1}", formula.formulaString()); 105 108 formula = report.findFormula(id); 109 assertNotNull(formula); 110 assertEquals(id, formula.getId()); 111 } 112 113 public void testFormulaReferences() { 114 refTest("1", true); 115 refTest("2", true); 116 refTest("3", false); 118 Formula f = new Formula(null, report, "startup script", 121 report.findFormula("3").formulaString()); 122 report.setStartFormula(f); 123 refTest("3", true); 124 } 125 126 protected void refTest(String formulaId, boolean shouldFindReference) { 127 Formula f = report.findFormula(formulaId); 128 assertNotNull(f); 129 assertTrue("reference to formula " + formulaId 130 + ": wanted to find = " + shouldFindReference 131 + " but opposite happened", 132 report.containsReferenceTo(f) == shouldFindReference); 133 } 134 135 public void testUserColumnReferences() { 136 UserColumn uc = report.findUserColumn("1"); 137 assertNotNull(uc); 138 139 assertTrue("shouldn't find reference to user column 1", 141 !report.containsReferenceTo(uc)); 142 143 Formula f = report.findFormula("1"); 146 f.setExpression(uc.formulaString()); 147 assertTrue("can't find reference to user column 1", 148 report.containsReferenceTo(uc)); 149 150 f.setExpression(""); 153 Section detail = report.findField("10").getSection(); 154 f = detail.getSuppressionProc().getFormula(); 155 f.setExpression(uc.formulaString()); 156 assertTrue("can't find reference to user column 1 in suppression proc", 157 report.containsReferenceTo(uc)); 158 159 f.setExpression(""); 162 f = new Formula(null, report, "startup script", uc.formulaString()); 163 report.setStartFormula(f); 164 assertTrue("can't find reference to user column 1 in startup script", 165 report.containsReferenceTo(uc)); 166 } 167 168 public void testParameterReferences() { 169 Parameter p = report.findParameter("1"); 170 assertNotNull(p); 171 172 assertTrue("should find reference to parameter 1", 176 report.containsReferenceTo(p)); 177 178 report.removeField(report.findField("15")); 181 Section detail = report.findField("10").getSection(); 182 Formula f = detail.getSuppressionProc().getFormula(); 183 f.setExpression(p.formulaString()); 184 assertTrue("can't find reference to parameter 1 in suppression proc", 185 report.containsReferenceTo(p)); 186 187 f.setExpression(""); 190 f = new Formula(null, report, "startup script", p.formulaString()); 191 report.setStartFormula(f); 192 assertTrue("can't find reference to parameter 1 in startup script", 193 report.containsReferenceTo(p)); 194 } 195 196 public void testFieldReferences() { 197 Field f; 198 for (int i = 0; (f = report.findField("" + i)) != null; ++i) 199 assertTrue("reference to field " + i + " not found", 200 report.containsReferenceTo(f)); 201 } 202 203 public void testParameters() { 204 Parameter param = report.findParameter("5"); 205 Long id = new Long (5); 206 assertNotNull(param); 207 assertEquals("parameter:5", param.dragString()); 208 assertEquals("{?5}", param.formulaString()); 209 assertEquals("{?Yes/No}", param.designLabel()); 210 assertEquals(Parameter.TYPE_BOOLEAN, param.getType()); 211 assertEquals(Parameter.ARITY_ONE, param.getArity()); 212 assertEquals(id, param.getId()); 213 assertEquals("Yes/No", param.getName()); 214 assertEquals("Do you breathe regularly?", param.getQuestion()); 215 216 param = report.findParameter(id); 217 assertNotNull(param); 218 assertEquals(id, param.getId()); 219 220 report.getDataSource().getQuery() 224 .setEditableWhereClause("office.name = {?String Param}"); 225 226 id = new Long (2); 227 param = report.findParameter(id); 228 assertNotNull(param); 229 230 Object obj = report.getParameterValue(id); assertNotNull("range param value should not be null", obj); 232 233 assertTrue("range param value is not a list; class = " 234 + obj.getClass().getName(), 235 obj instanceof List ); 236 List list = (List )obj; 237 238 assertTrue("range param value list does not have length 2; it has length " 239 + list.size(), list.size() == 2); 240 241 assertEquals(new Integer (50), list.get(0)); 242 assertEquals(new Integer (75), list.get(1)); 243 } 244 245 public void testNullFieldIds() { 246 Section detail = report.getFirstSectionByArea(SectionArea.DETAIL); 248 assertNotNull(detail); 249 250 TextField f = new TextField(null, report, detail, "field 1", true); 252 assertNotNull(f.getId()); 253 Object id = f.getId(); 254 assertTrue("field id is not a Long", id instanceof Long ); 255 long longVal = ((Long )id).longValue(); 256 assertTrue("field id is not > 1", longVal >= 1); 257 detail.addField(f); 258 259 f = new TextField(null, report, detail, "field 2", true); 260 Object newId = f.getId(); 261 assertNotNull(newId); 262 long newLongVal = ((Long )newId).longValue(); 263 assertEquals(longVal + 1, newLongVal); 264 detail.addField(f); 265 266 id = newId; 267 longVal = newLongVal; 268 269 f = new TextField(null, report, detail, "field 3", true); 270 newId = f.getId(); 271 assertNotNull(newId); 272 newLongVal = ((Long )newId).longValue(); 273 assertEquals(longVal + 1, newLongVal); 274 detail.addField(f); 275 } 276 277 public void testCaseSensitivity() { 278 assertNotNull(report.findColumn("jobs.ID")); 279 280 assertNull(report.findColumn("jobs.id")); 282 assertNull(report.findColumn("JOBS.ID")); 283 assertNull(report.findColumn("jOBs.id")); 284 285 assertNotNull(report.getDataSource().findColumn("ALL_CAPS.COL1")); 290 291 report.setCaseSensitiveDatabaseNames(false); 293 assertNotNull(report.findColumn("jobs.id")); 294 assertNotNull(report.findColumn("JOBS.ID")); 295 assertNotNull(report.findColumn("jOBs.id")); 296 297 assertNotNull(report.findFormulaByName("refs f1")); 299 assertNotNull(report.findFormulaByName("REFS F1")); 300 assertNotNull(report.findFormulaByName("rEFs f1")); 301 302 assertNotNull(report.findParameterByName("Yes/No")); 304 assertNotNull(report.findParameterByName("yes/no")); 305 assertNotNull(report.findParameterByName("yES/No")); 306 } 307 308 public void testFormat() { 309 Format f = report.getDefaultField().getFormat(); 310 Format f2 = (Format )f.clone(); 311 assertEquals("Default and cloned formats should be equal", f, f2); 312 313 assertNotNull(f.getColor()); 314 315 Field field = Field.createFromDragString(report, "column:jobs.ID"); 316 assertNotNull(field); 317 318 Format fmt = field.getFormat(); 319 assertNotNull(fmt); 320 321 Format defaultFormat = report.getDefaultField().getFormat(); 322 323 assertEquals(defaultFormat, field.getFormat()); 324 325 assertEquals(defaultFormat.getFormat(), fmt.getFormat()); 328 } 329 330 public void testCloning() { 331 Section s = report.getFirstSectionByArea(SectionArea.REPORT_HEADER); 334 SpecialField f = 335 (SpecialField)Field.create(null, report, s, "special", 336 "report.date", true); 337 338 f.setFormat((Format )report.getDefaultField().getFormat().clone()); 339 f.setBounds(new Rectangle(1.0, 2.0, 3.0, 4.0)); 340 f.setBorder(new Border(f)); 341 342 SpecialField f2 = (SpecialField)f.clone(); 343 assertNotNull(f2); 344 345 348 assertEquals(f.getBounds().x, f2.getBounds().x, 0.00001); 349 assertEquals(f.getBounds().y, f2.getBounds().y, 0.00001); 350 assertEquals(f.getBounds().width, f2.getBounds().width, 0.00001); 351 assertEquals(f.getBounds().height, f2.getBounds().height, 0.00001); 352 assertEquals(f.getFormat(), f2.getFormat()); 353 assertEquals(f.getBorder(), f2.getBorder()); 354 355 String fVal = f.getValue().toString(); 358 String f2Val = f2.getValue().toString(); 359 assertEquals(fVal.substring(0, 16), f2Val.substring(0, 16)); 360 assertEquals(fVal.substring(19), f2Val.substring(19)); 361 } 362 363 public void testHasParameterFields() { 364 assertEquals(true, report.hasParameterFields()); 365 } 366 367 public void testIdGeneration() { 368 long nextFormulaId = report.generateNewFormulaId().longValue(); 369 long nextParameterId = report.generateNewParameterId().longValue(); 370 long nextUserColumnId = report.generateNewUserColumnId().longValue(); 371 372 377 for (int i = 0; i < 3; ++i) { 378 Formula f = new Formula(null, report, ""); 379 Object id = f.getId(); 380 assertTrue("formula id is not a Long", id instanceof Long ); 381 long longVal = ((Long )id).longValue(); 382 assertEquals(nextFormulaId, longVal); 383 report.addFormula(f); 384 ++nextFormulaId; 385 386 Parameter p = new Parameter(null, report); 387 id = p.getId(); 388 assertTrue("parameter id is not a Long", id instanceof Long ); 389 longVal = ((Long )id).longValue(); 390 assertEquals(nextParameterId, longVal); 391 report.addParameter(p); 392 ++nextParameterId; 393 394 UserColumn uc = new UserColumn(null, report, "", ""); 395 id = uc.getId(); 396 assertTrue("user column id is not a Long", id instanceof Long ); 397 longVal = ((Long )id).longValue(); 398 assertEquals(nextUserColumnId, longVal); 399 report.addUserColumn(uc); 400 ++nextUserColumnId; 401 } 402 } 403 404 public static void main(String [] args) { 405 junit.textui.TestRunner.run(suite()); 406 System.exit(0); 407 } 408 409 } 410 | Popular Tags |