001    // Copyright 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.test;
016    
017    import org.apache.tapestry5.ioc.annotations.Inject;
018    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
019    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
020    import org.apache.tapestry5.services.Session;
021    
022    import java.util.*;
023    
024    public class TestableRequestImpl implements TestableRequest
025    {
026        private final String contextPath;
027    
028        private final Map<String, Object> parameters = CollectionFactory.newMap();
029    
030        private final Map<String, Object> attributes = CollectionFactory.newMap();
031    
032        private Session session;
033    
034        private String path = "/";
035    
036        private Locale locale = Locale.getDefault();
037    
038        @Inject
039        public TestableRequestImpl()
040        {
041            this("/foo");
042        }
043    
044        public TestableRequestImpl(String contextPath)
045        {
046            this.contextPath = contextPath;
047        }
048    
049        public TestableRequest clear()
050        {
051            parameters.clear();
052    
053            return this;
054        }
055    
056        public TestableRequest setPath(String path)
057        {
058            this.path = path;
059    
060            return this;
061        }
062    
063        public TestableRequest setLocale(Locale locale)
064        {
065            this.locale = locale;
066    
067            return this;
068        }
069    
070        public TestableRequest loadParameter(String parameterName, String parameterValue)
071        {
072            Object existing = parameters.get(parameterName);
073    
074            if (existing == null)
075            {
076                parameters.put(parameterName, parameterValue);
077                return this;
078            }
079    
080            if (existing instanceof List)
081            {
082                ((List) existing).add(parameterValue);
083                return this;
084            }
085    
086            // Convert from a single String to a List of Strings.
087    
088            List list = new ArrayList();
089            list.add(existing);
090            list.add(parameterValue);
091    
092            parameters.put(parameterName, list);
093    
094            return this;
095        }
096    
097        public TestableRequest overrideParameter(String parameterName, String parameterValue)
098        {
099            parameters.put(parameterName, parameterValue);
100    
101            return this;
102        }
103    
104        public long getDateHeader(String name)
105        {
106            return 0;
107        }
108    
109        /**
110         * Returns null.
111         */
112        public String getHeader(String name)
113        {
114            return null;
115        }
116    
117        /**
118         * Returns an empty list.
119         */
120        public List<String> getHeaderNames()
121        {
122            return Collections.emptyList();
123        }
124    
125        public Locale getLocale()
126        {
127            return locale;
128        }
129    
130        public List<String> getParameterNames()
131        {
132            return InternalUtils.sortedKeys(parameters);
133        }
134    
135        public String[] getParameters(String name)
136        {
137            Object value = parameters.get(name);
138    
139            if (value == null)
140                return null;
141    
142            if (value instanceof String)
143                return new String[]
144                { (String) value };
145    
146            List list = (List) value;
147    
148            return (String[]) list.toArray(new String[list.size()]);
149        }
150    
151        public String getPath()
152        {
153            return path;
154        }
155    
156        public String getContextPath()
157        {
158            return contextPath;
159        }
160    
161        public String getParameter(String name)
162        {
163            Object value = parameters.get(name);
164    
165            if (value == null || value instanceof String)
166                return (String) value;
167    
168            List<String> list = (List<String>) value;
169    
170            return list.get(0);
171        }
172    
173        public Session getSession(boolean create)
174        {
175            if (!create)
176                return session;
177    
178            if (session == null)
179                session = new PageTesterSession();
180    
181            return session;
182        }
183    
184        public void setEncoding(String requestEncoding)
185        {
186        }
187    
188        /**
189         * Always returns false. If you need to test Ajax functionality, you need to be using Selenium.
190         */
191        public boolean isXHR()
192        {
193            return false;
194        }
195    
196        public boolean isSecure()
197        {
198            return false;
199        }
200    
201        /**
202         * Always returns true.
203         */
204        public boolean isRequestedSessionIdValid()
205        {
206            return true;
207        }
208    
209        public Object getAttribute(String name)
210        {
211            return attributes.get(name);
212        }
213    
214        public void setAttribute(String name, Object value)
215        {
216            attributes.put(name, value);
217        }
218    
219        /**
220         * Returns "localhost" which is sufficient for testing purposes.
221         */
222        public String getServerName()
223        {
224            return "localhost";
225        }
226    
227        /**
228         * Always returns POST, to keep the Form component happy.
229         */
230        public String getMethod()
231        {
232            return "POST";
233        }
234    
235        /**
236         * Always returns 80.
237         */
238        public int getLocalPort()
239        {
240            return 80;
241        }
242    
243        /**
244         * Always returns 80.
245         * 
246         * @since 5.2.5
247         */
248        public int getServerPort()
249        {
250            return 80;
251        }
252    
253        /**
254         * Always returns "localhost".
255         *
256         * @since 5.3
257         */
258        public String getRemoteHost()
259        {
260            return "localhost";
261        }
262    }