1 /***
2 * AvgOperator.java
3 *
4 * $Author: mballesteros $
5 * $Date: 2003/11/28 19:18:03 $
6 * $Revision: 1.1 $
7 */
8 package net.sf.jec;
9
10 import java.util.*;
11 import junit.framework.*;
12
13 /***
14 *
15 * @author mballesteros
16 */
17 public class ExpressionEvalTest extends TestCase {
18
19 public class MyClass {
20 private Map viewData = new HashMap();
21 public Map get_view() {
22 return this.viewData;
23 }
24 }
25
26 public ExpressionEvalTest(java.lang.String testName) {
27 super(testName);
28 }
29
30 public static void main(java.lang.String[] args) {
31 junit.textui.TestRunner.run(suite());
32 }
33
34 public static Test suite() {
35 TestSuite suite = new TestSuite(ExpressionEvalTest.class);
36 return suite;
37 }
38
39 //-------------------------------------------------------------------------
40 // Prueba de operadores
41 //-------------------------------------------------------------------------
42 private HashMap ctx;
43 private Book book;
44 private ExpressionEval eval;
45 Operator op;
46
47 public void setUp() throws Exception {
48 //Operator.showFunctionString = true;
49 book = Book.getExample();
50 ctx = new HashMap();
51 ctx.put("book", book);
52 ctx.put("intIndex", "0");
53 ctx.put("strIndex", "1");
54
55 eval = new ExpressionEval(false);
56 }
57
58 public void testAccessorExpression() throws Exception {
59 // Probamos que obtenemos el libro que hemos almacenado en la HashMap
60 assertEquals(eval.get(ctx, "book"), book);
61
62 // Probamos que obtenermos las páginas del libro...
63 assertEquals(eval.get(ctx, "book.pages"), book.getPages());
64 }
65
66 public void testIndexerExpression() throws Exception {
67 // Probamos que obtenemos la primera página del libro
68 assertEquals(eval.get(ctx, "book.pages[0]"), book.getPages().get(0));
69
70 // Probamos que podemos anidar más allá del operador de indexación
71 assertEquals(
72 eval.get(ctx, "book.pages[0].text"),
73 ((Page) book.getPages().get(0)).getText());
74 }
75
76 public void testBooleanExpression() throws Exception {
77 // Probando '=='
78 assertEquals(eval.get(ctx, "10==10"), Boolean.TRUE);
79 assertEquals(eval.get(ctx, "10==1"), Boolean.FALSE);
80
81 // Probando '!='
82 assertEquals(eval.get(ctx, "10!=1"), Boolean.TRUE);
83 assertEquals(eval.get(ctx, "10!=10"), Boolean.FALSE);
84
85 // Probando '<'
86 assertEquals(eval.get(ctx, "1<10"), Boolean.TRUE);
87 assertEquals(eval.get(ctx, "10<10"), Boolean.FALSE);
88 assertEquals(eval.get(ctx, "'aaa'<'aaaa'"), Boolean.TRUE);
89 assertEquals(eval.get(ctx, "'aaab'<'aaaa'"), Boolean.FALSE);
90
91 // Probando '>'
92 assertEquals(eval.get(ctx, "10>1"), Boolean.TRUE);
93 assertEquals(eval.get(ctx, "10>10"), Boolean.FALSE);
94 }
95
96 public void testArithmeticExpression() throws Exception {
97 assertEquals(new Double(50 + 25), eval.get(ctx, "50+25"));
98 assertEquals(new Double(50 + 25 * 2), eval.get(ctx, "50+25*2"));
99 assertEquals(new Double((50 + 25) * 2), eval.get(ctx, "(50+25)*2"));
100 assertEquals(
101 new Double((50 + 25 * 2) / 100),
102 eval.get(ctx, "(50+25*2)/100"));
103 assertEquals(Boolean.TRUE, eval.get(ctx, "(50+25*2)/100==1"));
104 }
105
106 public void testAssignExpression() throws Exception {
107 assertEquals(
108 ((Paragraph) ((Page) book.getPages().get(0))
109 .getParagraphs()
110 .get(0))
111 .getText(),
112 eval.get(ctx, "book.pages[0].paragraphs[0].text"));
113
114 String testText = "Test String";
115 assertEquals(
116 Boolean.TRUE,
117 eval.get(
118 ctx,
119 "book.pages[0].paragraphs[0].text='" + testText + "'"));
120 assertEquals(
121 testText,
122 ((Paragraph) ((Page) book.getPages().get(0))
123 .getParagraphs()
124 .get(0))
125 .getText());
126
127 eval.set(ctx, "book.pages[0].paragraphs[0].text", testText);
128 assertEquals(
129 testText,
130 eval.get(ctx, "book.pages[0].paragraphs[0].text"));
131 }
132
133 /*
134 public void testEvaluationTime() throws Exception {
135 String testText = "Test String";
136
137 long before = System.currentTimeMillis();
138 for (long i = 0; i < 1000000; i++) {
139 eval.get(ctx, "book.pages[0].paragraphs[0].text");
140 eval.get(
141 ctx,
142 "book.pages[0].paragraphs[0].text='" + testText + "'");
143 }
144 long after = System.currentTimeMillis();
145 long elapse = after - before;
146 System.out.println("Time: " + elapse);
147
148 }*/
149
150 public void testMapAccess() throws Exception {
151 MyClass ctx = new MyClass();
152 String expr = "_view._active_tab";
153 String value = "2";
154
155 assertNull(eval.get(ctx, expr));
156 eval.set(ctx, expr, value);
157 assertEquals(value, eval.get(ctx, expr));
158 }
159 }
This page was automatically generated by Maven