InterpreterTest.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.java110.service;
  2. import bsh.Interpreter;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.java110.service.smo.impl.QueryServiceSMOImpl;
  5. import junit.framework.Test;
  6. import junit.framework.TestCase;
  7. import junit.framework.TestSuite;
  8. import org.apache.ibatis.ognl.OgnlException;
  9. import org.dom4j.DocumentException;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * Created by wuxw on 2018/4/23.
  14. */
  15. public class InterpreterTest extends TestCase {
  16. /**
  17. * Create the test case
  18. *
  19. * @param testName name of the test case
  20. */
  21. public InterpreterTest( String testName )
  22. {
  23. super( testName );
  24. }
  25. /**
  26. * @return the suite of tests being tested
  27. */
  28. public static Test suite()
  29. {
  30. return new TestSuite( InterpreterTest.class );
  31. }
  32. /**
  33. * Rigourous Test :-)
  34. */
  35. public void testInterpreter() throws Exception
  36. {
  37. String javaCode = "public int add(int a, int b){" +
  38. "return a+b;" +
  39. "}" +
  40. "" +
  41. "public int execute(int a,int b){" +
  42. "return add(a,b);" +
  43. "}";
  44. Interpreter interpreter = new Interpreter();
  45. interpreter.eval(javaCode);
  46. String param = "9,4";
  47. System.out.println(interpreter.eval("execute("+param+")").toString());
  48. }
  49. public void testDealSqlIf() throws OgnlException, DocumentException {
  50. String oldSql = "select * from s_a a\n" +
  51. " where <if test=\"name != null and name != ''\">\n" +
  52. " a.name = #name#\n" +
  53. " </if>" +
  54. " and a.sex = #name#" +
  55. " <if test=\"id != null and id!= ''\"> and a.id = #id# </if>";
  56. QueryServiceSMOImpl queryServiceSMO = new QueryServiceSMOImpl();
  57. JSONObject params = new JSONObject();
  58. params.put("id","123213");
  59. params.put("name","123213");
  60. System.out.println((queryServiceSMO.dealSqlIf(oldSql, params)));
  61. }
  62. }