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/09/01 02:35:38 UTC

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

dion        2004/08/31 17:35:38

  Modified:    jexl/src/java/org/apache/commons/jexl/parser
                        ASTReference.java
               jexl/src/test/org/apache/commons/jexl JexlTest.java
  Log:
  Add ant style dotted property names.
  Note names like 'foo.commons-logging' don't work as JEXL assumes
  that the '-' is subtraction.
  
  Revision  Changes    Path
  1.4       +28 -3     jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTReference.java
  
  Index: ASTReference.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTReference.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ASTReference.java	28 Feb 2004 13:45:20 -0000	1.3
  +++ ASTReference.java	1 Sep 2004 00:35:38 -0000	1.4
  @@ -67,12 +67,37 @@
            {
                o = ( (SimpleNode) jjtGetChild(i)).execute(o,jc);
   
  -             if(o==null)
  -                 return null;
  +             // check for a variable in the context named
  +             // child0.child1.child2 etc
  +             if(o == null) {
  +                 String varName = getIdentifierToDepth(i);
  +                 o = jc.getVars().get(varName);
  +             }
            }
   
            return o;
        }
  +
  +    /**
  +     * This method returns a variable from this identifier and
  +     * it's children. For an expression like 'a.b.c', a is child
  +     * zero, b is child 1 and c is child 2.
  +     *  
  +     * @param i the depth of the child nodes to go to
  +     * @return the a dotted variable from this identifier and it's
  +     *    child nodes.
  +     */
  +    private String getIdentifierToDepth(int i) {
  +        StringBuffer varName = new StringBuffer();
  +         for (int j = 0; j <=i; j++) {
  +             SimpleNode node = (SimpleNode) jjtGetChild(j);
  +             if (node instanceof ASTIdentifier) {
  +               varName.append(((ASTIdentifier)node).getIdentifierString());
  +               if (j != i) varName.append('.');
  +             }
  +         }
  +        return varName.toString();
  +    }
   
       public String getRootString()
           throws Exception
  
  
  
  1.60      +22 -32    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.59
  retrieving revision 1.60
  diff -u -r1.59 -r1.60
  --- JexlTest.java	24 Aug 2004 05:03:13 -0000	1.59
  +++ JexlTest.java	1 Sep 2004 00:35:38 -0000	1.60
  @@ -209,14 +209,7 @@
           assertExpression(jc, "num <= 5", Boolean.TRUE);
           assertExpression(jc, "num >= 5", Boolean.TRUE);
           assertExpression(jc, "num > 4", Boolean.TRUE);
  -
  -//
  -//   $$$ GMJ - trying to be spec conformant re addition means no string concat.
  -//         so get rid of it for the moment.  Will certainly revisit
  -//
  -//        e = ExpressionFactory.createExpression("\"foo\" + \"bar\" == \"foobar\"");
  -//        o = e.evaluate(jc);
  -//        assertTrue("9 : o incorrect", o.equals(Boolean.TRUE));
  +        assertExpression(jc, "\"foo\" + \"bar\" == \"foobar\"", Boolean.TRUE);
   
       }
   
  @@ -470,29 +463,14 @@
            throws Exception
       {
           Expression e = ExpressionFactory.createExpression("x.a");
  -        e.addPostResolver(new FlatResolver());
           JexlContext jc = JexlHelper.createContext();
   
           jc.getVars().put("x.a", Boolean.TRUE );
           jc.getVars().put("x.b", Boolean.FALSE );
  -        Object o = e.evaluate(jc);
  -
  -        assertTrue("o not instanceof Boolean", o instanceof Boolean);
  -        assertEquals("o incorrect", Boolean.TRUE, o );
   
  -// unfortunately the FlatResolver doesn't resolve variables, that is the job of the context,
  -// so the following tests would never have worked.
  -//        e = ExpressionFactory.createExpression("!x.a");
  -//        e.addPreResolver(new FlatResolver());
  -//        o = e.evaluate(jc);
  -//
  -//        assertEquals("o incorrect", Boolean.FALSE, o);
  -//
  -//        e = ExpressionFactory.createExpression("!x.b");
  -//        e.addPreResolver(new FlatResolver());
  -//        o = e.evaluate(jc);
  -//
  -//        assertEquals("o incorrect", Boolean.TRUE, o );
  +        assertExpression(jc, "x.a", Boolean.TRUE);
  +        assertExpression(jc, "!x.a", Boolean.FALSE);
  +        assertExpression(jc, "!x.b", Boolean.TRUE);
       }
   
       /**
  @@ -675,11 +653,7 @@
           assertExpression(jc, "foo.array[1]", GET_METHOD_ARRAY[1]);
           assertExpression(jc, "foo.array.1", GET_METHOD_ARRAY[1]);
           assertExpression(jc, "foo.array2[1][1]", GET_METHOD_ARRAY2[1][1]);
  -
  -//        dotForm =
  -//            ExpressionFactory.createExpression("foo.array2.1.1");
  -//        o2 = dotForm.evaluate(jc);
  -//        assertEquals("dot form failed", GET_METHOD_ARRAY2[1][1], o2);
  +        //assertExpression(jc, "foo.array2.1.1", GET_METHOD_ARRAY2[1][1]);
       }
   
       /**
  @@ -900,6 +874,22 @@
           SimpleNode tree = parser.parse(new StringReader("aString = 'World';"));
       }
       
  +    public void testAntPropertiesWithMethods() throws Exception
  +    {
  +        JexlContext jc = JexlHelper.createContext();
  +        String value = "Stinky Cheese";
  +        jc.getVars().put("maven.bob.food", value);
  +        assertExpression(jc, "maven.bob.food.length()", new Integer(value.length()));
  +        assertExpression(jc, "empty(maven.bob.food)", Boolean.FALSE);
  +        assertExpression(jc, "size(maven.bob.food)", new Integer(value.length()));
  +        assertExpression(jc, "maven.bob.food + ' is good'", value + " is good");
  +
  +        // DG: Note the following ant properties don't work
  +//        String version = "1.0.3";
  +//        jc.getVars().put("commons-logging", version);
  +//        assertExpression(jc, "commons-logging", version);
  +    }
  +
       /**
        * Asserts that the given expression returns the given value when applied to the
        * given context
  
  
  

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