KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
6 import java.io.File JavaDoc;
7 import java.text.DecimalFormat JavaDoc;
8 import junit.framework.TestCase;
9 import junit.framework.TestSuite;
10 import junit.framework.Test;
11
12 /**
13  * Reads a report from an XML file, tests its structure, and tests various
14  * pieces like parameter and formula substitution.
15  *
16  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
17  */

18 public class ReportTest extends TestCase {
19
20 protected static final File JavaDoc EXAMPLE_REPORT =
21         new File JavaDoc(AllTests.testDataFile("test.xml"));
22 protected static final File JavaDoc PARAM_INPUT_FILE =
23         new File JavaDoc(AllTests.testDataFile("test_parameters.xml"));
24
25 protected Report report;
26 protected DecimalFormat JavaDoc dollarFormatter;
27 protected DecimalFormat JavaDoc lastColFormatter;
28
29 public static Test suite() {
30     return new TestSuite(ReportTest.class);
31 }
32
33 public ReportTest(String JavaDoc name) {
34     super(name);
35 }
36
37 public void setUp() throws Exception JavaDoc {
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 JavaDoc(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     // Add an image, passing in a file path, and make sure the URL is
66
// correct.
67
ImageField image = new ImageField(new Long JavaDoc(99), report, detail,
68                       "/tmp/foo.gif", true);
69     // I expected to see "file:///tmp/foo.gif", but apparently the URL
70
// class modifies that to be "file:/tmp/foo.gif".
71
assertEquals("file:/tmp/foo.gif", image.getImageURL().toString());
72
73     // Now set value to a URL string.
74
String JavaDoc url = "http://localhost/foo.gif";
75     image.setValue(url);
76     assertEquals(url, image.getImageURL().toString());
77 }
78
79 public void testSections() {
80     // Sections
81
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     // Add a new detail section
89
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 JavaDoc id = new Long JavaDoc(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     // We test StringUtils.formulaToDisplay() and
106
// StringUtils.displayToFormula() in FormulaTest.java.
107

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); // Formula exists, but not in any report field
117

118     // Now put formula 3 in the report's startup script and look for it.
119
// (We already know that report.findFormula won't return null.)
120
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 JavaDoc 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     // We should not find the user column inside any report field
140
assertTrue("shouldn't find reference to user column 1",
141            !report.containsReferenceTo(uc));
142
143     // Add the user column to a formula that has a report field, so that
144
// containsRefereceTo should return true.
145
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     // Now remove the user column from that formula and instead place it
151
// inside a suppression proc.
152
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     // Now remove the user column from the suppression proc and put it in
160
// the report's startup script instead.
161
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     // Unlike the other testXXXReferences methods, we *should* find the
173
// parameter inside a report field because it's there in the group
174
// header.
175
assertTrue("should find reference to parameter 1",
176            report.containsReferenceTo(p));
177
178     // Now remove the parameter from the header and instead place it
179
// inside a suppression proc.
180
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     // Now remove the parameter from the suppression proc and put it in the
188
// report's startup script instead.
189
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 JavaDoc id = new Long JavaDoc(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     // Get ready to read values from parameter values file when asked.
221
// We have to add a parameter to the report somewhere. If we don't,
222
// the report won't read the XML file.
223
report.getDataSource().getQuery()
224     .setEditableWhereClause("office.name = {?String Param}");
225
226     id = new Long JavaDoc(2);
227     param = report.findParameter(id);
228     assertNotNull(param);
229
230     Object JavaDoc obj = report.getParameterValue(id); // Reads values from XML
231
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 JavaDoc);
236     List JavaDoc list = (List JavaDoc)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 JavaDoc(50), list.get(0));
242     assertEquals(new Integer JavaDoc(75), list.get(1));
243 }
244
245 public void testNullFieldIds() {
246     // Empty reports start with one detail section
247
Section detail = report.getFirstSectionByArea(SectionArea.DETAIL);
248     assertNotNull(detail);
249
250     // Add fields.
251
TextField f = new TextField(null, report, detail, "field 1", true);
252     assertNotNull(f.getId());
253     Object JavaDoc id = f.getId();
254     assertTrue("field id is not a Long", id instanceof Long JavaDoc);
255     long longVal = ((Long JavaDoc)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 JavaDoc newId = f.getId();
261     assertNotNull(newId);
262     long newLongVal = ((Long JavaDoc)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 JavaDoc)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     // These should fail since by default we search case-sensitively
281
assertNull(report.findColumn("jobs.id"));
282     assertNull(report.findColumn("JOBS.ID"));
283     assertNull(report.findColumn("jOBs.id"));
284
285     // These should succeed, because the table and column names are
286
// all upper-case in the database. We go straight to the data
287
// source because the report does not contain this column but
288
// the database does.
289
assertNotNull(report.getDataSource().findColumn("ALL_CAPS.COL1"));
290
291     // Now tell the report to ignore case with table and column names
292
report.setCaseSensitiveDatabaseNames(false);
293     assertNotNull(report.findColumn("jobs.id"));
294     assertNotNull(report.findColumn("JOBS.ID"));
295     assertNotNull(report.findColumn("jOBs.id"));
296
297     // Formula name search is always case-insensitive
298
assertNotNull(report.findFormulaByName("refs f1"));
299     assertNotNull(report.findFormulaByName("REFS F1"));
300     assertNotNull(report.findFormulaByName("rEFs f1"));
301
302     // Parameter name search is always case-insensitive
303
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 JavaDoc f = report.getDefaultField().getFormat();
310     Format JavaDoc f2 = (Format JavaDoc)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 JavaDoc fmt = field.getFormat();
319     assertNotNull(fmt);
320
321     Format JavaDoc defaultFormat = report.getDefaultField().getFormat();
322
323     assertEquals(defaultFormat, field.getFormat());
324
325     // Test "transparency"; the format's format string is really null, but
326
// it will return the default format's format string;
327
assertEquals(defaultFormat.getFormat(), fmt.getFormat());
328 }
329
330 public void testCloning() {
331     // Make sure whatever we create here does not have a value that
332
// depends upon running the report and having data.
333
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 JavaDoc)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     // assertEquals does too much, and we don't need Field.equals
346
// anywhere else, so let's do it piece by piece
347

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     // The value is a date. Ignore the seconds in both values
356
// just in case they are different.
357
String JavaDoc fVal = f.getValue().toString();
358     String JavaDoc 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     // The new objects' id numbers will be the same as nextXXXId (instead
373
// of nextXXXid + 1) because the new id numbers only get bumped up when
374
// a new object is added to the report, not wehn generateNewXXXId gets
375
// called or the object gets created.
376

377     for (int i = 0; i < 3; ++i) {
378     Formula f = new Formula(null, report, "");
379     Object JavaDoc id = f.getId();
380     assertTrue("formula id is not a Long", id instanceof Long JavaDoc);
381     long longVal = ((Long JavaDoc)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 JavaDoc);
389     longVal = ((Long JavaDoc)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 JavaDoc);
397     longVal = ((Long JavaDoc)id).longValue();
398     assertEquals(nextUserColumnId, longVal);
399     report.addUserColumn(uc);
400     ++nextUserColumnId;
401     }
402 }
403
404 public static void main(String JavaDoc[] args) {
405     junit.textui.TestRunner.run(suite());
406     System.exit(0);
407 }
408
409 }
410
Popular Tags