001: package jimm.datavision.test;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.layout.CharSepLE;
005: import jimm.datavision.source.Join;
006: import java.io.*;
007: import java.util.Iterator;
008: import junit.framework.TestCase;
009: import junit.framework.TestSuite;
010: import junit.framework.Test;
011:
012: /**
013: * Reads a report from an XML file, runs it, and verifies the output. Uses
014: * the {@link CharSepLE} layout engine to produce a tab-delimited output file.
015: * <p>
016: * These tests are tightly coupled with the contents of the file
017: * <code>test_sub.xml</code> and the contents of the test database generated
018: * by the files in <code>jimm/datavision/testdata</code>.
019: *
020: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
021: */
022: public class SubreportRunTest extends TestCase {
023:
024: protected static final File EXAMPLE_REPORT = new File(AllTests
025: .testDataFile("test_sub.xml"));
026: protected static final File OUT_FILE = new File(System
027: .getProperty("java.io.tmpdir"),
028: "datavision_subreport_run_test_out.txt");
029: // This must match the format string for the report.date field in
030: // EXAMPLE_REPORT.
031: protected static final String REPORT_DATE_FORMAT = "yyyy-MM-dd";
032: // This must be an alphabetically sorted list of office names from the
033: // offices table.
034: protected static final String OFFICES[] = { "Chicago",
035: "New Jersey", "New York" };
036: // The value of parameter one, which is at the beginning of the
037: // report header.
038: protected static final String STRING_PARAM_VALUE = "Chicago";
039: // The value of the report.title special field, which appears
040: // in the report header.
041: protected static final String REPORT_TITLE = "Example Report";
042:
043: protected Report report;
044:
045: public static Test suite() {
046: return new TestSuite(SubreportRunTest.class);
047: }
048:
049: public SubreportRunTest(String name) {
050: super (name);
051: }
052:
053: public void setUp() throws Exception {
054: report = new Report();
055: report.setDatabasePassword("");
056: report.read(EXAMPLE_REPORT); // Must come after setting password
057:
058: if (OUT_FILE.exists())
059: OUT_FILE.delete(); // Delete previous output
060: OUT_FILE.deleteOnExit();
061: PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
062: report.setLayoutEngine(new CharSepLE(out, '\t'));
063: }
064:
065: public void tearDown() {
066: if (OUT_FILE.exists())
067: OUT_FILE.delete();
068: }
069:
070: // Make sure a simple subreport runs
071: public void testReportRun() throws IOException,
072: FileNotFoundException {
073: // Run report in this thread, not a separate one. Running the
074: // report closes the output stream.
075: report.runReport();
076:
077: BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
078: String line;
079: int cityIndex = -1;
080: String expectedOffice;
081: while ((line = in.readLine()) != null) {
082: int tabPos = line.indexOf("\t");
083:
084: if (tabPos == -1) // New group
085: ++cityIndex;
086: expectedOffice = ReportRunTest.OFFICES[cityIndex];
087:
088: if (tabPos == -1)
089: assertEquals(expectedOffice, line);
090: else {
091: // Line is "NN<tab>name<tab>name"
092: line = line.substring(tabPos + 1);
093: tabPos = line.indexOf("\t");
094: assertEquals(expectedOffice, line.substring(tabPos + 1));
095: }
096: }
097: in.close();
098: }
099:
100: public void testMultipleJoins() throws IOException,
101: FileNotFoundException {
102: for (Iterator iter = report.subreports(); iter.hasNext();) {
103: Subreport s = (Subreport) iter.next();
104: s.addJoin(new Join(report.findColumn("office.name"), "=",
105: report.findColumn("office.name")));
106: }
107:
108: // Run report in this thread, not a separate one. Running the
109: // report closes the output stream.
110: report.runReport();
111:
112: BufferedReader in = new BufferedReader(new FileReader(OUT_FILE));
113: String line;
114: int cityIndex = -1;
115: String expectedOffice;
116: while ((line = in.readLine()) != null) {
117: int tabPos = line.indexOf("\t");
118:
119: if (tabPos == -1) // New group
120: ++cityIndex;
121: expectedOffice = ReportRunTest.OFFICES[cityIndex];
122:
123: if (tabPos == -1)
124: assertEquals(expectedOffice, line);
125: else {
126: // Line is "NN<tab>name<tab>name"
127: line = line.substring(tabPos + 1);
128: tabPos = line.indexOf("\t");
129: assertEquals(expectedOffice, line.substring(tabPos + 1));
130: }
131: }
132: in.close();
133: }
134:
135: public static void main(String[] args) {
136: junit.textui.TestRunner.run(suite());
137: System.exit(0);
138: }
139:
140: }
|