1 package jimm.datavision.test; 2 import jimm.datavision.*; 3 import jimm.datavision.source.Column; 4 import jimm.datavision.source.Query; 5 import jimm.datavision.test.mock.source.MockDataSource; 6 import java.io.*; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import junit.framework.TestCase; 10 import junit.framework.TestSuite; 11 import junit.framework.Test; 12 13 public class FormulaTest extends TestCase { 14 15 protected static final File EXAMPLE_REPORT = 16 new File(AllTests.testDataFile("test.xml")); 17 18 protected Report report; 19 20 public static Test suite() { 21 return new TestSuite(FormulaTest.class); 22 } 23 24 public FormulaTest(String name) { 25 super(name); 26 } 27 28 public void setUp() { 29 report = new Report(); 30 report.setDataSource(new MockDataSource(report)); 31 } 32 33 public void testSingleTokens() throws IOException { 34 HashMap testVals = new HashMap (); 35 Long theAnswer = new Long (42); 36 testVals.put("42", theAnswer); 37 testVals.put(" 42", theAnswer); 38 testVals.put("42 ", theAnswer); 39 testVals.put(" 42 ", theAnswer); 40 testVals.put(" \t 42 \t", theAnswer); 41 testVals.put("66.6", new Double (66.6)); 42 testVals.put("\"foo\"", "foo"); 43 testVals.put("\"don't forget apostrophes\"", 44 "don't forget apostrophes"); 45 testVals.put("\"or \\\"quoted quotes\\\" either\"", 46 "or \"quoted quotes\" either"); 47 48 runTests(testVals); 49 } 50 51 public void testExpressions() throws IOException { 52 HashMap testVals = new HashMap (); 53 Long theAnswer = new Long (42); 54 Double theAnswerAsDouble = new Double (42); 55 String theAnswerAsString = "42"; 56 Long aBoringAnswer = new Long (36); 57 58 testVals.put("39 + 3", theAnswer); 59 testVals.put(" 39 + 3 ", theAnswer); 60 testVals.put(" 39\t+ 3", theAnswer); 61 testVals.put(" 39 +\t3", theAnswer); 62 testVals.put("39 + 3", theAnswer); 63 testVals.put("45 - 3", theAnswer); 64 testVals.put("21 * 2", theAnswer); 65 testVals.put("84.00 / 2.0", theAnswerAsDouble); 66 testVals.put(" \"foo\" + \"bar\"", "foobar"); 67 testVals.put("\"foo\" * 3", "foofoofoo"); 68 testVals.put("\"foo\" * 3.5", "foofoofoo"); 69 testVals.put("\"foo\" * 3.5.to_i", "foofoofoo"); 70 testVals.put("42.3343.floor", theAnswer); 71 testVals.put("42.3343.round", theAnswer); 72 testVals.put("41.3343.ceil", theAnswer); 73 testVals.put("6 ** 2", aBoringAnswer); 74 testVals.put("[42, 36].max", theAnswer); 75 testVals.put("[36, 42].max", theAnswer); 76 testVals.put("[42, 36].min", aBoringAnswer); 77 testVals.put("[36, 42].min", aBoringAnswer); 78 testVals.put("42.abs", theAnswer); 79 testVals.put("-42.abs", theAnswer); 80 testVals.put("str='45';str.gsub!(/5/,'2');str.to_i", theAnswer); 81 testVals.put("Math.sqrt(1764)", theAnswerAsDouble); 82 testVals.put("Math.sqrt(Math.sqrt(1764) ** 2)", theAnswerAsDouble); 83 84 testVals.put("'42abc'[0, 2]", theAnswerAsString); 85 86 runTests(testVals); 87 } 88 89 public void testDisplay() throws Exception { 90 report.read(EXAMPLE_REPORT); 91 92 Formula f = report.findFormula(new Long (1)); 93 assertNotNull(f); 94 95 assertEquals("{jobs.hourly rate}.nil? ? nil : {jobs.hourly rate} / 100.0", 96 f.getEditableExpression()); 97 98 f.setExpression("{?1}"); 99 assertEquals("{?1}", f.getExpression()); 100 assertEquals("{?String Param}", f.getEditableExpression()); 101 102 f.setEditableExpression("{?String Param}"); 103 assertEquals("{?1}", f.getExpression()); 104 assertEquals("{?String Param}", f.getEditableExpression()); 105 106 f.setEditableExpression("{!Short Title}"); 107 assertEquals("{!1}", f.getExpression()); 108 assertEquals("{!Short Title}", f.getEditableExpression()); 109 } 110 111 public void runTests(HashMap testVals) throws IOException { 112 Formula f = new Formula(null, report, "Unnamed Formula"); 113 for (Iterator iter = testVals.keySet().iterator(); iter.hasNext(); ) { 114 String evalStr = (String )iter.next(); 115 Object answer = testVals.get(evalStr); 116 117 f.setExpression(evalStr); 118 Object calculated = f.eval(); 119 assertEquals(answer, calculated); 120 assertEquals(answer.getClass(), calculated.getClass()); 121 } 122 } 123 124 public void testContainsColumns() throws Exception { 125 report.read(EXAMPLE_REPORT); 126 127 UserColumn uc = report.findUserColumn(new Long (1)); 128 assertNotNull(uc); 129 130 Formula f = report.findFormula(new Long (2)); 133 assertNotNull(f); 134 f.setExpression(uc.formulaString()); 135 136 Query q = (Query)report.getDataSource().getQuery(); 140 q.findSelectablesUsed(); 141 assertTrue("user col contained in formula isn't in query", 142 q.indexOfSelectable(uc) >= 0); 143 144 f.setExpression("#{{aggregate_test.col1}}"); 149 Column col = report.findColumn("aggregate_test.col1"); 150 assertNotNull(col); 151 q.findSelectablesUsed(); 152 assertTrue("col contained in Ruby string expression isn't in query", 153 q.indexOfSelectable(col) >= 0); 154 } 155 156 public void testIgnoreNonColumns() { 157 Formula f = new Formula(new Long (0), report, "ignore non-columns"); 158 try { 159 f.setEditableExpression("x = 42"); 160 f.eval(null); 161 162 f.setEditableExpression("x = {}; x[:z] = 42"); 163 f.eval(null); 164 165 f.setEditableExpression("x = { }; x[:z] = 42"); 166 f.eval(null); 167 } 168 catch (Exception e) { 169 fail(e.toString()); 170 } 171 } 172 173 public static void main(String [] args) { 174 junit.textui.TestRunner.run(suite()); 175 System.exit(0); 176 } 177 178 } 179 | Popular Tags |