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

For more information, please explore the Attic.

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