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 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 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 [] 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 name) { 51 super(name); 52 } 53 54 public void setUp() throws Exception { 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); 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 report.findField("2").setVisible(false); 82 83 runEvalTest(new ExpectedLineModifier() { 84 String expected(String 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 report.findField("1").setVisible(false); 96 97 runEvalTest(new ExpectedLineModifier() { 98 String expected(String str) { 99 return ("0".equals(str)) ? "" : str; 100 } 101 }); 102 } 103 104 public void testGroupHeaderInvisible() 105 throws FileNotFoundException, IOException 106 { 107 Section s = report.findField("1").getSection(); 109 s.getSuppressionProc().setHidden(true); 110 111 runEvalTest(new ExpectedLineModifier() { 112 String expected(String str) { 113 return ("0".equals(str)) ? null : str; 114 } 115 }); 116 } 117 118 public void testParamInSuppressionProc() 119 throws FileNotFoundException, IOException 120 { 121 Parameter[] params = new Parameter[6]; 125 for (int i = 0; i < 6; ++i) { 126 params[i] = new Parameter(new Long (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 Section detail = report.findField("100").getSection(); 136 Formula f = detail.getSuppressionProc().getFormula(); 137 f.setExpression("\"{?1}\" == 'never'"); 138 139 runEvalTest(null); 140 141 Parameter p = report.findParameter(new Long (1)); 145 assertNotNull(p); 146 assertEquals("Chicago", p.getValue()); 147 } 148 149 void runEvalTest(ExpectedLineModifier elm) 150 throws FileNotFoundException, IOException 151 { 152 report.runReport(); 155 156 BufferedReader out = new BufferedReader(new FileReader(OUT_FILE)); 158 159 String outLine; 161 for (int lineNum = 0; lineNum < EVAL_RESULTS.length 162 && (outLine = out.readLine()) != null; ++lineNum) 163 { 164 String 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 assertNull(out.readLine()); 179 180 out.close(); 181 } 182 183 public static void main(String [] args) { 184 junit.textui.TestRunner.run(suite()); 185 System.exit(0); 186 } 187 188 193 abstract class ExpectedLineModifier { 194 199 abstract String expected(String str); 200 } 201 202 } 203 | Popular Tags |