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.context.servlet;
20  
21  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
22  import org.junit.After;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.junit.runner.RunWith;
27  import org.junit.runners.JUnit4;
28  
29  import java.util.Arrays;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Test cases for ServletExternalContextImpl.
36   * 
37   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
38   * @version $Revision: 1205204 $ $Date: 2011-11-22 17:40:11 -0500 (Tue, 22 Nov 2011) $
39   */
40  @RunWith(JUnit4.class)
41  public class ServletExternalContextImplTest extends AbstractJsfTestCase
42  {
43  
44      private ServletExternalContextImpl _testExternalContext;
45  
46      @Before
47      @Override
48      public void setUp() throws Exception
49      {
50          super.setUp();
51          
52          _testExternalContext = new ServletExternalContextImpl(servletContext, request, response);
53      }
54  
55      @After
56      @Override
57      public void tearDown() throws Exception
58      {
59          _testExternalContext = null;
60          
61          super.tearDown();
62      }
63      
64      /**
65       * Tests if encodeRedirectURL() and encodeBookmarkableURL() correctly
66       * evaluate ValueExpressions as parameters values.
67       */
68      /* TODO: Invalid test, because EL evaluation should be done before call these methods.
69      @Test
70      @SuppressWarnings("unchecked")
71      public void testEncodeURLHandlesValueExpressionParameters()
72      {
73          // put EL values in the application map
74          Map<String, Object> applicationMap = externalContext.getApplicationMap();
75          applicationMap.put("el1", "myvalue1");
76          applicationMap.put("el2", "myvalue2");
77          
78          // create parameters Map
79          // note that Arrays.asList() return an UnmodifiableList and thus we
80          // indirectly also check for modification here.
81          Map<String, List<String>> parameters = new HashMap<String, List<String>>();
82          parameters.put("param1", Arrays.asList("literalvalue", "#{el1}"));
83          parameters.put("param2", Arrays.asList("#{el2}"));
84          
85          // encode the URL with parameters
86          final String redirectUrl = _testExternalContext.encodeRedirectURL("someUrl.jsf", parameters);
87          final String bookmarkableUrl = _testExternalContext.encodeBookmarkableURL("someUrl.jsf", parameters);
88          
89          // asserts for redirectUrl
90          Assert.assertTrue(redirectUrl.contains("param1=literalvalue"));
91          Assert.assertTrue(redirectUrl.contains("param1=myvalue1"));
92          Assert.assertTrue(redirectUrl.contains("param2=myvalue2"));
93          
94          // asserts for bookmarkableUrl
95          Assert.assertTrue(bookmarkableUrl.contains("param1=literalvalue"));
96          Assert.assertTrue(bookmarkableUrl.contains("param1=myvalue1"));
97          Assert.assertTrue(bookmarkableUrl.contains("param2=myvalue2"));
98      }*/
99  
100     @Test
101     public void testEncodeRedirectUrlWithEmptyParamInBaseUrl()
102     {
103         // query parameter p1 has an empty value
104         String baseUrl = "/test?p1=&p2=test";
105         
106         // encode that URL without adding further parameters
107         final String redirectUrl = _testExternalContext.encodeRedirectURL(baseUrl, null);
108         
109         // the URL should not change
110         Assert.assertTrue(redirectUrl.contains("p1="));
111         Assert.assertTrue(redirectUrl.contains("p2=test"));
112 
113     }
114 
115     @Test
116     public void testEncodedSpaceInExistingQueryParameter()
117     {
118         // the base URL with an existing encoded query parameter 
119         String baseUrl = "/test?p1=a+b";
120         
121         // encode that URL without adding further parameters
122         final String redirectUrl = _testExternalContext.encodeRedirectURL(baseUrl, null);
123         
124         // the URL should not change
125         Assert.assertEquals(baseUrl, redirectUrl);
126 
127     }
128     
129     @Test
130     public void testEncodedAmpersandInExistingQueryParameter()
131     {
132         // the base URL with an existing encoded query parameter 
133         String baseUrl = "/test?p1=a%26b";
134         
135         // encode that URL without adding further parameters
136         final String redirectUrl = _testExternalContext.encodeRedirectURL(baseUrl, null);
137         
138         // the URL should not change
139         Assert.assertEquals(baseUrl, redirectUrl);
140         
141     }
142     
143 }