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 / TestCreateFunction.java @ 44710
History | View | Annotate | Download (4.13 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import java.net.URL; |
4 |
import junit.framework.TestCase; |
5 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
6 |
import org.gvsig.expressionevaluator.Compiler; |
7 |
import org.gvsig.expressionevaluator.SymbolTable; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.Code; |
10 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
12 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
13 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
14 |
import org.gvsig.tools.resourcesstorage.AbstractResourcesStorage; |
15 |
import org.gvsig.tools.resourcesstorage.URLResource; |
16 |
|
17 |
public class TestCreateFunction extends TestCase { |
18 |
|
19 |
private static class Test1ResourcesStorage extends AbstractResourcesStorage { |
20 |
|
21 |
public Test1ResourcesStorage() {
|
22 |
|
23 |
} |
24 |
|
25 |
@Override
|
26 |
public Resource getResource(String resourceName) { |
27 |
URL url = this.getClass().getResource("/org/gvsig/expressionevaluator/impl/"+resourceName+".cosa"); |
28 |
if( url == null ) { |
29 |
return null; |
30 |
} |
31 |
Resource res = new URLResource(resourceName+".cosa", url); |
32 |
return res;
|
33 |
} |
34 |
} |
35 |
|
36 |
public TestCreateFunction(String testName) { |
37 |
super(testName);
|
38 |
} |
39 |
|
40 |
@Override
|
41 |
protected void setUp() throws Exception { |
42 |
super.setUp();
|
43 |
new DefaultLibrariesInitializer().fullInitialize();
|
44 |
} |
45 |
|
46 |
@Override
|
47 |
protected void tearDown() throws Exception { |
48 |
super.tearDown();
|
49 |
} |
50 |
|
51 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
52 |
// public void testHello() {}
|
53 |
|
54 |
|
55 |
protected LexicalAnalyzer createLexicalAnalyzer() {
|
56 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
57 |
LexicalAnalyzer lexer = manager.createLexicalAnalyzer(); |
58 |
return lexer;
|
59 |
} |
60 |
|
61 |
protected org.gvsig.expressionevaluator.Compiler createCompiler() {
|
62 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
63 |
Compiler compiler = manager.createCompiler();
|
64 |
compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
65 |
return compiler;
|
66 |
} |
67 |
|
68 |
|
69 |
protected SymbolTable createSymbolTable() {
|
70 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
71 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
72 |
symbolTable.setVar("precio", 200); |
73 |
symbolTable.setVar("1990", 0.168873933773767); |
74 |
return symbolTable;
|
75 |
} |
76 |
|
77 |
protected Interpreter createInterpreter(SymbolTable symbolTable) {
|
78 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
79 |
manager.setScriptsResourcesStorage(new Test1ResourcesStorage());
|
80 |
Interpreter interpreter = manager.createInterpreter(); |
81 |
interpreter.setSymbolTable(symbolTable); |
82 |
return interpreter;
|
83 |
} |
84 |
|
85 |
public void testStringUtilsCapitalize() { |
86 |
SymbolTable symbolTable = createSymbolTable(); |
87 |
Compiler compiler = createCompiler();
|
88 |
Interpreter interpreter = createInterpreter(symbolTable); |
89 |
|
90 |
String source = "BEGIN " |
91 |
+ "CREATE FUNCTION CAPITALIZE(s) AS "
|
92 |
+ "'org.apache.commons.lang3.StringUtils', 'capitalize' "
|
93 |
+ "LANGUAGE 'java';"
|
94 |
+ "CAPITALIZE('hola');"
|
95 |
+ "END";
|
96 |
|
97 |
Code code = compiler.compileExpression(source); |
98 |
Object v = interpreter.run(code);
|
99 |
assertEquals("Hola", v.toString());
|
100 |
} |
101 |
|
102 |
public void testSQRTScriptEngine() { |
103 |
SymbolTable symbolTable = createSymbolTable(); |
104 |
Compiler compiler = createCompiler();
|
105 |
Interpreter interpreter = createInterpreter(symbolTable); |
106 |
|
107 |
String source = "BEGIN " |
108 |
+ "CREATE FUNCTION SQUARE(num) AS 'testlib', 'SQUARE';"
|
109 |
+ "SQUARE(4);"
|
110 |
+ "END";
|
111 |
|
112 |
Code code = compiler.compileExpression(source); |
113 |
Object v = interpreter.run(code);
|
114 |
assertEquals(16, ((Number)v).intValue()); |
115 |
} |
116 |
|
117 |
|
118 |
|
119 |
} |