2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.test.mock.MockExternalContext
 
Classes in this File Line Coverage Branch Coverage Complexity
MockExternalContext
100%
21/21
N/A
1.233
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.test.mock;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.util.Collections;
 25  
 import java.util.Enumeration;
 26  
 import java.util.HashMap;
 27  
 import java.util.Iterator;
 28  
 import java.util.Locale;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 import javax.faces.FacesException;
 33  
 import javax.faces.context.ExternalContext;
 34  
 import javax.servlet.ServletContext;
 35  
 import javax.servlet.http.Cookie;
 36  
 import javax.servlet.http.HttpServletRequest;
 37  
 import javax.servlet.http.HttpServletResponse;
 38  
 import javax.servlet.http.HttpSession;
 39  
 
 40  
 /**
 41  
  * <p>Mock implementation of <code>ExternalContext</code>.</p>
 42  
  *
 43  
  * $Id$
 44  
  */
 45  
 
 46  
 public class MockExternalContext extends ExternalContext {
 47  
 
 48  
 
 49  
     // ------------------------------------------------------------ Constructors
 50  
 
 51  
 
 52  
     /**
 53  
      * <p>Construct a wrapper instance.</p>
 54  
      *
 55  
      * @param context <code>ServletContext</code> for this application
 56  
      * @param request <code>HttpServetRequest</code> for this request
 57  
      * @param response <code>HttpServletResponse</code> for this request
 58  
      */
 59  
     public MockExternalContext(ServletContext context,
 60  
                                HttpServletRequest request,
 61  36
                                HttpServletResponse response) {
 62  
 
 63  36
         this.context = context;
 64  36
         this.request = request;
 65  36
         this.response = response;
 66  36
         applicationMap = new MockApplicationMap(context);
 67  36
         requestMap = new MockRequestMap(request);
 68  
 
 69  36
     }
 70  
 
 71  
 
 72  
     // ----------------------------------------------------- Mock Object Methods
 73  
 
 74  
 
 75  
     // ------------------------------------------------------ Instance Variables
 76  
 
 77  
 
 78  36
     private Map applicationMap = null;
 79  36
     private ServletContext context = null;
 80  36
     protected HttpServletRequest request = null;
 81  36
     private Map requestMap = null;
 82  36
     protected HttpServletResponse response = null;
 83  36
     private Map sessionMap = null;
 84  36
     private Map requestCookieMap = new HashMap();
 85  36
     private Map requestParameterMap = new HashMap();
 86  
 
 87  
 
 88  
     // ------------------------------------------------- setters for the mock object
 89  
 
 90  
 
 91  
     /**
 92  
      * <p>Add a new cookie for this request.</p>
 93  
      *
 94  
      * @param cookie The new cookie
 95  
      */
 96  
     public void addRequestCookieMap(Cookie cookie) {
 97  
         requestParameterMap.put(cookie.getName(), cookie);
 98  
     }
 99  
 
 100  
 
 101  
     /**
 102  
      * <p>Set the request cookie map for this request.</p>
 103  
      *
 104  
      * @param map The new request cookie map
 105  
      */
 106  
     public void setRequestCookieMap(Map map) {
 107  
         requestParameterMap = map;
 108  
     }
 109  
 
 110  
 
 111  
     /**
 112  
      * <p>Add the specified request parameter for this request.</p>
 113  
      *
 114  
      * @param key Parameter name
 115  
      * @param value Parameter value
 116  
      */
 117  
     public void addRequestParameterMap(String key, String value) {
 118  
         requestParameterMap.put(key, value);
 119  
     }
 120  
 
 121  
 
 122  
     /**
 123  
      * <p>Set the request parameter map for this request.</p>
 124  
      *
 125  
      * @param map The new request parameter map
 126  
      */
 127  
     public void setRequestParameterMap(Map map) {
 128  
         requestParameterMap = map;
 129  
     }
 130  
 
 131  
 
 132  
     // ------------------------------------------------- ExternalContext Methods
 133  
 
 134  
 
 135  
     /** {@inheritDoc} */
 136  
     public void dispatch(String requestURI)
 137  
       throws IOException, FacesException {
 138  
 
 139  
         throw new UnsupportedOperationException();
 140  
 
 141  
     }
 142  
 
 143  
 
 144  
     /** {@inheritDoc} */
 145  
     public String encodeActionURL(String sb) {
 146  
 
 147  
         return sb;
 148  
 
 149  
     }
 150  
 
 151  
 
 152  
     /** {@inheritDoc} */
 153  
     public String encodeNamespace(String aValue) {
 154  
 
 155  
         return aValue;
 156  
 
 157  
     }
 158  
 
 159  
 
 160  
     /** {@inheritDoc} */
 161  
     public String encodeResourceURL(String sb) {
 162  
 
 163  
         return sb;
 164  
 
 165  
     }
 166  
 
 167  
 
 168  
     /** {@inheritDoc} */
 169  
     public Map getApplicationMap() {
 170  
 
 171  6
         return this.applicationMap;
 172  
 
 173  
     }
 174  
 
 175  
 
 176  
     /** {@inheritDoc} */
 177  
     public String getAuthType() {
 178  
 
 179  
         return request.getAuthType();
 180  
 
 181  
     }
 182  
 
 183  
 
 184  
     /** {@inheritDoc} */
 185  
     public Object getContext() {
 186  
 
 187  
         return context;
 188  
 
 189  
     }
 190  
 
 191  
 
 192  
     /** {@inheritDoc} */
 193  
     public String getInitParameter(String name) {
 194  
 
 195  
         return context.getInitParameter(name);
 196  
 
 197  
     }
 198  
 
 199  
 
 200  
     /** {@inheritDoc} */
 201  
     public Map getInitParameterMap() {
 202  
 
 203  
         Map parameterMap = new HashMap();
 204  
         Enumeration names = context.getInitParameterNames();
 205  
         while (names.hasMoreElements()) {
 206  
             String name = (String) names.nextElement();
 207  
             parameterMap.put(name, context.getInitParameter(name));
 208  
         }
 209  
         return Collections.unmodifiableMap(parameterMap);
 210  
 
 211  
     }
 212  
 
 213  
 
 214  
     /** {@inheritDoc} */
 215  
     public String getRemoteUser() {
 216  
 
 217  
         return request.getRemoteUser();
 218  
 
 219  
     }
 220  
 
 221  
 
 222  
     /** {@inheritDoc} */
 223  
     public Object getRequest() {
 224  
 
 225  
         return request;
 226  
 
 227  
     }
 228  
 
 229  
 
 230  
     /** {@inheritDoc} */
 231  
     public String getRequestContextPath() {
 232  
 
 233  
         return request.getContextPath();
 234  
 
 235  
     }
 236  
 
 237  
 
 238  
     /** {@inheritDoc} */
 239  
     public Map getRequestCookieMap() {
 240  
 
 241  
         return requestCookieMap;
 242  
 
 243  
     }
 244  
 
 245  
 
 246  
     /** {@inheritDoc} */
 247  
     public Map getRequestHeaderMap() {
 248  
 
 249  
         throw new UnsupportedOperationException();
 250  
 
 251  
     }
 252  
 
 253  
 
 254  
     /** {@inheritDoc} */
 255  
     public Map getRequestHeaderValuesMap() {
 256  
 
 257  
         throw new UnsupportedOperationException();
 258  
 
 259  
     }
 260  
 
 261  
 
 262  
     /** {@inheritDoc} */
 263  
     public Locale getRequestLocale() {
 264  
 
 265  
         return request.getLocale();
 266  
 
 267  
     }
 268  
 
 269  
 
 270  
     /** {@inheritDoc} */
 271  
     public Iterator getRequestLocales() {
 272  
 
 273  
         return new LocalesIterator(request.getLocales());
 274  
 
 275  
     }
 276  
 
 277  
 
 278  
     /** {@inheritDoc} */
 279  
     public Map getRequestMap() {
 280  
 
 281  31
         return requestMap;
 282  
 
 283  
     }
 284  
 
 285  
 
 286  
     /** {@inheritDoc} */
 287  
     public Map getRequestParameterMap() {
 288  
 
 289  
         return requestParameterMap;
 290  
 
 291  
     }
 292  
 
 293  
 
 294  
     /** {@inheritDoc} */
 295  
     public Iterator getRequestParameterNames() {
 296  
 
 297  
         throw new UnsupportedOperationException();
 298  
 
 299  
     }
 300  
 
 301  
 
 302  
     /** {@inheritDoc} */
 303  
     public Map getRequestParameterValuesMap() {
 304  
 
 305  
         throw new UnsupportedOperationException();
 306  
 
 307  
     }
 308  
 
 309  
 
 310  
     /** {@inheritDoc} */
 311  
     public String getRequestPathInfo() {
 312  
 
 313  
         return request.getPathInfo();
 314  
 
 315  
     }
 316  
 
 317  
 
 318  
     /** {@inheritDoc} */
 319  
     public String getRequestServletPath() {
 320  
 
 321  
         return request.getServletPath();
 322  
 
 323  
     }
 324  
 
 325  
 
 326  
     /** {@inheritDoc} */
 327  
     public URL getResource(String path) throws MalformedURLException {
 328  
 
 329  
         return context.getResource(path);
 330  
 
 331  
     }
 332  
 
 333  
 
 334  
     /** {@inheritDoc} */
 335  
     public InputStream getResourceAsStream(String path) {
 336  
 
 337  
         return context.getResourceAsStream(path);
 338  
 
 339  
     }
 340  
 
 341  
 
 342  
     /** {@inheritDoc} */
 343  
     public Set getResourcePaths(String path) {
 344  
 
 345  
         throw new UnsupportedOperationException();
 346  
 
 347  
     }
 348  
 
 349  
 
 350  
     /** {@inheritDoc} */
 351  
     public Object getResponse() {
 352  
 
 353  
         return response;
 354  
 
 355  
     }
 356  
 
 357  
 
 358  
     /** {@inheritDoc} */
 359  
     public Object getSession(boolean create) {
 360  
 
 361  
         return request.getSession(create);
 362  
 
 363  
     }
 364  
 
 365  
 
 366  
     /** {@inheritDoc} */
 367  
     public Map getSessionMap() {
 368  
 
 369  7
         if (sessionMap == null) {
 370  2
             HttpSession session = request.getSession(true);
 371  2
             sessionMap = new MockSessionMap(session);
 372  
         }
 373  7
         return sessionMap;
 374  
 
 375  
     }
 376  
 
 377  
 
 378  
     /** {@inheritDoc} */
 379  
     public java.security.Principal getUserPrincipal() {
 380  
 
 381  
         return request.getUserPrincipal();
 382  
 
 383  
     }
 384  
 
 385  
 
 386  
     /** {@inheritDoc} */
 387  
     public boolean isUserInRole(String role) {
 388  
 
 389  
         return request.isUserInRole(role);
 390  
 
 391  
     }
 392  
 
 393  
 
 394  
     /** {@inheritDoc} */
 395  
     public void log(String message) {
 396  
 
 397  
         context.log(message);
 398  
 
 399  
     }
 400  
 
 401  
 
 402  
     /** {@inheritDoc} */
 403  
     public void log(String message, Throwable throwable) {
 404  
 
 405  
         context.log(message, throwable);
 406  
 
 407  
     }
 408  
 
 409  
 
 410  
     /** {@inheritDoc} */
 411  
     public void redirect(String requestURI)
 412  
       throws IOException {
 413  
 
 414  
         throw new UnsupportedOperationException();
 415  
 
 416  
     }
 417  
 
 418  
 
 419  
     /**
 420  
      * <p>Iterator implementation that wraps an enumeration
 421  
      * of Locales for the current request.</p>
 422  
      */
 423  
     private class LocalesIterator implements Iterator {
 424  
 
 425  
         /**
 426  
          * <p>Construct an iterator wrapping the specified
 427  
          * enumeration.</p>
 428  
          *
 429  
          * @param locales Locales enumeration to wrap
 430  
          */
 431  
         public LocalesIterator(Enumeration locales) {
 432  
             this.locales = locales;
 433  
         }
 434  
 
 435  
         /**
 436  
          * <p>The enumeration to be wrapped.</p>
 437  
          */
 438  
         private Enumeration locales;
 439  
 
 440  
         /** {@inheritDoc} */
 441  
         public boolean hasNext() { return locales.hasMoreElements(); }
 442  
 
 443  
         /** {@inheritDoc} */
 444  
         public Object next() { return locales.nextElement(); }
 445  
 
 446  
         /** {@inheritDoc} */
 447  
         public void remove() { throw new UnsupportedOperationException(); }
 448  
 
 449  
     }
 450  
 
 451  
 
 452  
 }