001: package jimm.datavision.test;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.layout.CharSepLE;
005: import jimm.datavision.source.charsep.CharSepSource;
006: import java.io.*;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009: import junit.framework.Test;
010:
011: /**
012: * Tests formula evals when formulas are hidden or appear multiple
013: * times.
014: * <p>
015: * These tests are tightly coupled with the contents of the files
016: * <code>eval.xml</code> and <code>eval.csv</code>.
017: *
018: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
019: */
020: public class FormulaEvalTest extends TestCase {
021:
022: protected static final File EVAL_REPORT = new File(AllTests
023: .testDataFile("eval.xml"));
024: protected static final String EVAL_DATA_FILE = AllTests
025: .testDataFile("eval.csv");
026: protected static final File OUT_FILE = new File(System
027: .getProperty("java.io.tmpdir"),
028: "datavision_charsep_eval_out.txt");
029: protected static final File PARAM_INPUT_FILE = new File(AllTests
030: .testDataFile("test_parameters.xml"));
031:
032: protected static final String[] EVAL_RESULTS = { "0", "1\t23.0\t1",
033: "1\t44.3\t2", "1\t50.0\t3", "39.1", "0", "2\t33.0\t1",
034: "2\t46.0\t2", "39.5" };
035:
036: protected Report report;
037: protected CharSepSource dataSource;
038:
039: public static Test suite() {
040: return new TestSuite(FormulaEvalTest.class);
041: }
042:
043: public FormulaEvalTest(String name) {
044: super (name);
045: }
046:
047: public void setUp() throws Exception {
048: report = new Report();
049:
050: OUT_FILE.deleteOnExit();
051: PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
052: report.setLayoutEngine(new CharSepLE(out, '\t'));
053:
054: report.read(EVAL_REPORT); // Must come after setting password
055:
056: dataSource = (CharSepSource) report.getDataSource();
057: dataSource.setSepChar(',');
058: dataSource.setInput(EVAL_DATA_FILE);
059: }
060:
061: public void tearDown() {
062: if (OUT_FILE.exists())
063: OUT_FILE.delete();
064: }
065:
066: public void testEvalAllVisible() throws FileNotFoundException,
067: IOException {
068: runEvalTest(null);
069: }
070:
071: public void testEvalDetalInvisible() throws FileNotFoundException,
072: IOException {
073: // Make detail formula field invisible.
074: report.findField("2").setVisible(false);
075:
076: runEvalTest(new ExpectedLineModifier() {
077: String expected(String str) {
078: int pos = str.lastIndexOf("\t");
079: return (pos != -1) ? str = str.substring(0, pos) : str;
080: }
081: });
082: }
083:
084: public void testEvalHeaderInvisible() throws IOException,
085: FileNotFoundException {
086: // Make detail formula field invisible.
087: report.findField("1").setVisible(false);
088:
089: runEvalTest(new ExpectedLineModifier() {
090: String expected(String str) {
091: return ("0".equals(str)) ? "" : str;
092: }
093: });
094: }
095:
096: public void testGroupHeaderInvisible()
097: throws FileNotFoundException, IOException {
098: // Make detail formula field invisible.
099: Section s = report.findField("1").getSection();
100: s.getSuppressionProc().setHidden(true);
101:
102: runEvalTest(new ExpectedLineModifier() {
103: String expected(String str) {
104: return ("0".equals(str)) ? null : str;
105: }
106: });
107: }
108:
109: public void testParamInSuppressionProc()
110: throws FileNotFoundException, IOException {
111: // We only need one parameter. This array of parameters is only
112: // necessary because the parameter file contains data for parameters
113: // with ids 1 - 6.
114: Parameter[] params = new Parameter[6];
115: for (int i = 0; i < 6; ++i) {
116: params[i] = new Parameter(new Long(i + 1), report,
117: "string", "str param", "what do YOU want?",
118: "single");
119: report.addParameter(params[i]);
120: }
121:
122: report.setParameterXMLInput(PARAM_INPUT_FILE);
123:
124: // Find detail section
125: Section detail = report.findField("100").getSection();
126: Formula f = detail.getSuppressionProc().getFormula();
127: f.setExpression("\"{?1}\" == 'never'");
128:
129: runEvalTest(null);
130:
131: // Make sure parameter's value has been read in. Thus we prove that the
132: // report recognized the parameter was used in the detail section's
133: // suppression proc.
134: Parameter p = report.findParameter(new Long(1));
135: assertNotNull(p);
136: assertEquals("Chicago", p.getValue());
137: }
138:
139: void runEvalTest(ExpectedLineModifier elm)
140: throws FileNotFoundException, IOException {
141: // Run report in this thread, not a separate one. Running the
142: // report closes the output stream.
143: report.runReport();
144:
145: // Open the output and look for various things.
146: BufferedReader out = new BufferedReader(
147: new FileReader(OUT_FILE));
148:
149: // Check output file contents
150: String outLine;
151: for (int lineNum = 0; lineNum < EVAL_RESULTS.length
152: && (outLine = out.readLine()) != null; ++lineNum) {
153: String expected = EVAL_RESULTS[lineNum];
154: if (elm == null)
155: expected = EVAL_RESULTS[lineNum];
156: else
157: expected = elm.expected(EVAL_RESULTS[lineNum]);
158: while (expected == null) {
159: ++lineNum;
160: expected = EVAL_RESULTS[lineNum];
161: expected = elm.expected(EVAL_RESULTS[lineNum]);
162: }
163: assertEquals(expected, outLine);
164: }
165:
166: // Make sure we are at the end of the file.
167: assertNull(out.readLine());
168:
169: out.close();
170: }
171:
172: public static void main(String[] args) {
173: junit.textui.TestRunner.run(suite());
174: System.exit(0);
175: }
176:
177: // ================================================================
178: /**
179: * Lets us modify the "standard" expected results to match
180: * a particular test's output.
181: */
182: abstract class ExpectedLineModifier {
183: /**
184: * Given the "standard" expected value, returns the expected
185: * text for a particular test. If we return <code>null</code> that means
186: * the input line should be skipped.
187: */
188: abstract String expected(String str);
189: }
190:
191: }
|