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

For more information, please explore the Attic.

Coverage Report - org.apache.shale.test.mock.MockRequestMap
 
Classes in this File Line Coverage Branch Coverage Complexity
MockRequestMap
100%
14/14
N/A
2.062
 
 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.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Enumeration;
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 
 29  
 import javax.servlet.http.HttpServletRequest;
 30  
 
 31  
 /**
 32  
  * <p>Mock impementation of <code>Map</code> for the request scope
 33  
  * attributes managed by {@link MockExternalContext}.</p>
 34  
  *
 35  
  * $Id$
 36  
  */
 37  
 
 38  
 class MockRequestMap implements Map {
 39  
 
 40  
 
 41  
     // ------------------------------------------------------------ Constructors
 42  
 
 43  
 
 44  
     /**
 45  
      * <p>Construct a new instance, exposing the attributes of the
 46  
      * specified request as a map.</p>
 47  
      *
 48  
      * @param request The HttpServletRequest to wrap
 49  
      */
 50  36
     public MockRequestMap(HttpServletRequest request) {
 51  
 
 52  36
         this.request = request;
 53  
 
 54  36
     }
 55  
 
 56  
 
 57  
     // ----------------------------------------------------- Mock Object Methods
 58  
 
 59  
 
 60  
     // ------------------------------------------------------ Instance Variables
 61  
 
 62  
 
 63  
     /**
 64  
      * <p>The HttpServletRequest whose attributes we are exposing
 65  
      * as a Map.</p>
 66  
      */
 67  36
     private HttpServletRequest request = null;
 68  
 
 69  
 
 70  
     // ------------------------------------------------------------- Map Methods
 71  
 
 72  
 
 73  
     /** {@inheritDoc} */
 74  
     public void clear() {
 75  
 
 76  
         Iterator keys = keySet().iterator();
 77  
         while (keys.hasNext()) {
 78  
             request.removeAttribute((String) keys.next());
 79  
         }
 80  
 
 81  
     }
 82  
 
 83  
 
 84  
     /** {@inheritDoc} */
 85  
     public boolean containsKey(Object key) {
 86  
 
 87  22
         return request.getAttribute(key(key)) != null;
 88  
 
 89  
     }
 90  
 
 91  
 
 92  
     /** {@inheritDoc} */
 93  
     public boolean containsValue(Object value) {
 94  
 
 95  
         if (value == null) {
 96  
             return false;
 97  
         }
 98  
         Enumeration keys = request.getAttributeNames();
 99  
         while (keys.hasMoreElements()) {
 100  
             Object next = request.getAttribute((String) keys.nextElement());
 101  
             if (next == value) {
 102  
                 return true;
 103  
             }
 104  
         }
 105  
         return false;
 106  
 
 107  
     }
 108  
 
 109  
 
 110  
     /** {@inheritDoc} */
 111  
     public Set entrySet() {
 112  
 
 113  
         Set set = new HashSet();
 114  
         Enumeration keys = request.getAttributeNames();
 115  
         while (keys.hasMoreElements()) {
 116  
             set.add(request.getAttribute((String) keys.nextElement()));
 117  
         }
 118  
         return set;
 119  
 
 120  
     }
 121  
 
 122  
 
 123  
     /** {@inheritDoc} */
 124  
     public boolean equals(Object o) {
 125  
 
 126  
         return request.equals(o);
 127  
 
 128  
     }
 129  
 
 130  
 
 131  
     /** {@inheritDoc} */
 132  
     public Object get(Object key) {
 133  
 
 134  21
         return request.getAttribute(key(key));
 135  
 
 136  
     }
 137  
 
 138  
 
 139  
     /** {@inheritDoc} */
 140  
     public int hashCode() {
 141  
 
 142  
         return request.hashCode();
 143  
 
 144  
     }
 145  
 
 146  
 
 147  
     /** {@inheritDoc} */
 148  
     public boolean isEmpty() {
 149  
 
 150  
         return size() < 1;
 151  
 
 152  
     }
 153  
 
 154  
 
 155  
     /** {@inheritDoc} */
 156  
     public Set keySet() {
 157  
 
 158  
         Set set = new HashSet();
 159  
         Enumeration keys = request.getAttributeNames();
 160  
         while (keys.hasMoreElements()) {
 161  
             set.add(keys.nextElement());
 162  
         }
 163  
         return set;
 164  
 
 165  
     }
 166  
 
 167  
 
 168  
     /** {@inheritDoc} */
 169  
     public Object put(Object key, Object value) {
 170  
 
 171  6
         if (value == null) {
 172  
             return (remove(key));
 173  
         }
 174  6
         String skey = key(key);
 175  6
         Object previous = request.getAttribute(skey);
 176  6
         request.setAttribute(skey, value);
 177  6
         return previous;
 178  
 
 179  
     }
 180  
 
 181  
 
 182  
     /** {@inheritDoc} */
 183  
     public void putAll(Map map) {
 184  
 
 185  
         Iterator keys = map.keySet().iterator();
 186  
         while (keys.hasNext()) {
 187  
             String key = (String) keys.next();
 188  
             request.setAttribute(key, map.get(key));
 189  
         }
 190  
 
 191  
     }
 192  
 
 193  
 
 194  
     /** {@inheritDoc} */
 195  
     public Object remove(Object key) {
 196  
 
 197  
         String skey = key(key);
 198  
         Object previous = request.getAttribute(skey);
 199  
         request.removeAttribute(skey);
 200  
         return previous;
 201  
 
 202  
     }
 203  
 
 204  
 
 205  
     /** {@inheritDoc} */
 206  
     public int size() {
 207  
 
 208  
         int n = 0;
 209  
         Enumeration keys = request.getAttributeNames();
 210  
         while (keys.hasMoreElements()) {
 211  
             keys.nextElement();
 212  
             n++;
 213  
         }
 214  
         return n;
 215  
 
 216  
     }
 217  
 
 218  
 
 219  
     /** {@inheritDoc} */
 220  
     public Collection values() {
 221  
 
 222  
         List list = new ArrayList();
 223  
         Enumeration keys = request.getAttributeNames();
 224  
         while (keys.hasMoreElements()) {
 225  
             list.add(request.getAttribute((String) keys.nextElement()));
 226  
         }
 227  
         return list;
 228  
 
 229  
     }
 230  
 
 231  
 
 232  
     // --------------------------------------------------------- Private Methods
 233  
 
 234  
 
 235  
     /**
 236  
      * <p>Return the specified key converted to a string.</p>
 237  
      *
 238  
      * @param key The key to convert
 239  
      */
 240  
     private String key(Object key) {
 241  
 
 242  49
         if (key == null) {
 243  
             throw new IllegalArgumentException();
 244  49
         } else if (key instanceof String) {
 245  49
             return ((String) key);
 246  
         } else {
 247  
             return (key.toString());
 248  
         }
 249  
 
 250  
     }
 251  
 
 252  
 
 253  
 }