Skip to content

Commit 76e7843

Browse files
authored
Merge pull request #23 from ming300/master
增加了几种常用表达式的例子测试和相关文档
2 parents 59ea42e + 45a6a71 commit 76e7843

File tree

4 files changed

+237
-28
lines changed

4 files changed

+237
-28
lines changed

doc/script.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@
6565

6666
脚本性能测试代码:com.test.script.ScriptTest,通过这个测试发现OGNL的性能最优。
6767

68-
已经实现了SpringEL、OGNL、JavaScript三种表达式的支持。
68+
已经实现了SpringEL、OGNL、JavaScript三种表达式的支持。
69+
70+
71+
### 几种常用表达式的例子
72+
73+
parse 解析器| 取retVal中的值(Map类型) |取retVal中的值(javaBean类型) | 使用hash函数 | 使用 empty 判断 |
74+
------------ | ------------- |------------- |------------- |-------------
75+
spring | `#retVal.get('rid')` |`#retVal.rid` | `#hash(#args)` | `#empty(#args[0])` |
76+
javascript | `retVal.get('rid')` | `retVal.rid` | `hash(args)` | `empty(args[0])` |
77+
ognl|`#retVal.rid` | `#retVal.rid` | `@@hash(#args)` | `@@empty(#args[0])` |

src/test/java/com/test/script/JavaScriptTest.java

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,114 @@
22

33
import junit.framework.TestCase;
44

5+
import java.util.HashMap;
6+
import java.util.Map;
7+
58
import com.jarvis.cache.script.AbstractScriptParser;
69
import com.jarvis.cache.script.JavaScriptParser;
10+
import com.jarvis.cache.script.OgnlParser;
11+
import com.test.Simple;
12+
13+
public class JavaScriptTest
14+
extends
15+
TestCase {
716

8-
public class JavaScriptTest extends TestCase {
17+
AbstractScriptParser scriptParser = new JavaScriptParser();
918

1019
public void testJavaScript() throws Exception {
11-
String javaVersion=System.getProperty("java.version");
20+
String javaVersion = System.getProperty("java.version");
1221
System.out.println(javaVersion);
13-
int ind=0;
14-
for(int i=0; i < 2; i++) {
15-
ind=javaVersion.indexOf(".", ind);
22+
int ind = 0;
23+
for (int i = 0; i < 2; i++) {
24+
ind = javaVersion.indexOf(".", ind);
1625
ind++;
1726
}
18-
javaVersion=javaVersion.substring(0, ind);
19-
javaVersion=javaVersion.replaceAll("\\.", "");
27+
javaVersion = javaVersion.substring(0, ind);
28+
javaVersion = javaVersion.replaceAll("\\.", "");
2029
System.out.println(Integer.parseInt(javaVersion));
2130

22-
String keySpEL="'test_'+args[0]+'_'+args[1]";
23-
Object[] arguments=new Object[]{"1111", "2222"};
24-
AbstractScriptParser scriptParser=new JavaScriptParser();
25-
String res=scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
31+
String keySpEL = "'test_'+args[0]+'_'+args[1]";
32+
Object[] arguments = new Object[]{"1111", "2222"};
33+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
2634
System.out.println(res);
2735
// 自定义函数使用
28-
Boolean rv=scriptParser.getElValue("empty(args[0])", arguments, Boolean.class);
36+
Boolean rv = scriptParser.getElValue("empty(args[0])", arguments, Boolean.class);
2937
assertFalse(rv);
3038
}
3139

40+
public void testJavaScript2() throws Exception {
41+
42+
String keySpEL = "'test_'+args[0]+'_'+args[1]";
43+
44+
Simple simple = new Simple();
45+
simple.setAge(18);
46+
simple.setName("刘德华");
47+
simple.setSex(0);
48+
Object[] arguments = new Object[]{"1111", "2222", simple};
49+
50+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
51+
System.out.println(res);
52+
assertEquals("test_1111_2222", res);
53+
// 自定义函数使用
54+
Boolean rv = scriptParser.getElValue("empty(args[0])", arguments, Boolean.class);
55+
assertFalse(rv);
56+
57+
String val = null;
58+
val = scriptParser.getElValue("hash(args[0])", arguments, String.class);
59+
System.out.println(val);
60+
assertEquals("1111", val);
61+
62+
val = scriptParser.getElValue("hash(args[1])", arguments, String.class);
63+
System.out.println(val);
64+
assertEquals("2222", val);
65+
66+
val = scriptParser.getElValue("hash(args[2])", arguments, String.class);
67+
System.out.println(val);
68+
assertEquals("-290203482_-550943035_-57743508_-1052004462", val);
69+
70+
val = scriptParser.getElValue("hash(args)", arguments, String.class);
71+
System.out.println(val);
72+
assertEquals("322960956_-1607969343_673194431_1921252123", val);
73+
}
74+
75+
/**
76+
*
77+
*
78+
* @throws Exception
79+
*/
80+
public void testReturnIsMapWithHfield() throws Exception {
81+
82+
String keySpEL = " (retVal['rid'])";
83+
84+
85+
keySpEL="typeof(retVal);" ;//object
86+
keySpEL = "(typeof retVal['rid'])";//undefined
87+
keySpEL = "typeof retVal.rid";//undefined
88+
keySpEL = "retVal.get('rid')";//undefined
89+
90+
Object[] arguments = new Object[]{"1111", "2222"};
91+
Map returnObj = new HashMap();
92+
returnObj.put("rid", "iamrid");
93+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, returnObj, true);
94+
System.out.println(res);
95+
96+
assertEquals("iamrid", res);
97+
98+
99+
Simple simple = new Simple();
100+
simple.setAge(18);
101+
simple.setName("刘德华");
102+
simple.setSex(0);
103+
keySpEL = "retVal.name";
104+
105+
res = scriptParser.getDefinedCacheKey(keySpEL, arguments, simple, true);
106+
System.out.println(res);
107+
assertEquals("刘德华", res);
108+
109+
110+
111+
// 自定义函数使用
112+
Boolean rv = scriptParser.getElValue("empty(args[0])", arguments, Boolean.class);
113+
assertFalse(rv);
114+
}
32115
}
Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,76 @@
11
package com.test.script;
22

3-
import junit.framework.TestCase;
3+
import java.util.HashMap;
4+
import java.util.Map;
45

56
import com.jarvis.cache.script.AbstractScriptParser;
67
import com.jarvis.cache.script.OgnlParser;
8+
import com.test.Simple;
9+
10+
import junit.framework.TestCase;
711

8-
public class OgnlTest extends TestCase {
12+
public class OgnlTest
13+
extends
14+
TestCase {
15+
AbstractScriptParser scriptParser = new OgnlParser();
916

1017
public void testJavaScript() throws Exception {
1118

12-
String keySpEL="'test_'+#args[0]+'_'+#args[1]";
13-
Object[] arguments=new Object[]{"1111", "2222"};
14-
AbstractScriptParser scriptParser=new OgnlParser();
15-
String res=scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
19+
String keySpEL = "'test_'+#args[0]+'_'+#args[1]";
20+
21+
Simple simple = new Simple();
22+
simple.setAge(18);
23+
simple.setName("刘德华");
24+
simple.setSex(0);
25+
Object[] arguments = new Object[]{"1111", "2222", simple};
26+
27+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
1628
System.out.println(res);
29+
assertEquals("test_1111_2222", res);
1730
// 自定义函数使用
18-
Boolean rv=scriptParser.getElValue("@@empty(#args[0])", arguments, Boolean.class);
31+
Boolean rv = scriptParser.getElValue("@@empty(#args[0])", arguments, Boolean.class);
1932
assertFalse(rv);
33+
34+
String val = null;
35+
val = scriptParser.getElValue("@@hash(#args[0])", arguments, String.class);
36+
System.out.println(val);
37+
assertEquals("1111", val);
38+
39+
val = scriptParser.getElValue("@@hash(#args[1])", arguments, String.class);
40+
System.out.println(val);
41+
assertEquals("2222", val);
42+
43+
val = scriptParser.getElValue("@@hash(#args[2])", arguments, String.class);
44+
System.out.println(val);
45+
assertEquals("-290203482_-550943035_-57743508_-1052004462", val);
46+
47+
val = scriptParser.getElValue("@@hash(#args)", arguments, String.class);
48+
System.out.println(val);
49+
assertEquals("322960956_-1607969343_673194431_1921252123", val);
2050
}
2151

52+
public void testReturnIsMapWithHfield() throws Exception {
53+
54+
String keySpEL = "#retVal.rid";
55+
Object[] arguments = new Object[]{"1111", "2222"};
56+
Map returnObj = new HashMap();
57+
returnObj.put("rid", "iamrid");
58+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, returnObj, true);
59+
System.out.println(res);
60+
assertEquals("iamrid", res);
61+
62+
Simple simple = new Simple();
63+
simple.setAge(18);
64+
simple.setName("刘德华");
65+
simple.setSex(0);
66+
keySpEL = "#retVal.name";
67+
68+
res = scriptParser.getDefinedCacheKey(keySpEL, arguments, simple, true);
69+
System.out.println(res);
70+
assertEquals("刘德华", res);
71+
72+
// 自定义函数使用
73+
Boolean rv = scriptParser.getElValue("@@empty(#args[0])", arguments, Boolean.class);
74+
assertFalse(rv);
75+
}
2276
}
Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,81 @@
11
package com.test.script;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
import com.jarvis.cache.script.AbstractScriptParser;
7+
import com.jarvis.cache.script.OgnlParser;
48
import com.jarvis.cache.script.SpringELParser;
9+
import com.test.Simple;
10+
11+
import junit.framework.TestCase;
12+
13+
public class SpELTest
14+
extends
15+
TestCase {
16+
AbstractScriptParser scriptParser = new SpringELParser();
17+
18+
public void testJavaScript() throws Exception {
519

6-
public class SpELTest {
20+
String keySpEL = "'test_'+#args[0]+'_'+#args[1]";
721

8-
public static void main(String[] args) throws Exception {
9-
String keySpEL="test";
10-
Object[] arguments=new Object[]{"1111", "2222"};
11-
AbstractScriptParser scriptParser=new SpringELParser();
12-
String res=scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
22+
Simple simple = new Simple();
23+
simple.setAge(18);
24+
simple.setName("刘德华");
25+
simple.setSex(0);
26+
Object[] arguments = new Object[]{"1111", "2222", simple};
27+
28+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, null, false);
1329
System.out.println(res);
14-
Boolean rv=scriptParser.getElValue("#empty(#args)", arguments, Boolean.class);
15-
System.out.println(rv);
30+
assertEquals("test_1111_2222", res);
31+
// 自定义函数使用
32+
Boolean rv = scriptParser.getElValue("#empty(#args[0])", arguments, Boolean.class);
33+
assertFalse(rv);
34+
35+
String val = null;
36+
val = scriptParser.getElValue("#hash(#args[0])", arguments, String.class);
37+
System.out.println(val);
38+
assertEquals("1111", val);
39+
40+
val = scriptParser.getElValue("#hash(#args[1])", arguments, String.class);
41+
System.out.println(val);
42+
assertEquals("2222", val);
43+
44+
val = scriptParser.getElValue("#hash(#args[2])", arguments, String.class);
45+
System.out.println(val);
46+
assertEquals("-290203482_-550943035_-57743508_-1052004462", val);
47+
48+
val = scriptParser.getElValue("#hash(#args)", arguments, String.class);
49+
System.out.println(val);
50+
assertEquals("322960956_-1607969343_673194431_1921252123", val);
1651
}
1752

53+
public void testReturnIsMapWithHfield() throws Exception {
54+
55+
String keySpEL = "#retVal.get('rid')";
56+
Object[] arguments = new Object[]{"1111", "2222"};
57+
Map returnObj = new HashMap();
58+
returnObj.put("rid", "iamrid");
59+
String res = scriptParser.getDefinedCacheKey(keySpEL, arguments, returnObj, true);
60+
System.out.println(res);
61+
62+
63+
assertEquals("iamrid", res);
64+
65+
66+
Simple simple = new Simple();
67+
simple.setAge(18);
68+
simple.setName("刘德华");
69+
simple.setSex(0);
70+
keySpEL = "#retVal.name";
71+
72+
res = scriptParser.getDefinedCacheKey(keySpEL, arguments, simple, true);
73+
System.out.println(res);
74+
assertEquals("刘德华", res);
75+
76+
77+
// 自定义函数使用
78+
Boolean rv = scriptParser.getElValue("#empty(#args[0])", arguments, Boolean.class);
79+
assertFalse(rv);
80+
}
1881
}

0 commit comments

Comments
 (0)