Source Code Cross Referenced for ServletTestModuleTest.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:
005:        import javax.servlet.Filter;
006:        import javax.servlet.FilterChain;
007:        import javax.servlet.FilterConfig;
008:        import javax.servlet.ServletConfig;
009:        import javax.servlet.ServletException;
010:        import javax.servlet.ServletRequest;
011:        import javax.servlet.ServletResponse;
012:        import javax.servlet.http.HttpServlet;
013:        import javax.servlet.http.HttpServletRequest;
014:        import javax.servlet.http.HttpServletRequestWrapper;
015:        import javax.servlet.http.HttpServletResponse;
016:        import javax.servlet.http.HttpServletResponseWrapper;
017:
018:        import com.mockrunner.base.BaseTestCase;
019:        import com.mockrunner.base.VerifyFailedException;
020:        import com.mockrunner.mock.web.MockFilterChain;
021:        import com.mockrunner.servlet.ServletTestModule;
022:
023:        public class ServletTestModuleTest extends BaseTestCase {
024:            private ServletTestModule module;
025:
026:            protected void setUp() throws Exception {
027:                super .setUp();
028:                module = new ServletTestModule(getWebMockObjectFactory());
029:            }
030:
031:            public void testServletInitCalls() throws Exception {
032:                TestServlet servlet = (TestServlet) module
033:                        .createServlet(TestServlet.class);
034:                assertTrue(servlet.wasInitCalled());
035:                servlet = new TestServlet();
036:                module.setServlet(servlet);
037:                assertFalse(servlet.wasInitCalled());
038:                assertNull(servlet.getPassedServletConfig());
039:                servlet = new TestServlet();
040:                module.setServlet(servlet, false);
041:                assertFalse(servlet.wasInitCalled());
042:                assertNull(servlet.getPassedServletConfig());
043:                servlet = new TestServlet();
044:                module.setServlet(servlet, true);
045:                assertTrue(servlet.wasInitCalled());
046:                assertSame(getWebMockObjectFactory().getMockServletConfig(),
047:                        servlet.getPassedServletConfig());
048:                servlet = new TestServlet();
049:                module.setServlet(servlet, false);
050:                module.init();
051:                assertTrue(servlet.wasInitCalled());
052:                assertSame(getWebMockObjectFactory().getMockServletConfig(),
053:                        servlet.getPassedServletConfig());
054:            }
055:
056:            public void testFilterInitCalls() throws Exception {
057:                TestFilter filter1 = (TestFilter) module
058:                        .createFilter(TestFilter.class);
059:                assertTrue(filter1.wasInitCalled());
060:                TestFilter filter2 = new TestFilter();
061:                module.addFilter(filter2);
062:                assertFalse(filter2.wasInitCalled());
063:                TestFilter filter3 = new TestFilter();
064:                module.addFilter(filter3, false);
065:                assertFalse(filter3.wasInitCalled());
066:                TestFilter filter4 = new TestFilter();
067:                module.addFilter(filter4, true);
068:                assertTrue(filter4.wasInitCalled());
069:            }
070:
071:            public void testVerifyOutput() throws Exception {
072:                getWebMockObjectFactory().getMockResponse().getWriter().write(
073:                        "This is a test");
074:                try {
075:                    module.verifyOutput("this is a test");
076:                    fail();
077:                } catch (VerifyFailedException exc) {
078:                    //should throw exception
079:                }
080:                module.setCaseSensitive(false);
081:                module.verifyOutput("this is a test");
082:                module.verifyOutputContains("TeSt");
083:                module.verifyOutputRegularExpression("THIS.*");
084:                module.setCaseSensitive(true);
085:                try {
086:                    module.verifyOutputContains("THIS");
087:                    fail();
088:                } catch (VerifyFailedException exc) {
089:                    //should throw exception
090:                }
091:                try {
092:                    module.verifyOutputRegularExpression(".*TEST");
093:                    fail();
094:                } catch (VerifyFailedException exc) {
095:                    //should throw exception
096:                }
097:            }
098:
099:            public void testServletMethodsCalled() {
100:                TestServlet servlet = (TestServlet) module
101:                        .createServlet(TestServlet.class);
102:                module.doDelete();
103:                assertTrue(servlet.wasDoDeleteCalled());
104:                module.doGet();
105:                assertTrue(servlet.wasDoGetCalled());
106:                module.doOptions();
107:                assertTrue(servlet.wasDoOptionsCalled());
108:                module.doPost();
109:                assertTrue(servlet.wasDoPostCalled());
110:                module.doPut();
111:                assertTrue(servlet.wasDoPutCalled());
112:                module.doTrace();
113:                assertTrue(servlet.wasDoTraceCalled());
114:                module.doHead();
115:                assertTrue(servlet.wasDoHeadCalled());
116:            }
117:
118:            public void testFilterChain() {
119:                module.setServlet(new TestServlet(), true);
120:                TestServlet servlet = (TestServlet) module.getServlet();
121:                TestFilter filter1 = (TestFilter) module
122:                        .createFilter(TestFilter.class);
123:                TestFilter filter2 = (TestFilter) module
124:                        .createFilter(TestFilter.class);
125:                TestFilter filter3 = (TestFilter) module
126:                        .createFilter(TestFilter.class);
127:                module.doGet();
128:                assertTrue(filter1.wasInitCalled());
129:                assertTrue(filter2.wasInitCalled());
130:                assertTrue(filter3.wasInitCalled());
131:                assertFalse(filter1.wasDoFilterCalled());
132:                assertFalse(filter2.wasDoFilterCalled());
133:                assertFalse(filter3.wasDoFilterCalled());
134:                assertTrue(servlet.wasDoGetCalled());
135:                module.setDoChain(true);
136:                module.doGet();
137:                assertTrue(filter1.wasDoFilterCalled());
138:                assertTrue(filter2.wasDoFilterCalled());
139:                assertTrue(filter3.wasDoFilterCalled());
140:                assertSame(getWebMockObjectFactory().getMockFilterChain(),
141:                        filter1.getLastFilterChain());
142:                assertSame(getWebMockObjectFactory().getMockFilterChain(),
143:                        filter2.getLastFilterChain());
144:                assertSame(getWebMockObjectFactory().getMockFilterChain(),
145:                        filter3.getLastFilterChain());
146:                filter1.reset();
147:                filter2.reset();
148:                filter3.reset();
149:                module.setDoChain(true);
150:                module.doGet();
151:                assertTrue(filter1.wasDoFilterCalled());
152:                assertTrue(filter2.wasDoFilterCalled());
153:                assertTrue(filter3.wasDoFilterCalled());
154:            }
155:
156:            public void testFilterChainWithRelease() {
157:                TestServlet servlet = (TestServlet) module
158:                        .createServlet(TestServlet.class);
159:                TestFilter filter = new TestFilter();
160:                module.setDoChain(true);
161:                module.addFilter(filter, true);
162:                module.doPost();
163:                assertTrue(servlet.wasInitCalled());
164:                assertTrue(servlet.wasDoPostCalled());
165:                assertTrue(filter.wasInitCalled());
166:                assertTrue(filter.wasDoFilterCalled());
167:                servlet.reset();
168:                filter.reset();
169:                module.releaseFilters();
170:                module.doPost();
171:                assertTrue(servlet.wasDoPostCalled());
172:                assertFalse(filter.wasInitCalled());
173:                assertFalse(filter.wasDoFilterCalled());
174:            }
175:
176:            public void testFilterChainWithWrapper() {
177:                TestFilter filter1 = new TestFilter();
178:                TestFilter filter2 = new TestFilter();
179:                module.addFilter(filter1);
180:                module.addFilter(filter2);
181:                HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(
182:                        getWebMockObjectFactory().getMockRequest());
183:                HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(
184:                        getWebMockObjectFactory().getMockResponse());
185:                filter1.setRequestWrapper(requestWrapper);
186:                filter2.setResponseWrapper(responseWrapper);
187:                module.setDoChain(true);
188:                module.doPost();
189:                MockFilterChain chain = getWebMockObjectFactory()
190:                        .getMockFilterChain();
191:                assertSame(requestWrapper, chain.getLastRequest());
192:                assertSame(responseWrapper, chain.getLastResponse());
193:                assertSame(requestWrapper, module.getFilteredRequest());
194:                assertSame(responseWrapper, module.getFilteredResponse());
195:                assertEquals(3, chain.getRequestList().size());
196:                assertEquals(3, chain.getResponseList().size());
197:                assertSame(getWebMockObjectFactory().getMockRequest(), chain
198:                        .getRequestList().get(0));
199:                assertSame(requestWrapper, chain.getRequestList().get(1));
200:                assertSame(requestWrapper, chain.getRequestList().get(2));
201:                assertSame(getWebMockObjectFactory().getMockResponse(), chain
202:                        .getResponseList().get(0));
203:                assertSame(getWebMockObjectFactory().getMockResponse(), chain
204:                        .getResponseList().get(1));
205:                assertSame(responseWrapper, chain.getResponseList().get(2));
206:            }
207:
208:            public void testWrappedRequestAndResponse() {
209:                HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(
210:                        getWebMockObjectFactory().getMockRequest());
211:                HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(
212:                        getWebMockObjectFactory().getMockResponse());
213:                getWebMockObjectFactory().addRequestWrapper(requestWrapper);
214:                getWebMockObjectFactory().addResponseWrapper(responseWrapper);
215:                module.createServlet(TestServlet.class);
216:                module.setDoChain(true);
217:                module.doPost();
218:                assertSame(requestWrapper, getWebMockObjectFactory()
219:                        .getMockFilterChain().getLastRequest());
220:                assertSame(responseWrapper, getWebMockObjectFactory()
221:                        .getMockFilterChain().getLastResponse());
222:            }
223:
224:            public void testDoFilterReset() {
225:                TestFilter filter1 = new TestFilter();
226:                SkipTestFilter filter2 = new SkipTestFilter();
227:                TestFilter filter3 = new TestFilter();
228:                module.addFilter(filter1);
229:                module.addFilter(filter2);
230:                module.addFilter(filter3);
231:                module.doFilter();
232:                assertTrue(filter1.wasDoFilterCalled());
233:                assertFalse(filter3.wasDoFilterCalled());
234:                module.doFilter();
235:                assertTrue(filter1.wasDoFilterCalled());
236:                assertFalse(filter3.wasDoFilterCalled());
237:            }
238:
239:            public static class SkipTestFilter implements  Filter {
240:                public void init(FilterConfig config) throws ServletException {
241:
242:                }
243:
244:                public void doFilter(ServletRequest request,
245:                        ServletResponse response, FilterChain chain)
246:                        throws IOException, ServletException {
247:
248:                }
249:
250:                public void destroy() {
251:
252:                }
253:            }
254:
255:            public static class TestFilter implements  Filter {
256:                private boolean initCalled = false;
257:                private boolean doFilterCalled = false;
258:                private HttpServletRequestWrapper requestWrapper;
259:                private HttpServletResponseWrapper responseWrapper;
260:                private FilterChain lastChain;
261:
262:                public void init(FilterConfig config) throws ServletException {
263:                    initCalled = true;
264:                }
265:
266:                public void doFilter(ServletRequest request,
267:                        ServletResponse response, FilterChain chain)
268:                        throws IOException, ServletException {
269:                    doFilterCalled = true;
270:                    lastChain = chain;
271:                    if (null != requestWrapper) {
272:                        request = requestWrapper;
273:                    }
274:                    if (null != responseWrapper) {
275:                        response = responseWrapper;
276:                    }
277:                    chain.doFilter(request, response);
278:                }
279:
280:                public void destroy() {
281:
282:                }
283:
284:                public void setRequestWrapper(
285:                        HttpServletRequestWrapper requestWrapper) {
286:                    this .requestWrapper = requestWrapper;
287:                }
288:
289:                public void setResponseWrapper(
290:                        HttpServletResponseWrapper responseWrapper) {
291:                    this .responseWrapper = responseWrapper;
292:                }
293:
294:                public boolean wasDoFilterCalled() {
295:                    return doFilterCalled;
296:                }
297:
298:                public boolean wasInitCalled() {
299:                    return initCalled;
300:                }
301:
302:                public FilterChain getLastFilterChain() {
303:                    return lastChain;
304:                }
305:
306:                public void reset() {
307:                    initCalled = false;
308:                    doFilterCalled = false;
309:                    lastChain = null;
310:                }
311:            }
312:
313:            public static class TestServlet extends HttpServlet {
314:                private boolean initCalled = false;
315:                private boolean doGetCalled = false;
316:                private boolean doPostCalled = false;
317:                private boolean doDeleteCalled = false;
318:                private boolean doOptionsCalled = false;
319:                private boolean doPutCalled = false;
320:                private boolean doTraceCalled = false;
321:                private boolean doHeadCalled = false;
322:                private ServletConfig passedServletConfig;
323:
324:                public void init(ServletConfig config) throws ServletException {
325:                    initCalled = true;
326:                    passedServletConfig = config;
327:                }
328:
329:                protected void doGet(HttpServletRequest request,
330:                        HttpServletResponse response) throws ServletException,
331:                        IOException {
332:                    doGetCalled = true;
333:                }
334:
335:                protected void doPost(HttpServletRequest request,
336:                        HttpServletResponse response) throws ServletException,
337:                        IOException {
338:                    doPostCalled = true;
339:                }
340:
341:                protected void doDelete(HttpServletRequest arg0,
342:                        HttpServletResponse arg1) throws ServletException,
343:                        IOException {
344:                    doDeleteCalled = true;
345:                }
346:
347:                protected void doOptions(HttpServletRequest arg0,
348:                        HttpServletResponse arg1) throws ServletException,
349:                        IOException {
350:                    doOptionsCalled = true;
351:                }
352:
353:                protected void doPut(HttpServletRequest arg0,
354:                        HttpServletResponse arg1) throws ServletException,
355:                        IOException {
356:                    doPutCalled = true;
357:                }
358:
359:                protected void doTrace(HttpServletRequest arg0,
360:                        HttpServletResponse arg1) throws ServletException,
361:                        IOException {
362:                    doTraceCalled = true;
363:                }
364:
365:                protected void doHead(HttpServletRequest arg0,
366:                        HttpServletResponse arg1) throws ServletException,
367:                        IOException {
368:                    doHeadCalled = true;
369:                }
370:
371:                public boolean wasInitCalled() {
372:                    return initCalled;
373:                }
374:
375:                public boolean wasDoDeleteCalled() {
376:                    return doDeleteCalled;
377:                }
378:
379:                public boolean wasDoGetCalled() {
380:                    return doGetCalled;
381:                }
382:
383:                public boolean wasDoOptionsCalled() {
384:                    return doOptionsCalled;
385:                }
386:
387:                public boolean wasDoPostCalled() {
388:                    return doPostCalled;
389:                }
390:
391:                public boolean wasDoPutCalled() {
392:                    return doPutCalled;
393:                }
394:
395:                public boolean wasDoTraceCalled() {
396:                    return doTraceCalled;
397:                }
398:
399:                public boolean wasDoHeadCalled() {
400:                    return doHeadCalled;
401:                }
402:
403:                public ServletConfig getPassedServletConfig() {
404:                    return passedServletConfig;
405:                }
406:
407:                public void reset() {
408:                    initCalled = false;
409:                    doGetCalled = false;
410:                    doPostCalled = false;
411:                    doDeleteCalled = false;
412:                    doOptionsCalled = false;
413:                    doPutCalled = false;
414:                    doTraceCalled = false;
415:                    doHeadCalled = false;
416:                }
417:            }
418:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.