|
2 | 2 |
|
3 | 3 | import junit.framework.TestCase; |
4 | 4 |
|
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
5 | 8 | import com.jarvis.cache.script.AbstractScriptParser; |
6 | 9 | 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 { |
7 | 16 |
|
8 | | -public class JavaScriptTest extends TestCase { |
| 17 | + AbstractScriptParser scriptParser = new JavaScriptParser(); |
9 | 18 |
|
10 | 19 | public void testJavaScript() throws Exception { |
11 | | - String javaVersion=System.getProperty("java.version"); |
| 20 | + String javaVersion = System.getProperty("java.version"); |
12 | 21 | 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); |
16 | 25 | ind++; |
17 | 26 | } |
18 | | - javaVersion=javaVersion.substring(0, ind); |
19 | | - javaVersion=javaVersion.replaceAll("\\.", ""); |
| 27 | + javaVersion = javaVersion.substring(0, ind); |
| 28 | + javaVersion = javaVersion.replaceAll("\\.", ""); |
20 | 29 | System.out.println(Integer.parseInt(javaVersion)); |
21 | 30 |
|
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); |
26 | 34 | System.out.println(res); |
27 | 35 | // 自定义函数使用 |
28 | | - Boolean rv=scriptParser.getElValue("empty(args[0])", arguments, Boolean.class); |
| 36 | + Boolean rv = scriptParser.getElValue("empty(args[0])", arguments, Boolean.class); |
29 | 37 | assertFalse(rv); |
30 | 38 | } |
31 | 39 |
|
| 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 | + } |
32 | 115 | } |
0 commit comments