You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/07/09 12:38:49 UTC

svn commit: r962476 - in /myfaces/core/trunk/impl/src: main/java/org/apache/myfaces/context/servlet/ test/java/org/apache/myfaces/context/servlet/

Author: jakobk
Date: Fri Jul  9 10:38:49 2010
New Revision: 962476

URL: http://svn.apache.org/viewvc?rev=962476&view=rev
Log:
MYFACES-2791 render view params not working for EL expressions

Added:
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java   (with props)
Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java?rev=962476&r1=962475&r2=962476&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java Fri Jul  9 10:38:49 2010
@@ -751,7 +751,7 @@ public final class ServletExternalContex
             {
                 if (pair.getKey() != null && pair.getKey().trim().length() != 0)
                 {
-                    paramMap.put(pair.getKey(), pair.getValue());
+                    paramMap.put(pair.getKey(), _evaluateValueExpressions(pair.getValue()));
                 }
             }
         }
@@ -802,6 +802,37 @@ public final class ServletExternalContex
     }
     
     /**
+     * Checks the Strings in the List for EL expressions and evaluates them.
+     * Note that the returned List will be a copy of the given List, because
+     * otherwise it will have unwanted side-effects.
+     * @param values
+     * @return
+     */
+    private List<String> _evaluateValueExpressions(List<String> values)
+    {
+        // note that we have to create a new List here, because if we
+        // change any value on the given List, it will be changed in the
+        // NavigationCase too and the EL expression won't be evaluated again
+        List<String> target = new ArrayList<String>(values.size());
+        FacesContext context = FacesContext.getCurrentInstance();
+        for (String value : values)
+        {
+            if (_isExpression(value))
+            {
+                // evaluate the ValueExpression
+                value = context.getApplication().evaluateExpressionGet(context, value, String.class);
+            }
+            target.add(value);
+        }
+        return target;
+    }
+    
+    private boolean _isExpression(String text)
+    {
+        return text.indexOf("#{") != -1;
+    }
+    
+    /**
      * @since 2.0
      */
     public Flash getFlash()

Added: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java?rev=962476&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java (added)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java Fri Jul  9 10:38:49 2010
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.context.servlet;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Test cases for ServletExternalContextImpl.
+ * 
+ * @author Jakob Korherr (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+@RunWith(JUnit4.class)
+public class ServletExternalContextImplTest extends AbstractJsfTestCase
+{
+
+    private ServletExternalContextImpl _testExternalContext;
+
+    @Before
+    @Override
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        
+        _testExternalContext = new ServletExternalContextImpl(servletContext, request, response);
+    }
+
+    @After
+    @Override
+    public void tearDown() throws Exception
+    {
+        _testExternalContext = null;
+        
+        super.tearDown();
+    }
+    
+    /**
+     * Tests if encodeRedirectURL() and encodeBookmarkableURL() correctly
+     * evaluate ValueExpressions as parameters values.
+     */
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testEncodeURLHandlesValueExpressionParameters()
+    {
+        // put EL values in the application map
+        Map<String, Object> applicationMap = externalContext.getApplicationMap();
+        applicationMap.put("el1", "myvalue1");
+        applicationMap.put("el2", "myvalue2");
+        
+        // create parameters Map
+        // note that Arrays.asList() return an UnmodifiableList and thus we
+        // indirectly also check for modification here.
+        Map<String, List<String>> parameters = new HashMap<String, List<String>>();
+        parameters.put("param1", Arrays.asList("literalvalue", "#{el1}"));
+        parameters.put("param2", Arrays.asList("#{el2}"));
+        
+        // encode the URL with parameters
+        final String redirectUrl = _testExternalContext.encodeRedirectURL("someUrl.jsf", parameters);
+        final String bookmarkableUrl = _testExternalContext.encodeBookmarkableURL("someUrl.jsf", parameters);
+        
+        // asserts for redirectUrl
+        Assert.assertTrue(redirectUrl.contains("param1=literalvalue"));
+        Assert.assertTrue(redirectUrl.contains("param1=myvalue1"));
+        Assert.assertTrue(redirectUrl.contains("param2=myvalue2"));
+        
+        // asserts for bookmarkableUrl
+        Assert.assertTrue(bookmarkableUrl.contains("param1=literalvalue"));
+        Assert.assertTrue(bookmarkableUrl.contains("param1=myvalue1"));
+        Assert.assertTrue(bookmarkableUrl.contains("param2=myvalue2"));
+    }
+    
+}

Propchange: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/context/servlet/ServletExternalContextImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain