001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.action.basic;
016:
017: import static org.easymock.EasyMock.expect;
018: import static org.easymock.classextension.EasyMock.createMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.verify;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.struts.action.Action;
025: import org.apache.struts.action.ActionForward;
026: import org.apache.struts.action.ActionMapping;
027: import org.strecks.action.basic.impl.SimpleDispatchAction;
028: import org.strecks.context.impl.TestContextImpl;
029: import org.strecks.controller.ActionCreator;
030: import org.strecks.controller.ActionCreatorImpl;
031: import org.strecks.exceptions.ApplicationConfigurationException;
032: import org.strecks.view.ActionForwardViewAdapter;
033: import org.strecks.view.ViewAdapter;
034: import org.testng.Assert;
035: import org.testng.annotations.Test;
036:
037: /**
038: * @author Phil Zoio
039: */
040: public class TestBasicDispatchController {
041:
042: @Test
043: public void testCreationAction() throws Exception {
044: ActionCreator actionCreator = new ActionCreatorImpl();
045: Action action = actionCreator
046: .createAction(SimpleDispatchAction.class);
047: BasicDispatchController controller = (BasicDispatchController) action;
048: assert controller.getBeanSource() != null;
049: }
050:
051: @Test
052: public void testInsertMethod() {
053:
054: BasicDispatchController action = new BasicDispatchController();
055: ActionForward actionForward = new ActionForward();
056:
057: SimpleDispatchAction actionBean = createMock(SimpleDispatchAction.class);
058: ActionMapping mapping = createMock(ActionMapping.class);
059: HttpServletRequest request = createMock(HttpServletRequest.class);
060:
061: expect(mapping.getParameter()).andReturn("method");
062: expect(request.getParameter("method")).andReturn("insert");
063: expect(actionBean.insert()).andReturn("insertResult");
064: expect(mapping.findForward("insertResult")).andReturn(
065: actionForward);
066:
067: replay(actionBean);
068: replay(mapping);
069: replay(request);
070:
071: ViewAdapter viewAdapter = action
072: .executeAction(actionBean, new TestContextImpl(request,
073: null, null, null, mapping));
074: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
075: assert (va.getActionForward() == actionForward);
076:
077: verify(actionBean);
078: verify(mapping);
079: verify(request);
080:
081: }
082:
083: @Test
084: public void testUnspecified() {
085:
086: BasicDispatchController action = new BasicDispatchController();
087: ActionForward actionForward = new ActionForward();
088:
089: SimpleDispatchAction actionBean = createMock(SimpleDispatchAction.class);
090: ActionMapping mapping = createMock(ActionMapping.class);
091: HttpServletRequest request = createMock(HttpServletRequest.class);
092:
093: expect(mapping.getParameter()).andReturn("method");
094: expect(request.getParameter("method")).andReturn(null);
095: expect(actionBean.unspecified()).andReturn("unspecifiedResult");
096: expect(mapping.findForward("unspecifiedResult")).andReturn(
097: actionForward);
098:
099: replay(actionBean);
100: replay(mapping);
101: replay(request);
102:
103: ViewAdapter viewAdapter = action
104: .executeAction(actionBean, new TestContextImpl(request,
105: null, null, null, mapping));
106: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
107: assert (va.getActionForward() == actionForward);
108:
109: verify(actionBean);
110: verify(mapping);
111: verify(request);
112:
113: }
114:
115: @Test
116: public void testNoParameter() {
117:
118: BasicDispatchController action = new BasicDispatchController();
119:
120: SimpleDispatchAction actionBean = createMock(SimpleDispatchAction.class);
121: ActionMapping mapping = createMock(ActionMapping.class);
122: HttpServletRequest request = createMock(HttpServletRequest.class);
123:
124: expect(mapping.getParameter()).andReturn(null);
125: expect(mapping.getPath()).andReturn("path");
126:
127: replay(actionBean);
128: replay(mapping);
129: replay(request);
130:
131: try {
132: action.executeAction(actionBean, new TestContextImpl(
133: request, null, null, null, mapping));
134: } catch (ApplicationConfigurationException e) {
135: Assert
136: .assertEquals(
137: e.getMessage(),
138: "The element 'parameter' is required to determine the method name, and is required in required in path path");
139: }
140: verify(actionBean);
141: verify(mapping);
142: verify(request);
143:
144: }
145: }
|