commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r927895 - in /commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2: ArithmeticTest.java junit/Asserter.java
Date Fri, 26 Mar 2010 15:00:59 GMT
Author: henrib
Date: Fri Mar 26 15:00:58 2010
New Revision: 927895

URL: http://svn.apache.org/viewvc?rev=927895&view=rev
Log:
Added tests about null operands;
Added failure handling ability in Asserter.

Modified:
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ArithmeticTest.java
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/junit/Asserter.java

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ArithmeticTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ArithmeticTest.java?rev=927895&r1=927894&r2=927895&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ArithmeticTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/ArithmeticTest.java Fri
Mar 26 15:00:58 2010
@@ -32,6 +32,55 @@ public class ArithmeticTest extends Jexl
         asserter = new Asserter(JEXL);
     }
 
+    public void testUndefinedVar() throws Exception {
+        asserter.failExpression("objects[1].status", ".* undefined variable objects.*");
+    }
+    
+    public void testLeftNullOperand() throws Exception {
+        asserter.setVariable("left", null);
+        asserter.setVariable("right", 8);
+        asserter.failExpression("left + right", ".*null.*");
+        asserter.failExpression("left - right", ".*null.*");
+        asserter.failExpression("left * right", ".*null.*");
+        asserter.failExpression("left / right", ".*null.*");
+        asserter.failExpression("left % right", ".*null.*");
+        asserter.failExpression("left & right", ".*null.*");
+        asserter.failExpression("left | right", ".*null.*");
+        asserter.failExpression("left ^ right", ".*null.*");
+    }
+
+    public void testRightNullOperand() throws Exception {
+        asserter.setVariable("left", 9);
+        asserter.setVariable("right", null);
+        asserter.failExpression("left + right", ".*null.*");
+        asserter.failExpression("left - right", ".*null.*");
+        asserter.failExpression("left * right", ".*null.*");
+        asserter.failExpression("left / right", ".*null.*");
+        asserter.failExpression("left % right", ".*null.*");
+        asserter.failExpression("left & right", ".*null.*");
+        asserter.failExpression("left | right", ".*null.*");
+        asserter.failExpression("left ^ right", ".*null.*");
+    }
+
+    public void testNullOperands() throws Exception {
+        asserter.setVariable("left", null);
+        asserter.setVariable("right", null);
+        asserter.failExpression("left + right", ".*null.*");
+        asserter.failExpression("left - right", ".*null.*");
+        asserter.failExpression("left * right", ".*null.*");
+        asserter.failExpression("left / right", ".*null.*");
+        asserter.failExpression("left % right", ".*null.*");
+        asserter.failExpression("left & right", ".*null.*");
+        asserter.failExpression("left | right", ".*null.*");
+        asserter.failExpression("left ^ right", ".*null.*");
+    }
+
+    public void testNullOperand() throws Exception {
+        asserter.setVariable("right", null);
+        asserter.failExpression("~right", ".*null.*");
+        asserter.failExpression("-right", ".*number.*");
+    }
+
     public void testBigDecimal() throws Exception {
         asserter.setVariable("left", new BigDecimal(2));
         asserter.setVariable("right", new BigDecimal(6));
@@ -237,12 +286,14 @@ public class ArithmeticTest extends Jexl
                     }
                 }
             }
-            if (!jexl.isLenient())
+            if (!jexl.isLenient()) {
                 assertTrue("All expressions should have thrown " + zthrow + "/" + PERMS,
                         zthrow == PERMS);
-            else
+            }
+            else {
                 assertTrue("All expressions should have zeroed " + zeval + "/" + PERMS,
                         zeval == PERMS);
+            }
         }
         debuggerCheck(jexl);
     }

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/junit/Asserter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/junit/Asserter.java?rev=927895&r1=927894&r2=927895&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/junit/Asserter.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/junit/Asserter.java Fri
Mar 26 15:00:58 2010
@@ -26,6 +26,7 @@ import org.apache.commons.jexl2.Expressi
 import org.apache.commons.jexl2.JexlContext;
 import org.apache.commons.jexl2.MapContext;
 import org.apache.commons.jexl2.JexlEngine;
+import org.apache.commons.jexl2.JexlException;
 
 /**
  * A utility class for performing JUnit based assertions using Jexl
@@ -48,11 +49,27 @@ public class Asserter extends Assert {
     /**
      * 
      * Create an asserter.
+     * @param jexl the JEXL engine to use
      */
     public Asserter(JexlEngine jexl) {
         engine = jexl;
     }
 
+    /**
+     * Retrieves the underlying JEXL engine.
+     * @return the JEXL engine
+     */
+    public JexlEngine getEngine() {
+        return engine;
+    }
+
+    /**
+     * Retrieves the underlying JEXL context.
+     * @return the JEXL context
+     */
+    public JexlContext getContext() {
+        return context;
+    }
 
     /**
      * Performs an assertion that the value of the given Jexl expression 
@@ -71,6 +88,32 @@ public class Asserter extends Assert {
     }
 
     /**
+     * Performs an assertion that the expression fails throwing an exception.
+     * If matchException is not null, the exception message is expected to match it as a
regexp.
+     * The engine is temporarily switched to strict * verbose to maximize error detection
abilities.
+     * @param expression the expression that should fail
+     * @param matchException the exception message pattern
+     * @throws Exception if the expression did not fail or the exception did not match the
expected pattern
+     */
+    public void failExpression(String expression, String matchException) throws Exception
{
+        boolean[] flags = { engine.isLenient(), engine.isSilent() };
+        try {
+            engine.setLenient(false);
+            engine.setSilent(false);
+            Expression exp = engine.createExpression(expression);
+            Object value = exp.evaluate(context);
+            fail("expression: " + expression);
+        } catch(JexlException xjexl) {
+            if (matchException != null && !xjexl.getMessage().matches(matchException))
{
+                fail("expression: " + expression + ", expected: " + matchException + ", got
" + xjexl.getMessage());
+            }
+        } finally {
+            engine.setLenient(flags[0]);
+            engine.setSilent(flags[1]);
+        }
+    }
+
+    /**
      * Puts a variable of a certain name in the context so that it can be used from
      * assertion expressions.
      * 
@@ -81,4 +124,13 @@ public class Asserter extends Assert {
         variables.put(name, value);
     }
 
+    /**
+     * Removes a variable of a certain name from the context.
+     * @param name variable name
+     * @return variable value
+     */
+    public Object removeVariable(String name) {
+        return variables.remove(name);
+    }
+
 }



Mime
View raw message