|
18 | 18 | */
|
19 | 19 | package org.apache.struts2.result;
|
20 | 20 |
|
21 |
| -import com.mockobjects.dynamic.C; |
22 |
| -import com.mockobjects.dynamic.Mock; |
23 |
| -import jakarta.servlet.RequestDispatcher; |
24 |
| -import jakarta.servlet.http.HttpServletRequest; |
25 |
| -import jakarta.servlet.http.HttpServletResponse; |
26 | 21 | import org.apache.struts2.ActionContext;
|
27 |
| -import org.apache.struts2.ServletActionContext; |
28 | 22 | import org.apache.struts2.StrutsInternalTestCase;
|
29 | 23 | import org.apache.struts2.StrutsStatics;
|
30 |
| -import org.apache.struts2.dispatcher.DispatcherConstants; |
31 |
| -import org.apache.struts2.dispatcher.Parameter; |
32 | 24 | import org.apache.struts2.mock.MockActionInvocation;
|
| 25 | +import org.apache.struts2.util.ValueStack; |
33 | 26 | import org.apache.struts2.util.ValueStackFactory;
|
34 |
| - |
35 |
| -import java.util.Map; |
| 27 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 28 | +import org.springframework.mock.web.MockHttpServletResponse; |
36 | 29 |
|
37 | 30 | public class ServletDispatcherResultTest extends StrutsInternalTestCase implements StrutsStatics {
|
38 | 31 |
|
39 |
| - public void testInclude() { |
| 32 | + private MockHttpServletRequest request; |
| 33 | + private MockHttpServletResponse response; |
| 34 | + private MockActionInvocation invocation; |
| 35 | + private ValueStack stack; |
| 36 | + |
| 37 | + public void testForward() { |
40 | 38 | ServletDispatcherResult view = new ServletDispatcherResult();
|
41 | 39 | view.setLocation("foo.jsp");
|
42 | 40 |
|
43 |
| - Mock dispatcherMock = new Mock(RequestDispatcher.class); |
44 |
| - dispatcherMock.expect("include", C.ANY_ARGS); |
45 |
| - |
46 |
| - Mock requestMock = new Mock(HttpServletRequest.class); |
47 |
| - requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null); |
48 |
| - requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy()); |
| 41 | + request.setAttribute("struts.actiontag.invocation", null); |
| 42 | + request.setAttribute("jakarta.servlet.include.servlet_path", null); |
| 43 | + request.setRequestURI("foo.jsp"); |
49 | 44 |
|
50 |
| - Mock responseMock = new Mock(HttpServletResponse.class); |
51 |
| - responseMock.expectAndReturn("isCommitted", Boolean.TRUE); |
52 |
| - |
53 |
| - ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy()); |
54 |
| - ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy()); |
| 45 | + response.setCommitted(Boolean.FALSE); |
55 | 46 |
|
56 | 47 | try {
|
57 |
| - view.execute(null); |
| 48 | + view.execute(invocation); |
58 | 49 | } catch (Exception e) {
|
59 | 50 | fail(e.getMessage());
|
60 | 51 | }
|
61 | 52 |
|
62 |
| - dispatcherMock.verify(); |
63 |
| - requestMock.verify(); |
64 |
| - dispatcherMock.verify(); |
| 53 | + assertEquals("foo.jsp", response.getForwardedUrl()); |
65 | 54 | }
|
66 | 55 |
|
67 |
| - public void testSimple() { |
| 56 | + public void testInclude() { |
68 | 57 | ServletDispatcherResult view = new ServletDispatcherResult();
|
69 | 58 | view.setLocation("foo.jsp");
|
70 | 59 |
|
71 |
| - Mock dispatcherMock = new Mock(RequestDispatcher.class); |
72 |
| - dispatcherMock.expect("forward", C.ANY_ARGS); |
73 |
| - |
74 |
| - Mock requestMock = new Mock(HttpServletRequest.class); |
75 |
| - requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null); |
76 |
| - requestMock.expectAndReturn("getAttribute", "jakarta.servlet.include.servlet_path", null); |
77 |
| - requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy()); |
78 |
| - requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works |
79 |
| - requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works |
80 |
| - requestMock.matchAndReturn("getRequestURI", "foo.jsp"); |
81 |
| - |
82 |
| - Mock responseMock = new Mock(HttpServletResponse.class); |
83 |
| - responseMock.expectAndReturn("isCommitted", Boolean.FALSE); |
84 |
| - |
85 |
| - ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy()); |
86 |
| - ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy()); |
| 60 | + request.setAttribute("struts.actiontag.invocation", null); |
| 61 | + response.setCommitted(Boolean.TRUE); |
| 62 | + request.setRequestURI("foo.jsp"); |
87 | 63 |
|
88 | 64 | try {
|
89 |
| - view.execute(null); |
| 65 | + view.execute(invocation); |
90 | 66 | } catch (Exception e) {
|
91 | 67 | fail(e.getMessage());
|
92 | 68 | }
|
93 | 69 |
|
94 |
| - dispatcherMock.verify(); |
95 |
| - requestMock.verify(); |
96 |
| - dispatcherMock.verify(); |
| 70 | + assertEquals("foo.jsp", response.getIncludedUrl()); |
97 | 71 | }
|
98 | 72 |
|
99 | 73 | public void testWithParameter() {
|
100 | 74 | ServletDispatcherResult view = container.inject(ServletDispatcherResult.class);
|
101 | 75 | view.setLocation("foo.jsp?bar=1");
|
102 | 76 |
|
103 |
| - Mock dispatcherMock = new Mock(RequestDispatcher.class); |
104 |
| - dispatcherMock.expect("forward", C.ANY_ARGS); |
105 |
| - |
106 |
| - Mock requestMock = new Mock(HttpServletRequest.class); |
107 |
| - requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null); |
108 |
| - requestMock.expectAndReturn("getAttribute", "jakarta.servlet.include.servlet_path", null); |
109 |
| - requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp?bar=1")), dispatcherMock.proxy()); |
110 |
| - requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works |
111 |
| - requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works |
112 |
| - requestMock.matchAndReturn("getRequestURI", "foo.jsp"); |
113 |
| - |
114 |
| - Mock responseMock = new Mock(HttpServletResponse.class); |
115 |
| - responseMock.expectAndReturn("isCommitted", Boolean.FALSE); |
116 |
| - |
117 |
| - ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy()); |
118 |
| - ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy()); |
119 |
| - |
120 |
| - MockActionInvocation mockActionInvocation = new MockActionInvocation(); |
121 |
| - mockActionInvocation.setInvocationContext(ActionContext.getContext()); |
122 |
| - mockActionInvocation.setStack(container.getInstance(ValueStackFactory.class).createValueStack()); |
123 |
| - |
124 | 77 | try {
|
125 |
| - view.execute(mockActionInvocation); |
| 78 | + view.execute(invocation); |
126 | 79 | } catch (Exception e) {
|
127 | 80 | fail(e.getMessage());
|
128 | 81 | }
|
129 | 82 |
|
130 |
| - assertTrue(mockActionInvocation.getInvocationContext().getParameters().contains("bar")); |
131 |
| - assertEquals("1", mockActionInvocation.getInvocationContext().getParameters().get("bar").getValue()); |
| 83 | + assertTrue(invocation.getInvocationContext().getParameters().contains("bar")); |
| 84 | + assertEquals("1", invocation.getInvocationContext().getParameters().get("bar").getValue()); |
132 | 85 |
|
133 | 86 | // See https://issues.apache.org/jira/browse/WW-5486
|
134 |
| - Map<String, Parameter> contextMap = (Map<String, Parameter>) mockActionInvocation.getInvocationContext().getContextMap().get(DispatcherConstants.PARAMETERS); |
135 |
| - assertTrue(contextMap.containsKey("bar")); |
136 |
| - assertEquals("1", contextMap.get("bar").getValue()); |
| 87 | + assertEquals("1", stack.findString("#parameters.bar")); |
| 88 | + } |
137 | 89 |
|
138 |
| - dispatcherMock.verify(); |
139 |
| - requestMock.verify(); |
140 |
| - dispatcherMock.verify(); |
| 90 | + public void setUp() throws Exception { |
| 91 | + super.setUp(); |
| 92 | + invocation = new MockActionInvocation(); |
| 93 | + request = new MockHttpServletRequest(); |
| 94 | + response = new MockHttpServletResponse(); |
| 95 | + stack = container.getInstance(ValueStackFactory.class).createValueStack(); |
| 96 | + invocation.setStack(stack); |
| 97 | + |
| 98 | + stack.getActionContext() |
| 99 | + .withServletRequest(request) |
| 100 | + .withServletResponse(response) |
| 101 | + .withActionInvocation(invocation) |
| 102 | + .withValueStack(stack) |
| 103 | + .bind(); |
| 104 | + |
| 105 | + invocation.setInvocationContext(ActionContext.getContext()); |
141 | 106 | }
|
| 107 | + |
142 | 108 | }
|
0 commit comments