You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mr...@apache.org on 2004/09/11 00:43:20 UTC

cvs commit: jakarta-commons/chain/src/test/org/apache/commons/chain/web/servlet ServletWebContextTestCase.java

mrdon       2004/09/10 15:43:20

  Modified:    chain/src/java/org/apache/commons/chain/web/servlet
                        ServletApplicationScopeMap.java
                        ServletHeaderMap.java ServletHeaderValuesMap.java
                        ServletInitParamMap.java ServletParamMap.java
                        ServletParamValuesMap.java
                        ServletRequestScopeMap.java
                        ServletSessionScopeMap.java
               chain/src/test/org/apache/commons/chain/web/servlet
                        ServletWebContextTestCase.java
  Added:       chain/src/java/org/apache/commons/chain/web MapEntry.java
  Log:
  Changed all servlet Map implementations to use the Map.Entry implementation
  MapEntry, rather than returning the values which caused Map.putAll() to
  throw confusing ClassCastExceptions
  
  Revision  Changes    Path
  1.1                  jakarta-commons/chain/src/java/org/apache/commons/chain/web/MapEntry.java
  
  Index: MapEntry.java
  ===================================================================
  /*
   * Copyright 1999-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.chain.web;
  
  
  import java.util.Map;
  
  
  /**
   * <p>Map.Entry implementation that can be constructed to either be read-only
   * or not.</p>
   *
   * @version $Revision: 1.1 $ $Date: 2004/09/10 22:43:20 $
   */
  
  public class MapEntry implements Map.Entry {
  
  
      /**
       * <p>The entry key.</p>
       */
      private Object key;
  
      /**
       * <p>The entry value.</p>
       */
      private Object value;
  
      /**
       * <p>Whether the entry can be modified.</p>
       */
      private boolean modifiable = false;
  
  
      /**
       * <p>Creates a map entry that can either allow modifications or not.</p>
       * 
       * @param key The entry key
       * @param value The entry value
       * @param modifiable Whether the entry should allow modification or not
       */
      public MapEntry(Object key, Object value, boolean modifiable) {
          this.key = key;
          this.value = value;
          this.modifiable = modifiable;
      }
  
  
      /**
       * <p>Gets the entry key.</p>
       * 
       * @return The entry key
       */
      public Object getKey() {
          return key;
      }
  
  
      /**
       * <p>Gets the entry value.</p>
       * 
       * @return The entry key
       */
      public Object getValue() {
          return value;
      }
  
  
      /**
       * <p>Sets the entry value if the entry can be modified.</p>
       * 
       * @param val The new value
       * @return The old entry value
       * @throw UnsupportedOperationException If the entry cannot be modified
       */
      public Object setValue(Object val) {
          if (modifiable) {
              Object oldVal = this.value;
              this.value = val;
              return oldVal;
          } else {
              throw new UnsupportedOperationException();
          }
      }
  
  
      /**
       * <p>Determines if this entry is equal to the passed object.</p>
       * 
       * @param o The object to test
       * @return True if equal, else false
       */
      public boolean equals(Object o) {
          if (o != null && o instanceof Map.Entry) {
              Map.Entry entry = (Map.Entry)o; 
              return (this.getKey()==null ?
                      entry.getKey()==null : this.getKey().equals(entry.getKey()))  &&
                     (this.getValue()==null ?
                      entry.getValue()==null : this.getValue().equals(entry.getValue()));
          }
          return false;
      }    
  
  
      /**
       * <p>Returns the hashcode for this entry.</p>
       * 
       * @return The and'ed hashcode of the key and value
       */
      public int hashCode() {
          return (this.getKey()==null   ? 0 : this.getKey().hashCode()) ^
                 (this.getValue()==null ? 0 : this.getValue().hashCode());
      }
  }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletApplicationScopeMap.java
  
  Index: ServletApplicationScopeMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletApplicationScopeMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletApplicationScopeMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletApplicationScopeMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.ServletContext;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -77,8 +78,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = context.getAttributeNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(context.getAttribute((String) keys.nextElement()));
  +            key = (String)keys.nextElement();
  +            set.add(new MapEntry(key, context.getAttribute(key), true));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletHeaderMap.java
  
  Index: ServletHeaderMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletHeaderMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletHeaderMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletHeaderMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.http.HttpServletRequest;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -70,8 +71,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = request.getHeaderNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(request.getHeader((String) keys.nextElement()));
  +            key = (String) keys.nextElement();
  +            set.add(new MapEntry(key, request.getHeader(key), false));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletHeaderValuesMap.java
  
  Index: ServletHeaderValuesMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletHeaderValuesMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletHeaderValuesMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletHeaderValuesMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.http.HttpServletRequest;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -84,8 +85,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = request.getHeaderNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(request.getHeaders((String) keys.nextElement()));
  +            key = (String) keys.nextElement();
  +            set.add(new MapEntry(key, request.getHeaders(key), false));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletInitParamMap.java
  
  Index: ServletInitParamMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletInitParamMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletInitParamMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletInitParamMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.ServletContext;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -70,8 +71,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = context.getInitParameterNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(context.getInitParameter((String) keys.nextElement()));
  +            key = (String) keys.nextElement();
  +            set.add(new MapEntry(key, context.getInitParameter(key), false));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletParamMap.java
  
  Index: ServletParamMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletParamMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletParamMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletParamMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.http.HttpServletRequest;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -70,8 +71,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = request.getParameterNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(request.getParameter((String) keys.nextElement()));
  +            key = (String)keys.nextElement();
  +            set.add(new MapEntry(key, request.getParameter(key), false));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletParamValuesMap.java
  
  Index: ServletParamValuesMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletParamValuesMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletParamValuesMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletParamValuesMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.http.HttpServletRequest;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -70,8 +71,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = request.getParameterNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(request.getParameterValues((String) keys.nextElement()));
  +            key = (String) keys.nextElement();
  +            set.add(new MapEntry(key, request.getParameterValues(key), false));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletRequestScopeMap.java
  
  Index: ServletRequestScopeMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletRequestScopeMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletRequestScopeMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletRequestScopeMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.http.HttpServletRequest;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -77,8 +78,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = request.getAttributeNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(request.getAttribute((String) keys.nextElement()));
  +            key = (String) keys.nextElement();
  +            set.add(new MapEntry(key, request.getAttribute(key), true));
           }
           return (set);
       }
  
  
  
  1.4       +5 -2      jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java
  
  Index: ServletSessionScopeMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/java/org/apache/commons/chain/web/servlet/ServletSessionScopeMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletSessionScopeMap.java	25 Feb 2004 00:01:04 -0000	1.3
  +++ ServletSessionScopeMap.java	10 Sep 2004 22:43:20 -0000	1.4
  @@ -25,6 +25,7 @@
   import java.util.Map;
   import java.util.Set;
   import javax.servlet.http.HttpSession;
  +import org.apache.commons.chain.web.MapEntry;
   
   
   /**
  @@ -77,8 +78,10 @@
       public Set entrySet() {
           Set set = new HashSet();
           Enumeration keys = session.getAttributeNames();
  +        String key;
           while (keys.hasMoreElements()) {
  -            set.add(session.getAttribute((String) keys.nextElement()));
  +            key = (String) keys.nextElement();
  +            set.add(new MapEntry(key, session.getAttribute(key), true));
           }
           return (set);
       }
  
  
  
  1.6       +44 -1     jakarta-commons/chain/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java
  
  Index: ServletWebContextTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/chain/src/test/org/apache/commons/chain/web/servlet/ServletWebContextTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ServletWebContextTestCase.java	25 Feb 2004 00:01:06 -0000	1.5
  +++ ServletWebContextTestCase.java	10 Sep 2004 22:43:20 -0000	1.6
  @@ -29,6 +29,7 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Map;
  +import java.util.Set;
   
   
   /**
  @@ -132,6 +133,9 @@
           assertEquals("avalue3", (String) map.get("akey3"));
           assertEquals("avalue4", (String) map.get("akey4"));
   
  +        // Transparency - entrySet()
  +        checkEntrySet(map, true);
  + 
           // Transparency - removal via web object
           scontext.removeAttribute("akey1");
           checkMapSize(map, 3);
  @@ -213,6 +217,9 @@
           assertTrue(map.containsValue("hvalue1"));
           assertTrue(map.containsValue("hvalue2a"));
   
  +        // Transparency - entrySet()
  +        checkEntrySet(map, false);
  + 
           // Unsupported operations on read-only map
           try {
               map.clear();
  @@ -268,6 +275,9 @@
           assertTrue(map.containsValue(values1));
           assertTrue(map.containsValue(values2));
   
  +        // Transparency - entrySet()
  +        checkEntrySet(map, false);
  + 
           // Unsupported operations on read-only map
           try {
               map.clear();
  @@ -315,6 +325,9 @@
           assertTrue(map.containsValue("ivalue2"));
           assertTrue(map.containsValue("ivalue3"));
   
  +        // Transparency - entrySet()
  +        checkEntrySet(map, false);
  + 
           // Unsupported operations on read-only map
           try {
               map.clear();
  @@ -359,6 +372,8 @@
           assertTrue(map.containsValue("pvalue1"));
           assertTrue(map.containsValue("pvalue2a"));
   
  +        checkEntrySet(map, false);
  +
           // Unsupported operations on read-only map
           try {
               map.clear();
  @@ -520,12 +535,15 @@
           assertEquals("rvalue1", (String) map.get("rkey1"));
           assertEquals("rvalue2", (String) map.get("rkey2"));
   
  +        // Transparency - entrySet()
  +        checkEntrySet(map, true);
  + 
           // Transparency - removal via web object
           request.removeAttribute("rkey1");
           checkMapSize(map, 1);
           assertNull(map.get("rkey1"));
   
  -        // Transparency - removal via map
  +       // Transparency - removal via map
           map.remove("rkey2");
           checkMapSize(map, 0);
           assertNull(request.getAttribute("rkey2"));
  @@ -569,6 +587,9 @@
           assertEquals("svalue2", (String) map.get("skey2"));
           assertEquals("svalue3", (String) map.get("skey3"));
   
  +        // Transparency - entrySet()
  +        checkEntrySet(map, true);
  + 
           // Transparency - removal via web object
           session.removeAttribute("skey1");
           checkMapSize(map, 2);
  @@ -632,6 +653,28 @@
           assertEquals(size, map.values().size());
       }
   
  +    // Test to ensure proper entrySet() and are modifiable optionally
  +    protected void checkEntrySet(Map map, boolean modifiable) {
  +        assertTrue(map.size() > 1);
  +        Set entries = map.entrySet();
  +        assertTrue(map.size() == entries.size());
  +        Object o = entries.iterator().next();
  +
  +        assertTrue(o instanceof Map.Entry);
  +
  +        if (!modifiable) {
  +            try {
  +                ((Map.Entry)o).setValue(new Object());
  +                fail("Should have thrown UnsupportedOperationException");
  +            } catch (UnsupportedOperationException e) {
  +                ; // expected result
  +            }
  +        } else {
  +            // Should pass and not throw UnsupportedOperationException
  +            Map.Entry e = (Map.Entry)o;
  +            e.setValue(e.setValue(new Object()));
  +        }    
  +    }    
   
       // Create a new instance of the appropriate Context type for this test case
       protected Context createContext() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org