You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2004/08/17 15:20:48 UTC

cvs commit: jakarta-commons/jexl/src/test/org/apache/commons/jexl JexlTest.java Foo.java

dion        2004/08/17 06:20:48

  Modified:    jexl/src/java/org/apache/commons/jexl/parser ASTAndNode.java
                        ASTOrNode.java
               jexl/xdocs changes.xml
               jexl/src/test/org/apache/commons/jexl JexlTest.java Foo.java
  Log:
  Issue 29550 -  Implement short-circuit boolean evaluation
  
  Revision  Changes    Path
  1.4       +6 -5      jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTAndNode.java
  
  Index: ASTAndNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTAndNode.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ASTAndNode.java	28 Feb 2004 13:45:20 -0000	1.3
  +++ ASTAndNode.java	17 Aug 2004 13:20:47 -0000	1.4
  @@ -47,14 +47,15 @@
           throws Exception
       {
           Object left = ((SimpleNode) jjtGetChild(0)).value(jc);
  -        Object right = ((SimpleNode) jjtGetChild(1)).value(jc);
  +        boolean leftValue = Coercion.coerceBoolean(left).booleanValue(); 
   
           /*
            * coercion rules
            */
  -
  -        return (Coercion.coerceBoolean(left).booleanValue()
  -                && Coercion.coerceBoolean(right).booleanValue()) ?
  +        return (leftValue
  +        		&& 
  +				Coercion.coerceBoolean(
  +						((SimpleNode) jjtGetChild(1)).value(jc)).booleanValue()) ?
               Boolean.TRUE : Boolean.FALSE;
       }
   }
  
  
  
  1.4       +5 -5      jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTOrNode.java
  
  Index: ASTOrNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTOrNode.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ASTOrNode.java	28 Feb 2004 13:45:20 -0000	1.3
  +++ ASTOrNode.java	17 Aug 2004 13:20:47 -0000	1.4
  @@ -47,14 +47,14 @@
           throws Exception
       {
           Object left = ((SimpleNode) jjtGetChild(0)).value(jc);
  -        Object right = ((SimpleNode) jjtGetChild(1)).value(jc);
  +        boolean leftValue = Coercion.coerceBoolean(left).booleanValue(); 
   
           /*
            * coercion rules
            */
  -
  -        return (Coercion.coerceBoolean(left).booleanValue()
  -                || Coercion.coerceBoolean(right).booleanValue()) ?
  +        return (leftValue
  +                || Coercion.coerceBoolean(((SimpleNode) jjtGetChild(1)).value(jc))
  +					.booleanValue()) ?
               Boolean.TRUE : Boolean.FALSE;
       }
   }
  
  
  
  1.5       +1 -0      jakarta-commons/jexl/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/xdocs/changes.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- changes.xml	15 Aug 2004 16:01:12 -0000	1.4
  +++ changes.xml	17 Aug 2004 13:20:48 -0000	1.5
  @@ -25,6 +25,7 @@
     </properties>
     <body>
       <release version="1.0-beta-3-SNAPSHOT" date="in CVS">
  +      <action dev="dion" type="fix" issue="29550">Implement short circuit logic for boolean and/or</action>
         <action dev="dion" type="add">Handle any size() method that returns an int</action>
         <action dev="dion" type="fix" issue="30562">Can't issue .size() on java.util.Set</action>
       </release>
  
  
  
  1.39      +45 -1     jakarta-commons/jexl/src/test/org/apache/commons/jexl/JexlTest.java
  
  Index: JexlTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/src/test/org/apache/commons/jexl/JexlTest.java,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- JexlTest.java	15 Aug 2004 16:01:12 -0000	1.38
  +++ JexlTest.java	17 Aug 2004 13:20:48 -0000	1.39
  @@ -1045,6 +1045,50 @@
       }
   
       /**
  +     * Test that 'and' only evaluates the second item if needed
  +     * @throws Exception if there are errors
  +     */
  +    public void testBooleanShortCircuitAnd() throws Exception
  +    {
  +        // handle false for the left arg of 'and'
  +        Foo tester = new Foo();
  +        JexlContext jc = JexlHelper.createContext();
  +        jc.getVars().put("first", Boolean.FALSE);
  +        jc.getVars().put("foo", tester);
  +        Expression expr = ExpressionFactory.createExpression("first and foo.trueAndModify");
  +        expr.evaluate(jc);
  +        assertTrue("Short circuit failure: rhs evaluated when lhs FALSE", !tester.getModified());
  +        // handle true for the left arg of 'and' 
  +        tester = new Foo();
  +        jc.getVars().put("first", Boolean.TRUE);
  +        jc.getVars().put("foo", tester);
  +        expr.evaluate(jc);
  +        assertTrue("Short circuit failure: rhs not evaluated when lhs TRUE", tester.getModified());
  +    }
  +    
  +    /**
  +     * Test that 'or' only evaluates the second item if needed
  +     * @throws Exception if there are errors
  +     */
  +    public void testBooleanShortCircuitOr() throws Exception
  +    {
  +        // handle false for the left arg of 'or'
  +        Foo tester = new Foo();
  +        JexlContext jc = JexlHelper.createContext();
  +        jc.getVars().put("first", Boolean.FALSE);
  +        jc.getVars().put("foo", tester);
  +        Expression expr = ExpressionFactory.createExpression("first or foo.trueAndModify");
  +        expr.evaluate(jc);
  +        assertTrue("Short circuit failure: rhs not evaluated when lhs FALSE", tester.getModified());
  +        // handle true for the left arg of 'or' 
  +        tester = new Foo();
  +        jc.getVars().put("first", Boolean.TRUE);
  +        jc.getVars().put("foo", tester);
  +        expr.evaluate(jc);
  +        assertTrue("Short circuit failure: rhs evaluated when lhs TRUE", !tester.getModified());
  +    }
  +
  +    /**
        * Asserts that the given expression returns the given value when applied to the
        * given context
        */
  
  
  
  1.4       +13 -1     jakarta-commons/jexl/src/test/org/apache/commons/jexl/Foo.java
  
  Index: Foo.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/src/test/org/apache/commons/jexl/Foo.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Foo.java	28 Feb 2004 13:45:22 -0000	1.3
  +++ Foo.java	17 Aug 2004 13:20:48 -0000	1.4
  @@ -26,6 +26,8 @@
    */
   public class Foo {
       
  +	private boolean beenModified = false;
  +	
       public String bar()
       {
           return JexlTest.METHOD_STRING;
  @@ -84,4 +86,14 @@
           return value * value;
       }
   
  +    public boolean getTrueAndModify()
  +    {
  +    	beenModified = true;
  +    	return true;
  +    }
  +
  +    public boolean getModified()
  +    {
  +    	return beenModified;
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org