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

For more information, please explore the Attic.

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