Add a test case for bug 48112 (note this passes - need to check when used in a JSP)
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 6 Jan 2010 18:51:24 +0000 (18:51 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 6 Jan 2010 18:51:24 +0000 (18:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@896620 13f79535-47bb-0310-9956-ffa450edef68

test/org/apache/el/TestELEvaluation.java

index 08f6d91..0c21ff4 100644 (file)
 package org.apache.el;
 
 import java.io.File;
+import java.lang.reflect.Method;
 import java.util.Date;
 
 import javax.el.ValueExpression;
+import javax.el.FunctionMapper;
 
 import org.apache.el.ExpressionFactoryImpl;
 import org.apache.el.lang.ELSupport;
@@ -90,6 +92,11 @@ public class TestELEvaluation extends TestCase {
         assertEquals("\"\\", evaluateExpression("${\"\\\"\\\\\"}"));
     }
 
+    public void testParserFunction() {
+        // bug 48112
+        assertEquals("{world}", evaluateExpression("${fn:trim('{world}')}"));
+    }
+
     private void compareBoth(String msg, int expected, Object o1, Object o2){
         int i1 = ELSupport.compare(o1, o2);
         int i2 = ELSupport.compare(o2, o1);
@@ -116,9 +123,33 @@ public class TestELEvaluation extends TestCase {
 
     private String evaluateExpression(String expression) {
         ELContextImpl ctx = new ELContextImpl();
+        ctx.setFunctionMapper(new FMapper());
         ExpressionFactoryImpl exprFactory = new ExpressionFactoryImpl();
         ValueExpression ve = exprFactory.createValueExpression(ctx, expression,
                 String.class);
         return (String) ve.getValue(ctx);
     }
+    
+    public static class FMapper extends FunctionMapper {
+
+        @Override
+        public Method resolveFunction(String prefix, String localName) {
+            if ("trim".equals(localName)) {
+                Method m;
+                try {
+                    m = this.getClass().getMethod("trim", String.class);
+                    return m;
+                } catch (SecurityException e) {
+                    // Ignore
+                } catch (NoSuchMethodException e) {
+                    // Ignore
+                } 
+            }
+            return null;
+        }
+        
+        public static String trim(String input) {
+            return input.trim();
+        }
+    }
 }