svn commit: r833012 - in /struts/sandbox/trunk/struts2-uel-plugin/src: main/java/org/apache/struts2/uelplugin/ main/java/org/apache/struts2/uelplugin/elresolvers/ test/java/org/apache/struts2/uelplugin/

View: New views
1 Messages — Rating Filter:   Alert me  

svn commit: r833012 - in /struts/sandbox/trunk/struts2-uel-plugin/src: main/java/org/apache/struts2/uelplugin/ main/java/org/apache/struts2/uelplugin/elresolvers/ test/java/org/apache/struts2/uelplugin/

by musachy-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Author: musachy
Date: Thu Nov  5 06:30:40 2009
New Revision: 833012

URL: http://svn.apache.org/viewvc?rev=833012&view=rev
Log:
add tests and improve #XYZ format

Modified:
    struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java
    struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java
    struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java
    struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java
    struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java
    struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java

Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java?rev=833012&r1=833011&r2=833012&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/JUELExtensionBuilder.java Thu Nov  5 06:30:40 2009
@@ -33,7 +33,6 @@
     };
 
     static AstBinary.Operator EXTENDED_ADD_OPERATOR = new AstBinary.SimpleOperator() {
-
         public Object apply(TypeConverter converter, Object o1, Object o2) {
             if (o1 instanceof String || o2 instanceof String)
                 return StringUtils.join(new Object[]{o1, o2});
@@ -41,7 +40,6 @@
                 return NumberOperations.add(converter, o1, o2);
         }
 
-
         public String toString() {
             return "+";
         }
@@ -52,9 +50,9 @@
      */
     static Parser.ExtensionHandler SHARP_HANDLER = new Parser.ExtensionHandler(Parser.ExtensionPoint.UNARY) {
         public AstNode createAstNode(AstNode... children) {
-            AstIdentifier astIdentifier = (AstIdentifier) children[0];
-            ValueStackAstIdentifier valueStackAstIdentifier = new ValueStackAstIdentifier(astIdentifier.getName(), astIdentifier.getIndex());
-            return new AstUnary(valueStackAstIdentifier, SHARP_OPERATOR);
+            //AstIdentifier astIdentifier = (AstIdentifier) children[0];
+            //ValueStackAstIdentifier valueStackAstIdentifier = new ValueStackAstIdentifier(astIdentifier.getName(), astIdentifier.getIndex());
+            return new AstUnary(children[0], SHARP_OPERATOR);
         }
     };
 
@@ -88,12 +86,17 @@
                     char current = input.charAt(currentIndex);
 
                     if (current == '#' && (!StringUtils.substring(input, currentIndex + 1, currentIndex + 2).equals('{'))) {
+                        //fake unary operator so it is accepted by the parser
                         return SHARP_TOKEN;
+                    } else if (currentIndex > 0 && input.charAt(currentIndex - 1) == '#' && current != '{') {
+                        //direct reference, like #obj, let the parser extract the token
+                        Token token = super.nextEval();
+                        //add #to the name of the token
+                        return token(Symbol.IDENTIFIER, "#" + token.getImage(), token.getSize());
                     } else if (current == '+') {
                         return EXTENDED_ADD_TOKEN;
                     }
 
-
                     return super.nextEval();
                 }
             };

Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java?rev=833012&r1=833011&r2=833012&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/CompoundRootELResolver.java Thu Nov  5 06:30:40 2009
@@ -103,7 +103,7 @@
             throw new IllegalArgumentException("ElContext cannot be null");
         }
 
-        String propertyName = (String) property;
+        String propertyName = property.toString();
 
         if (StringUtils.startsWith(propertyName, "#"))
             return null;

Modified: struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java?rev=833012&r1=833011&r2=833012&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/main/java/org/apache/struts2/uelplugin/elresolvers/ValueStackContextResolver.java Thu Nov  5 06:30:40 2009
@@ -8,19 +8,24 @@
 import java.util.Iterator;
 import java.beans.FeatureDescriptor;
 
+import org.apache.commons.lang.xwork.StringUtils;
+
 
 public class ValueStackContextResolver extends ELResolver {
     public Object getValue(ELContext context, Object base, Object property) {
         String objectName = property.toString();
+        if (StringUtils.startsWith(objectName, "#")) {
+            objectName = StringUtils.removeStart(property.toString(), "#");
 
-        ActionContext actionContext = ActionContext.getContext();
-        if (context != null) {
-            ValueStack valueStack = actionContext.getValueStack();
-            if (valueStack != null) {
-                Object obj = valueStack.getContext().get(objectName);
-                if (obj != null) {
-                    context.setPropertyResolved(true);
-                    return obj;
+            ActionContext actionContext = ActionContext.getContext();
+            if (context != null) {
+                ValueStack valueStack = actionContext.getValueStack();
+                if (valueStack != null) {
+                    Object obj = valueStack.getContext().get(objectName);
+                    if (obj != null) {
+                        context.setPropertyResolved(true);
+                        return obj;
+                    }
                 }
             }
         }

Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java?rev=833012&r1=833011&r2=833012&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/JuelMethodInvocationTest.java Thu Nov  5 06:30:40 2009
@@ -19,36 +19,37 @@
     private UelValueStack stack;
 
     public void testBasicMethods() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
-
-        ActionContext.getContext().setValueStack(stack);
-
         assertEquals("text", stack.findValue("${' text '.trim()}"));
         assertEquals(3, stack.findValue("${'123'.length()}"));
     }
 
     public void testMethodsWithParams() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
-
-        ActionContext.getContext().setValueStack(stack);
-
         assertEquals('2', stack.findValue("${'123'.charAt(1)}"));
         assertEquals("123456", stack.findValue("${'123'.concat('456')}"));
     }
 
+      public void testMethodsWithParamsAndContextReference() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+          stack.getContext().put("s0", "Lex");
+          stack.getContext().put("s1", "Luthor");
+          assertEquals("Lex Luthor", stack.findValue("${#s0.concat(' ').concat(#s1)}"));
+      }
+
     protected void setUp() throws Exception {
         super.setUp();
 
+        converter = container.getInstance(XWorkConverter.class);
+        this.root = new CompoundRoot();
+        this.stack = new UelValueStack(converter);
+        stack.setRoot(root);
+        stack.getContext().put(ActionContext.CONTAINER, container);
+
         MockServletContext servletContext = new MockServletContext();
-        ActionContext context = new ActionContext(new HashMap());
+        ActionContext context = new ActionContext(stack.getContext());
         ActionContext.setContext(context);
         ServletActionContext.setServletContext(servletContext);
 
         //simulate start up
         UelServletContextListener listener = new UelServletContextListener();
         listener.contextInitialized(new ServletContextEvent(servletContext));
-
-        converter = container.getInstance(XWorkConverter.class);
-        this.root = new CompoundRoot();
-        this.stack = new UelValueStack(converter);
-        stack.setRoot(root);
     }
 }

Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java?rev=833012&r1=833011&r2=833012&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/TestObject.java Thu Nov  5 06:30:40 2009
@@ -10,6 +10,22 @@
     private Date date;
     private TestObject inner;
     private Map parameters;
+    private Object object;
+
+    public TestObject() {
+    }
+
+    public TestObject(String value) {
+        this.value = value;
+    }
+
+    public Object getObject() {
+        return object;
+    }
+
+    public void setObject(Object object) {
+        this.object = object;
+    }
 
     public String getValue() {
         return value;

Modified: struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java?rev=833012&r1=833011&r2=833012&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java (original)
+++ struts/sandbox/trunk/struts2-uel-plugin/src/test/java/org/apache/struts2/uelplugin/UelTest.java Thu Nov  5 06:30:40 2009
@@ -14,9 +14,7 @@
 import java.lang.reflect.InvocationTargetException;
 import java.text.DateFormat;
 import java.text.ParseException;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 public class UelTest extends XWorkTestCase {
     private ExpressionFactory factory = ExpressionFactory.newInstance();
@@ -83,6 +81,43 @@
 
     }
 
+    public void testExpressionSyntax() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+        TestObject obj = new TestObject();
+        obj.setValue("val");
+        obj.setAge(1);
+        stack.getContext().put("obj", obj);
+
+        assertEquals("val", stack.findValue("${#obj.value}"));
+        assertEquals("val", stack.findValue("%{#obj.value}"));
+        assertEquals("val", stack.findValue("#{#obj.value}"));
+    }
+
+    public void testSuperNested() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+        TestObject obj0 = new TestObject("0");
+        root.push(obj0);
+
+        TestObject obj1 = new TestObject("1");
+        obj0.setInner(obj1);
+
+        TestObject obj2 = new TestObject("2");
+        Map map = new HashMap();
+        map.put("key0", obj2);
+        obj1.setParameters(map);
+
+        TestObject obj3 = new TestObject("3");
+        List list = new ArrayList();
+        list.add(obj3);
+        obj2.setObject(obj3);
+
+        TestObject obj4 = new TestObject("4");
+        TestObject[] array = new TestObject[]{obj4};
+        obj3.setObject(array);
+
+        stack.getContext().put("obj", obj0);
+
+        assertEquals("4", stack.findValue("${inner.parameters['key0'].object.object[0].value}"));
+    }
+
     public void testContextReferences() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
         TestObject obj = new TestObject();
         obj.setValue("val");
@@ -105,6 +140,20 @@
         //string addition
         assertEquals("valval2", stack.findValue("#obj.value + #obj2.value"));
         assertEquals("1val2", stack.findValue("#obj.age + #obj2.value"));
+
+        //map
+        Map someMap = new HashMap();
+        obj.setInner(obj2);
+        someMap.put("val", obj);
+        stack.getContext().put("map", someMap);
+        assertEquals("val", stack.findValue("#map[#obj.value].value"));
+
+        //list
+        List someList = new ArrayList(3);
+        obj.setAge(0);
+        someList.add(obj);
+        stack.getContext().put("list", someList);
+        assertEquals("val", stack.findValue("#list[#obj.age].value"));
     }
 
     public void testBasicFind() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {