commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1243029 [2/2] - in /commons/proper/jexl/trunk: ./ src/main/java/org/apache/commons/jexl3/ src/main/java/org/apache/commons/jexl3/internal/ src/main/java/org/apache/commons/jexl3/parser/ src/test/java/org/apache/commons/jexl3/ src/test/java...
Date Sat, 11 Feb 2012 11:26:43 GMT
Copied: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PropertyAccessTest.java
(from r1212868, commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PropertyAccessTest.java?p2=commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PropertyAccessTest.java&p1=commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java&r1=1212868&r2=1243029&rev=1243029&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArrayAccessTest.java
(original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/PropertyAccessTest.java
Sat Feb 11 11:26:42 2012
@@ -16,35 +16,23 @@
  */
 package org.apache.commons.jexl3;
 
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.jexl3.internal.Debugger;
 import org.apache.commons.jexl3.junit.Asserter;
 
 
 /**
- * Tests for array access operator []
- * 
- * @since 2.0
+ * Tests for property access operator '.'
+ * @since 3.0
  */
-public class ArrayAccessTest extends JexlTestCase {
+public class PropertyAccessTest extends JexlTestCase {
 
     private Asserter asserter;
 
-    private static final String GET_METHOD_STRING = "GetMethod string";
-    
-    // Needs to be accessible by Foo.class
-    static final String[] GET_METHOD_ARRAY =
-        new String[] { "One", "Two", "Three" };
-
-    // Needs to be accessible by Foo.class
-    static final String[][] GET_METHOD_ARRAY2 =
-        new String[][] { {"One", "Two", "Three"},{"Four", "Five", "Six"} };
-
-    public ArrayAccessTest() {
-        super("ArrayAccessTest");
+    public PropertyAccessTest() {
+        super("PropertyAccessTest");
     }
     
     @Override
@@ -52,110 +40,8 @@ public class ArrayAccessTest extends Jex
         asserter = new Asserter(JEXL);
     }
 
-    /**
-     * test simple array access
-     */
-    public void testArrayAccess() throws Exception {
-
-        /*
-         * test List access
-         */
-
-        List<Integer> l = new ArrayList<Integer>();
-        l.add(new Integer(1));
-        l.add(new Integer(2));
-        l.add(new Integer(3));
-
-        asserter.setVariable("list", l);
-
-        asserter.assertExpression("list[1]", new Integer(2));
-        asserter.assertExpression("list[1+1]", new Integer(3));
-        asserter.setVariable("loc", new Integer(1));
-        asserter.assertExpression("list[loc+1]", new Integer(3));
-
-        /*
-         * test array access
-         */
-
-        String[] args = { "hello", "there" };
-        asserter.setVariable("array", args);
-        asserter.assertExpression("array[0]", "hello");
-
-        /*
-         * to think that this was an intentional syntax...
-         */
-        asserter.assertExpression("array.0", "hello");
-
-        /*
-         * test map access
-         */
-        Map<String, String> m = new HashMap<String, String>();
-        m.put("foo", "bar");
-
-        asserter.setVariable("map", m);
-        asserter.setVariable("key", "foo");
-
-        asserter.assertExpression("map[\"foo\"]", "bar");
-        asserter.assertExpression("map[key]", "bar");
-
-        /*
-         * test bean access
-         */
-        asserter.setVariable("foo", new Foo());
-        asserter.assertExpression("foo[\"bar\"]", GET_METHOD_STRING);
-        asserter.assertExpression("foo[\"bar\"] == foo.bar", Boolean.TRUE);
-    }
-
-    /**
-     * test some simple double array lookups
-     */
-    public void testDoubleArrays() throws Exception {
-        Object[][] foo = new Object[2][2];
-        foo[0][0] = "one";
-        foo[0][1] = "two";
-
-        asserter.setVariable("foo", foo);
-
-        asserter.assertExpression("foo[0][1]", "two");
-    }
-
-    public void testArrayProperty() throws Exception {
-        Foo foo = new Foo();
-
-        asserter.setVariable("foo", foo);
 
-        asserter.assertExpression("foo.array[1]", GET_METHOD_ARRAY[1]);
-        asserter.assertExpression("foo.array.1", GET_METHOD_ARRAY[1]);
-        asserter.assertExpression("foo.array2[1][1]", GET_METHOD_ARRAY2[1][1]);
-        asserter.assertExpression("foo.array2[1].1", GET_METHOD_ARRAY2[1][1]);
-    }
-    
-    // This is JEXL-26
-    public void testArrayAndDottedConflict() throws Exception {
-        Object[] objects = new Object[] {"an", "array", new Long(0)};
-        
-        asserter.setVariable("objects", objects);
-        asserter.setVariable("status", "Enabled");
-        asserter.assertExpression("objects[1].status", null);
-        asserter.assertExpression("objects.1.status", null);
-        
-        asserter.setVariable("base.status", "Ok");
-        asserter.assertExpression("base.objects[1].status", null);
-        asserter.assertExpression("base.objects.1.status", null);
-    }
-
-    public void testArrayMethods() throws Exception {
-        Object[] objects = new Object[] {"an", "array", new Long(0)};
-        
-        asserter.setVariable("objects", objects);
-        asserter.assertExpression("objects.get(1)", "array");
-        asserter.assertExpression("objects.size()", new Integer(3));
-        // setting an index returns the old value
-        asserter.assertExpression("objects.set(1, 'dion')", "array");
-        asserter.assertExpression("objects[1]", "dion");
-    }
-
-    public void testArrayArray() throws Exception {
+    public void testPropertyProperty() throws Exception {
         Integer i42 = Integer.valueOf(42);
         Integer i43 = Integer.valueOf(43);
         String s42 = "fourty-two";
@@ -169,55 +55,144 @@ public class ArrayAccessTest extends Jex
         asserter.setVariable("one", Integer.valueOf(1));
         asserter.setVariable("two", Integer.valueOf(2));
         for(int l = 0; l < 2; ++l) {
-            asserter.assertExpression("foo[0]", foo);
-            asserter.assertExpression("foo[0][0]", foo);
-            asserter.assertExpression("foo[1]", foo[1]);
-            asserter.assertExpression("foo[0][1]", foo[1]);
-            asserter.assertExpression("foo[0][1] = 43", i43);
-            asserter.assertExpression("foo[0][1]", i43);
-            asserter.assertExpression("foo[0][1] = 42", i42);
-            asserter.assertExpression("foo[0][1]", i42);
-            asserter.assertExpression("foo[0][0][1]", foo[1]);
-            asserter.assertExpression("foo[0][0][1] = 43", i43);
-            asserter.assertExpression("foo[0][0][1]", i43);
-            asserter.assertExpression("foo[0][0][1] = 42", i42);
-            asserter.assertExpression("foo[0][0][1]", i42);
-            asserter.assertExpression("foo[2]", foo[2]);
-            asserter.assertExpression("foo[0][2]", foo[2]);
-            asserter.assertExpression("foo[0][2] = 'fourty-three'", s43);
-            asserter.assertExpression("foo[0][2]", s43);
-            asserter.assertExpression("foo[0][2] = 'fourty-two'", s42);
-            asserter.assertExpression("foo[0][2]", s42);
-            asserter.assertExpression("foo[0][0][2]", foo[2]);
-            asserter.assertExpression("foo[0][0][2] = 'fourty-three'", s43);
-            asserter.assertExpression("foo[0][0][2]", s43);
-            asserter.assertExpression("foo[0][0][2] = 'fourty-two'", s42);
-            asserter.assertExpression("foo[0][0][2]", s42);
-
-            asserter.assertExpression("foo[zero]", foo);
-            asserter.assertExpression("foo[zero][zero]", foo);
-            asserter.assertExpression("foo[one]", foo[1]);
-            asserter.assertExpression("foo[zero][one]", foo[1]);
-            asserter.assertExpression("foo[zero][one] = 43", i43);
-            asserter.assertExpression("foo[zero][one]", i43);
-            asserter.assertExpression("foo[zero][one] = 42", i42);
-            asserter.assertExpression("foo[zero][one]", i42);
-            asserter.assertExpression("foo[zero][zero][one]", foo[1]);
-            asserter.assertExpression("foo[zero][zero][one] = 43", i43);
-            asserter.assertExpression("foo[zero][zero][one]", i43);
-            asserter.assertExpression("foo[zero][zero][one] = 42", i42);
-            asserter.assertExpression("foo[zero][zero][one]", i42);
-            asserter.assertExpression("foo[two]", foo[2]);
-            asserter.assertExpression("foo[zero][two]", foo[2]);
-            asserter.assertExpression("foo[zero][two] = 'fourty-three'", s43);
-            asserter.assertExpression("foo[zero][two]", s43);
-            asserter.assertExpression("foo[zero][two] = 'fourty-two'", s42);
-            asserter.assertExpression("foo[zero][two]", s42);
-            asserter.assertExpression("foo[zero][zero][two]", foo[2]);
-            asserter.assertExpression("foo[zero][zero][two] = 'fourty-three'", s43);
-            asserter.assertExpression("foo[zero][zero][two]", s43);
-            asserter.assertExpression("foo[zero][zero][two] = 'fourty-two'", s42);
-            asserter.assertExpression("foo[zero][zero][two]", s42);
+            asserter.assertExpression("foo.0", foo);
+            asserter.assertExpression("foo.0.'0'", foo);
+            asserter.assertExpression("foo.'1'", foo[1]);
+            asserter.assertExpression("foo.0.'1'", foo[1]);
+            asserter.assertExpression("foo.0.'1' = 43", i43);
+            asserter.assertExpression("foo.0.'1'", i43);
+            asserter.assertExpression("foo.0.'1' = 42", i42);
+            asserter.assertExpression("foo.0.'1'", i42);
+        }
+    }
+    
+    public static class Container {
+        String value0;
+        int value1;
+
+        public Container(String name, int number) {
+            value0 = name;
+            value1 = number;
+        }
+
+        public Object getProperty(String name) {
+            if ("name".equals(name)) {
+                return value0;
+            } else if ("number".equals(name)) {
+                return value1;
+            } else {
+                return null;
+            }
+        }
+
+        public Object getProperty(int ref) {
+            if (0 == ref) {
+                return value0;
+            } else if (1 == ref) {
+                return value1;
+            } else {
+                return null;
+            }
+        }
+
+        public void setProperty(String name, String value) {
+            if ("name".equals(name)) {
+                this.value0 = value;
+            }
+        }
+
+        public void setProperty(String name, int value) {
+            if ("number".equals(name)) {
+                this.value1 = value;
+            }
+        }
+
+        public void setProperty(int ref, String value) {
+            if (0 == ref) {
+                this.value0 = value;
+            }
         }
+
+        public void setProperty(int ref, int value) {
+            if (1 == ref) {
+                this.value1 = value;
+            }
+        }
+    }
+
+    public void testInnerProperty() throws Exception {
+        Container quux = new Container("quux", 42);
+        JexlScript get;
+        Object result;
+
+        JexlScript getName = JEXL.createScript("foo.property.name", "foo");
+        result = getName.execute(null, quux);
+        assertEquals("quux", result);
+
+        JexlScript get0 = JEXL.createScript("foo.property.0", "foo");
+        result = get0.execute(null, quux);
+        assertEquals("quux", result);
+
+        JexlScript getNumber = JEXL.createScript("foo.property.number", "foo");
+        result = getNumber.execute(null, quux);
+        assertEquals(42, result);
+
+        JexlScript get1 = JEXL.createScript("foo.property.1", "foo");
+        result = get1.execute(null, quux);
+        assertEquals(42, result);
+
+        JexlScript setName = JEXL.createScript("foo.property.name = $0", "foo", "$0");
+        setName.execute(null, quux, "QUUX");
+        result = getName.execute(null, quux);
+        assertEquals("QUUX", result);
+        result = get0.execute(null, quux);
+        assertEquals("QUUX", result);
+
+        JexlScript set0 = JEXL.createScript("foo.property.0 = $0", "foo", "$0");
+        set0.execute(null, quux, "BAR");
+        result = getName.execute(null, quux);
+        assertEquals("BAR", result);
+        result = get0.execute(null, quux);
+        assertEquals("BAR", result);
+
+        JexlScript setNumber = JEXL.createScript("foo.property.number = $0", "foo", "$0");
+        setNumber.execute(null, quux, -42);
+        result = getNumber.execute(null, quux);
+        assertEquals(-42, result);
+        result = get1.execute(null, quux);
+        assertEquals(-42, result);
+
+        JexlScript set1 = JEXL.createScript("foo.property.1 = $0", "foo", "$0");
+        set1.execute(null, quux, 24);
+        result = getNumber.execute(null, quux);
+        assertEquals(24, result);
+        result = get1.execute(null, quux);
+        assertEquals(24, result);
+    }
+    
+
+    public void testStringIdentifier() throws Exception {
+        Map<String, String> foo = new HashMap<String, String>();
+
+        JexlContext jc = new MapContext();
+        jc.set("foo", foo);
+        foo.put("q u u x", "456");
+        JexlExpression e = JEXL.createExpression("foo.\"q u u x\"");
+        Object result = e.evaluate(jc);
+        assertEquals("456", result);
+        e = JEXL.createExpression("foo.'q u u x'");
+        result = e.evaluate(jc);
+        assertEquals("456", result);
+        JexlScript s = JEXL.createScript("foo.\"q u u x\"");
+        result = s.execute(jc);
+        assertEquals("456", result);
+        s = JEXL.createScript("foo.'q u u x'");
+        result = s.execute(jc);
+        assertEquals("456", result);
+
+        Debugger dbg = new Debugger();
+        dbg.debug(e);
+        String dbgdata = dbg.data();
+        assertEquals("foo.'q u u x';", dbgdata);
     }
 }
\ No newline at end of file

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java?rev=1243029&r1=1243028&r2=1243029&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
(original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
Sat Feb 11 11:26:42 2012
@@ -19,7 +19,6 @@ package org.apache.commons.jexl3.parser;
 import java.io.StringReader;
 
 import junit.framework.TestCase;
-import org.apache.commons.jexl3.parser.Parser;
 
 /**
  * @since 1.0
@@ -36,17 +35,17 @@ public class ParserTest extends TestCase
     public void testParse1() throws Exception {
         Parser parser = new Parser(new StringReader(";"));
 
-        SimpleNode sn = parser.parse(new StringReader("foo = 1;"), null);
+        SimpleNode sn = parser.parse(new StringReader("foo = 1;"), null, null);
         assertNotNull("parsed node is null", sn);
     }
 
     public void testParse2() throws Exception {
         Parser parser = new Parser(new StringReader(";"));
 
-        SimpleNode sn = parser.parse(new StringReader("foo = \"bar\";"), null);
+        SimpleNode sn = parser.parse(new StringReader("foo = \"bar\";"), null, null);
         assertNotNull("parsed node is null", sn);
 
-        sn = parser.parse(new StringReader("foo = 'bar';"), null);
+        sn = parser.parse(new StringReader("foo = 'bar';"), null, null);
         assertNotNull("parsed node is null", sn);
     }
 



Mime
View raw message