Author: henrib
Date: Sat Aug 29 17:33:23 2009
New Revision: 809169

URL: http://svn.apache.org/viewvc?rev=809169&view=rev
Log:
Modified tests to check the Debugger on tearDown.
Main change in JexlTestCase in the form of methods that check the equality of 
JexlNode.

Modified:
    
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/CacheTest.java
    
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java
    
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTest.java
    
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTestCase.java
    
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MethodTest.java
    
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/UnifiedJEXLTest.java

Modified: 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/CacheTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/CacheTest.java?rev=809169&r1=809168&r2=809169&view=diff
==============================================================================
--- 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/CacheTest.java
 (original)
+++ 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/CacheTest.java
 Sat Aug 29 17:33:23 2009
@@ -34,6 +34,7 @@
     static JexlEngine jexl = new JexlEngine();
 
     static {
+        jexl.setCache(512);
         jexl.setLenient(false);
         jexl.setSilent(false);
     }
@@ -44,6 +45,11 @@
         3, 3, 3, 4, 4, 4, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 2, 2, 3, 3, 0
     };
 
+    @Override
+    protected void tearDown() throws Exception {
+        debuggerCheck(jexl);
+    }
+    
     public static class Cached {
         public String compute(String arg) {
             if (arg == null) {

Modified: 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java?rev=809169&r1=809168&r2=809169&view=diff
==============================================================================
--- 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java
 (original)
+++ 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/IfTest.java
 Sat Aug 29 17:33:23 2009
@@ -135,7 +135,7 @@
      */
     public void testIfWithDecimalArithmeticExpression() throws Exception {
         Expression e = JEXL
-                .createExpression("if ((x * 2) == 5) true;");
+                .createExpression("if ((x * 2) == 5) true");
         JexlContext jc = JexlHelper.createContext();
         jc.getVars().put("x", new Float(2.5f));
 
@@ -150,7 +150,7 @@
      */
     public void testIfWithAssignment() throws Exception {
         Expression e = JEXL
-                .createExpression("if ((x * 2) == 5) {y = 1;} else {y = 2;}");
+                .createExpression("if ((x * 2) == 5) {y = 1} else {y = 2;}");
         JexlContext jc = JexlHelper.createContext();
         jc.getVars().put("x", new Float(2.5f));
 
@@ -166,6 +166,7 @@
      */
     public void testTernary() throws Exception {
         JexlEngine jexl = new JexlEngine();
+        jexl.setCache(64);
         JexlContext jc = JexlHelper.createContext();
         Expression e = jexl.createExpression("x.y.z = foo ?'bar':'quux'");
         Object o;
@@ -213,6 +214,8 @@
             o = jc.getVars().get("x.y.z");
             assertEquals("Should be bar", "bar", o);
         }
+
+        debuggerCheck(jexl);
     }
 
     /**
@@ -222,6 +225,7 @@
      */
     public void testTernaryShorthand() throws Exception {
         JexlEngine jexl = new JexlEngine();
+        jexl.setCache(64);
         JexlContext jc = JexlHelper.createContext();
         Expression e = JEXL.createExpression("x.y.z = foo?:'quux'");
         Object o;
@@ -269,5 +273,7 @@
             o = jc.getVars().get("x.y.z");
             assertEquals("Should be bar", "bar", o);
         }
+
+        debuggerCheck(jexl);
     }
 }

Modified: 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTest.java?rev=809169&r1=809168&r2=809169&view=diff
==============================================================================
--- 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTest.java
 (original)
+++ 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTest.java
 Sat Aug 29 17:33:23 2009
@@ -777,7 +777,7 @@
 
     @SuppressWarnings("boxing")
     public void testDuck() throws Exception {
-        JexlEngine jexl = new JexlEngine();
+        JexlEngine jexl = JEXL;
         JexlContext jc = JexlHelper.createContext();
         jc.getVars().put("duck", new Duck());
         Expression expr;
@@ -804,7 +804,7 @@
     @SuppressWarnings("boxing")
     public void testArray() throws Exception {
         int[] array = { 100, 101 , 102 };
-        JexlEngine jexl = new JexlEngine();
+        JexlEngine jexl = JEXL;
         JexlContext jc = JexlHelper.createContext();
         jc.getVars().put("array", array);
         Expression expr;

Modified: 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTestCase.java?rev=809169&r1=809168&r2=809169&view=diff
==============================================================================
--- 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTestCase.java
 (original)
+++ 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/JexlTestCase.java
 Sat Aug 29 17:33:23 2009
@@ -16,7 +16,16 @@
  */
 
 package org.apache.commons.jexl;
+
 import java.lang.reflect.Method;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.commons.jexl.parser.JexlNode;
+import org.apache.commons.jexl.parser.ParseException;
+
 import junit.framework.TestCase;
 /**
  * Implements a runTest method to dynamically invoke a test,
@@ -32,9 +41,119 @@
 
     public JexlTestCase(String name) {
         super(name);
+        JEXL.setCache(512);
     }
     public JexlTestCase() {
         super();
+        JEXL.setCache(512);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        debuggerCheck(JEXL);
+    }
+    
+    /**
+     * Will force testing the debugger for each derived test class by
+     * recreating each expression from the JexlNode in the JexlEngine cache &
+     * testing them for equality with the origin.
+     * @throws Exception
+     */
+    public static void debuggerCheck(JexlEngine jexl) throws Exception {
+        // without a cache, nothing to check
+        if (jexl.cache == null) {
+            return;
+        }
+        JexlEngine jdbg = new JexlEngine();
+        Debugger dbg = new Debugger();
+        // iterate over all expression in cache
+        Iterator<Map.Entry<String,JexlNode>> inodes = 
jexl.cache.entrySet().iterator();
+        while (inodes.hasNext()) {
+            Map.Entry<String,JexlNode> entry = inodes.next();
+            JexlNode node = entry.getValue();
+            // recreate expr string from AST
+            dbg.debug(node);
+            String expressiondbg = dbg.data();
+            try {
+                // recreate expr from string
+                Expression exprdbg = jdbg.createExpression(expressiondbg);
+                // make arg cause become the root cause
+                JexlNode root = ((ExpressionImpl) exprdbg).node;
+                while (root.jjtGetParent() != null) {
+                    root = root.jjtGetParent();
+                }
+                // test equality
+                String reason = JexlTestCase.checkEquals(root, node);
+                if (reason != null) {
+                    throw new RuntimeException("debugger equal failed: "
+                                               + expressiondbg
+                                               +" /**** "  +reason+" **** */ "
+                                               + entry.getKey());
+                }
+            }
+            catch(ParseException xparse) {
+                    throw new RuntimeException("debugger parse failed: "
+                                               + expressiondbg
+                                               +" /**** != **** */ "
+                                               + entry.getKey());
+            }
+        }
+    }
+
+    /**
+     * Creates a list of all descendants of a node including itself.
+     * @param node the node to flatten
+     * @return the descendants-and-self list
+     */
+    private static ArrayList<JexlNode> flatten(JexlNode node) {
+        ArrayList<JexlNode> list = new ArrayList<JexlNode>();
+        flatten(list, node);
+        return list;
+    }
+
+    /**
+     * Recursively adds all children of a node to the list of descendants.
+     * @param list the list of descendants to add to
+     * @param node the node & descendants to add
+     */
+    private static void flatten(List<JexlNode> list, JexlNode node) {
+        int nc = node.jjtGetNumChildren();
+        list.add(node);
+        for(int c = 0; c < nc; ++c) {
+            flatten(list, node.jjtGetChild(c));
+        }
+    }
+
+    /**
+     * Checks the equality of 2 nodes by comparing all their descendants.
+     * Descendants must have the same class and same image if non null.
+     * @param lhs the left node
+     * @param rhs the right node
+     * @return null if true, a reason otherwise
+     */
+    private static String checkEquals(JexlNode lhs, JexlNode rhs) {
+        if (lhs != rhs) {
+            ArrayList<JexlNode> lhsl = flatten(lhs);
+            ArrayList<JexlNode> rhsl = flatten(rhs);
+            if (lhsl.size() != rhsl.size()) {
+                 return "size: " + lhsl.size() + " != " + rhsl.size();
+            }
+            for(int n = 0; n < lhsl.size(); ++n) {
+                lhs = lhsl.get(n);
+                rhs = rhsl.get(n);
+                if (lhs.getClass() != rhs.getClass()) {
+                    return "class: " + lhs.getClass() + " != " + 
rhs.getClass();
+                }
+                if ((lhs.image == null && rhs.image != null)
+                    || (lhs.image != null && rhs.image == null)) {
+                    return "image: " + lhs.image + " != " + rhs.image;
+                }
+                if (lhs.image != null && !lhs.image.equals(rhs.image)) {
+                    return "image: " + lhs.image + " != " + rhs.image;
+                }
+            }
+        }
+        return null;
     }
 
     public void runTest(String name) throws Exception {

Modified: 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MethodTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MethodTest.java?rev=809169&r1=809168&r2=809169&view=diff
==============================================================================
--- 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MethodTest.java
 (original)
+++ 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MethodTest.java
 Sat Aug 29 17:33:23 2009
@@ -102,7 +102,6 @@
    public void testTopLevelCall() throws Exception {
         java.util.Map<String, Object> funcs = new java.util.HashMap<String, 
Object>();
         funcs.put(null, new Functor());
-        JexlEngine JEXL = new JexlEngine();
         JEXL.setFunctions(funcs);
 
         Expression e = JEXL.createExpression("ten()");

Modified: 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/UnifiedJEXLTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/UnifiedJEXLTest.java?rev=809169&r1=809168&r2=809169&view=diff
==============================================================================
--- 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/UnifiedJEXLTest.java
 (original)
+++ 
commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/UnifiedJEXLTest.java
 Sat Aug 29 17:33:23 2009
@@ -40,7 +40,13 @@
         context = JexlHelper.createContext();
         vars = context.getVars();
     }
-    
+
+    @Override
+    protected void tearDown() throws Exception {
+        debuggerCheck(ENGINE);
+        super.tearDown();
+    }
+
     public static class Froboz {
         int value;
         public Froboz(int v) {


Reply via email to