001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.test;
017:
018: import java.util.HashMap;
019:
020: import org.springframework.binding.collection.SharedMapDecorator;
021: import org.springframework.webflow.context.ExternalContext;
022: import org.springframework.webflow.core.collection.LocalAttributeMap;
023: import org.springframework.webflow.core.collection.LocalSharedAttributeMap;
024: import org.springframework.webflow.core.collection.MutableAttributeMap;
025: import org.springframework.webflow.core.collection.ParameterMap;
026: import org.springframework.webflow.core.collection.SharedAttributeMap;
027:
028: /**
029: * Mock implementation of the {@link ExternalContext} interface.
030: *
031: * @see ExternalContext
032: *
033: * @author Keith Donald
034: */
035: public class MockExternalContext implements ExternalContext {
036:
037: private String contextPath;
038:
039: private String dispatcherPath;
040:
041: private String requestPathInfo;
042:
043: private ParameterMap requestParameterMap = new MockParameterMap();
044:
045: private MutableAttributeMap requestMap = new LocalAttributeMap();
046:
047: private SharedAttributeMap sessionMap = new LocalSharedAttributeMap(
048: new SharedMapDecorator(new HashMap()));
049:
050: private SharedAttributeMap globalSessionMap = sessionMap;
051:
052: private SharedAttributeMap applicationMap = new LocalSharedAttributeMap(
053: new SharedMapDecorator(new HashMap()));
054:
055: /**
056: * Creates a mock external context with an empty request parameter map.
057: * Allows for bean style usage.
058: */
059: public MockExternalContext() {
060: }
061:
062: /**
063: * Creates a mock external context with the specified parameters in the
064: * request parameter map. All other properties of the external context
065: * can be set using the appropriate setter.
066: * @param requestParameterMap the request parameters
067: */
068: public MockExternalContext(ParameterMap requestParameterMap) {
069: if (requestParameterMap != null) {
070: this .requestParameterMap = requestParameterMap;
071: }
072: }
073:
074: // implementing external context
075:
076: public String getContextPath() {
077: return contextPath;
078: }
079:
080: public String getDispatcherPath() {
081: return dispatcherPath;
082: }
083:
084: public String getRequestPathInfo() {
085: return requestPathInfo;
086: }
087:
088: public ParameterMap getRequestParameterMap() {
089: return requestParameterMap;
090: }
091:
092: public MutableAttributeMap getRequestMap() {
093: return requestMap;
094: }
095:
096: public SharedAttributeMap getSessionMap() {
097: return sessionMap;
098: }
099:
100: public SharedAttributeMap getGlobalSessionMap() {
101: return globalSessionMap;
102: }
103:
104: public SharedAttributeMap getApplicationMap() {
105: return applicationMap;
106: }
107:
108: // helper setters
109:
110: /**
111: * Set the context path.
112: * @see ExternalContext#getContextPath()
113: */
114: public void setContextPath(String contextPath) {
115: this .contextPath = contextPath;
116: }
117:
118: /**
119: * Set the dispatcher path.
120: * @see ExternalContext#getDispatcherPath()
121: */
122: public void setDispatcherPath(String dispatcherPath) {
123: this .dispatcherPath = dispatcherPath;
124: }
125:
126: /**
127: * Set the request path info.
128: * @see ExternalContext#getRequestPathInfo()
129: */
130: public void setRequestPathInfo(String requestPathInfo) {
131: this .requestPathInfo = requestPathInfo;
132: }
133:
134: /**
135: * Set the request parameter map.
136: * @see ExternalContext#getRequestParameterMap()
137: */
138: public void setRequestParameterMap(ParameterMap requestParameterMap) {
139: this .requestParameterMap = requestParameterMap;
140: }
141:
142: /**
143: * Set the request attribute map.
144: * @see ExternalContext#getRequestMap()
145: */
146: public void setRequestMap(MutableAttributeMap requestMap) {
147: this .requestMap = requestMap;
148: }
149:
150: /**
151: * Set the session attribute map.
152: * @see ExternalContext#getSessionMap()
153: */
154: public void setSessionMap(SharedAttributeMap sessionMap) {
155: this .sessionMap = sessionMap;
156: }
157:
158: /**
159: * Set the global session attribute map. By default the session attribute
160: * map and the global session attribute map are one and the same.
161: * @see ExternalContext#getGlobalSessionMap()
162: */
163: public void setGlobalSessionMap(SharedAttributeMap globalSessionMap) {
164: this .globalSessionMap = globalSessionMap;
165: }
166:
167: /**
168: * Set the application attribute map.
169: * @see ExternalContext#getApplicationMap()
170: */
171: public void setApplicationMap(SharedAttributeMap applicationMap) {
172: this .applicationMap = applicationMap;
173: }
174:
175: // convenience helpers
176:
177: /**
178: * Returns the request parameter map as a {@link MockParameterMap}
179: * for convenient access in a unit test.
180: * @see #getRequestParameterMap()
181: */
182: public MockParameterMap getMockRequestParameterMap() {
183: return (MockParameterMap) requestParameterMap;
184: }
185:
186: /**
187: * Puts a request parameter into the mock parameter map.
188: * @param parameterName the parameter name
189: * @param parameterValue the parameter value
190: */
191: public void putRequestParameter(String parameterName,
192: String parameterValue) {
193: getMockRequestParameterMap().put(parameterName, parameterValue);
194: }
195:
196: /**
197: * Puts a multi-valued request parameter into the mock parameter map.
198: * @param parameterName the parameter name
199: * @param parameterValues the parameter values
200: */
201: public void putRequestParameter(String parameterName,
202: String[] parameterValues) {
203: getMockRequestParameterMap()
204: .put(parameterName, parameterValues);
205: }
206: }
|