KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.test;
2 import jimm.datavision.*;
3 import jimm.datavision.layout.CharSepLE;
4 import jimm.datavision.source.Join;
5 import java.io.*;
6 import java.util.Iterator JavaDoc;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import junit.framework.Test;
10
11 /**
12  * Reads a report from an XML file, runs it, and verifies the output. Uses
13  * the {@link CharSepLE} layout engine to produce a tab-delimited output file.
14  * <p>
15  * These tests are tightly coupled with the contents of the file
16  * <code>test_sub.xml</code> and the contents of the test database generated
17  * by the files in <code>jimm/datavision/testdata</code>.
18  *
19  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
20  */

21 public class SubreportRunTest extends TestCase {
22
23 protected static final File EXAMPLE_REPORT =
24     new File(AllTests.testDataFile("test_sub.xml"));
25 protected static final File OUT_FILE =
26     new File(System.getProperty("java.io.tmpdir"),
27          "datavision_subreport_run_test_out.txt");
28 // This must match the format string for the report.date field in
29
// EXAMPLE_REPORT.
30
protected static final String JavaDoc REPORT_DATE_FORMAT = "yyyy-MM-dd";
31 // This must be an alphabetically sorted list of office names from the
32
// offices table.
33
protected static final String JavaDoc OFFICES[] = {
34     "Chicago", "New Jersey", "New York"
35 };
36 // The value of parameter one, which is at the beginning of the
37
// report header.
38
protected static final String JavaDoc STRING_PARAM_VALUE = "Chicago";
39 // The value of the report.title special field, which appears
40
// in the report header.
41
protected static final String JavaDoc REPORT_TITLE = "Example Report";
42
43 protected Report report;
44
45 public static Test suite() {
46     return new TestSuite(SubreportRunTest.class);
47 }
48
49 public SubreportRunTest(String JavaDoc name) {
50     super(name);
51 }
52
53 public void setUp() throws Exception JavaDoc {
54     report = new Report();
55     report.setDatabasePassword("");
56     report.read(EXAMPLE_REPORT); // Must come after setting password
57

58     if (OUT_FILE.exists())
59     OUT_FILE.delete(); // Delete previous output
60
OUT_FILE.deleteOnExit();
61     PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
62     report.setLayoutEngine(new CharSepLE(out, '\t'));
63 }
64
65 public void tearDown() {
66     if (OUT_FILE.exists())
67     OUT_FILE.delete();
68 }
69
70 // Make sure a simple subreport runs
71
public void testReportRun() throws IOException, FileNotFoundException {
72     // Run report in this thread, not a separate one. Running the
73
// report closes the output stream.
74
report.runReport();
75
76     BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
77     String JavaDoc line;
78     int cityIndex = -1;
79     String JavaDoc expectedOffice;
80     while ((line = in.readLine()) != null) {
81     int tabPos = line.indexOf("\t");
82
83     if (tabPos == -1) // New group
84
++cityIndex;
85     expectedOffice = ReportRunTest.OFFICES[cityIndex];
86
87     if (tabPos == -1)
88         assertEquals(expectedOffice, line);
89     else {
90         // Line is "NN<tab>name<tab>name"
91
line = line.substring(tabPos + 1);
92         tabPos = line.indexOf("\t");
93         assertEquals(expectedOffice, line.substring(tabPos + 1));
94     }
95     }
96     in.close();
97 }
98
99 public void testMultipleJoins() throws IOException, FileNotFoundException {
100     for (Iterator JavaDoc iter = report.subreports(); iter.hasNext(); ) {
101     Subreport s = (Subreport)iter.next();
102     s.addJoin(new Join(report.findColumn("office.name"), "=",
103                report.findColumn("office.name")));
104     }
105
106     // Run report in this thread, not a separate one. Running the
107
// report closes the output stream.
108
report.runReport();
109
110     BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
111     String JavaDoc line;
112     int cityIndex = -1;
113     String JavaDoc expectedOffice;
114     while ((line = in.readLine()) != null) {
115     int tabPos = line.indexOf("\t");
116
117     if (tabPos == -1) // New group
118
++cityIndex;
119     expectedOffice = ReportRunTest.OFFICES[cityIndex];
120
121     if (tabPos == -1)
122         assertEquals(expectedOffice, line);
123     else {
124         // Line is "NN<tab>name<tab>name"
125
line = line.substring(tabPos + 1);
126         tabPos = line.indexOf("\t");
127         assertEquals(expectedOffice, line.substring(tabPos + 1));
128     }
129     }
130     in.close();
131 }
132
133 public static void main(String JavaDoc[] args) {
134     junit.textui.TestRunner.run(suite());
135     System.exit(0);
136 }
137
138 }
139
Popular Tags