View Javadoc

1   /***
2    * Copyright 2007 ATG DUST Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * 
7    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software 
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and limitations under the License.
13   */
14  package atg.servlet;
15  
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.Enumeration;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Properties;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  
29  import atg.nucleus.naming.ParameterName;
30  
31  /***
32   * Mock DynamoHttpServletRequest object
33   * @author robert
34   * @deprecated Instead use atg.servlet.ServletTestUtils.createDynamoRequest()
35   * 
36   */
37  public class MockDynamoHttpServletRequest extends DynamoHttpServletRequest {
38  
39    private final Map<String, String> cookieParameters = new HashMap<String, String>(),
40        headers = new HashMap<String, String>();
41  
42    private final Map<String, Object> parameters = new HashMap<String, Object>(),
43        components = new HashMap<String, Object>(),
44        attributes = new HashMap<String, Object>();
45  
46    private final List<String> serviceParameters = new ArrayList<String>(),
47        servicedLocalParameter = new ArrayList<String>();
48  
49    public MockDynamoHttpServletRequest() {
50      super();
51    }
52  
53    @Override
54    public String encodeURL(String str) {
55      return str + ";sessionId=" + System.currentTimeMillis();
56    }
57  
58    @Override
59    public Object getAttribute(String attribute) {
60      return attributes.get(attribute);
61    }
62  
63    @Override
64    @SuppressWarnings("unchecked")
65    public Enumeration getAttributeNames() {
66  
67      // Cheesy way converting an iterator to an enumeration
68      final Properties properties = new Properties();
69      for (final Iterator<String> it = attributes.keySet().iterator(); it
70          .hasNext();) {
71        final String s = it.next();
72        properties.put(s, s);
73      }
74      return properties.elements();
75    }
76  
77    @Override
78    public String getCookieParameter(String parameter) {
79      return cookieParameters.get(parameter);
80    }
81  
82    @Override
83    public String getHeader(String param) {
84      return (String) headers.get(param);
85    }
86  
87    @Override
88    public Object getLocalParameter(String name) {
89      return parameters.get(name);
90    }
91  
92    @Override
93    public Object getObjectParameter(ParameterName name) {
94      return getObjectParameter(name.getName());
95    }
96  
97    @Override
98    public Object getObjectParameter(String name) {
99      return parameters.get(name);
100   }
101 
102   @Override
103   public String getParameter(String name) {
104     return (String) parameters.get(name);
105   }
106 
107   public List<String> getServiceParameters() {
108     return serviceParameters;
109   }
110 
111   @Override
112   public void removeAttribute(String name) {
113     attributes.remove(name);
114   }
115 
116   @Override
117   public boolean serviceLocalParameter(String name, ServletRequest request,
118       ServletResponse response) throws ServletException, IOException {
119     servicedLocalParameter.add(name);
120     return true;
121   }
122 
123   @Override
124   public boolean serviceParameter(String name, ServletRequest request,
125       ServletResponse response) throws ServletException, IOException {
126     serviceParameters.add(name);
127     return serviceParameter(name, request, response, null, null);
128   }
129 
130   @Override
131   public void setAttribute(String name, Object value) {
132     attributes.put(name, value);
133   }
134 
135   public void setCookieParameter(String name, String value) {
136     cookieParameters.put(name, value);
137   }
138 
139   public void setHeaders(String name, String value) {
140     headers.put(name, value);
141   }
142 
143   public void setInputParameter(ParameterName name, Object value) {
144     setInputParameter(name.getName(), value);
145   }
146 
147   public void setInputParameter(String name, Object value) {
148     parameters.put(name, value);
149   }
150 
151   public void setNamedParameter(String name, Object value) {
152     components.put(name, value);
153   }
154 
155   public void setParameter(String key, Object value) {
156     parameters.put(key, value);
157   }
158 
159 }