KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > test > MockExternalContext


1 /*
2  * Copyright 2002-2006 the original author or authors.
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  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.test;
17
18 import java.util.HashMap JavaDoc;
19
20 import org.springframework.binding.collection.SharedMapDecorator;
21 import org.springframework.webflow.context.ExternalContext;
22 import org.springframework.webflow.core.collection.LocalAttributeMap;
23 import org.springframework.webflow.core.collection.LocalSharedAttributeMap;
24 import org.springframework.webflow.core.collection.MutableAttributeMap;
25 import org.springframework.webflow.core.collection.ParameterMap;
26 import org.springframework.webflow.core.collection.SharedAttributeMap;
27
28 /**
29  * Mock implementation of the {@link ExternalContext} interface.
30  *
31  * @see ExternalContext
32  *
33  * @author Keith Donald
34  */

35 public class MockExternalContext implements ExternalContext {
36
37     private String JavaDoc contextPath;
38     
39     private String JavaDoc dispatcherPath;
40
41     private String JavaDoc requestPathInfo;
42
43     private ParameterMap requestParameterMap = new MockParameterMap();
44
45     private MutableAttributeMap requestMap = new LocalAttributeMap();
46
47     private SharedAttributeMap sessionMap = new LocalSharedAttributeMap(new SharedMapDecorator(new HashMap JavaDoc()));
48     
49     private SharedAttributeMap globalSessionMap = sessionMap;
50
51     private SharedAttributeMap applicationMap = new LocalSharedAttributeMap(new SharedMapDecorator(new HashMap JavaDoc()));
52
53     /**
54      * Creates a mock external context with an empty request parameter map.
55      * Allows for bean style usage.
56      */

57     public MockExternalContext() {
58     }
59
60     /**
61      * Creates a mock external context with the specified parameters in the
62      * request parameter map. All other properties of the external context
63      * can be set using the appropriate setter.
64      * @param requestParameterMap the request parameters
65      */

66     public MockExternalContext(ParameterMap requestParameterMap) {
67         if (requestParameterMap != null) {
68             this.requestParameterMap = requestParameterMap;
69         }
70     }
71
72     // implementing external context
73

74     public String JavaDoc getContextPath() {
75         return contextPath;
76     }
77     
78     public String JavaDoc getDispatcherPath() {
79         return dispatcherPath;
80     }
81
82     public String JavaDoc getRequestPathInfo() {
83         return requestPathInfo;
84     }
85
86     public ParameterMap getRequestParameterMap() {
87         return requestParameterMap;
88     }
89
90     public MutableAttributeMap getRequestMap() {
91         return requestMap;
92     }
93
94     public SharedAttributeMap getSessionMap() {
95         return sessionMap;
96     }
97
98     public SharedAttributeMap getGlobalSessionMap() {
99         return globalSessionMap;
100     }
101     
102     public SharedAttributeMap getApplicationMap() {
103         return applicationMap;
104     }
105
106     // helper setters
107

108     /**
109      * Set the context path.
110      * @see ExternalContext#getContextPath()
111      */

112     public void setContextPath(String JavaDoc contextPath) {
113         this.contextPath = contextPath;
114     }
115     
116     /**
117      * Set the dispatcher path.
118      * @see ExternalContext#getDispatcherPath()
119      */

120     public void setDispatcherPath(String JavaDoc dispatcherPath) {
121         this.dispatcherPath = dispatcherPath;
122     }
123
124     /**
125      * Set the request path info.
126      * @see ExternalContext#getRequestPathInfo()
127      */

128     public void setRequestPathInfo(String JavaDoc requestPathInfo) {
129         this.requestPathInfo = requestPathInfo;
130     }
131
132     /**
133      * Set the request parameter map.
134      * @see ExternalContext#getRequestParameterMap()
135      */

136     public void setRequestParameterMap(ParameterMap requestParameterMap) {
137         this.requestParameterMap = requestParameterMap;
138     }
139     
140     /**
141      * Set the request attribute map.
142      * @see ExternalContext#getRequestMap()
143      */

144     public void setRequestMap(MutableAttributeMap requestMap) {
145         this.requestMap = requestMap;
146     }
147
148     /**
149      * Set the session attribute map.
150      * @see ExternalContext#getSessionMap()
151      */

152     public void setSessionMap(SharedAttributeMap sessionMap) {
153         this.sessionMap = sessionMap;
154     }
155     
156     /**
157      * Set the global session attribute map. By default the session attribute
158      * map and the global session attribute map are one and the same.
159      * @see ExternalContext#getGlobalSessionMap()
160      */

161     public void setGlobalSessionMap(SharedAttributeMap globalSessionMap) {
162         this.globalSessionMap = globalSessionMap;
163     }
164
165     /**
166      * Set the application attribute map.
167      * @see ExternalContext#getApplicationMap()
168      */

169     public void setApplicationMap(SharedAttributeMap applicationMap) {
170         this.applicationMap = applicationMap;
171     }
172     
173     // convenience helpers
174

175     /**
176      * Returns the request parameter map as a {@link MockParameterMap}
177      * for convenient access in a unit test.
178      * @see #getRequestParameterMap()
179      */

180     public MockParameterMap getMockRequestParameterMap() {
181         return (MockParameterMap)requestParameterMap;
182     }
183     
184     /**
185      * Puts a request parameter into the mock parameter map.
186      * @param parameterName the parameter name
187      * @param parameterValue the parameter value
188      */

189     public void putRequestParameter(String JavaDoc parameterName, String JavaDoc parameterValue) {
190         getMockRequestParameterMap().put(parameterName, parameterValue);
191     }
192
193     /**
194      * Puts a multi-valued request parameter into the mock parameter map.
195      * @param parameterName the parameter name
196      * @param parameterValues the parameter values
197      */

198     public void putRequestParameter(String JavaDoc parameterName, String JavaDoc[] parameterValues) {
199         getMockRequestParameterMap().put(parameterName, parameterValues);
200     }
201 }
Popular Tags