001 package com.mockrunner.mock.web;
002
003 import org.apache.struts.Globals;
004
005 /**
006 * Used to create all types of struts mock objects. Maintains
007 * the necessary dependencies between the mock objects.
008 * If you use the mock objects returned by this
009 * factory in your tests you can be sure, they are all
010 * up to date.
011 */
012 public class ActionMockObjectFactory extends WebMockObjectFactory
013 {
014 private MockActionMapping mapping;
015 private MockActionServlet actionServlet;
016 private MockModuleConfig moduleConfig;
017
018 /**
019 * Creates a new set of mock objects.
020 */
021 public ActionMockObjectFactory()
022 {
023 createMockObjects();
024 }
025
026 /**
027 * Creates a set of mock objects based on another one.
028 * The created mock objects will have their own
029 * request and session objects, but they will share
030 * one <code>ServletContext</code>.
031 * @param factory the other factory
032 * @see com.mockrunner.base.BaseTestCase#createWebMockObjectFactory(WebMockObjectFactory)
033 */
034 public ActionMockObjectFactory(WebMockObjectFactory factory)
035 {
036 super(factory);
037 createMockObjects();
038 }
039
040 /**
041 * Creates a set of mock objects based on another one.
042 * You can specify, if the created mock objects should
043 * share the same session. They will share one
044 * <code>ServletContext</code> anyway.
045 * @param factory the other factory
046 * @param createNewSession <code>true</code> creates a new session,
047 * <code>false</code> uses the session from factory
048 * @see com.mockrunner.base.BaseTestCase#createWebMockObjectFactory(WebMockObjectFactory, boolean)
049 */
050 public ActionMockObjectFactory(WebMockObjectFactory factory, boolean createNewSession)
051 {
052 super(factory, createNewSession);
053 createMockObjects();
054 }
055
056 private void createMockObjects()
057 {
058 mapping = new MockActionMapping();
059 moduleConfig = new MockModuleConfig("testmodule");
060 actionServlet = new MockActionServlet();
061 actionServlet.setServletConfig(getMockServletConfig());
062 actionServlet.setServletContext(getMockServletContext());
063 refresh();
064 }
065
066 /**
067 * Refreshes the mock objects dependencies. May be called after setting request
068 * and response wrappers.
069 */
070 public void refresh()
071 {
072 super.refresh();
073 getWrappedRequest().setAttribute(Globals.MAPPING_KEY, mapping);
074 getWrappedRequest().setAttribute(Globals.MODULE_KEY, moduleConfig);
075 }
076
077 /**
078 * Returns the {@link com.mockrunner.mock.web.MockActionMapping}.
079 * @return the {@link com.mockrunner.mock.web.MockActionMapping}
080 */
081 public MockActionMapping getMockActionMapping()
082 {
083 return mapping;
084 }
085
086 /**
087 * Returns the {@link com.mockrunner.mock.web.MockModuleConfig}.
088 * @return the {@link com.mockrunner.mock.web.MockModuleConfig}
089 */
090 public MockModuleConfig getMockModuleConfig()
091 {
092 return moduleConfig;
093 }
094
095 /**
096 * Returns the {@link com.mockrunner.mock.web.MockModuleConfig}.
097 * @return the {@link com.mockrunner.mock.web.MockModuleConfig}
098 */
099 public MockActionServlet getMockActionServlet()
100 {
101 return actionServlet;
102 }
103 }