KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.test;
2 import jimm.datavision.*;
3 import jimm.datavision.layout.CharSepLE;
4 import jimm.datavision.source.charsep.CharSepSource;
5 import java.io.*;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import junit.framework.Test;
9
10 /**
11  * Tests formula evals when formulas are hidden or appear multiple
12  * times.
13  * <p>
14  * These tests are tightly coupled with the contents of the files
15  * <code>eval.xml</code> and <code>eval.csv</code>.
16  *
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public class FormulaEvalTest extends TestCase {
20
21 protected static final File EVAL_REPORT =
22     new File(AllTests.testDataFile("eval.xml"));
23 protected static final String JavaDoc EVAL_DATA_FILE =
24     AllTests.testDataFile("eval.csv");
25 protected static final File OUT_FILE =
26     new File(System.getProperty("java.io.tmpdir"),
27          "datavision_charsep_eval_out.txt");
28 protected static final File PARAM_INPUT_FILE =
29     new File(AllTests.testDataFile("test_parameters.xml"));
30
31 protected static final String JavaDoc[] EVAL_RESULTS = {
32     "0",
33     "1\t23.0\t1",
34     "1\t44.3\t2",
35     "1\t50.0\t3",
36     "39.1",
37     "0",
38     "2\t33.0\t1",
39     "2\t46.0\t2",
40     "39.5"
41 };
42
43 protected Report report;
44 protected CharSepSource dataSource;
45
46 public static Test suite() {
47     return new TestSuite(FormulaEvalTest.class);
48 }
49
50 public FormulaEvalTest(String JavaDoc name) {
51     super(name);
52 }
53
54 public void setUp() throws Exception JavaDoc {
55     report = new Report();
56
57     OUT_FILE.deleteOnExit();
58     PrintWriter out = new PrintWriter(new FileWriter(OUT_FILE));
59     report.setLayoutEngine(new CharSepLE(out, '\t'));
60
61     report.read(EVAL_REPORT); // Must come after setting password
62

63     dataSource = (CharSepSource)report.getDataSource();
64     dataSource.setSepChar(',');
65     dataSource.setInput(EVAL_DATA_FILE);
66 }
67
68 public void tearDown() {
69     if (OUT_FILE.exists())
70     OUT_FILE.delete();
71 }
72
73 public void testEvalAllVisible() throws FileNotFoundException, IOException {
74     runEvalTest(null);
75 }
76
77 public void testEvalDetalInvisible()
78     throws FileNotFoundException, IOException
79 {
80     // Make detail formula field invisible.
81
report.findField("2").setVisible(false);
82
83     runEvalTest(new ExpectedLineModifier() {
84     String JavaDoc expected(String JavaDoc str) {
85         int pos = str.lastIndexOf("\t");
86         return (pos != -1) ? str = str.substring(0, pos) : str;
87     }
88     });
89 }
90
91 public void testEvalHeaderInvisible()
92     throws IOException, FileNotFoundException
93 {
94     // Make detail formula field invisible.
95
report.findField("1").setVisible(false);
96
97     runEvalTest(new ExpectedLineModifier() {
98     String JavaDoc expected(String JavaDoc str) {
99         return ("0".equals(str)) ? "" : str;
100     }
101     });
102 }
103
104 public void testGroupHeaderInvisible()
105     throws FileNotFoundException, IOException
106 {
107     // Make detail formula field invisible.
108
Section s = report.findField("1").getSection();
109     s.getSuppressionProc().setHidden(true);
110
111     runEvalTest(new ExpectedLineModifier() {
112     String JavaDoc expected(String JavaDoc str) {
113         return ("0".equals(str)) ? null : str;
114     }
115     });
116 }
117
118 public void testParamInSuppressionProc()
119     throws FileNotFoundException, IOException
120 {
121     // We only need one parameter. This array of parameters is only
122
// necessary because the parameter file contains data for parameters
123
// with ids 1 - 6.
124
Parameter[] params = new Parameter[6];
125     for (int i = 0; i < 6; ++i) {
126     params[i] = new Parameter(new Long JavaDoc(i + 1), report, "string",
127                   "str param", "what do YOU want?",
128                   "single");
129     report.addParameter(params[i]);
130     }
131
132     report.setParameterXMLInput(PARAM_INPUT_FILE);
133
134     // Find detail section
135
Section detail = report.findField("100").getSection();
136     Formula f = detail.getSuppressionProc().getFormula();
137     f.setExpression("\"{?1}\" == 'never'");
138
139     runEvalTest(null);
140
141     // Make sure parameter's value has been read in. Thus we prove that the
142
// report recognized the parameter was used in the detail section's
143
// suppression proc.
144
Parameter p = report.findParameter(new Long JavaDoc(1));
145     assertNotNull(p);
146     assertEquals("Chicago", p.getValue());
147 }
148
149 void runEvalTest(ExpectedLineModifier elm)
150     throws FileNotFoundException, IOException
151 {
152     // Run report in this thread, not a separate one. Running the
153
// report closes the output stream.
154
report.runReport();
155
156     // Open the output and look for various things.
157
BufferedReader out = new BufferedReader(new FileReader(OUT_FILE));
158
159     // Check output file contents
160
String JavaDoc outLine;
161     for (int lineNum = 0; lineNum < EVAL_RESULTS.length
162          && (outLine = out.readLine()) != null; ++lineNum)
163     {
164     String JavaDoc expected = EVAL_RESULTS[lineNum];
165     if (elm == null)
166         expected = EVAL_RESULTS[lineNum];
167     else
168         expected = elm.expected(EVAL_RESULTS[lineNum]);
169     while (expected == null) {
170         ++lineNum;
171         expected = EVAL_RESULTS[lineNum];
172         expected = elm.expected(EVAL_RESULTS[lineNum]);
173     }
174     assertEquals(expected, outLine);
175     }
176
177     // Make sure we are at the end of the file.
178
assertNull(out.readLine());
179
180     out.close();
181 }
182
183 public static void main(String JavaDoc[] args) {
184     junit.textui.TestRunner.run(suite());
185     System.exit(0);
186 }
187
188 // ================================================================
189
/**
190  * Lets us modify the &quot;standard&quot; expected results to match
191  * a particular test's output.
192  */

193 abstract class ExpectedLineModifier {
194 /**
195  * Given the &quot;standard&quot; expected value, returns the expected
196  * text for a particular test. If we return <code>null</code> that means
197  * the input line should be skipped.
198  */

199 abstract String JavaDoc expected(String JavaDoc str);
200 }
201
202 }
203
Popular Tags