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.navigable;
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.navigable.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.navigate.internal.BeanNavigationReader;
033: import org.strecks.view.ActionForwardViewAdapter;
034: import org.strecks.view.ViewAdapter;
035: import org.testng.Assert;
036: import org.testng.annotations.BeforeMethod;
037: import org.testng.annotations.Test;
038:
039: /**
040: * @author Phil Zoio
041: */
042: public class TestNavigableDispatchController {
043:
044: private NavigableDispatchController action;
045:
046: @BeforeMethod
047: public void setUp() {
048: action = new NavigableDispatchController();
049: BeanNavigationReader navigationReader = new BeanNavigationReader();
050: navigationReader.readAnnotations(SimpleDispatchAction.class);
051: action.setNavigationHolder(navigationReader
052: .getNavigationHolder());
053: }
054:
055: @Test
056: public void testCreationAction() throws Exception {
057: ActionCreator actionCreator = new ActionCreatorImpl();
058: Action action = actionCreator
059: .createAction(SimpleDispatchAction.class);
060: NavigableDispatchController controller = (NavigableDispatchController) action;
061: assert controller.getBeanSource() != null;
062: assert controller.getNavigationHolder() != null;
063: }
064:
065: @Test
066: public void testInsertMethod() {
067:
068: ActionForward actionForward = new ActionForward();
069:
070: SimpleDispatchAction actionBean = createMock(SimpleDispatchAction.class);
071: ActionMapping mapping = createMock(ActionMapping.class);
072: HttpServletRequest request = createMock(HttpServletRequest.class);
073:
074: expect(mapping.getParameter()).andReturn("method");
075: expect(request.getParameter("method")).andReturn("insert");
076: actionBean.insert();
077: expect(actionBean.getSuccessResult()).andReturn("insertResult");
078: expect(mapping.findForward("insertResult")).andReturn(
079: actionForward);
080:
081: replay(actionBean);
082: replay(mapping);
083: replay(request);
084:
085: ViewAdapter viewAdapter = action
086: .executeAction(actionBean, new TestContextImpl(request,
087: null, null, null, mapping));
088: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
089: assert (va.getActionForward() == actionForward);
090:
091: verify(actionBean);
092: verify(mapping);
093: verify(request);
094:
095: }
096:
097: @Test
098: public void testUnspecified() {
099:
100: ActionForward actionForward = new ActionForward();
101:
102: SimpleDispatchAction actionBean = createMock(SimpleDispatchAction.class);
103: ActionMapping mapping = createMock(ActionMapping.class);
104: HttpServletRequest request = createMock(HttpServletRequest.class);
105:
106: expect(mapping.getParameter()).andReturn("method");
107: expect(request.getParameter("method")).andReturn(null);
108: actionBean.unspecified();
109: expect(actionBean.getSuccessResult()).andReturn(
110: "unspecifiedResult");
111: expect(mapping.findForward("unspecifiedResult")).andReturn(
112: actionForward);
113:
114: replay(actionBean);
115: replay(mapping);
116: replay(request);
117:
118: ViewAdapter viewAdapter = action
119: .executeAction(actionBean, new TestContextImpl(request,
120: null, null, null, mapping));
121: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
122: assert (va.getActionForward() == actionForward);
123:
124: verify(actionBean);
125: verify(mapping);
126: verify(request);
127:
128: }
129:
130: @Test
131: public void testNoParameter() {
132:
133: SimpleDispatchAction actionBean = createMock(SimpleDispatchAction.class);
134: ActionMapping mapping = createMock(ActionMapping.class);
135: HttpServletRequest request = createMock(HttpServletRequest.class);
136:
137: expect(mapping.getParameter()).andReturn(null);
138: expect(mapping.getPath()).andReturn("path");
139:
140: replay(actionBean);
141: replay(mapping);
142: replay(request);
143:
144: try {
145: action.executeAction(actionBean, new TestContextImpl(
146: request, null, null, null, mapping));
147: } catch (ApplicationConfigurationException e) {
148: Assert
149: .assertEquals(
150: e.getMessage(),
151: "The element 'parameter' is required to determine the method name, and is required in required in path path");
152: }
153: verify(actionBean);
154: verify(mapping);
155: verify(request);
156:
157: }
158: }
|