On Sun, 2004-01-11 at 21:27, peter royal wrote:

> A new patch would be easiest :)

Here it is. Some documentation added. And I also took the liberty of
adding a clear() method to JexlContext.

> If you're working against an anon CVS checkout, -N to 'cvs diff' will 
> include new files.

That only works if you use "cvs add" first. And I can't do that with
anon access. So, in the meantime, I continue with clumsy additional file
:)

-
Bill
? src/java/org/apache/commons/jexl/context/UnknownVariableException.java
Index: examples/ArrayExample.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/examples/ArrayExample.java,v
retrieving revision 1.3
diff -u -r1.3 ArrayExample.java
--- examples/ArrayExample.java	9 Oct 2003 21:28:55 -0000	1.3
+++ examples/ArrayExample.java	12 Jan 2004 08:50:52 -0000
@@ -79,7 +79,7 @@
         List l = new ArrayList();
         l.add("Hello from location 0");
         l.add(new Integer(2));
-        jc.getVars().put("array", l);
+        jc.putVar("array", l);
 
         Expression e = ExpressionFactory.createExpression("array[1]");
         Object o = e.evaluate(jc);
Index: examples/MethodPropertyExample.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/examples/MethodPropertyExample.java,v
retrieving revision 1.3
diff -u -r1.3 MethodPropertyExample.java
--- examples/MethodPropertyExample.java	9 Oct 2003 21:28:55 -0000	1.3
+++ examples/MethodPropertyExample.java	12 Jan 2004 08:50:52 -0000
@@ -73,8 +73,8 @@
          */
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo());
-        jc.getVars().put("number", new Integer(10));
+        jc.putVar("foo", new Foo());
+        jc.putVar("number", new Integer(10));
 
         /*
          *  access a method w/o args
Index: src/java/org/apache/commons/jexl/ExpressionImpl.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/ExpressionImpl.java,v
retrieving revision 1.5
diff -u -r1.5 ExpressionImpl.java
--- src/java/org/apache/commons/jexl/ExpressionImpl.java	9 Oct 2003 21:28:55 -0000	1.5
+++ src/java/org/apache/commons/jexl/ExpressionImpl.java	12 Jan 2004 08:50:52 -0000
@@ -58,6 +58,7 @@
 import java.util.List;
 
 import org.apache.commons.jexl.parser.SimpleNode;
+import org.apache.commons.jexl.context.UnknownVariableException;
 
 /**
  *  Implelmentation of an Expression.  Created by the ExpressionFactory.
@@ -142,8 +143,7 @@
      *  @param context JexlContext to use for evauluation
      *  @return value (including null) or JexlExprResolver.NO_VALUE
      */
-    protected Object tryResolver(List resolverList, JexlContext context)
-    {
+    protected Object tryResolver(List resolverList, JexlContext context) throws UnknownVariableException {
         Object val = JexlExprResolver.NO_VALUE;
         String expr = getExpression();
 
Index: src/java/org/apache/commons/jexl/JexlContext.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/JexlContext.java,v
retrieving revision 1.2
diff -u -r1.2 JexlContext.java
--- src/java/org/apache/commons/jexl/JexlContext.java	9 Oct 2003 21:28:55 -0000	1.2
+++ src/java/org/apache/commons/jexl/JexlContext.java	12 Jan 2004 08:50:52 -0000
@@ -53,6 +53,8 @@
  */
 package org.apache.commons.jexl;
 
+import org.apache.commons.jexl.context.UnknownVariableException;
+
 import java.util.Map;
 
 /**
@@ -64,6 +66,39 @@
  */
 public interface JexlContext
 {
+    /**
+     * Add a lot of variables at once. All existing variables are
+     * [EMAIL PROTECTED] #clear() cleared} first.
+     * @param vars the map of variables to add
+     */
     public void setVars(Map vars);
-    public Map getVars();
-}
+
+    /**
+     * Clear all the variables. This isn't quite the same as starting with a new
+     * instance, because the [EMAIL PROTECTED] #isStrict() strictness} is preserved.
+     */
+    public void clear();
+
+    /**
+     * Add a variable to the context
+     * @param key the name of the variable
+     * @param value the value of the variable
+     */
+    public void putVar(String key, Object value);
+
+    /**
+     * Lookup a variable
+     * @param key the variable to lookup
+     * @return the variable value
+     * @throws UnknownVariableException if the context is [EMAIL PROTECTED] #isStrict() strict} and the variable is unknown
+     */
+    public Object getVar(String key) throws UnknownVariableException;
+
+    /**
+     * Being strict means throwing an [EMAIL PROTECTED] UnknownVariableException} if a variable is asked
+     * for that hasn't been added to this context. 
+     * @return whether this context is strict about unknown variables or not
+     * @see #getVar(String)
+     */
+    boolean isStrict();
+}
\ No newline at end of file
Index: src/java/org/apache/commons/jexl/JexlExprResolver.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/JexlExprResolver.java,v
retrieving revision 1.1
diff -u -r1.1 JexlExprResolver.java
--- src/java/org/apache/commons/jexl/JexlExprResolver.java	13 Jun 2002 16:09:32 -0000	1.1
+++ src/java/org/apache/commons/jexl/JexlExprResolver.java	12 Jan 2004 08:50:52 -0000
@@ -1,5 +1,7 @@
 package org.apache.commons.jexl;
 
+import org.apache.commons.jexl.context.UnknownVariableException;
+
 /**
  *  A Resolver allows custom resolution of the expression, and can be
  *  added in front of the jexl engine, or after in the evaluation
@@ -19,5 +21,5 @@
      *  @return value (may be null) po the NO_VALUE object to
      *       indicate no resolution.
      */
-    Object evaluate(JexlContext context, String expression);
+    Object evaluate(JexlContext context, String expression) throws UnknownVariableException;
 }
Index: src/java/org/apache/commons/jexl/JexlHelper.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/JexlHelper.java,v
retrieving revision 1.2
diff -u -r1.2 JexlHelper.java
--- src/java/org/apache/commons/jexl/JexlHelper.java	9 Oct 2003 21:28:55 -0000	1.2
+++ src/java/org/apache/commons/jexl/JexlHelper.java	12 Jan 2004 08:50:52 -0000
@@ -70,14 +70,28 @@
 {
     protected static JexlHelper helper = new JexlHelper();
 
+    /**
+     * Create a new context that is [EMAIL PROTECTED] JexlContext#isStrict() lenient} about unknown variables
+     * @return the new context
+     */
     public static JexlContext createContext()
     {
-        return getInstance().newContext();
+        return createContext(false);
     }
 
-    protected JexlContext newContext()
+    /**
+     * Create a new context
+     * @param strict see [EMAIL PROTECTED] JexlContext#isStrict()}
+     * @return the new context
+     */
+    public static JexlContext createContext(boolean strict)
     {
-        return new HashMapContext();
+        return getInstance().newContext(strict);
+    }
+
+    protected JexlContext newContext(boolean strict)
+    {
+        return new HashMapContext(strict);
     }
 
     protected static JexlHelper getInstance()
Index: src/java/org/apache/commons/jexl/context/HashMapContext.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/context/HashMapContext.java,v
retrieving revision 1.2
diff -u -r1.2 HashMapContext.java
--- src/java/org/apache/commons/jexl/context/HashMapContext.java	9 Oct 2003 21:28:55 -0000	1.2
+++ src/java/org/apache/commons/jexl/context/HashMapContext.java	12 Jan 2004 08:50:52 -0000
@@ -60,21 +60,69 @@
 import java.util.Map;
 
 /**
- *  Implementation of JexlContext based on a HashMap
+ *  Implementation of JexlContext that uses a Map to store the variables
  *
  *  @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  *  @version $Id: HashMapContext.java,v 1.2 2003/10/09 21:28:55 rdonkin Exp $
  */
-public class HashMapContext extends HashMap implements JexlContext
+public class HashMapContext implements JexlContext
 {
+    private boolean strict = false;
+
+    private Map vars = new HashMap();
+
+    /**
+     * @param strict see [EMAIL PROTECTED] #isStrict()}
+     */
+    public HashMapContext(boolean strict)
+    {
+        this.strict = strict;
+    }
+
+    /**
+     * @see JexlContext#clear()
+     */
+    public void clear()
+    {
+        this.vars.clear();
+    }
+
+    /**
+     * @see JexlContext#setVars(Map)
+     */
     public void setVars(Map vars)
     {
-        this.clear();
-        this.putAll(vars);
+        this.vars.clear();
+        this.vars.putAll(vars);
     }
 
-    public Map getVars()
+    /**
+     * @see JexlContext#putVar(String, Object)
+     */
+    public void putVar(String key, Object value)
     {
-        return this;
+        this.vars.put(key, value);
     }
+
+    /**
+     * @see JexlContext#getVar(String)
+     */
+    public Object getVar(String key) throws UnknownVariableException
+    {
+        if (!isStrict()|| vars.containsKey(key)) {
+            return vars.get(key);
+        }
+        else {
+            throw new UnknownVariableException(key);
+        }
+    }
+
+    /**
+     * @see JexlContext#isStrict()
+     */
+    public boolean isStrict()
+    {
+        return strict;
+    }
+
 }
Index: src/java/org/apache/commons/jexl/parser/ASTIdentifier.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTIdentifier.java,v
retrieving revision 1.5
diff -u -r1.5 ASTIdentifier.java
--- src/java/org/apache/commons/jexl/parser/ASTIdentifier.java	9 Oct 2003 21:28:55 -0000	1.5
+++ src/java/org/apache/commons/jexl/parser/ASTIdentifier.java	12 Jan 2004 08:50:52 -0000
@@ -88,7 +88,7 @@
     public Object value(JexlContext jc)
         throws Exception
     {
-        return jc.getVars().get(val);
+        return jc.getVar(val);
     }
 
     /**
Index: src/java/org/apache/commons/jexl/resolver/FlatResolver.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/java/org/apache/commons/jexl/resolver/FlatResolver.java,v
retrieving revision 1.1
diff -u -r1.1 FlatResolver.java
--- src/java/org/apache/commons/jexl/resolver/FlatResolver.java	13 Jun 2002 16:10:44 -0000	1.1
+++ src/java/org/apache/commons/jexl/resolver/FlatResolver.java	12 Jan 2004 08:50:52 -0000
@@ -2,6 +2,7 @@
 
 import org.apache.commons.jexl.JexlExprResolver;
 import org.apache.commons.jexl.JexlContext;
+import org.apache.commons.jexl.context.UnknownVariableException;
 
 /**
  *  Simple resolver to try the expression as-is from the context.
@@ -38,9 +39,8 @@
         this.noValOnNull = noValOnNull;
     }
 
-    public Object evaluate(JexlContext context, String expression)
-    {
-        Object val = context.getVars().get(expression);
+    public Object evaluate(JexlContext context, String expression) throws UnknownVariableException {
+        Object val = context.getVar(expression);
 
         if (val == null && noValOnNull)
         {
Index: src/test/org/apache/commons/jexl/JexlTest.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/jexl/src/test/org/apache/commons/jexl/JexlTest.java,v
retrieving revision 1.34
diff -u -r1.34 JexlTest.java
--- src/test/org/apache/commons/jexl/JexlTest.java	30 Dec 2003 20:47:53 -0000	1.34
+++ src/test/org/apache/commons/jexl/JexlTest.java	12 Jan 2004 08:50:52 -0000
@@ -64,6 +64,7 @@
 import junit.framework.TestSuite;
 
 import org.apache.commons.jexl.resolver.FlatResolver;
+import org.apache.commons.jexl.context.UnknownVariableException;
 
 /**
  *  Simple testcases
@@ -105,7 +106,7 @@
         Expression e = ExpressionFactory.createExpression("foo.bar");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo() );
+        jc.putVar("foo", new Foo() );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof String", o instanceof String);
@@ -125,7 +126,7 @@
         Expression e = ExpressionFactory.createExpression("foo.bar()");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo() );
+        jc.putVar("foo", new Foo() );
 
         Object o = e.evaluate(jc);
 
@@ -151,7 +152,7 @@
         l.add(new Integer(2));
         l.add(new Integer(3));
 
-        jc.getVars().put("list", l);
+        jc.putVar("list", l);
 
         Object o = e.evaluate(jc);
 
@@ -167,7 +168,7 @@
 
         e = ExpressionFactory.createExpression("list[loc+1]");
 
-        jc.getVars().put("loc", new Integer(1));
+        jc.putVar("loc", new Integer(1));
         o = e.evaluate(jc);
 
         assertTrue("o not instanceof Integer", o instanceof Integer);
@@ -179,14 +180,14 @@
 
         String[] args = {"hello", "there"};
 
-        jc.getVars().put("array", args);
+        jc.putVar("array", args);
 
         e = ExpressionFactory.createExpression("array[0]");
         o = e.evaluate(jc);
 
         assertTrue("array[0]", o.equals("hello"));
 
-        jc.getVars().put("zero", new Integer(0));
+        jc.putVar("zero", new Integer(0));
 
         /*
          * to think that this was an intentional syntax...
@@ -203,8 +204,8 @@
         Map m = new HashMap();
         m.put("foo", "bar");
 
-        jc.getVars().put("map", m);
-        jc.getVars().put("key", "foo");
+        jc.putVar("map", m);
+        jc.putVar("key", "foo");
 
         e = ExpressionFactory.createExpression("map[\"foo\"]");
         o = e.evaluate(jc);
@@ -218,7 +219,7 @@
          *  test bean access
          */
 
-        jc.getVars().put("foo", new Foo());
+        jc.putVar("foo", new Foo());
 
         e = ExpressionFactory.createExpression("foo[\"bar\"]");
         o = e.evaluate(jc);
@@ -242,7 +243,7 @@
         Expression e = ExpressionFactory.createExpression("foo.innerFoo.bar()");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo() );
+        jc.putVar("foo", new Foo() );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof String", o instanceof String);
@@ -255,9 +256,9 @@
         Expression e = ExpressionFactory.createExpression("foo.convertBoolean(a==b)");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo() );
-        jc.getVars().put("a", Boolean.TRUE);
-        jc.getVars().put("b", Boolean.FALSE);
+        jc.putVar("foo", new Foo() );
+        jc.putVar("a", Boolean.TRUE);
+        jc.putVar("b", Boolean.FALSE);
 
         Object o = e.evaluate(jc);
 
@@ -298,7 +299,7 @@
         Expression e = ExpressionFactory.createExpression("foo.get(\"woogie\")");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo() );
+        jc.putVar("foo", new Foo() );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof String", o instanceof String);
@@ -311,10 +312,10 @@
         Expression e = ExpressionFactory.createExpression("a == b");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Foo() );
-        jc.getVars().put("a", Boolean.TRUE);
-        jc.getVars().put("b", Boolean.FALSE);
-        jc.getVars().put("num", new Integer(5));
+        jc.putVar("foo", new Foo() );
+        jc.putVar("a", Boolean.TRUE);
+        jc.putVar("b", Boolean.FALSE);
+        jc.putVar("num", new Integer(5));
 
         Object o = e.evaluate(jc);
 
@@ -362,12 +363,12 @@
          throws Exception
     {
         JexlContext jc = JexlHelper.createContext();
-        jc.getVars().put("string", "");
-        jc.getVars().put("array", new Object[0]);
-        jc.getVars().put("map", new HashMap());
-        jc.getVars().put("list", new ArrayList());
-        jc.getVars().put("set", (new HashMap()).keySet());
-        jc.getVars().put("longstring", "thingthing");
+        jc.putVar("string", "");
+        jc.putVar("array", new Object[0]);
+        jc.putVar("map", new HashMap());
+        jc.putVar("list", new ArrayList());
+        jc.putVar("set", (new HashMap()).keySet());
+        jc.putVar("longstring", "thingthing");
 
         /*
          *  I can't believe anyone thinks this is a syntax.. :)
@@ -411,8 +412,8 @@
          throws Exception
     {
         JexlContext jc = JexlHelper.createContext();
-        jc.getVars().put("s", "five!");
-        jc.getVars().put("array", new Object[5]);
+        jc.putVar("s", "five!");
+        jc.putVar("array", new Object[5]);
 
         Map map = new HashMap();
 
@@ -422,7 +423,7 @@
         map.put("4", new Integer(4));
         map.put("5", new Integer(5));
 
-        jc.getVars().put("map", map);
+        jc.putVar("map", map);
 
         List list = new ArrayList();
 
@@ -432,7 +433,7 @@
         list.add("4");
         list.add("5");
 
-        jc.getVars().put("list", list);
+        jc.putVar("list", list);
 
         assertExpression(jc, "size(s)", new Integer(5));
         assertExpression(jc, "size(array)", new Integer(5));
@@ -455,7 +456,7 @@
     {
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", "abcdef");
+        jc.putVar("foo", "abcdef");
 
         assertExpression(jc, "foo.substring(3)", "def");
         assertExpression(jc, "foo.substring(0,(size(foo)-3))", "abc");
@@ -474,7 +475,7 @@
         Expression e = ExpressionFactory.createExpression("foo + 2");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Integer(2) );
+        jc.putVar("foo", new Integer(2) );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof Long", o instanceof Long);
@@ -550,7 +551,7 @@
 
         e = ExpressionFactory.createExpression("stringy + 2");
 
-        jc.getVars().put("stringy", "thingy" );
+        jc.putVar("stringy", "thingy" );
 
         o = e.evaluate(jc);
         assertTrue("o not instanceof String", o instanceof String);
@@ -562,7 +563,7 @@
 
         e = ExpressionFactory.createExpression("imanull + 2");
 
-        jc.getVars().put("imanull", null );
+        jc.putVar("imanull", null );
 
         o = e.evaluate(jc);
         assertEquals("o incorrect", new Long(2), o);
@@ -582,7 +583,7 @@
         Expression e = ExpressionFactory.createExpression("foo == 2");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", new Integer(2) );
+        jc.putVar("foo", new Integer(2) );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof Boolean", o instanceof Boolean);
@@ -619,9 +620,9 @@
         JexlContext jc = JexlHelper.createContext();
 
         Foo foo = new Foo();
-        jc.getVars().put("x", Boolean.TRUE );
-        jc.getVars().put("foo", foo );
-        jc.getVars().put("bar", "true" );
+        jc.putVar("x", Boolean.TRUE );
+        jc.putVar("foo", foo );
+        jc.putVar("bar", "true" );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof Boolean", o instanceof Boolean);
@@ -681,8 +682,8 @@
         e.addPostResolver(new FlatResolver());
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("x.a", Boolean.TRUE );
-        jc.getVars().put("x.b", Boolean.FALSE );
+        jc.putVar("x.a", Boolean.TRUE );
+        jc.putVar("x.b", Boolean.FALSE );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof Boolean", o instanceof Boolean);
@@ -710,7 +711,7 @@
         Expression e = ExpressionFactory.createExpression("foo.indexOf('quick') > 0");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", "the quick and lazy fox" );
+        jc.putVar("foo", "the quick and lazy fox" );
         Object o = e.evaluate(jc);
 
         assertTrue("o not instanceof Boolean", o instanceof Boolean);
@@ -736,7 +737,7 @@
          throws Exception
     {
         JexlContext jc = JexlHelper.createContext();
-        jc.getVars().put("bar", new Integer(2) );
+        jc.putVar("bar", new Integer(2) );
 
         Expression e = ExpressionFactory.createExpression("empty foo");
         Object o = e.evaluate(jc);
@@ -760,7 +761,7 @@
          throws Exception
     {
         JexlContext jc = JexlHelper.createContext();
-        jc.getVars().put("bar", "" );
+        jc.putVar("bar", "" );
 
         Expression e = ExpressionFactory.createExpression("foo == ''");
         Object o = e.evaluate(jc);
@@ -782,8 +783,8 @@
          throws Exception
     {
         JexlContext jc = JexlHelper.createContext();
-        jc.getVars().put("foo", "abc" );
-        jc.getVars().put("bar", "def" );
+        jc.putVar("foo", "abc" );
+        jc.putVar("bar", "def" );
 
         assertExpression(jc, "foo == 'abc' || bar == 'abc'", Boolean.TRUE);
         assertExpression(jc, "foo == 'abc' or bar == 'abc'", Boolean.TRUE);
@@ -810,7 +811,7 @@
         foo[0][0] = "one";
         foo[0][1] = "two";
 
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo", foo );
         Object o = e.evaluate(jc);
 
         assertEquals("o incorrect", "two", o);
@@ -826,7 +827,7 @@
         JexlContext jc = JexlHelper.createContext();
 
 
-        jc.getVars().put("foo_bar", "123" );
+        jc.putVar("foo_bar", "123" );
         Object o = e.evaluate(jc);
 
         assertEquals("o incorrect", "123", o);
@@ -844,7 +845,7 @@
         Map foo = new HashMap();
         foo.put( "bar", "123" );
 
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo", foo );
         Object o = e.evaluate(jc);
 
         assertEquals("o incorrect", "123", o);
@@ -859,7 +860,7 @@
         Expression e = ExpressionFactory.createExpression("foo == \"bar\"");
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", "bar" );
+        jc.putVar("foo", "bar" );
 
         Object o = e.evaluate(jc);
 
@@ -883,7 +884,7 @@
         assertEquals(4, foo.square(2));
         assertEquals(4, foo.square(-2));
 
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo", foo );
         Object o = e.evaluate(jc);
 
         assertEquals("o incorrect", new Integer(5), o);
@@ -900,7 +901,7 @@
     {
         JexlContext jc = JexlHelper.createContext();
         Foo foo = new Foo();
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo", foo );
 
         assertExpression(jc, "foo.count != -1", Boolean.TRUE);
         assertExpression(jc, "foo.count == 5", Boolean.TRUE);
@@ -920,7 +921,7 @@
 
         Foo foo = new Foo();
 
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo", foo );
 
         Object o1 = bracketForm.evaluate(jc);
         assertEquals("bracket form failed", GET_METHOD_ARRAY[1], o1);
@@ -937,7 +938,7 @@
 
         jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo", foo );
 
         o1 = bracketForm.evaluate(jc);
         assertEquals("bracket form failed", GET_METHOD_ARRAY2[1][1], o1);
@@ -954,7 +955,7 @@
     {
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put("foo", "abcdef");
+        jc.putVar("foo", "abcdef");
 
         assertExpression(jc, "foo.substring(2,4)", "cd");
         assertExpression(jc, "foo.charAt(2)", new Character('c'));
@@ -975,7 +976,7 @@
     {
         JexlContext jc = JexlHelper.createContext();
 
-        jc.getVars().put( "this.is.a.test", "");
+        jc.putVar( "this.is.a.test", "");
 
         assertExpression(jc, "empty(this.is.a.test)", Boolean.TRUE);
     }
@@ -985,7 +986,7 @@
         JexlContext jc = JexlHelper.createContext();
         Map m = Collections.singletonMap("aList", Collections.EMPTY_LIST);
 
-        jc.getVars().put( "aMap", m );
+        jc.putVar( "aMap", m );
 
         assertExpression( jc, "empty( aMap.aList )", Boolean.TRUE );
     }
@@ -1019,8 +1020,8 @@
 
         Foo foo = new Foo();
 
-        jc.getVars().put("foo.bar", "flat value");
-        jc.getVars().put("foo", foo );
+        jc.putVar("foo.bar", "flat value");
+        jc.putVar("foo", foo );
 
         Object o = expr.evaluate(jc);
 
@@ -1051,6 +1052,40 @@
         o = expr.evaluate(jc);
 
         assertEquals("flat override 2", o, null);
+
+    }
+
+    /**
+     * Test that we can spot unknown variables when we are being strict
+     * @throws Exception if anything went wrong
+     */
+    public void testUnknownVariables() throws Exception
+    {
+        final String variableName = "a";
+        Expression e = ExpressionFactory.createExpression(variableName);
+        JexlContext jc = null;
+
+        // Try lenient behaviour
+        jc = JexlHelper.createContext(false);
+        e.evaluate(jc);
+
+        // Default behaviour is to be lenient
+        jc = JexlHelper.createContext();
+        e.evaluate(jc);
+
+        // Being strict should throw an exception
+        jc = JexlHelper.createContext(true);
+        try {
+            e.evaluate(jc);
+            fail("Strict behaviour should have thrown an exception here");
+        }
+        catch (UnknownVariableException e1) {
+            assertEquals("Unknown variable name", variableName, e1.getVariableName());
+        }
+
+        // Just check that we aren't too strict
+        jc.putVar(variableName, null);
+        e.evaluate(jc);
 
     }
 
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowledgement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgement may appear in the software itself,
 *    if and wherever such third-party acknowledgements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", "Jexl" and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.commons.jexl.context;

/**
 *  Exception that occurs if an unknown variable is requested from a JexlContext
 *  @author <a href="mailto:[EMAIL PROTECTED]">Bill Horsman.</a>
 *  @version $Id: $
 */
public class UnknownVariableException extends Exception
{

    private String variableName;

    /**
     * @param variableName see [EMAIL PROTECTED] #getVariableName()}
     */
    public UnknownVariableException(String variableName)
    {
        super("'" + variableName + "' is unknown");
        this.variableName = variableName;
    }

    /**
     * Get the name of the variable that is unknown. This exception was caused by
     * it being asked for.
     * @return the unknown variable name
     */
    public String getVariableName()
    {
        return variableName;
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to