001: package com.mockrunner.test.web;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import javax.servlet.http.HttpServletRequestWrapper;
007:
008: import junit.framework.TestCase;
009:
010: import org.apache.struts.Globals;
011: import org.apache.struts.action.ActionForward;
012: import org.apache.struts.action.ActionMapping;
013:
014: import com.mockrunner.mock.web.ActionMockObjectFactory;
015: import com.mockrunner.mock.web.MockActionForward;
016: import com.mockrunner.mock.web.MockActionMapping;
017: import com.mockrunner.mock.web.MockActionServlet;
018: import com.mockrunner.mock.web.MockHttpServletRequest;
019: import com.mockrunner.mock.web.MockModuleConfig;
020:
021: public class ActionMockObjectFactoryTest extends TestCase {
022: public void testRefresh() {
023: ActionMockObjectFactory factory = new ActionMockObjectFactory();
024: TestWrapper wrapper = new TestWrapper();
025: factory.addRequestWrapper(wrapper);
026: assertTrue(wrapper.getAttributesMap().isEmpty());
027: factory.refresh();
028: assertSame(factory.getMockActionMapping(), wrapper
029: .getAttributesMap().get(Globals.MAPPING_KEY));
030: assertSame(factory.getMockModuleConfig(), wrapper
031: .getAttributesMap().get(Globals.MODULE_KEY));
032: }
033:
034: public void testPrepareActionMapping() {
035: ActionMockObjectFactory factory = new ActionMockObjectFactory();
036: assertSame(factory.getMockActionMapping(), factory
037: .getActionMapping());
038: ActionMapping mapping = factory
039: .prepareActionMapping(TestMapping.class);
040: assertSame(mapping, factory.getActionMapping());
041: assertNotSame(mapping, factory.getMockActionMapping());
042: assertSame(factory.getActionMapping(), factory.getMockRequest()
043: .getAttribute(Globals.MAPPING_KEY));
044: assertSame(factory.getMockModuleConfig(), factory
045: .getMockRequest().getAttribute(Globals.MODULE_KEY));
046: TestMapping testMapping = (TestMapping) mapping;
047: testMapping.method();
048: assertTrue(testMapping.wasMethodCalled());
049: testMapping.setInput("testInput");
050: assertEquals("testInput", testMapping.getInput());
051: assertEquals("testInput", factory.getMockActionMapping()
052: .getInput());
053: MockActionForward inputForward = (MockActionForward) testMapping
054: .getInputForward();
055: assertEquals("testInput", inputForward.getPath());
056: factory.getMockActionMapping()
057: .addForward("success", "testpath");
058: MockActionForward forward = (MockActionForward) testMapping
059: .findForward("success");
060: assertEquals("testpath", forward.getPath());
061: }
062:
063: public void testOverrideCreate() {
064: ActionMockObjectFactory factory = new TestActionMockObjectFactory();
065: assertNotSame(factory.getMockActionMapping().getClass(),
066: MockActionMapping.class);
067: assertNotSame(factory.getActionMapping().getClass(),
068: MockActionMapping.class);
069: assertNotSame(factory.getMockActionServlet().getClass(),
070: MockActionServlet.class);
071: assertNotSame(factory.getMockModuleConfig().getClass(),
072: MockModuleConfig.class);
073: }
074:
075: private class TestWrapper extends HttpServletRequestWrapper {
076: private Map attributes = new HashMap();
077:
078: public TestWrapper() {
079: super (new MockHttpServletRequest());
080: }
081:
082: public void setAttribute(String key, Object value) {
083: attributes.put(key, value);
084: }
085:
086: public Map getAttributesMap() {
087: return attributes;
088: }
089: }
090:
091: public static class TestMapping extends ActionMapping {
092: private boolean findForwardCalled = false;
093: private boolean findForwardsCalled = false;
094: private boolean getInputForwardCalled = false;
095: private boolean methodCalled = false;
096:
097: public boolean wasFindForwardCalled() {
098: return findForwardCalled;
099: }
100:
101: public boolean wasFindForwardsCalled() {
102: return findForwardsCalled;
103: }
104:
105: public boolean wasMethodCalled() {
106: return methodCalled;
107: }
108:
109: public boolean wasGetInputForwardCalled() {
110: return getInputForwardCalled;
111: }
112:
113: public ActionForward findForward(String name) {
114: findForwardCalled = true;
115: return null;
116: }
117:
118: public String[] findForwards() {
119: findForwardsCalled = true;
120: return null;
121: }
122:
123: public ActionForward getInputForward() {
124: getInputForwardCalled = true;
125: return null;
126: }
127:
128: public void method() {
129: methodCalled = true;
130: }
131: }
132:
133: public static class TestActionMockObjectFactory extends
134: ActionMockObjectFactory {
135: public MockActionMapping createMockActionMapping() {
136: return new MockActionMapping() {
137: };
138: }
139:
140: public MockActionServlet createMockActionServlet() {
141: return new MockActionServlet() {
142: };
143: }
144:
145: public MockModuleConfig createMockModuleConfig() {
146: return new MockModuleConfig("testmodule") {
147: };
148: }
149: }
150: }
|