Source Code Cross Referenced for TestingHttpServletRequest.java in  » Groupware » coefficient » za » org » coefficient » util » testing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » coefficient » za.org.coefficient.util.testing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Coefficient - facilitates project based collaboration
003:         * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004:         * PO Box 395
005:         * Pretoria 0001, RSA
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or (at your option) any later version.
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         */
019:        package za.org.coefficient.util.testing;
020:
021:        import java.io.BufferedReader;
022:        import java.io.IOException;
023:        import java.io.UnsupportedEncodingException;
024:        import java.security.Principal;
025:        import java.util.Enumeration;
026:        import java.util.HashMap;
027:        import java.util.Locale;
028:        import java.util.Map;
029:        import java.util.Set;
030:        import java.util.Vector;
031:
032:        import javax.servlet.RequestDispatcher;
033:        import javax.servlet.ServletInputStream;
034:        import javax.servlet.http.Cookie;
035:        import javax.servlet.http.HttpServletRequest;
036:        import javax.servlet.http.HttpSession;
037:
038:        /**
039:         * <p>Project: coefficient</p>
040:         * <p>Description: This is an implementation of the HttpServletRequest
041:         * interace that is backed by Maps for the parameter and attribute 
042:         * data - it exists completely independently of any container (it uses a
043:         * TestingHttpSession for session data) - this makes 
044:         * it useful for running containerless unit tests on modules.</p>
045:         * <p>Copyright: Copyright (c) 2003</p>
046:         * <p>Company: CSIR</p>
047:         * @author tfogwill
048:         * @version 1.0
049:         */
050:        public class TestingHttpServletRequest implements  HttpServletRequest {
051:
052:            private HttpSession session;
053:            private Map attributes;
054:            private Map parameters;
055:            private boolean streamOpened;
056:
057:            public TestingHttpServletRequest(Map sessionData, Map parameters,
058:                    Map attributes) {
059:                this .parameters = parameters;
060:                this .attributes = attributes;
061:                this .session = new TestingHttpSession(sessionData);
062:            }
063:
064:            /* (non-Javadoc)
065:             * @see javax.servlet.http.HttpServletRequest#getAuthType()
066:             */
067:            public String getAuthType() {
068:                //one of BASIC, SSL, or null
069:                return null;
070:            }
071:
072:            /* (non-Javadoc)
073:             * @see javax.servlet.http.HttpServletRequest#getCookies()
074:             */
075:            public Cookie[] getCookies() {
076:                return null;
077:            }
078:
079:            /* (non-Javadoc)
080:             * @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
081:             */
082:            public long getDateHeader(String arg0) {
083:                return -1;
084:            }
085:
086:            /* (non-Javadoc)
087:             * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
088:             */
089:            public String getHeader(String arg0) {
090:                return null;
091:            }
092:
093:            /* (non-Javadoc)
094:             * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
095:             */
096:            public Enumeration getHeaders(String arg0) {
097:                return null;
098:            }
099:
100:            /* (non-Javadoc)
101:             * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
102:             */
103:            public Enumeration getHeaderNames() {
104:                return null;
105:            }
106:
107:            /* (non-Javadoc)
108:             * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
109:             */
110:            public int getIntHeader(String arg0) {
111:                return -1;
112:            }
113:
114:            /* (non-Javadoc)
115:             * @see javax.servlet.http.HttpServletRequest#getMethod()
116:             */
117:            public String getMethod() {
118:                //one of GET, PUT, POST
119:                return "POST";
120:            }
121:
122:            /* (non-Javadoc)
123:             * @see javax.servlet.http.HttpServletRequest#getPathInfo()
124:             */
125:            public String getPathInfo() {
126:                return null;
127:            }
128:
129:            /* (non-Javadoc)
130:             * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
131:             */
132:            public String getPathTranslated() {
133:                return null;
134:            }
135:
136:            /* (non-Javadoc)
137:             * @see javax.servlet.http.HttpServletRequest#getContextPath()
138:             */
139:            public String getContextPath() {
140:                return "";
141:            }
142:
143:            /* (non-Javadoc)
144:             * @see javax.servlet.http.HttpServletRequest#getQueryString()
145:             */
146:            public String getQueryString() {
147:                return null;
148:            }
149:
150:            /* (non-Javadoc)
151:             * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
152:             */
153:            public String getRemoteUser() {
154:                return null;
155:            }
156:
157:            /* (non-Javadoc)
158:             * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
159:             */
160:            public boolean isUserInRole(String arg0) {
161:                return true;
162:            }
163:
164:            /* (non-Javadoc)
165:             * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
166:             */
167:            public Principal getUserPrincipal() {
168:                return null;
169:            }
170:
171:            /* (non-Javadoc)
172:             * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
173:             */
174:            public String getRequestedSessionId() {
175:                return "sessionID";
176:            }
177:
178:            /* (non-Javadoc)
179:             * @see javax.servlet.http.HttpServletRequest#getRequestURI()
180:             */
181:            public String getRequestURI() {
182:                return "/unit-testing";
183:            }
184:
185:            /* (non-Javadoc)
186:             * @see javax.servlet.http.HttpServletRequest#getRequestURL()
187:             */
188:            public StringBuffer getRequestURL() {
189:                return new StringBuffer("/unit-testing");
190:            }
191:
192:            /* (non-Javadoc)
193:             * @see javax.servlet.http.HttpServletRequest#getServletPath()
194:             */
195:            public String getServletPath() {
196:                return getRequestURL().toString();
197:            }
198:
199:            /* (non-Javadoc)
200:             * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
201:             */
202:            public HttpSession getSession(boolean arg0) {
203:                if (arg0) {
204:                    if (session == null) {
205:                        session = new TestingHttpSession(new HashMap());
206:                    }
207:                }
208:                return session;
209:            }
210:
211:            /* (non-Javadoc)
212:             * @see javax.servlet.http.HttpServletRequest#getSession()
213:             */
214:            public HttpSession getSession() {
215:                return session;
216:            }
217:
218:            /* (non-Javadoc)
219:             * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
220:             */
221:            public boolean isRequestedSessionIdValid() {
222:                return true;
223:            }
224:
225:            /* (non-Javadoc)
226:             * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
227:             */
228:            public boolean isRequestedSessionIdFromCookie() {
229:                return true;
230:            }
231:
232:            /* (non-Javadoc)
233:             * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
234:             */
235:            public boolean isRequestedSessionIdFromURL() {
236:                return false;
237:            }
238:
239:            /* (non-Javadoc)
240:             * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
241:             */
242:            public boolean isRequestedSessionIdFromUrl() {
243:                return isRequestedSessionIdFromURL();
244:            }
245:
246:            /* (non-Javadoc)
247:             * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
248:             */
249:            public Object getAttribute(String arg0) {
250:                return attributes.get(arg0);
251:            }
252:
253:            /* (non-Javadoc)
254:             * @see javax.servlet.ServletRequest#getAttributeNames()
255:             */
256:            public Enumeration getAttributeNames() {
257:                Set keys = attributes.keySet();
258:                return new Vector(keys).elements();
259:            }
260:
261:            /* (non-Javadoc)
262:             * @see javax.servlet.ServletRequest#getCharacterEncoding()
263:             */
264:            public String getCharacterEncoding() {
265:                return null;
266:            }
267:
268:            /* (non-Javadoc)
269:             * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
270:             */
271:            public void setCharacterEncoding(String arg0)
272:                    throws UnsupportedEncodingException {
273:                throw new UnsupportedOperationException();
274:            }
275:
276:            /* (non-Javadoc)
277:             * @see javax.servlet.ServletRequest#getContentLength()
278:             */
279:            public int getContentLength() {
280:                return -1;
281:            }
282:
283:            /* (non-Javadoc)
284:             * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
285:             */
286:            public String getLocalName() {
287:                throw new UnsupportedOperationException();
288:            }
289:
290:            /* (non-Javadoc)
291:             * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
292:             */
293:            public String getLocalAddr() {
294:                throw new UnsupportedOperationException();
295:            }
296:
297:            /* (non-Javadoc)
298:             * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
299:             */
300:            public int getRemotePort() {
301:                throw new UnsupportedOperationException();
302:            }
303:
304:            /* (non-Javadoc)
305:             * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
306:             */
307:            public int getLocalPort() {
308:                throw new UnsupportedOperationException();
309:            }
310:
311:            /* (non-Javadoc)
312:             * @see javax.servlet.ServletRequest#getContentType()
313:             */
314:            public String getContentType() {
315:                return null;
316:            }
317:
318:            /* (non-Javadoc)
319:             * @see javax.servlet.ServletRequest#getInputStream()
320:             */
321:            public ServletInputStream getInputStream() throws IOException {
322:                if (streamOpened)
323:                    throw new IllegalStateException("Reader already opened!");
324:                streamOpened = true;
325:                return null;
326:            }
327:
328:            /* (non-Javadoc)
329:             * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
330:             */
331:            public String getParameter(String arg0) {
332:                String[] vals = getParameterValues(arg0);
333:                if (vals == null || vals.length == 0) {
334:                    return null;
335:                } else
336:                    return vals[0];
337:            }
338:
339:            /* (non-Javadoc)
340:             * @see javax.servlet.ServletRequest#getParameterNames()
341:             */
342:            public Enumeration getParameterNames() {
343:                return new Vector(parameters.keySet()).elements();
344:            }
345:
346:            /* (non-Javadoc)
347:             * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
348:             */
349:            public String[] getParameterValues(String arg0) {
350:                Object obj = parameters.get(arg0);
351:                if (obj == null) {
352:                    return null;
353:                } else if (obj instanceof  String[]) {
354:                    return (String[]) obj;
355:                } else if (obj instanceof  String) {
356:                    return new String[] { (String) obj };
357:                } else
358:                    return null;
359:            }
360:
361:            /* (non-Javadoc)
362:             * @see javax.servlet.ServletRequest#getParameterMap()
363:             */
364:            public Map getParameterMap() {
365:                return parameters;
366:            }
367:
368:            /* (non-Javadoc)
369:             * @see javax.servlet.ServletRequest#getProtocol()
370:             */
371:            public String getProtocol() {
372:                return "HTTP/1.1";
373:            }
374:
375:            /* (non-Javadoc)
376:             * @see javax.servlet.ServletRequest#getScheme()
377:             */
378:            public String getScheme() {
379:                return "http";
380:            }
381:
382:            /* (non-Javadoc)
383:             * @see javax.servlet.ServletRequest#getServerName()
384:             */
385:            public String getServerName() {
386:                return "unit-testing";
387:            }
388:
389:            /* (non-Javadoc)
390:             * @see javax.servlet.ServletRequest#getServerPort()
391:             */
392:            public int getServerPort() {
393:                return 8080;
394:            }
395:
396:            /* (non-Javadoc)
397:             * @see javax.servlet.ServletRequest#getReader()
398:             */
399:            public BufferedReader getReader() throws IOException {
400:                if (streamOpened)
401:                    throw new IllegalStateException("Stream already opened!");
402:                streamOpened = true;
403:                return null;
404:            }
405:
406:            /* (non-Javadoc)
407:             * @see javax.servlet.ServletRequest#getRemoteAddr()
408:             */
409:            public String getRemoteAddr() {
410:                return "127.0.0.1";
411:            }
412:
413:            /* (non-Javadoc)
414:             * @see javax.servlet.ServletRequest#getRemoteHost()
415:             */
416:            public String getRemoteHost() {
417:                return "localhost";
418:            }
419:
420:            /* (non-Javadoc)
421:             * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
422:             */
423:            public void setAttribute(String arg0, Object arg1) {
424:                attributes.put(arg0, arg1);
425:            }
426:
427:            /* (non-Javadoc)
428:             * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
429:             */
430:            public void removeAttribute(String arg0) {
431:                attributes.remove(arg0);
432:
433:            }
434:
435:            /* (non-Javadoc)
436:             * @see javax.servlet.ServletRequest#getLocale()
437:             */
438:            public Locale getLocale() {
439:                return Locale.getDefault();
440:            }
441:
442:            /* (non-Javadoc)
443:             * @see javax.servlet.ServletRequest#getLocales()
444:             */
445:            public Enumeration getLocales() {
446:                return new Enumeration() {
447:                    Locale[] locales = new Locale[] { Locale.getDefault() };
448:                    int index = 0;
449:
450:                    public boolean hasMoreElements() {
451:                        return index < locales.length;
452:                    }
453:
454:                    public Object nextElement() {
455:                        return locales[index++];
456:                    }
457:                };
458:            }
459:
460:            /* (non-Javadoc)
461:             * @see javax.servlet.ServletRequest#isSecure()
462:             */
463:            public boolean isSecure() {
464:                return false;
465:            }
466:
467:            /* (non-Javadoc)
468:             * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
469:             */
470:            public RequestDispatcher getRequestDispatcher(String arg0) {
471:                throw new UnsupportedOperationException();
472:            }
473:
474:            /* (non-Javadoc)
475:             * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
476:             */
477:            public String getRealPath(String arg0) {
478:                throw new UnsupportedOperationException();
479:            }
480:
481:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.