KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > AbstractServletDispatcherTestCase


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 /*
6  * Created on 2/10/2003
7  *
8  */

9 package com.opensymphony.webwork.dispatcher;
10
11 import com.mockobjects.dynamic.C;
12 import com.mockobjects.dynamic.Mock;
13 import com.mockobjects.servlet.MockHttpServletResponse;
14 import com.mockobjects.servlet.MockHttpSession;
15 import com.mockobjects.servlet.MockServletConfig;
16 import com.mockobjects.servlet.MockServletOutputStream;
17 import com.opensymphony.webwork.WebWorkTestCase;
18 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest;
19 import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletResponse;
20 import com.opensymphony.xwork.config.ConfigurationManager;
21 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
22
23 import javax.servlet.RequestDispatcher JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServlet JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31
32 /**
33  * @author CameronBraid
34  */

35 public abstract class AbstractServletDispatcherTestCase extends WebWorkTestCase {
36     //~ Methods ////////////////////////////////////////////////////////////////
37

38     public String JavaDoc getConfigFilename() {
39         return "xwork.xml";
40     }
41
42     public abstract ServletDispatcher getServletDispatcher();
43
44     public abstract String JavaDoc getServletPath();
45
46     public void testServletDispatcher() throws ServletException JavaDoc, IOException JavaDoc {
47         service(getServletDispatcher());
48     }
49
50     protected Map JavaDoc getParameterMap() {
51         return new HashMap JavaDoc();
52     }
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56
57         loadConfig();
58     }
59
60     protected void loadConfig() {
61         XmlConfigurationProvider c = new XmlConfigurationProvider(getConfigFilename());
62         ConfigurationManager.clearConfigurationProviders();
63         ConfigurationManager.destroyConfiguration();
64         ConfigurationManager.addConfigurationProvider(c);
65         ConfigurationManager.getConfiguration();
66     }
67
68     protected void tearDown() throws Exception JavaDoc {
69         ConfigurationManager.destroyConfiguration();
70     }
71
72     private void service(HttpServlet JavaDoc servlet) throws ServletException JavaDoc, IOException JavaDoc {
73         Mock dispatcherMock = new Mock(RequestDispatcher JavaDoc.class);
74         dispatcherMock.expect("include", C.ANY_ARGS);
75
76         MockHttpSession session = new MockHttpSession();
77
78         WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
79         request.setSession(session);
80         request.setupAddHeader("Content-Type", "dunno what this should be... just not multipart !");
81         request.setParameterMap(getParameterMap());
82         request.setupGetServletPath(getServletPath());
83         request.setupGetPathInfo(getServletPath());
84         request.setupGetContentType("text/plain");
85
86         MockHttpServletResponse response = new WebWorkMockHttpServletResponse();
87         response.setupOutputStream(new MockServletOutputStream());
88
89         Mock servletContextDMock = new Mock(ServletContext JavaDoc.class);
90         servletContextDMock.matchAndReturn("getRealPath", C.args(C.eq("")), "");
91         servletContextDMock.matchAndReturn("getRealPath", C.args(C.eq("velocity.properties")), null);
92         servletContextDMock.matchAndReturn("getRealPath", C.args(C.eq("/WEB-INF/velocity.properties")), null);
93         servletContextDMock.matchAndReturn("log", C.ANY_ARGS, null);
94
95         ServletContext JavaDoc servletContextMock = (ServletContext JavaDoc) servletContextDMock.proxy();
96         servletContextDMock.expectAndReturn("getServerInfo", "Resin");
97
98         MockServletConfig servletConfigMock = new MockServletConfig();
99         servletConfigMock.setServletContext(servletContextMock);
100
101         servlet.init(servletConfigMock);
102         servlet.service(request, response);
103     }
104 }
105
Popular Tags