View Javadoc

1   /*
2    * $Id: PortletTilesRequestContextTest.java 531867 2007-04-24 10:37:07Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.portlet.context;
23  
24  import java.util.Map;
25  
26  //import org.apache.shale.test.mock.MockPortletContext;
27  //import org.apache.shale.test.mock.MockPortletRequest;
28  //import org.apache.shale.test.mock.MockPortletResponse;
29  //import org.apache.shale.test.mock.MockPortletSession;
30  import org.apache.tiles.context.TilesRequestContext;
31  
32  import junit.framework.TestCase;
33  
34  /***
35   * @version $Rev: 531867 $ $Date: 2007-04-24 12:37:07 +0200 (Tue, 24 Apr 2007) $
36   */
37  public class PortletTilesRequestContextTest extends TestCase {
38  
39      /***
40       * The used request context.
41       */
42      private TilesRequestContext context;
43  
44      /*** {@inheritDoc} */
45      @Override
46      protected void setUp() throws Exception {
47          super.setUp();
48  //        MockPortletContext servletContext = new MockPortletContext();
49  //        servletContext
50  //                .addInitParameter("initParameter1", "initParameterValue1");
51  //        MockPortletSession session = new MockPortletSession(servletContext);
52  //        MockPortletRequest request = new MockPortletRequest(session);
53  //        MockPortletResponse response = new MockPortletResponse();
54  //        request.addParameter("myParam", "value1");
55  //        request.addParameter("myParam", "value2");
56  //
57  //        context = new PortletTilesRequestContext(servletContext, request,
58  //                response);
59  //
60  //        Map<String, Object> requestScope = context.getRequestScope();
61  //        requestScope.put("attribute1", "value1");
62  //        requestScope.put("attribute2", "value2");
63  //
64  //        Map<String, Object> sessionScope = context.getSessionScope();
65  //        sessionScope.put("sessionAttribute1", "sessionValue1");
66  //        sessionScope.put("sessionAttribute2", "sessionValue2");
67  //
68  //        Map<String, Object> applicationScope = ((TilesApplicationContext) context)
69  //                .getApplicationScope();
70  //        applicationScope.put("applicationAttribute1", "applicationValue1");
71  //        applicationScope.put("applicationAttribute2", "applicationValue2");
72      }
73  
74      /***
75       * Tests getting the header.
76       */
77      public void testGetHeader() {
78  //        Map<String, String> map = context.getHeader();
79  //        assertTrue("The portlet cannot have headers!", map.isEmpty());
80  //        doTestReadMap(map, String.class, String.class, "header map");
81      }
82  
83      /***
84       * Tests getting the header values.
85       */
86      public void testGetHeaderValues() {
87  //        Map<String, String[]> map = context.getHeaderValues();
88  //        assertTrue("The portlet cannot have headers!", map.isEmpty());
89  //        doTestReadMap(map, String.class, String[].class, "header values map");
90      }
91  
92      /***
93       * Tests getting request parameters.
94       */
95      public void testGetParam() {
96  //        Map<String, String> map = context.getParam();
97  //        assertTrue("The parameters do not contain a set value", "value1"
98  //                .equals(map.get("myParam"))
99  //                || "value2".equals(map.get("myParam")));
100 //        doTestReadMap(map, String.class, String.class, "parameter map");
101     }
102 
103     /***
104      * Tests getting request parameters values.
105      */
106     public void testGetParamValues() {
107 //        Map<String, String[]> map = context.getParamValues();
108 //        String[] array = map.get("myParam");
109 //        assertTrue(
110 //                "The parameters not contain a set value",
111 //                array.length == 2
112 //                        && (("value1".equals(array[0]) && "value2"
113 //                                .equals(array[1])) || ("value1"
114 //                                .equals(array[1]) && "value2".equals(array[0]))));
115 //        doTestReadMap(map, String.class, String[].class, "parameter values map");
116     }
117 
118     /***
119      * Tests getting request scope attributes.
120      */
121     public void testGetRequestScope() {
122 //        Map<String, Object> map = context.getRequestScope();
123 //        assertTrue("The request scope does not contain a set value", "value1"
124 //                .equals(map.get("attribute1")));
125 //        assertTrue("The request scope does not contain a set value", "value2"
126 //                .equals(map.get("attribute2")));
127 //        doTestReadMap(map, String.class, Object.class, "request scope map");
128     }
129 
130     /***
131      * Tests getting session scope attributes.
132      */
133     public void testGetSessionScope() {
134 //        Map<String, Object> map = context.getSessionScope();
135 //        assertTrue("The session scope does not contain a set value",
136 //                "sessionValue1".equals(map.get("sessionAttribute1")));
137 //        assertTrue("The session scope does not contain a set value",
138 //                "sessionValue2".equals(map.get("sessionAttribute2")));
139 //        doTestReadMap(map, String.class, Object.class, "session scope map");
140     }
141 
142     /***
143      * Tests getting application scope attributes.
144      */
145     public void testGetApplicationScope() {
146 //        Map<String, Object> map = ((TilesApplicationContext) context)
147 //                .getApplicationScope();
148 //        assertTrue("The application scope does not contain a set value",
149 //                "applicationValue1".equals(map.get("applicationAttribute1")));
150 //        assertTrue("The application scope does not contain a set value",
151 //                "applicationValue2".equals(map.get("applicationAttribute2")));
152 //        doTestReadMap(map, String.class, Object.class, "application scope map");
153     }
154 
155     /***
156      * Tests getting init parameters..
157      */
158     public void testGetInitParams() {
159 //        Map<String, String> map = ((TilesApplicationContext) context)
160 //                .getInitParams();
161 //        assertTrue("The init parameters do not contain a set value",
162 //                "initParameterValue1".equals(map.get("initParameter1")));
163 //        doTestReadMap(map, String.class, String.class,
164 //                "init parameters scope map");
165     }
166 
167     /***
168      * Tests a generic map.
169      *
170      * @param <K> The key type.
171      * @param <V> The value type.
172      * @param currentMap The map to check.
173      * @param keyClass The key class.
174      * @param valueClass The value class.
175      * @param mapName The name of the map to test (for messages).
176      */
177     private <K, V> void doTestReadMap(Map<K, V> currentMap, Class<K> keyClass,
178             Class<V> valueClass, String mapName) {
179         int size1, size2;
180         size1 = currentMap.keySet().size();
181         size2 = currentMap.entrySet().size();
182         assertEquals("The map" + mapName
183                 + " has keySet and entrySet of different size", size1, size2);
184         for (K key : currentMap.keySet()) {
185             assertTrue("The key is not of class" + keyClass.getName(), keyClass
186                     .isInstance(key));
187             V value = currentMap.get(key);
188             assertTrue("The value is not of class" + valueClass.getName(),
189                     valueClass.isInstance(value));
190             assertTrue("The map " + mapName
191                     + " does not return the correct value for 'containsValue'",
192                     currentMap.containsValue(value));
193         }
194     }
195 }