View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.application;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.faces.FactoryFinder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIPanel;
30  import javax.faces.component.UIViewParameter;
31  import javax.faces.component.UIViewRoot;
32  import javax.faces.context.FacesContext;
33  import javax.faces.view.ViewDeclarationLanguage;
34  import javax.faces.view.ViewMetadata;
35  
36  import junit.framework.Assert;
37  
38  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
39  import org.apache.myfaces.test.el.MockValueExpression;
40  import org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl;
41  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
42  import org.apache.myfaces.view.facelets.impl.FaceletCacheFactoryImpl;
43  import org.junit.After;
44  import org.junit.Before;
45  import org.junit.Test;
46  import org.junit.runner.RunWith;
47  import org.junit.runners.JUnit4;
48  
49  /**
50   * Test class for ViewHandlerImpl
51   * 
52   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
53   * @version $Revision: 1099119 $ $Date: 2011-05-03 11:44:32 -0500 (Tue, 03 May 2011) $
54   */
55  @RunWith(JUnit4.class)
56  public class ViewHandlerImplTest extends AbstractJsfTestCase
57  {
58      
59      private ViewHandlerImpl _viewHandler;
60      
61      @Before
62      @Override
63      public void setUp() throws Exception
64      {
65          super.setUp();
66          
67          // configure VDL factory
68          FactoryFinder.setFactory(FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY,
69                  ViewDeclarationLanguageFactoryImpl.class.getName());
70          
71          FactoryFinder.setFactory(FactoryFinder.FACELET_CACHE_FACTORY,
72                  FaceletCacheFactoryImpl.class.getName());
73          
74          // configure the ViewHandler
75          _viewHandler = new TestViewHandlerImpl();
76          facesContext.getApplication().setViewHandler(_viewHandler);
77          
78          // add UIViewRoot as component
79          facesContext.getApplication().addComponent(UIViewRoot.COMPONENT_TYPE,
80                  UIViewRoot.class.getName());
81      }
82  
83      @After
84      @Override
85      public void tearDown() throws Exception
86      {
87          _viewHandler = null;
88          
89          super.tearDown();
90      }
91  
92      /**
93       *  Checks if ViewHandler.getBookmarkableURL() wants to change the parameter Map,
94       *  which is not allowed, because this Map comes from the NavigationCase and
95       *  any changes on it will change the NavigationCase itself.
96       *  Normally this would mean that a <f:viewParam> is added as a static value
97       *  to the NavigationCase (which is, of course, not allowed).
98       */
99      @Test
100     @SuppressWarnings("unchecked")
101     public void testGetBookmarkableURLDoesNotChangeParametersMap()
102     {
103         // set the required path elements
104         request.setPathElements("/", null, "/newview.jsf", null);
105         
106         // set the value for the ValueExpression #{paramvalue}
107         externalContext.getApplicationMap().put("paramvalue", "paramvalue");
108         
109         // create a parameter map and make everything unmodifiable (Map and Lists)
110         Map<String, List<String>> parameters = new HashMap<String, List<String>>();
111         parameters.put("key1", Collections.unmodifiableList(Arrays.asList("value11", "value12")));
112         parameters.put("key2", Collections.unmodifiableList(Arrays.asList("value2")));
113         parameters = Collections.unmodifiableMap(parameters);
114         
115         String url = null;
116         try
117         {
118             url = _viewHandler.getBookmarkableURL(facesContext, "/newview.xhtml", parameters, true);
119         }
120         catch (UnsupportedOperationException uoe)
121         {
122             // an UnsupportedOperationException occured, which means getBookmarkableURL()
123             // wanted to change the Map or any of the Lists and this is not allowed!
124             Assert.fail("ViewHandler.getBookmarkableURL() must not change the parameter Map!");
125         }
126         
127         // additional checks:
128         // the URL must contain all params from the parameters map
129         // and from the <f:viewParam> components of the target view
130         Assert.assertTrue(url.contains("key1=value11"));
131         Assert.assertTrue(url.contains("key1=value12"));
132         Assert.assertTrue(url.contains("key2=value2"));
133         Assert.assertTrue(url.contains("myparam=paramvalue"));
134     }
135     
136     /**
137      *  Checks if ViewHandler.getRedirectURL() wants to change the parameter Map,
138      *  which is not allowed, because this Map comes from the NavigationCase and
139      *  any changes on it will change the NavigationCase itself.
140      *  Normally this would mean that a <f:viewParam> is added as a static value
141      *  to the NavigationCase (which is, of course, not allowed).
142      */
143     @Test
144     @SuppressWarnings("unchecked")
145     public void testGetRedirectURLDoesNotChangeParametersMap()
146     {
147         // set the required path elements
148         request.setPathElements("/", null, "/newview.jsf", null);
149         
150         // set the value for the ValueExpression #{paramvalue}
151         externalContext.getApplicationMap().put("paramvalue", "paramvalue");
152         
153         // create a parameter map and make everything unmodifiable (Map and Lists)
154         Map<String, List<String>> parameters = new HashMap<String, List<String>>();
155         parameters.put("key1", Collections.unmodifiableList(Arrays.asList("value11", "value12")));
156         parameters.put("key2", Collections.unmodifiableList(Arrays.asList("value2")));
157         parameters = Collections.unmodifiableMap(parameters);
158         
159         String url = null;
160         try
161         {
162             url = _viewHandler.getRedirectURL(facesContext, "/newview.xhtml", parameters, true);
163         }
164         catch (UnsupportedOperationException uoe)
165         {
166             // an UnsupportedOperationException occured, which means getRedirectURL()
167             // wanted to change the Map or any of the Lists and this is not allowed!
168             Assert.fail("ViewHandler.getRedirectURL() must not change the parameter Map!");
169         }
170         
171         // additional checks:
172         // the URL must contain all params from the parameters map
173         // and from the <f:viewParam> components of the target view
174         Assert.assertTrue(url.contains("key1=value11"));
175         Assert.assertTrue(url.contains("key1=value12"));
176         Assert.assertTrue(url.contains("key2=value2"));
177         Assert.assertTrue(url.contains("myparam=paramvalue"));
178     }
179     
180     /**
181      * A ViewHandler implementation that extends the default implementation
182      * and returns a TestFaceletViewDeclarationLanguage in getVDL() for test purposes.
183      * @author Jakob Korherr
184      */
185     private class TestViewHandlerImpl extends ViewHandlerImpl
186     {
187 
188         @Override
189         public ViewDeclarationLanguage getViewDeclarationLanguage(
190                 FacesContext context, String viewId)
191         {
192             return new TestFaceletViewDeclarationLanguage(context);
193         }
194         
195     }
196     
197     /**
198      * A VDL implementation which extends FaceletViewDeclarationLanguage, but
199      * returns a TestViewMetadata instance on getViewMetadata().
200      * @author Jakob Korherr
201      */
202     private class TestFaceletViewDeclarationLanguage extends FaceletViewDeclarationLanguage
203     {
204 
205         public TestFaceletViewDeclarationLanguage(FacesContext context)
206         {
207             super(context);
208         }
209 
210         @Override
211         public ViewMetadata getViewMetadata(FacesContext context, String viewId)
212         {
213             return new TestViewMetadata(viewId);
214         }
215         
216     }
217     
218     /**
219      * A custom ViewMetadata implementation for the test, which returns
220      * one UIViewParameter with a name of myparam and a value of #{paramvalue}.
221      * @author Jakob Korherr
222      */
223     private class TestViewMetadata extends ViewMetadata
224     {
225         
226         private String _viewId;
227         
228         public TestViewMetadata(String viewId)
229         {
230             _viewId = viewId;
231         }
232 
233         @Override
234         public UIViewRoot createMetadataView(FacesContext context)
235         {
236             UIViewRoot root = new UIViewRoot();
237             root.setViewId(_viewId);
238             UIComponent metadataFacet = new UIPanel();
239             root.getFacets().put(UIViewRoot.METADATA_FACET_NAME, metadataFacet);
240             UIViewParameter viewparam = new UIViewParameter();
241             viewparam.setName("myparam");
242             viewparam.setValueExpression("value", new MockValueExpression("#{paramvalue}", String.class));
243             metadataFacet.getChildren().add(viewparam);
244             return root;
245         }
246 
247         @Override
248         public String getViewId()
249         {
250             return _viewId;
251         }
252         
253     }
254     
255 }