Source Code Cross Referenced for MockPageContextTest.java in  » Testing » mockrunner-0.4 » com » mockrunner » test » web » 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 » Testing » mockrunner 0.4 » com.mockrunner.test.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.mockrunner.test.web;
002:
003:        import java.io.IOException;
004:        import java.io.StringWriter;
005:        import java.util.Enumeration;
006:
007:        import javax.servlet.jsp.JspWriter;
008:        import javax.servlet.jsp.PageContext;
009:
010:        import com.mockrunner.base.BaseTestCase;
011:        import com.mockrunner.mock.web.MockBodyContent;
012:        import com.mockrunner.mock.web.MockExpressionEvaluator;
013:        import com.mockrunner.mock.web.MockHttpServletRequest;
014:        import com.mockrunner.mock.web.MockHttpServletResponse;
015:        import com.mockrunner.mock.web.MockJspWriter;
016:        import com.mockrunner.mock.web.MockPageContext;
017:        import com.mockrunner.mock.web.MockRequestDispatcher;
018:        import com.mockrunner.mock.web.MockServletConfig;
019:        import com.mockrunner.mock.web.MockVariableResolver;
020:
021:        public class MockPageContextTest extends BaseTestCase {
022:            private MockPageContext pageContext;
023:
024:            protected void setUp() throws Exception {
025:                super .setUp();
026:                pageContext = getWebMockObjectFactory().getMockPageContext();
027:            }
028:
029:            public void testPushPopBody() throws Exception {
030:                JspWriter writer = pageContext.getOut();
031:                writer.print("TestOut");
032:                MockBodyContent content1 = (MockBodyContent) pageContext
033:                        .pushBody();
034:                assertTrue(writer == content1.getEnclosingWriter());
035:                content1.clear();
036:                content1.print("TestContent");
037:                assertEquals("TestContent", content1.getString());
038:                StringWriter outWriter = new StringWriter();
039:                content1.writeOut(outWriter);
040:                assertEquals("TestContent", outWriter.toString());
041:                MockBodyContent content2 = (MockBodyContent) pageContext
042:                        .pushBody();
043:                assertTrue(content1 == content2.getEnclosingWriter());
044:                assertEquals("", content2.getString());
045:                pageContext.popBody();
046:                writer = pageContext.getOut();
047:                assertEquals("TestContent", ((MockBodyContent) writer)
048:                        .getOutputAsString());
049:                pageContext.popBody();
050:                writer = pageContext.getOut();
051:                assertEquals("TestOut", ((MockJspWriter) writer)
052:                        .getOutputAsString());
053:            }
054:
055:            public void testPushBodyWithWriter() throws Exception {
056:                JspWriter writer1 = pageContext.getOut();
057:                StringWriter providedWriter = new StringWriter();
058:                JspWriter writer2 = pageContext.pushBody(providedWriter);
059:                writer1.print("writer1");
060:                pageContext.getOut().print("writer");
061:                pageContext.getOut().print(2);
062:                writer2.print("writer2");
063:                MockBodyContent content3 = (MockBodyContent) pageContext
064:                        .pushBody();
065:                content3.print("writer3");
066:                pageContext.getOut().print("writer");
067:                pageContext.getOut().print(3);
068:                pageContext.getOut().print("writer3");
069:                pageContext.popBody();
070:                pageContext.getOut().print("anotherWriter2Text");
071:                pageContext.popBody();
072:                pageContext.getOut().print("anotherWriter1Text");
073:                writer1.flush();
074:                writer2.flush();
075:                assertEquals("writer1anotherWriter1Text",
076:                        ((MockJspWriter) writer1).getOutputAsString());
077:                assertEquals("", ((MockJspWriter) writer2).getOutputAsString());
078:                assertEquals("writer2writer2anotherWriter2Text", providedWriter
079:                        .toString());
080:                assertEquals("writer3writer3writer3", content3.getString());
081:            }
082:
083:            public void testGetAttribute() throws Exception {
084:                pageContext.setAttribute("pageKey", "pageValue");
085:                getWebMockObjectFactory().getMockRequest().setAttribute(
086:                        "requestKey", "requestValue");
087:                getWebMockObjectFactory().getMockSession().setAttribute(
088:                        "sessionKey", "sessionValue");
089:                getWebMockObjectFactory().getMockServletContext().setAttribute(
090:                        "contextKey", "contextValue");
091:                assertEquals("pageValue", pageContext.getAttribute("pageKey"));
092:                assertEquals("pageValue", pageContext.getAttribute("pageKey",
093:                        PageContext.PAGE_SCOPE));
094:                assertEquals("requestValue", pageContext.getAttribute(
095:                        "requestKey", PageContext.REQUEST_SCOPE));
096:                assertEquals("sessionValue", pageContext.getAttribute(
097:                        "sessionKey", PageContext.SESSION_SCOPE));
098:                assertEquals("contextValue", pageContext.getAttribute(
099:                        "contextKey", PageContext.APPLICATION_SCOPE));
100:                assertNull(pageContext.getAttribute("pageKey1"));
101:                assertNull(pageContext.getAttribute("pageKey1",
102:                        PageContext.PAGE_SCOPE));
103:                assertNull(pageContext.getAttribute("requestKey1",
104:                        PageContext.REQUEST_SCOPE));
105:                assertNull(pageContext.getAttribute("sessionKey1",
106:                        PageContext.SESSION_SCOPE));
107:                assertNull(pageContext.getAttribute("contextKey1",
108:                        PageContext.APPLICATION_SCOPE));
109:                pageContext.setServletRequest(null);
110:                assertNull(pageContext.getAttribute("requestKey",
111:                        PageContext.REQUEST_SCOPE));
112:                assertNull(pageContext.getAttribute("sessionKey",
113:                        PageContext.SESSION_SCOPE));
114:                assertEquals("contextValue", pageContext.getAttribute(
115:                        "contextKey", PageContext.APPLICATION_SCOPE));
116:                MockHttpServletRequest otherRequest = new MockHttpServletRequest();
117:                otherRequest.setAttribute("otherRequestKey",
118:                        "otherRequestValue");
119:                pageContext.setServletRequest(otherRequest);
120:                assertEquals("otherRequestValue", pageContext.getAttribute(
121:                        "otherRequestKey", PageContext.REQUEST_SCOPE));
122:                assertNull(pageContext.getAttribute("sessionKey",
123:                        PageContext.SESSION_SCOPE));
124:                pageContext.setServletConfig(null);
125:                assertNull(pageContext.getAttribute("contextKey",
126:                        PageContext.APPLICATION_SCOPE));
127:                pageContext.setServletConfig(new MockServletConfig());
128:                assertNull(pageContext.getAttribute("contextKey",
129:                        PageContext.APPLICATION_SCOPE));
130:                try {
131:                    pageContext.getAttribute("contextKey", 60000);
132:                    fail();
133:                } catch (IllegalArgumentException exc) {
134:                    //should throw exception
135:                }
136:            }
137:
138:            public void testSetAttribute() throws Exception {
139:                pageContext.setAttribute("pageKey", "pageValue");
140:                pageContext.setAttribute("requestKey", "requestValue",
141:                        PageContext.REQUEST_SCOPE);
142:                pageContext.setAttribute("sessionKey", "sessionValue",
143:                        PageContext.SESSION_SCOPE);
144:                pageContext.setAttribute("contextKey", "contextValue",
145:                        PageContext.APPLICATION_SCOPE);
146:                assertEquals("pageValue", pageContext.getAttribute("pageKey"));
147:                assertEquals("requestValue", getWebMockObjectFactory()
148:                        .getMockRequest().getAttribute("requestKey"));
149:                assertEquals("sessionValue", getWebMockObjectFactory()
150:                        .getMockSession().getAttribute("sessionKey"));
151:                assertEquals("contextValue", getWebMockObjectFactory()
152:                        .getMockServletContext().getAttribute("contextKey"));
153:                pageContext.setServletRequest(null);
154:                pageContext.setAttribute("requestKey1", "requestValue1",
155:                        PageContext.REQUEST_SCOPE);
156:                pageContext.setAttribute("sessionKey1", "sessionValue1",
157:                        PageContext.SESSION_SCOPE);
158:                assertNull(getWebMockObjectFactory().getMockRequest()
159:                        .getAttribute("requestKey1"));
160:                assertNull(getWebMockObjectFactory().getMockSession()
161:                        .getAttribute("sessionKey1"));
162:                pageContext.setServletRequest(new MockHttpServletRequest());
163:                pageContext.setAttribute("sessionKey1", "sessionValue1",
164:                        PageContext.SESSION_SCOPE);
165:                assertNull(getWebMockObjectFactory().getMockSession()
166:                        .getAttribute("sessionKey1"));
167:                pageContext.setServletConfig(null);
168:                pageContext.setAttribute("contextKey1", "contextValue1",
169:                        PageContext.APPLICATION_SCOPE);
170:                assertNull(getWebMockObjectFactory().getMockServletContext()
171:                        .getAttribute("contextKey1"));
172:                try {
173:                    pageContext.setAttribute("requestKey", "requestValue",
174:                            60000);
175:                    fail();
176:                } catch (IllegalArgumentException exc) {
177:                    //should throw exception
178:                }
179:            }
180:
181:            public void testRemoveAttribute() throws Exception {
182:                pageContext.setAttribute("pageKey", "pageValue");
183:                pageContext.removeAttribute("pageKey",
184:                        PageContext.APPLICATION_SCOPE);
185:                assertNotNull(pageContext.getAttribute("pageKey"));
186:                pageContext.removeAttribute("pageKey");
187:                assertNull(pageContext.getAttribute("pageKey"));
188:                pageContext.setAttribute("requestKey", "requestValue",
189:                        PageContext.REQUEST_SCOPE);
190:                pageContext.removeAttribute("requestKey",
191:                        PageContext.SESSION_SCOPE);
192:                assertNotNull(pageContext.getAttribute("requestKey",
193:                        PageContext.REQUEST_SCOPE));
194:                pageContext.removeAttribute("requestKey",
195:                        PageContext.REQUEST_SCOPE);
196:                assertNull(pageContext.getAttribute("requestKey",
197:                        PageContext.REQUEST_SCOPE));
198:                pageContext.setAttribute("sessionKey", "sessionValue",
199:                        PageContext.SESSION_SCOPE);
200:                pageContext.removeAttribute("sessionKey",
201:                        PageContext.PAGE_SCOPE);
202:                assertNotNull(pageContext.getAttribute("sessionKey",
203:                        PageContext.SESSION_SCOPE));
204:                pageContext.removeAttribute("sessionKey",
205:                        PageContext.SESSION_SCOPE);
206:                assertNull(pageContext.getAttribute("sessionKey",
207:                        PageContext.SESSION_SCOPE));
208:                pageContext.setAttribute("contextKey", "contextValue",
209:                        PageContext.APPLICATION_SCOPE);
210:                pageContext.removeAttribute("contextKey",
211:                        PageContext.REQUEST_SCOPE);
212:                assertNotNull(pageContext.getAttribute("contextKey",
213:                        PageContext.APPLICATION_SCOPE));
214:                pageContext.removeAttribute("contextKey",
215:                        PageContext.APPLICATION_SCOPE);
216:                assertNull(pageContext.getAttribute("contextKey",
217:                        PageContext.APPLICATION_SCOPE));
218:                getWebMockObjectFactory().getMockRequest().setAttribute(
219:                        "requestKey1", "requestValue1");
220:                getWebMockObjectFactory().getMockSession().setAttribute(
221:                        "sessionKey1", "sessionValue1");
222:                pageContext.setServletRequest(null);
223:                pageContext.removeAttribute("requestKey1",
224:                        PageContext.REQUEST_SCOPE);
225:                pageContext.removeAttribute("sessionKey1",
226:                        PageContext.SESSION_SCOPE);
227:                assertNotNull(getWebMockObjectFactory().getMockRequest()
228:                        .getAttribute("requestKey1"));
229:                assertNotNull(getWebMockObjectFactory().getMockSession()
230:                        .getAttribute("sessionKey1"));
231:                getWebMockObjectFactory().getMockRequest().setSession(null);
232:                pageContext.setServletRequest(getWebMockObjectFactory()
233:                        .getMockRequest());
234:                pageContext.removeAttribute("requestKey1",
235:                        PageContext.REQUEST_SCOPE);
236:                pageContext.removeAttribute("sessionKey1",
237:                        PageContext.SESSION_SCOPE);
238:                assertNull(getWebMockObjectFactory().getMockRequest()
239:                        .getAttribute("requestKey1"));
240:                assertNotNull(getWebMockObjectFactory().getMockSession()
241:                        .getAttribute("sessionKey1"));
242:                getWebMockObjectFactory().getMockServletContext().setAttribute(
243:                        "contextKey1", "contextValue1");
244:                pageContext.setServletConfig(null);
245:                pageContext.removeAttribute("contextKey1",
246:                        PageContext.APPLICATION_SCOPE);
247:                assertNotNull(getWebMockObjectFactory().getMockServletContext()
248:                        .getAttribute("contextKey1"));
249:                try {
250:                    pageContext.removeAttribute("requestKey", 60000);
251:                    fail();
252:                } catch (IllegalArgumentException exc) {
253:                    //should throw exception
254:                }
255:            }
256:
257:            public void testFindAttribute() {
258:                assertNull(pageContext.findAttribute("attribute"));
259:                getWebMockObjectFactory().getMockServletContext().setAttribute(
260:                        "attribute", "contextValue");
261:                assertEquals("contextValue", pageContext
262:                        .findAttribute("attribute"));
263:                getWebMockObjectFactory().getMockSession().setAttribute(
264:                        "attribute", "sessionValue");
265:                assertEquals("sessionValue", pageContext
266:                        .findAttribute("attribute"));
267:                getWebMockObjectFactory().getMockRequest().setAttribute(
268:                        "attribute", "requestValue");
269:                assertEquals("requestValue", pageContext
270:                        .findAttribute("attribute"));
271:                pageContext.setAttribute("attribute", "pageValue");
272:                assertEquals("pageValue", pageContext
273:                        .findAttribute("attribute"));
274:                pageContext.setAttribute("attribute", null);
275:                pageContext.setServletRequest(null);
276:                assertEquals("contextValue", pageContext
277:                        .findAttribute("attribute"));
278:                pageContext.setServletConfig(null);
279:                assertNull(pageContext.findAttribute("attribute"));
280:            }
281:
282:            public void testGetAttributesScope() {
283:                assertEquals(0, pageContext.getAttributesScope("attribute"));
284:                getWebMockObjectFactory().getMockServletContext().setAttribute(
285:                        "attribute", "contextValue");
286:                assertEquals(PageContext.APPLICATION_SCOPE, pageContext
287:                        .getAttributesScope("attribute"));
288:                getWebMockObjectFactory().getMockSession().setAttribute(
289:                        "attribute", "sessionValue");
290:                assertEquals(PageContext.SESSION_SCOPE, pageContext
291:                        .getAttributesScope("attribute"));
292:                getWebMockObjectFactory().getMockRequest().setAttribute(
293:                        "attribute", "requestValue");
294:                assertEquals(PageContext.REQUEST_SCOPE, pageContext
295:                        .getAttributesScope("attribute"));
296:                pageContext.setAttribute("attribute", "pageValue");
297:                assertEquals(PageContext.PAGE_SCOPE, pageContext
298:                        .getAttributesScope("attribute"));
299:                pageContext.setAttribute("attribute", null);
300:                pageContext.setServletRequest(null);
301:                assertEquals(PageContext.APPLICATION_SCOPE, pageContext
302:                        .getAttributesScope("attribute"));
303:                pageContext.setServletConfig(null);
304:                assertEquals(0, pageContext.getAttributesScope("attribute"));
305:            }
306:
307:            public void testGetAttributeNamesInScope() {
308:                getWebMockObjectFactory().getMockRequest().clearAttributes();
309:                assertFalse(pageContext.getAttributeNamesInScope(
310:                        PageContext.PAGE_SCOPE).hasMoreElements());
311:                assertFalse(pageContext.getAttributeNamesInScope(
312:                        PageContext.REQUEST_SCOPE).hasMoreElements());
313:                assertFalse(pageContext.getAttributeNamesInScope(
314:                        PageContext.SESSION_SCOPE).hasMoreElements());
315:                assertFalse(pageContext.getAttributeNamesInScope(
316:                        PageContext.APPLICATION_SCOPE).hasMoreElements());
317:                pageContext.setAttribute("pageKey", "pageValue");
318:                getWebMockObjectFactory().getMockRequest().setAttribute(
319:                        "requestKey", "requestValue");
320:                getWebMockObjectFactory().getMockSession().setAttribute(
321:                        "sessionKey", "sessionValue");
322:                getWebMockObjectFactory().getMockServletContext().setAttribute(
323:                        "contextKey", "contextValue");
324:                Enumeration pageEnumeration = pageContext
325:                        .getAttributeNamesInScope(PageContext.PAGE_SCOPE);
326:                assertEquals("pageKey", pageEnumeration.nextElement());
327:                assertFalse(pageEnumeration.hasMoreElements());
328:                Enumeration requestEnumeration = pageContext
329:                        .getAttributeNamesInScope(PageContext.REQUEST_SCOPE);
330:                assertEquals("requestKey", requestEnumeration.nextElement());
331:                assertFalse(requestEnumeration.hasMoreElements());
332:                Enumeration sessionEnumeration = pageContext
333:                        .getAttributeNamesInScope(PageContext.SESSION_SCOPE);
334:                assertEquals("sessionKey", sessionEnumeration.nextElement());
335:                assertFalse(sessionEnumeration.hasMoreElements());
336:                Enumeration contextEnumeration = pageContext
337:                        .getAttributeNamesInScope(PageContext.APPLICATION_SCOPE);
338:                assertEquals("contextKey", contextEnumeration.nextElement());
339:                assertFalse(contextEnumeration.hasMoreElements());
340:                pageContext.setServletRequest(null);
341:                assertFalse(pageContext.getAttributeNamesInScope(
342:                        PageContext.REQUEST_SCOPE).hasMoreElements());
343:                assertFalse(pageContext.getAttributeNamesInScope(
344:                        PageContext.SESSION_SCOPE).hasMoreElements());
345:                pageContext.setServletConfig(null);
346:                assertFalse(pageContext.getAttributeNamesInScope(
347:                        PageContext.APPLICATION_SCOPE).hasMoreElements());
348:                try {
349:                    pageContext.getAttributeNamesInScope(60000);
350:                    fail();
351:                } catch (IllegalArgumentException exc) {
352:                    //should throw exception
353:                }
354:            }
355:
356:            public void testForward() throws Exception {
357:                MockHttpServletRequest request = new MockHttpServletRequest();
358:                MockHttpServletResponse response = new MockHttpServletResponse();
359:                pageContext.setServletRequest(request);
360:                pageContext.setServletResponse(response);
361:                pageContext.forward("aPath");
362:                MockRequestDispatcher dispatcher = (MockRequestDispatcher) request
363:                        .getRequestDispatcher("aPath");
364:                assertSame(request, dispatcher.getForwardedRequest());
365:                assertSame(response, dispatcher.getForwardedResponse());
366:            }
367:
368:            public void testInclude() throws Exception {
369:                MockHttpServletRequest request = new MockHttpServletRequest();
370:                MockHttpServletResponse response = new MockHttpServletResponse();
371:                pageContext.setServletRequest(request);
372:                pageContext.setServletResponse(response);
373:                pageContext.include("aPath");
374:                MockRequestDispatcher dispatcher = (MockRequestDispatcher) request
375:                        .getRequestDispatcher("aPath");
376:                assertSame(request, dispatcher.getIncludedRequest());
377:                assertSame(response, dispatcher.getIncludedResponse());
378:            }
379:
380:            public void testIncludeWithFlush() throws Exception {
381:                MockHttpServletRequest request = new MockHttpServletRequest();
382:                MockHttpServletResponse response = new MockHttpServletResponse();
383:                TestJspWriter jspWriter = new TestJspWriter();
384:                pageContext.setServletRequest(request);
385:                pageContext.setServletResponse(response);
386:                pageContext.setJspWriter(jspWriter);
387:                pageContext.include("aPath", false);
388:                MockRequestDispatcher dispatcher = (MockRequestDispatcher) request
389:                        .getRequestDispatcher("aPath");
390:                assertSame(request, dispatcher.getIncludedRequest());
391:                assertSame(response, dispatcher.getIncludedResponse());
392:                assertFalse(jspWriter.isFlushed());
393:                pageContext.include("aPath", true);
394:                assertTrue(jspWriter.isFlushed());
395:            }
396:
397:            public void testExpressionEvaluatorAndVariable() throws Exception {
398:                assertTrue(pageContext.getExpressionEvaluator() instanceof  MockExpressionEvaluator);
399:                MockExpressionEvaluator evaluator = new MockExpressionEvaluator() {
400:                };
401:                pageContext.setExpressionEvaluator(evaluator);
402:                assertSame(evaluator, pageContext.getExpressionEvaluator());
403:                assertTrue(pageContext.getVariableResolver() instanceof  MockVariableResolver);
404:                MockVariableResolver resolver = new MockVariableResolver() {
405:                };
406:                pageContext.setVariableResolver(resolver);
407:                assertSame(resolver, pageContext.getVariableResolver());
408:            }
409:
410:            private class TestJspWriter extends MockJspWriter {
411:                private boolean flushed = false;
412:
413:                public boolean isFlushed() {
414:                    return flushed;
415:                }
416:
417:                public void flush() throws IOException {
418:                    super .flush();
419:                    flushed = true;
420:                }
421:            }
422:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.