You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2008/11/26 15:14:29 UTC

svn commit: r720860 - in /myfaces/core/branches/2_0_0/impl/src: main/java/org/apache/myfaces/context/servlet/ test/java/org/apache/myfaces/context/

Author: werpu
Date: Wed Nov 26 06:14:29 2008
New Revision: 720860

URL: http://svn.apache.org/viewvc?rev=720860&view=rev
Log:
https://issues.apache.org/jira/browse/MYFACES-2004

Added:
    myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java   (with props)
Modified:
    myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java
    myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/ContextRegexpTest.java

Modified: myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java?rev=720860&r1=720859&r2=720860&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java (original)
+++ myfaces/core/branches/2_0_0/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java Wed Nov 26 06:14:29 2008
@@ -19,7 +19,10 @@
 package org.apache.myfaces.context.servlet;
 
 import java.io.IOException;
+import java.lang.String;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
@@ -27,6 +30,8 @@
 import java.util.Map;
 import java.util.Set;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import javax.el.ELContext;
 import javax.el.ELContextEvent;
 import javax.el.ELContextListener;
@@ -51,8 +56,10 @@
 import org.apache.myfaces.context.ReleaseableExternalContext;
 import org.apache.myfaces.el.unified.FacesELContext;
 import org.apache.myfaces.shared_impl.util.NullIterator;
+import sun.misc.Regexp;
 
-/**
+
+    /**
  * @author Manfred Geiler (latest modification by $Author$)
  * @author Anton Koinov
  * @version $Revision$ $Date$
@@ -60,6 +67,8 @@
 public class FacesContextImpl extends FacesContext {
 
     public static final String AJAX_REQ_KEY = "javax.faces.partial.ajax";
+    static final String RE_SPLITTER = "[\\s\\t\\r\\n]*\\,[\\s\\t\\r\\n]*";
+
     // ~ Instance fields ----------------------------------------------------------------------------
 
     // TODO: I think a Map<String, List<FacesMessage>> would more efficient than those two -= Simon Lessard =-
@@ -84,6 +93,11 @@
 
     private Boolean _renderAll = null;
 
+
+
+  
+
+
     // ~ Constructors -------------------------------------------------------------------------------
     public FacesContextImpl(final ServletContext servletContext, final ServletRequest servletRequest,
             final ServletResponse servletResponse) {
@@ -97,6 +111,8 @@
         }
     }
 
+
+
     private void init(final ReleaseableExternalContext externalContext) {
         _application = ((ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY)).getApplication();
         _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
@@ -400,6 +416,8 @@
      */
     @Override
     public void enableResponseWriting(boolean enable) {
+        assertContextState("enableResponseWriting");
+
         _responseWrapper.setEnabled(enable);
         super.enableResponseWriting(enable);
     }
@@ -410,34 +428,90 @@
      */
     @Override
     public List<String> getExecutePhaseClientIds() {
+        assertContextState("getExecutePhaseClientIds");
+
         return super.getExecutePhaseClientIds();
     }
 
+
     /**
-     * @return the list of client ids to be processed in the
-     * render phase null if all have to be processed
+     *
+     * @return a list of client ids which are fetched
+     * from the request <b>parameter</b> map.
+     * The key for the map entries is {@link javax.faces.context.FacesContext.PARTIAL_RENDER_PARAM_NAME}.
+     * The list is a comma separated list of client ids in the request map!
+     * if the value {@link javax.faces.context.FacesContext.NO_PARTIAL_PHASE_CLIENT_IDS}
+     * is set or null or empty then an empty list is returned!
+     *
+     * The client ids are the ones which have to be processed during the render phase
+     *
+     * @since 2.0
+     * @throws IllegalStateException if the current context already is released!
      */
     @Override
     public List<String> getRenderPhaseClientIds() {
-        return super.getRenderPhaseClientIds();
+        assertContextState("getRenderPhaseClientIds");
+
+        /*already processed or set from the outside*/
+        if(null != _renderPhaseClientIds) {
+            return _renderPhaseClientIds;
+        }
+
+        Map paramMap = ((ServletRequest) getExternalContext().getRequest()).getParameterMap();
+        String clientIds = (String) paramMap.get(PARTIAL_RENDER_PARAM_NAME);
+        if(clientIds == null ) {//no value given
+            _renderPhaseClientIds = Collections.EMPTY_LIST;
+            return _renderPhaseClientIds;
+        }
+        clientIds = clientIds.trim();
+        if(clientIds.equals("") || clientIds.equals(NO_PARTIAL_PHASE_CLIENT_IDS)) {//empty String!
+            _renderPhaseClientIds = Collections.EMPTY_LIST;
+            return _renderPhaseClientIds;
+        }
+
+        /**
+         * we have to process the params list
+         * we now split the params as fast as possible
+         */
+        String[] splitted = clientIds.split(RE_SPLITTER);
+        /*we have to retrim the first and last entry we could
+         have pending blanks!*/
+        splitted[0] = splitted[0].trim();
+        int trimLast = splitted.length-1;
+        if(trimLast > 0) {//all others trimmed by the re
+            splitted[trimLast] =  splitted[trimLast].trim();
+        }
+        _renderPhaseClientIds = Arrays.asList(splitted);
+
+      
+        return _renderPhaseClientIds;
     }
 
     /**
      * @param executePhaseClientIds the list of client ids
      * to be processed by the execute phase
+     * 
+     * @since 2.0
+     * @throws IllegalStateException if the current context already is released!
      */
     @Override
-public void setExecutePhaseClientIds(List<String> executePhaseClientIds) {
+    public void setExecutePhaseClientIds(List<String> executePhaseClientIds) {
+        assertContextState("setExecutePhaseClientIds");
+
         super.setExecutePhaseClientIds(executePhaseClientIds);
     }
 
     /**
      * @param the list of client ids to be processed by the render
      * phase!
+     * @since 2.0
+     * @throws IllegalStateException if the current context already is released!
      */
     @Override
     public void setRenderPhaseClientIds(List<String> renderPhaseClientIds) {
-        super.setRenderPhaseClientIds(renderPhaseClientIds);
+        assertContextState("setExecutePhaseClientIds");
+
+        _renderPhaseClientIds = renderPhaseClientIds;
     }
 
     /**
@@ -465,6 +539,8 @@
      */
     @Override
     public boolean isExecuteNone() {
+        assertContextState("isExecuteNone");
+
         Map requestMap = getExternalContext().getRequestParameterMap();
         String param = (String) requestMap.get(PARTIAL_EXECUTE_PARAM_NAME);
         return NO_PARTIAL_PHASE_CLIENT_IDS.equals(param);
@@ -477,6 +553,8 @@
      */
     @Override
     public boolean isRenderNone() {
+        assertContextState("isRenderNone");
+
         Map requestMap = getExternalContext().getRequestParameterMap();
         String param = (String) requestMap.get(PARTIAL_RENDER_PARAM_NAME);
         return NO_PARTIAL_PHASE_CLIENT_IDS.equals(param);
@@ -490,6 +568,8 @@
      */
     @Override
     public boolean isRenderAll() {
+        assertContextState("isRenderAll");
+
         if(_renderAll != null) {
             return _renderAll;
         }
@@ -513,10 +593,25 @@
      */
     @Override
     public void setRenderAll(boolean renderAll) {
+        assertContextState("setRenderAll");
+        
         _renderAll = renderAll;//autoboxing does the conversation here, no need to do casting
     }
 
+   
 
-
-
+    /**
+     * has to be thrown in many of the methods
+     * if the method is called after the instance has been released!
+     */
+    private void assertContextState(String string) {
+        if(_released) {
+            StringBuilder errorMessage = new StringBuilder(128);
+            errorMessage.append("Error in method call on javax.faces.context.FacesContext.");
+            errorMessage.append(string);
+            errorMessage.append(", the facesContext is already released!");
+            throw new IllegalStateException(errorMessage.toString());
+        }
+    }
+ 
 }

Modified: myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/ContextRegexpTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/ContextRegexpTest.java?rev=720860&r1=720859&r2=720860&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/ContextRegexpTest.java (original)
+++ myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/ContextRegexpTest.java Wed Nov 26 06:14:29 2008
@@ -16,6 +16,10 @@
  */
 package org.apache.myfaces.context;
 
+import java.util.LinkedList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import junit.framework.TestCase;
 
 /**
@@ -26,16 +30,26 @@
  */
 public class ContextRegexpTest extends TestCase {
 
-    static final String SPLITTER = "\\,";
+    static final String RE_SPLITTER = "[\\s\\t\\r\\n]*\\,[\\s\\t\\r\\n]*";
 
+
+ 
+
+   
     /**
      * condition valid string
      */
     public void testCondition1() {
-        String[] splitted = " hello ,world ".split(SPLITTER);
-        assertTrue("length assertion", splitted.length == 2);
+        String[] splitted = " hello ,world          \n ,bla ".split(RE_SPLITTER);
+        splitted[0] = splitted[0].trim();
+        int len = splitted.length-1;
+        if(len > 0) {//all others trimmed by the re
+            splitted[len] =  splitted[len].trim();
+        }
+        assertTrue("length assertion", splitted.length == 3);
         assertTrue(splitted[0].trim().equals("hello"));
         assertTrue(splitted[1].trim().equals("world"));
+        assertTrue(splitted[2].trim().equals("bla"));
     }
 
     /**
@@ -43,7 +57,7 @@
      * empty string
      */
     public void testCondition2() {
-        String[] splitted = " ".split(SPLITTER);
+        String[] splitted = " ".split(RE_SPLITTER);
         assertTrue(splitted.length == 1);
         assertTrue(splitted[0] != null);
     }
@@ -53,7 +67,7 @@
      * empty string no blanks
      */
     public void testCondition3() {
-        String[] splitted = "".split(SPLITTER);
+        String[] splitted = "".split(RE_SPLITTER);
         assertTrue(splitted.length == 1);
         assertTrue(splitted[0] != null);
     }

Added: myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java?rev=720860&view=auto
==============================================================================
--- myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java (added)
+++ myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java Wed Nov 26 06:14:29 2008
@@ -0,0 +1,131 @@
+/*
+ * 
+ *  Licensed 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.
+ *  under the License.
+ */
+package org.apache.myfaces.context;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import javax.faces.context.FacesContext;
+import org.apache.myfaces.context.servlet.FacesContextImpl;
+import org.apache.shale.test.base.AbstractJsfTestCase;
+
+/**
+ * Testcases for the request parameter handling
+ * and setter handing on the get and
+ * set renderPhaseClientIds in the FacesContext class!
+ *
+ * @author Werner Punz(latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class RenderPhaseClientIdsTest extends AbstractJsfTestCase {
+
+    public RenderPhaseClientIdsTest() {
+        super("RenderPhaseClientIdsTest");
+    }
+
+    /**
+     * Empty String as request param
+     * has to result in an empty list
+     */
+    public void testRequestParams1() {
+        String empty = "    \n \t  ";
+        Map<String, String> requestParamMap = new HashMap<String, String>();
+        requestParamMap.put(FacesContext.PARTIAL_RENDER_PARAM_NAME, empty);
+        ContextTestRequestWrapper wrapper = new ContextTestRequestWrapper(request, requestParamMap);
+
+        FacesContext context = new FacesContextImpl(servletContext, wrapper, response);
+        assertTrue(context.getRenderPhaseClientIds().isEmpty());
+    }
+
+    /**
+     * no request param, has to result in an empty list
+     */
+    public void testRequestParams2() {
+        String empty = "";
+        Map<String, String> requestParamMap = new HashMap<String, String>();
+        ContextTestRequestWrapper wrapper = new ContextTestRequestWrapper(request, requestParamMap);
+
+        FacesContext context = new FacesContextImpl(servletContext, wrapper, response);
+        assertTrue(context.getRenderPhaseClientIds().isEmpty());
+    }
+
+    /**
+     * NO_PARTIAL_PHASE_CLIENT_IDS as request param, has to result in an empty list
+     */
+    public void testRequestParams4() {
+        Map<String, String> requestParamMap = new HashMap<String, String>();
+        requestParamMap.put(FacesContext.PARTIAL_RENDER_PARAM_NAME, FacesContext.NO_PARTIAL_PHASE_CLIENT_IDS);
+        ContextTestRequestWrapper wrapper = new ContextTestRequestWrapper(request, requestParamMap);
+
+        FacesContext context = new FacesContextImpl(servletContext, wrapper, response);
+        assertTrue(context.getRenderPhaseClientIds().isEmpty());
+    }
+
+    /**
+     * list with one element has to result in a list with one element
+     */
+    public void testRequestParams5() {
+        String params = " view1:panel1:_component1  ";
+        Map<String, String> requestParamMap = new HashMap<String, String>();
+        requestParamMap.put(FacesContext.PARTIAL_RENDER_PARAM_NAME, params);
+        ContextTestRequestWrapper wrapper = new ContextTestRequestWrapper(request, requestParamMap);
+
+        FacesContext context = new FacesContextImpl(servletContext, wrapper, response);
+        assertTrue("Length must be one",context.getRenderPhaseClientIds().size() == 1);
+        assertTrue("Value match",context.getRenderPhaseClientIds().get(0).equals("view1:panel1:_component1"));
+    }
+
+    /**
+     * test on a full blown list containing various
+     * blank chars
+     */
+    public void testRequestParams6() {
+        String params = " view1:panel1:_component1,view1:panel1:_component2 \n , component3, component4  ";
+        Map<String, String> requestParamMap = new HashMap<String, String>();
+        requestParamMap.put(FacesContext.PARTIAL_RENDER_PARAM_NAME, params);
+        ContextTestRequestWrapper wrapper = new ContextTestRequestWrapper(request, requestParamMap);
+
+        FacesContext context = new FacesContextImpl(servletContext, wrapper, response);
+        assertTrue("Length must be four",context.getRenderPhaseClientIds().size() == 4);
+
+        assertTrue("Value match",context.getRenderPhaseClientIds().get(0).equals("view1:panel1:_component1"));
+        assertTrue("Value match",context.getRenderPhaseClientIds().get(2).equals("component3"));
+
+
+        assertTrue("Value match",context.getRenderPhaseClientIds().get(3).equals("component4"));
+    }
+
+    /**
+     * priority the setter has higer priority
+     * than the request query
+     */
+    public void testSetter1() {
+        List<String> renderPhaseClientIds = new LinkedList<String>();
+        renderPhaseClientIds.add("component1");
+        renderPhaseClientIds.add("component2");
+        String params = " view1:panel1:_component1,view1:panel1:_component2 \n , component3, component4  ";
+        Map<String, String> requestParamMap = new HashMap<String, String>();
+        requestParamMap.put(FacesContext.PARTIAL_RENDER_PARAM_NAME, params);
+        ContextTestRequestWrapper wrapper = new ContextTestRequestWrapper(request, requestParamMap);
+
+        FacesContext context = new FacesContextImpl(servletContext, wrapper, response);
+        context.setRenderPhaseClientIds(renderPhaseClientIds);
+        assertTrue(context.getRenderPhaseClientIds().size() == 2);
+
+    }
+
+}

Propchange: myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/branches/2_0_0/impl/src/test/java/org/apache/myfaces/context/RenderPhaseClientIdsTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL