svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / test / java / org / gvsig / expresionevaluator / impl / TestExpression.java @ 44619
History | View | Annotate | Download (3.75 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import junit.framework.TestCase; |
4 |
import org.apache.commons.lang3.Range; |
5 |
import org.gvsig.expressionevaluator.Expression; |
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
7 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
10 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
11 |
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable; |
12 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
13 |
|
14 |
public class TestExpression extends TestCase { |
15 |
|
16 |
private static class MyValueSymbolTable extends AbstractSymbolTable { |
17 |
|
18 |
private class MyValueFunction extends AbstractFunction { |
19 |
|
20 |
public MyValueFunction() {
|
21 |
super("Numeric", "MyValue", Range.is(0)); |
22 |
} |
23 |
|
24 |
@Override
|
25 |
public Object call(Interpreter interpreter, Object[] args) { |
26 |
return value;
|
27 |
} |
28 |
|
29 |
@Override
|
30 |
public boolean allowConstantFolding() { |
31 |
return false; |
32 |
} |
33 |
} |
34 |
private int value; |
35 |
|
36 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
37 |
public MyValueSymbolTable(String name) { |
38 |
super(name);
|
39 |
this.addFunction(new MyValueFunction()); |
40 |
} |
41 |
|
42 |
public void setValue(int value) { |
43 |
this.value = value;
|
44 |
} |
45 |
} |
46 |
|
47 |
public TestExpression(String testName) { |
48 |
super(testName);
|
49 |
} |
50 |
|
51 |
@Override
|
52 |
protected void setUp() throws Exception { |
53 |
super.setUp();
|
54 |
new DefaultLibrariesInitializer().fullInitialize();
|
55 |
} |
56 |
|
57 |
@Override
|
58 |
protected void tearDown() throws Exception { |
59 |
super.tearDown();
|
60 |
} |
61 |
|
62 |
private MutableSymbolTable createMutableSymbolTable() {
|
63 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
64 |
MutableSymbolTable t = manager.createSymbolTable(); |
65 |
return t;
|
66 |
} |
67 |
|
68 |
private Expression createExpression() { |
69 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
70 |
Expression e = manager.createExpression();
|
71 |
return e;
|
72 |
} |
73 |
|
74 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
75 |
// public void testHello() {}
|
76 |
|
77 |
public void testToJSON1() { |
78 |
String source = "precio + 10"; |
79 |
|
80 |
Expression expression = createExpression();
|
81 |
expression.setPhrase(source); |
82 |
|
83 |
assertEquals( |
84 |
"{\"phrase\":\"precio + 10\"}",
|
85 |
expression.toJSON() |
86 |
); |
87 |
} |
88 |
|
89 |
public void testA() { |
90 |
|
91 |
Expression expression = createExpression();
|
92 |
expression.setPhrase("precio * 10");
|
93 |
|
94 |
MutableSymbolTable t1 = createMutableSymbolTable(); |
95 |
t1.setVar("precio", 2); |
96 |
Object v1 = expression.execute(t1);
|
97 |
assertEquals(20, (int) v1); |
98 |
|
99 |
t1.setVar("precio", 3); |
100 |
v1 = expression.execute(t1); |
101 |
assertEquals(30, (int) v1); |
102 |
|
103 |
MutableSymbolTable t2 = createMutableSymbolTable(); |
104 |
t2.setVar("precio", 4); |
105 |
v1 = expression.execute(t2); |
106 |
assertEquals(40, (int) v1); |
107 |
|
108 |
} |
109 |
|
110 |
public void testB() { |
111 |
|
112 |
Expression expression = createExpression();
|
113 |
expression.setPhrase("MYVALUE()");
|
114 |
|
115 |
MyValueSymbolTable t1 = new MyValueSymbolTable("MyValueSymbolTable1"); |
116 |
t1.setValue(2);
|
117 |
Object v1 = expression.execute(t1);
|
118 |
assertEquals(2, (int) v1); |
119 |
|
120 |
t1.setValue(3);
|
121 |
v1 = expression.execute(t1); |
122 |
assertEquals(3, (int) v1); |
123 |
|
124 |
MyValueSymbolTable t2 = new MyValueSymbolTable("MyValueSymbolTable2"); |
125 |
expression.link(t2); |
126 |
t2.setValue(4);
|
127 |
v1 = expression.execute(t2); |
128 |
assertEquals(4, (int) v1); |
129 |
|
130 |
} |
131 |
|
132 |
|
133 |
} |