用jakarta commons JEXL在JAVA中运算表达式

java 代码
 
  1.  1import org.apache.commons.jexl.Expression;    
  2.  2import org.apache.commons.jexl.ExpressionFactory;    
  3.  3import org.apache.commons.jexl.JexlContext;    
  4.  4import org.apache.commons.jexl.JexlHelper;    
  5.  5.     
  6.  6import junit.framework.TestCase;    
  7.  7.     
  8.  8public class JexlTest extends TestCase {    
  9.  9.     public void test() throws Exception {    
  10. 10.     
  11. 11.         //Create an expression object    
  12. 12.         String jexlExp = "9*(1+2)";    
  13. 13.         Expression e = ExpressionFactory.createExpression(jexlExp);    
  14. 14.     
  15. 15.         // Create a context    
  16. 16.         JexlContext jc = JexlHelper.createContext();    
  17. 17.     
  18. 18.         // Now evaluate the expression, getting the result    
  19. 19.         Object result = e.evaluate(jc);    
  20. 20.     
  21. 21.         System.out.println("result:" + result);    
  22. 22.         System.out.println("result type:" + result.getClass().getName());    
  23. 23.     
  24. 24.         assertEquals(((Long) result).intValue(), 27);    
  25. 25.     }    
  26. 26. }    

你可能感兴趣的:(java,apache,JUnit)